From 81f870eaa1eefccc615ef2c1c6ef5fcf9cf09ba1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcin=20Wo=C5=BAniak?= Date: Sun, 10 Jan 2021 14:15:58 +0100 Subject: [PATCH] Checkpoint: 15/1 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Marcin Woźniak --- .../controllers/ExchangeControllerTest.java | 162 ++++++++++++++---- 1 file changed, 125 insertions(+), 37 deletions(-) diff --git a/restservice/src/test/java/com/plannaplan/controllers/ExchangeControllerTest.java b/restservice/src/test/java/com/plannaplan/controllers/ExchangeControllerTest.java index 89f35bb..02b7f7d 100644 --- a/restservice/src/test/java/com/plannaplan/controllers/ExchangeControllerTest.java +++ b/restservice/src/test/java/com/plannaplan/controllers/ExchangeControllerTest.java @@ -21,11 +21,14 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers. import java.nio.charset.Charset; -import javax.validation.constraints.AssertTrue; - +import com.plannaplan.entities.Assignment; +import com.plannaplan.entities.Commision; +import com.plannaplan.entities.Exchange; import com.plannaplan.entities.Groups; import com.plannaplan.entities.User; -import com.plannaplan.exceptions.UserNotFoundException; +import com.plannaplan.services.AssignmentService; +import com.plannaplan.services.CommisionService; +import com.plannaplan.services.ExchangeService; import com.plannaplan.services.GroupService; import com.plannaplan.services.UserService; import com.plannaplan.types.UserRoles; @@ -46,12 +49,30 @@ public class ExchangeControllerTest extends AbstractControllerTest { @Autowired private GroupService groupService; + @Autowired + private AssignmentService assignmentService; + + @Autowired + private CommisionService commisionService; + + @Autowired + private ExchangeService exchangeService; + @Test @DirtiesContext(methodMode = MethodMode.BEFORE_METHOD) public void shouldGetAllUsersExchanges() throws Exception { final User user = this.userService.save(new User(null, null, - "shouldGetAllUsersExchanges@ExchangeController.test", UserRoles.STUDENT)); - assertTrue(false); + "shouldGetAllUsersExchanges@ExchangeController.test", "11111", UserRoles.STUDENT, 320)); + final String token = this.userService.login(user).getToken(); + final Groups group = this.groupService.save(new Groups(215, "A2-2", null, 520, WeekDay.TUESDAY, null)); + final Groups groupDesired = this.groupService.save(new Groups(216, "A2-3", null, 530, WeekDay.MONDAY, null)); + final Commision commision = this.commisionService.save(new Commision(user)); + final Assignment assignment = this.assignmentService.save(new Assignment(group, commision)); + this.exchangeService.save(new Exchange(assignment, groupDesired)); + + MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build(); + mockMvc.perform(get(EXCHANGE_ENDPOINT + "/all").header("Authorization", "Bearer " + + token)).andExpect(status().isOk()); } @Test @@ -67,17 +88,38 @@ public class ExchangeControllerTest extends AbstractControllerTest { @Test @DirtiesContext(methodMode = MethodMode.BEFORE_METHOD) - public void shouldGetSingleExchange(){ + public void shouldGetSingleExchange() throws Exception{ final User user = this.userService.save(new User(null, null, - "shouldGetSingleExchange@ExchangeController.test", UserRoles.STUDENT)); - assertTrue(false); + "shouldGetSingleExchange@ExchangeController.test", "11111", UserRoles.STUDENT, 320)); + final String token = this.userService.login(user).getToken(); + final Groups group = this.groupService.save(new Groups(215, "A2-2", null, 520, WeekDay.TUESDAY, null)); + final Groups groupDesired = this.groupService.save(new Groups(216, "A2-3", null, 530, WeekDay.MONDAY, null)); + final Commision commision = this.commisionService.save(new Commision(user)); + final Assignment assignment = this.assignmentService.save(new Assignment(group, commision)); + final Exchange exchange = this.exchangeService.save(new Exchange(assignment, groupDesired)); + + MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build(); + mockMvc.perform(get(EXCHANGE_ENDPOINT + "/" + exchange.getId()).header("Authorization", "Bearer " + + token)).andExpect(status().isOk()); } @Test - public void shouldFailGettingExchangeDueToPermission(){ + public void shouldFailGettingExchangeDueToPermission() throws Exception{ final User user = this.userService.save(new User(null, null, - "shouldFailGettingExchangeDueToPermission@ExchangeController.test", UserRoles.STUDENT)); - assertTrue(false); + "shouldFailGettingExchangeDueToPermission@ExchangeController.test", "11111", UserRoles.STUDENT, 320)); + final Groups group = this.groupService.save(new Groups(215, "A2-2", null, 520, WeekDay.TUESDAY, null)); + final Groups groupDesired = this.groupService.save(new Groups(216, "A2-3", null, 530, WeekDay.MONDAY, null)); + final Commision commision = this.commisionService.save(new Commision(user)); + final Assignment assignment = this.assignmentService.save(new Assignment(group, commision)); + final Exchange exchange = this.exchangeService.save(new Exchange(assignment, groupDesired)); + + final User user2 = this.userService.save(new User(null, null, + "shouldFailGettingExchangeDueToPermission2@ExchangeController.test", "11112", UserRoles.STUDENT, 321)); + final String token2 = this.userService.login(user2).getToken(); + + MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build(); + mockMvc.perform(post(EXCHANGE_ENDPOINT + "/" + exchange.getId()).header("Authorization", "Bearer " + + token2)).andExpect(status().is4xxClientError()); } @Test @@ -95,10 +137,20 @@ public class ExchangeControllerTest extends AbstractControllerTest { @Test @DirtiesContext(methodMode = MethodMode.BEFORE_METHOD) - public void shouldInsertExchange(){ + public void shouldInsertExchange() throws Exception{ final User user = this.userService.save(new User(null, null, - "shouldFailGettingNotExistingExchange@ExchangeController.test", UserRoles.STUDENT)); - assertTrue(false); + "shouldInsertExchange@ExchangeController.test", "11111", UserRoles.STUDENT, 320)); + final String token = this.userService.login(user).getToken(); + final Groups group = this.groupService.save(new Groups(215, "A2-2", null, 520, WeekDay.TUESDAY, null)); + final Groups groupDesired = this.groupService.save(new Groups(216, "A2-3", null, 530, WeekDay.MONDAY, null)); + final Commision commision = this.commisionService.save(new Commision(user)); + final Assignment assignment = this.assignmentService.save(new Assignment(group, commision)); + + this.assignmentService.callAcceptAlgorythm(); + + MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build(); + mockMvc.perform(post(EXCHANGE_ENDPOINT).header("Authorization", "Bearer " + + token).contentType(APPLICATION_JSON_UTF8).content("{\"assignment\": "+ assignment.getId() +", \"group\": "+ groupDesired.getId() +" }")).andExpect(status().isOk()); } @Test @@ -107,8 +159,7 @@ public class ExchangeControllerTest extends AbstractControllerTest { "shouldFailInsertExchangeDueToMissingGroup@ExchangeController.test", UserRoles.STUDENT)); final String token = this.userService.login(user).getToken(); - MockMvc mockMvc = - MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build(); + MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build(); mockMvc.perform(post(EXCHANGE_ENDPOINT).header("Authorization", "Bearer " + token).contentType(APPLICATION_JSON_UTF8).content("{\"assignment\": "+ user.getId() +" }")).andExpect(status().isBadRequest()); } @@ -120,8 +171,7 @@ public class ExchangeControllerTest extends AbstractControllerTest { final String token = this.userService.login(user).getToken(); final Groups group = this.groupService.save(new Groups(212, "A2-1", null, 420, WeekDay.WEDNESDAY, null)); - MockMvc mockMvc = - MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build(); + MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build(); mockMvc.perform(post(EXCHANGE_ENDPOINT).header("Authorization", "Bearer " + token).contentType(APPLICATION_JSON_UTF8).content("{\"group\": "+ group.getId() +" }")).andExpect(status().isBadRequest()); } @@ -132,59 +182,97 @@ public class ExchangeControllerTest extends AbstractControllerTest { "shouldFailInsertExchangeDueToMissingParam@ExchangeController.test", UserRoles.STUDENT)); final String token = this.userService.login(user).getToken(); - MockMvc mockMvc = - MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build(); + MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build(); mockMvc.perform(post(EXCHANGE_ENDPOINT).header("Authorization", "Bearer " + token).contentType(APPLICATION_JSON_UTF8)).andExpect(status().isBadRequest()); } @Test @DirtiesContext(methodMode = MethodMode.BEFORE_METHOD) - public void shouldDenyExchangeDueToAssigmentOverlapping(){ + public void shouldDenyExchangeDueToAssigmentOverlapping() throws Exception{ final User user = this.userService.save(new User(null, null, - "shouldDenyExchangeDueToAssigmentOverlapping@ExchangeController.test", UserRoles.STUDENT)); - assertTrue(false); + "shouldDenyExchangeDueToAssigmentOverlapping@ExchangeController.test", "11111", UserRoles.STUDENT, 320)); + final String token = this.userService.login(user).getToken(); + final Groups group = this.groupService.save(new Groups(215, "A2-2", null, 520, WeekDay.TUESDAY, null)); + final Commision commision = this.commisionService.save(new Commision(user)); + final Assignment assignment = this.assignmentService.save(new Assignment(group, commision)); + this.exchangeService.save(new Exchange(assignment, group)); + + MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build(); + mockMvc.perform(post(EXCHANGE_ENDPOINT).header("Authorization", "Bearer " + + token).contentType(APPLICATION_JSON_UTF8).content("{\"assignment\": "+ user.getId() +", \"group\": "+ group.getId() +" }")).andExpect(status().isBadRequest()); } + @Test public void shouldDenyPostDueToAssignmentNotAccepted() throws Exception{ final User user = this.userService.save(new User(null, null, "shouldDenyPostDueToAssignmentNotAccepted@ExchangeController.test", UserRoles.STUDENT)); final String token = this.userService.login(user).getToken(); final Groups group = this.groupService.save(new Groups(212, "A2-1", null, 420, WeekDay.WEDNESDAY, null)); + final Groups group2 = this.groupService.save(new Groups(213, "A2-2", null, 420, WeekDay.MONDAY, null)); + final Commision commision = this.commisionService.save(new Commision(user)); + final Assignment assignment = this.assignmentService.save(new Assignment(group,commision)); - MockMvc mockMvc = - MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build(); + MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build(); mockMvc.perform(post(EXCHANGE_ENDPOINT).header("Authorization", "Bearer " + - token).contentType(APPLICATION_JSON_UTF8).content("{\"assignment\": "+ user.getId() +", \"group\": "+ group.getId() +" }")).andExpect(status().isBadRequest()); + token).contentType(APPLICATION_JSON_UTF8).content("{\"assignment\": "+ assignment.getId() +", \"group\": "+ group2.getId() +" }")).andExpect(status().isBadRequest()); } @Test @DirtiesContext(methodMode = MethodMode.BEFORE_METHOD) - public void shouldDeleteExchange(){ + public void shouldDeleteExchange() throws Exception{ final User user = this.userService.save(new User(null, null, - "shouldDeleteExchange@ExchangeController.test", UserRoles.STUDENT)); - assertTrue(false); + "shouldDeleteExchange@ExchangeController.test", "11111", UserRoles.STUDENT, 320)); + final String token = this.userService.login(user).getToken(); + final Groups group = this.groupService.save(new Groups(215, "A2-2", null, 520, WeekDay.TUESDAY, null)); + final Groups groupDesired = this.groupService.save(new Groups(216, "A2-3", null, 530, WeekDay.MONDAY, null)); + final Commision commision = this.commisionService.save(new Commision(user)); + final Assignment assignment = this.assignmentService.save(new Assignment(group, commision)); + final Exchange exchange = this.exchangeService.save(new Exchange(assignment, groupDesired)); + + MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build(); + mockMvc.perform(delete(EXCHANGE_ENDPOINT + "/" + exchange.getId()).header("Authorization", "Bearer " + + token)).andExpect(status().isOk()); } @Test - public void shouldFailDeleteDueToWrongPermissions(){ + public void shouldFailDeleteDueToWrongPermissions() throws Exception{ final User user = this.userService.save(new User(null, null, - "shouldFailDeleteDueToWrongPermissions@ExchangeController.test", UserRoles.STUDENT)); - assertTrue(false); + "shouldFailDeleteDueToWrongPermissions@ExchangeController.test", "11111", UserRoles.STUDENT, 320)); + final Groups group = this.groupService.save(new Groups(215, "A2-2", null, 520, WeekDay.TUESDAY, null)); + final Groups groupDesired = this.groupService.save(new Groups(216, "A2-3", null, 530, WeekDay.MONDAY, null)); + final Commision commision = this.commisionService.save(new Commision(user)); + final Assignment assignment = this.assignmentService.save(new Assignment(group, commision)); + final Exchange exchange = this.exchangeService.save(new Exchange(assignment, groupDesired)); + + final User user2 = this.userService.save(new User(null, null, + "shouldFailDeleteDueToWrongPermissions@ExchangeController2.test", "11112", UserRoles.STUDENT, 322)); + final String token2 = this.userService.login(user2).getToken(); + + MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build(); + mockMvc.perform(get(EXCHANGE_ENDPOINT + "/" + exchange.getId()).header("Authorization", "Bearer " + + token2)).andExpect(status().isBadRequest()); } @Test - public void shouldFailDeleteDueToMissingParam(){ + public void shouldFailDeleteDueToMissingParam() throws Exception{ final User user = this.userService.save(new User(null, null, - "shouldFailDeleteDueToMissingParam@ExchangeController.test", UserRoles.STUDENT)); - assertTrue(false); + "shouldFailDeleteDueToMissingParam@ExchangeController.test", UserRoles.STUDENT)); + final String token = this.userService.login(user).getToken(); + + MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build(); + mockMvc.perform(delete(EXCHANGE_ENDPOINT).header("Authorization", "Bearer " + token)).andExpect(status().is4xxClientError()); } @Test - public void shouldFailDeleteDueToExchangeNotFound(){ + public void shouldFailDeleteDueToExchangeNotFound() throws Exception{ final User user = this.userService.save(new User(null, null, "shouldFailDeleteDueToExchangeNotFound@ExchangeController.test", UserRoles.STUDENT)); - assertTrue(false); + final String token = this.userService.login(user).getToken(); + + MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build(); + mockMvc.perform(delete(EXCHANGE_ENDPOINT + "/" + user.getId()).header("Authorization", "Bearer " + + token)).andExpect(status().isBadRequest()); } @Test