From b740562e9d5861ee60eeccaa0c717d2774feefed Mon Sep 17 00:00:00 2001 From: Filip Izydorczyk Date: Mon, 18 Jan 2021 15:51:58 +0100 Subject: [PATCH 1/3] Added cheks for type and course --- .../com/plannaplan/controllers/ExchangeController.java | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/restservice/src/main/java/com/plannaplan/controllers/ExchangeController.java b/restservice/src/main/java/com/plannaplan/controllers/ExchangeController.java index 30a703f..ee12862 100755 --- a/restservice/src/main/java/com/plannaplan/controllers/ExchangeController.java +++ b/restservice/src/main/java/com/plannaplan/controllers/ExchangeController.java @@ -88,6 +88,14 @@ public class ExchangeController extends TokenBasedController { final Assignment assignmentInstance = assignment.get(); final Groups groupInstance = group.get(); + if (assignmentInstance.getGroup().getCourseId().getId() != groupInstance.getCourseId().getId()) { + return new ResponseEntity<>("You can performe exchange only within one course.", HttpStatus.BAD_REQUEST); + } + + if (assignmentInstance.getGroup().getType() != groupInstance.getType()) { + return new ResponseEntity<>("You can't exchange lecture to class and otherwise.", HttpStatus.BAD_REQUEST); + } + if (!(assignmentInstance.getCommision().getCommisionOwner().getId().equals(asker.getId()) && assignmentInstance.isAccepted())) { return new ResponseEntity<>( From 0103a028b4e111e9f8ff823e02ef1e83ee330210 Mon Sep 17 00:00:00 2001 From: Filip Izydorczyk Date: Mon, 18 Jan 2021 16:07:48 +0100 Subject: [PATCH 2/3] tests repair --- .../java/com/plannaplan/controllers/ExchangeController.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/restservice/src/main/java/com/plannaplan/controllers/ExchangeController.java b/restservice/src/main/java/com/plannaplan/controllers/ExchangeController.java index ee12862..e2aa8b7 100755 --- a/restservice/src/main/java/com/plannaplan/controllers/ExchangeController.java +++ b/restservice/src/main/java/com/plannaplan/controllers/ExchangeController.java @@ -88,7 +88,8 @@ public class ExchangeController extends TokenBasedController { final Assignment assignmentInstance = assignment.get(); final Groups groupInstance = group.get(); - if (assignmentInstance.getGroup().getCourseId().getId() != groupInstance.getCourseId().getId()) { + if (assignmentInstance.getGroup().getCourseId() != null + && assignmentInstance.getGroup().getCourseId().getId() != groupInstance.getCourseId().getId()) { return new ResponseEntity<>("You can performe exchange only within one course.", HttpStatus.BAD_REQUEST); } From 5ed445449c23bd532d77fba0ca9fc62442223a30 Mon Sep 17 00:00:00 2001 From: Filip Izydorczyk Date: Mon, 18 Jan 2021 16:21:32 +0100 Subject: [PATCH 3/3] new tests --- .../controllers/ExchangeControllerTest.java | 209 ++++++++++++------ 1 file changed, 138 insertions(+), 71 deletions(-) diff --git a/restservice/src/test/java/com/plannaplan/controllers/ExchangeControllerTest.java b/restservice/src/test/java/com/plannaplan/controllers/ExchangeControllerTest.java index a0736c2..04db1f8 100755 --- a/restservice/src/test/java/com/plannaplan/controllers/ExchangeControllerTest.java +++ b/restservice/src/test/java/com/plannaplan/controllers/ExchangeControllerTest.java @@ -22,11 +22,13 @@ import java.nio.charset.Charset; import com.plannaplan.entities.Assignment; import com.plannaplan.entities.Commision; +import com.plannaplan.entities.Course; import com.plannaplan.entities.Exchange; import com.plannaplan.entities.Groups; import com.plannaplan.entities.User; import com.plannaplan.services.AssignmentService; import com.plannaplan.services.CommisionService; +import com.plannaplan.services.CourseService; import com.plannaplan.services.ExchangeService; import com.plannaplan.services.GroupService; import com.plannaplan.services.UserService; @@ -57,11 +59,14 @@ public class ExchangeControllerTest extends AbstractControllerTest { @Autowired private ExchangeService exchangeService; + @Autowired + private CourseService courseService; + @Test @DirtiesContext(methodMode = MethodMode.BEFORE_METHOD) public void shouldGetAllUsersExchanges() throws Exception { final User user = this.userService.save(new User(null, null, - "shouldGetAllUsersExchanges@ExchangeController.test", "11111", UserRoles.STUDENT, 320)); + "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)); @@ -70,26 +75,27 @@ public class ExchangeControllerTest extends AbstractControllerTest { 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()); + mockMvc.perform(get(EXCHANGE_ENDPOINT + "/all").header("Authorization", "Bearer " + token)) + .andExpect(status().isOk()); } @Test public void shouldFailGettingNotExistingExchange() throws Exception { final User user = this.userService.save(new User(null, null, - "shouldFailGettingNotExistingExchange@ExchangeController.test", UserRoles.STUDENT)); + "shouldFailGettingNotExistingExchange@ExchangeController.test", UserRoles.STUDENT)); final String token = this.userService.login(user).getToken(); MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build(); - mockMvc.perform(get(EXCHANGE_ENDPOINT + "/" + user.getId()).header("Authorization", "Bearer " + token)).andExpect(status().isBadRequest()); + mockMvc.perform(get(EXCHANGE_ENDPOINT + "/" + user.getId()).header("Authorization", "Bearer " + token)) + .andExpect(status().isBadRequest()); } @Test @DirtiesContext(methodMode = MethodMode.BEFORE_METHOD) - public void shouldGetSingleExchange() throws Exception{ - final User user = this.userService.save(new User(null, null, - "shouldGetSingleExchange@ExchangeController.test", "11111", UserRoles.STUDENT, 320)); + public void shouldGetSingleExchange() throws Exception { + final User user = this.userService.save(new User(null, null, "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)); @@ -98,14 +104,14 @@ public class ExchangeControllerTest extends AbstractControllerTest { 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()); + mockMvc.perform(get(EXCHANGE_ENDPOINT + "/" + exchange.getId()).header("Authorization", "Bearer " + token)) + .andExpect(status().isOk()); } @Test - public void shouldFailGettingExchangeDueToPermission() throws Exception{ + public void shouldFailGettingExchangeDueToPermission() throws Exception { final User user = this.userService.save(new User(null, null, - "shouldFailGettingExchangeDueToPermission@ExchangeController.test", "11111", UserRoles.STUDENT, 320)); + "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)); @@ -113,32 +119,33 @@ public class ExchangeControllerTest extends AbstractControllerTest { 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)); + "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()); + mockMvc.perform(post(EXCHANGE_ENDPOINT + "/" + exchange.getId()).header("Authorization", "Bearer " + token2)) + .andExpect(status().is4xxClientError()); } @Test - public void shouldFailPostDueToAssignmentNotFound() throws Exception{ + public void shouldFailPostDueToAssignmentNotFound() throws Exception { final User user = this.userService.save(new User(null, null, - "shouldFailPostDueToAssignmentNotFound@ExchangeController.test", UserRoles.STUDENT)); + "shouldFailPostDueToAssignmentNotFound@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)); - 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()); + 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 @DirtiesContext(methodMode = MethodMode.BEFORE_METHOD) - public void shouldInsertExchange() throws Exception{ - final User user = this.userService.save(new User(null, null, - "shouldInsertExchange@ExchangeController.test", "11111", UserRoles.STUDENT, 320)); + public void shouldInsertExchange() throws Exception { + final User user = this.userService.save( + new User(null, null, "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)); @@ -148,49 +155,55 @@ public class ExchangeControllerTest extends AbstractControllerTest { 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()); + mockMvc.perform(post(EXCHANGE_ENDPOINT).header("Authorization", "Bearer " + token) + .contentType(APPLICATION_JSON_UTF8) + .content("{\"assignment\": " + assignment.getId() + ", \"group\": " + groupDesired.getId() + " }")) + .andExpect(status().isOk()); } @Test - public void shouldFailInsertExchangeDueToMissingGroup() throws Exception{ + public void shouldFailInsertExchangeDueToMissingGroup() throws Exception { final User user = this.userService.save(new User(null, null, - "shouldFailInsertExchangeDueToMissingGroup@ExchangeController.test", UserRoles.STUDENT)); + "shouldFailInsertExchangeDueToMissingGroup@ExchangeController.test", UserRoles.STUDENT)); final String token = this.userService.login(user).getToken(); 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()); + mockMvc.perform(post(EXCHANGE_ENDPOINT).header("Authorization", "Bearer " + token) + .contentType(APPLICATION_JSON_UTF8).content("{\"assignment\": " + user.getId() + " }")) + .andExpect(status().isBadRequest()); } @Test - public void shouldFailInsertExchangeDueToMissingAssignment() throws Exception{ + public void shouldFailInsertExchangeDueToMissingAssignment() throws Exception { final User user = this.userService.save(new User(null, null, - "shouldFailInsertExchangeDueToMissingAssignment@ExchangeController.test", UserRoles.STUDENT)); + "shouldFailInsertExchangeDueToMissingAssignment@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)); 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()); + mockMvc.perform(post(EXCHANGE_ENDPOINT).header("Authorization", "Bearer " + token) + .contentType(APPLICATION_JSON_UTF8).content("{\"group\": " + group.getId() + " }")) + .andExpect(status().isBadRequest()); } @Test - public void shouldFailInsertExchangeDueToMissingParam() throws Exception{ + public void shouldFailInsertExchangeDueToMissingParam() throws Exception { final User user = this.userService.save(new User(null, null, - "shouldFailInsertExchangeDueToMissingParam@ExchangeController.test", UserRoles.STUDENT)); + "shouldFailInsertExchangeDueToMissingParam@ExchangeController.test", UserRoles.STUDENT)); final String token = this.userService.login(user).getToken(); MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build(); - mockMvc.perform(post(EXCHANGE_ENDPOINT).header("Authorization", "Bearer " + - token).contentType(APPLICATION_JSON_UTF8)).andExpect(status().isBadRequest()); + 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() throws Exception{ - final User user = this.userService.save(new User(null, null, - "shouldDenyExchangeDueToAssigmentOverlapping@ExchangeController.test", "11111", UserRoles.STUDENT, 320)); + public void shouldDenyExchangeDueToAssigmentOverlapping() throws Exception { + final User user = this.userService + .save(new User(null, null, "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)); @@ -198,30 +211,34 @@ public class ExchangeControllerTest extends AbstractControllerTest { 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()); + 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{ + public void shouldDenyPostDueToAssignmentNotAccepted() throws Exception { final User user = this.userService.save(new User(null, null, - "shouldDenyPostDueToAssignmentNotAccepted@ExchangeController.test", UserRoles.STUDENT)); + "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)); + 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.perform(post(EXCHANGE_ENDPOINT).header("Authorization", "Bearer " + - token).contentType(APPLICATION_JSON_UTF8).content("{\"assignment\": "+ assignment.getId() +", \"group\": "+ group2.getId() +" }")).andExpect(status().isBadRequest()); + mockMvc.perform( + post(EXCHANGE_ENDPOINT).header("Authorization", "Bearer " + token).contentType(APPLICATION_JSON_UTF8) + .content("{\"assignment\": " + assignment.getId() + ", \"group\": " + group2.getId() + " }")) + .andExpect(status().isBadRequest()); } @Test @DirtiesContext(methodMode = MethodMode.BEFORE_METHOD) - public void shouldDeleteExchange() throws Exception{ - final User user = this.userService.save(new User(null, null, - "shouldDeleteExchange@ExchangeController.test", "11111", UserRoles.STUDENT, 320)); + public void shouldDeleteExchange() throws Exception { + final User user = this.userService.save( + new User(null, null, "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)); @@ -230,14 +247,14 @@ public class ExchangeControllerTest extends AbstractControllerTest { 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()); + mockMvc.perform(delete(EXCHANGE_ENDPOINT + "/" + exchange.getId()).header("Authorization", "Bearer " + token)) + .andExpect(status().isOk()); } @Test - public void shouldFailDeleteDueToWrongPermissions() throws Exception{ + public void shouldFailDeleteDueToWrongPermissions() throws Exception { final User user = this.userService.save(new User(null, null, - "shouldFailDeleteDueToWrongPermissions@ExchangeController.test", "11111", UserRoles.STUDENT, 320)); + "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)); @@ -245,39 +262,41 @@ public class ExchangeControllerTest extends AbstractControllerTest { 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)); + "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()); + mockMvc.perform(get(EXCHANGE_ENDPOINT + "/" + exchange.getId()).header("Authorization", "Bearer " + token2)) + .andExpect(status().isBadRequest()); } @Test - public void shouldFailDeleteDueToMissingParam() throws Exception{ - final User user = this.userService.save(new User(null, null, - "shouldFailDeleteDueToMissingParam@ExchangeController.test", UserRoles.STUDENT)); + public void shouldFailDeleteDueToMissingParam() throws Exception { + final User user = this.userService.save( + new User(null, null, "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()); + mockMvc.perform(delete(EXCHANGE_ENDPOINT).header("Authorization", "Bearer " + token)) + .andExpect(status().is4xxClientError()); } @Test - public void shouldFailDeleteDueToExchangeNotFound() throws Exception{ + public void shouldFailDeleteDueToExchangeNotFound() throws Exception { final User user = this.userService.save(new User(null, null, - "shouldFailDeleteDueToExchangeNotFound@ExchangeController.test", UserRoles.STUDENT)); + "shouldFailDeleteDueToExchangeNotFound@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 + "/" + user.getId()).header("Authorization", "Bearer " + - token)).andExpect(status().isBadRequest()); + mockMvc.perform(delete(EXCHANGE_ENDPOINT + "/" + user.getId()).header("Authorization", "Bearer " + token)) + .andExpect(status().isBadRequest()); } @Test @DirtiesContext(methodMode = MethodMode.BEFORE_METHOD) - public void shouldFailPostDueToGroupAlreadyAccepted() throws Exception{ - final User user = this.userService.save(new User(null, null, "shouldFailPostDueToGroupAlreadyAccepted@ExchangeController.test", "11111", UserRoles.STUDENT, 320)); + public void shouldFailPostDueToGroupAlreadyAccepted() throws Exception { + final User user = this.userService.save(new User(null, null, + "shouldFailPostDueToGroupAlreadyAccepted@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 group2 = this.groupService.save(new Groups(216, "A2-3", null, 530, WeekDay.MONDAY, null)); @@ -288,7 +307,55 @@ public class ExchangeControllerTest extends AbstractControllerTest { this.exchangeService.save(new Exchange(assignment, group2)); 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\": "+ group2.getId() +" }")).andExpect(status().isBadRequest()); + mockMvc.perform( + post(EXCHANGE_ENDPOINT).header("Authorization", "Bearer " + token).contentType(APPLICATION_JSON_UTF8) + .content("{\"assignment\": " + assignment.getId() + ", \"group\": " + group2.getId() + " }")) + .andExpect(status().isBadRequest()); + } + + @Test + @DirtiesContext(methodMode = MethodMode.BEFORE_METHOD) + public void shouldFailForDiffrentCoursesExchange() throws Exception { + final User user = this.userService.save( + new User(null, null, "shouldInsertExchange@ExchangeController.test", "11111", UserRoles.STUDENT, 320)); + final String token = this.userService.login(user).getToken(); + final Course course = this.courseService + .save(new Course("shouldFailForDiffrentCoursesExchangeCourse", "SFFDCEC")); + final Course course2 = this.courseService + .save(new Course("shouldFailForDiffrentCoursesExchangeCourse2", "SFFDCEC-2")); + final Groups group = this.groupService.save(new Groups(215, "A2-2", course, 520, WeekDay.TUESDAY, null)); + final Groups groupDesired = this.groupService.save(new Groups(216, "A2-3", course2, 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().isBadRequest()); + } + + @Test + @DirtiesContext(methodMode = MethodMode.BEFORE_METHOD) + public void shouldFailForDiffrentGroupTypesExchange() throws Exception { + final User user = this.userService.save( + new User(null, null, "shouldInsertExchange@ExchangeController.test", "11111", UserRoles.STUDENT, 320)); + final String token = this.userService.login(user).getToken(); + final Course course = this.courseService + .save(new Course("shouldFailForDiffrentCoursesExchangeCourse", "SFFDCEC")); + final Groups group = this.groupService.save(new Groups(12, "A2-2", course, 520, WeekDay.TUESDAY, null)); + final Groups groupDesired = this.groupService.save(new Groups(216, "A2-3", course, 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().isBadRequest()); } }