2020-10-02 16:51:49 +02:00
|
|
|
package com.plannaplan.controllers;
|
|
|
|
|
|
|
|
import org.junit.Test;
|
|
|
|
import org.junit.runner.RunWith;
|
2020-10-05 17:56:35 +02:00
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
2020-10-02 16:51:49 +02:00
|
|
|
import org.springframework.boot.test.context.SpringBootTest;
|
2020-10-08 15:24:23 +02:00
|
|
|
import org.springframework.http.MediaType;
|
2020-10-02 16:51:49 +02:00
|
|
|
import org.springframework.test.context.ContextConfiguration;
|
|
|
|
import org.springframework.test.context.junit4.SpringRunner;
|
2020-10-05 17:56:35 +02:00
|
|
|
import org.springframework.test.web.servlet.MockMvc;
|
|
|
|
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
|
|
|
import org.springframework.web.context.WebApplicationContext;
|
|
|
|
|
|
|
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
|
|
|
|
2020-10-08 15:24:23 +02:00
|
|
|
import java.nio.charset.Charset;
|
|
|
|
|
2020-10-05 17:56:35 +02:00
|
|
|
import com.plannaplan.entities.User;
|
|
|
|
import com.plannaplan.services.UserService;
|
|
|
|
import com.plannaplan.types.UserRoles;
|
|
|
|
|
|
|
|
import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.*;
|
|
|
|
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
|
|
|
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
|
2020-10-02 16:51:49 +02:00
|
|
|
|
|
|
|
@RunWith(SpringRunner.class)
|
|
|
|
@SpringBootTest
|
|
|
|
@ContextConfiguration
|
|
|
|
public class CommisionControllerTest {
|
|
|
|
|
2020-10-05 17:56:35 +02:00
|
|
|
@Autowired
|
|
|
|
private WebApplicationContext webApplicationContext;
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
private UserService service;
|
|
|
|
|
|
|
|
private static User user;
|
|
|
|
|
2020-10-30 17:22:40 +01:00
|
|
|
private static final String ADD_COMMISION_ENDPOINT = "/api/v1/commisions/user";
|
|
|
|
private static final String GET_COMMISIONS_ENDPOINT = "/api/v1/commisions/user";
|
2020-10-05 17:56:35 +02:00
|
|
|
private static final String TEST_COMMISIONS_EMAIL = "commisions@notexisting.domain";
|
2020-10-08 15:24:23 +02:00
|
|
|
private static final MediaType APPLICATION_JSON_UTF8 = new MediaType(MediaType.APPLICATION_JSON.getType(),
|
|
|
|
MediaType.APPLICATION_JSON.getSubtype(), Charset.forName("utf8"));
|
2020-10-05 17:56:35 +02:00
|
|
|
|
2020-10-02 16:51:49 +02:00
|
|
|
@Test
|
2020-10-05 17:56:35 +02:00
|
|
|
public void shouldReturn4xxAddingCommision() throws Exception {
|
|
|
|
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
|
|
|
|
mockMvc.perform(post(ADD_COMMISION_ENDPOINT)).andExpect(status().is4xxClientError());
|
|
|
|
|
2020-10-02 16:51:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2020-10-08 15:24:23 +02:00
|
|
|
public void shouldFailedAddingCommisionDueToNoArgs() throws Exception {
|
2020-10-05 17:56:35 +02:00
|
|
|
this.checkUser();
|
|
|
|
final String token = this.service.login(TEST_COMMISIONS_EMAIL);
|
|
|
|
|
|
|
|
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
|
|
|
|
mockMvc.perform(post(ADD_COMMISION_ENDPOINT).header("Authorization", "Bearer " + token))
|
2020-10-08 15:24:23 +02:00
|
|
|
.andExpect(status().is4xxClientError());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void shouldReturnOkAddingCommision() throws Exception {
|
|
|
|
this.checkUser();
|
|
|
|
final String token = this.service.login(TEST_COMMISIONS_EMAIL);
|
|
|
|
|
|
|
|
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
|
|
|
|
mockMvc.perform(post(ADD_COMMISION_ENDPOINT).header("Authorization", "Bearer " + token)
|
|
|
|
.contentType(APPLICATION_JSON_UTF8).content("[]")).andExpect(status().isOk());
|
2020-10-02 16:51:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2020-10-05 17:56:35 +02:00
|
|
|
public void shouldReturn4xxGettingAllCommisions() throws Exception {
|
|
|
|
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
|
|
|
|
mockMvc.perform(get(GET_COMMISIONS_ENDPOINT)).andExpect(status().is4xxClientError());
|
2020-10-02 16:51:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2020-10-05 17:56:35 +02:00
|
|
|
public void shouldReturnOkGettingAllCommisions() throws Exception {
|
|
|
|
this.checkUser();
|
|
|
|
final String token = this.service.login(TEST_COMMISIONS_EMAIL);
|
|
|
|
|
|
|
|
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
|
|
|
|
mockMvc.perform(get(GET_COMMISIONS_ENDPOINT).header("Authorization", "Bearer " + token))
|
|
|
|
.andExpect(status().isOk());
|
|
|
|
}
|
|
|
|
|
|
|
|
private void checkUser() {
|
|
|
|
if (CommisionControllerTest.user == null) {
|
|
|
|
CommisionControllerTest.user = new User(null, null, TEST_COMMISIONS_EMAIL, UserRoles.TEST_USER);
|
|
|
|
this.service.save(user);
|
|
|
|
}
|
2020-10-02 16:51:49 +02:00
|
|
|
}
|
|
|
|
}
|