Merge pull request 'exchanges-new-case' (#46) from exchanges-new-case into master

Reviewed-on: http://git.plannaplan.pl/filipizydorczyk/backend/pulls/46
This commit is contained in:
Marcin Woźniak 2021-01-18 16:28:11 +01:00
commit c89d629c61
2 changed files with 147 additions and 71 deletions

View File

@ -88,6 +88,15 @@ public class ExchangeController extends TokenBasedController {
final Assignment assignmentInstance = assignment.get(); final Assignment assignmentInstance = assignment.get();
final Groups groupInstance = group.get(); final Groups groupInstance = group.get();
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);
}
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()) if (!(assignmentInstance.getCommision().getCommisionOwner().getId().equals(asker.getId())
&& assignmentInstance.isAccepted())) { && assignmentInstance.isAccepted())) {
return new ResponseEntity<>( return new ResponseEntity<>(

View File

@ -22,11 +22,13 @@ import java.nio.charset.Charset;
import com.plannaplan.entities.Assignment; import com.plannaplan.entities.Assignment;
import com.plannaplan.entities.Commision; import com.plannaplan.entities.Commision;
import com.plannaplan.entities.Course;
import com.plannaplan.entities.Exchange; import com.plannaplan.entities.Exchange;
import com.plannaplan.entities.Groups; import com.plannaplan.entities.Groups;
import com.plannaplan.entities.User; import com.plannaplan.entities.User;
import com.plannaplan.services.AssignmentService; import com.plannaplan.services.AssignmentService;
import com.plannaplan.services.CommisionService; import com.plannaplan.services.CommisionService;
import com.plannaplan.services.CourseService;
import com.plannaplan.services.ExchangeService; import com.plannaplan.services.ExchangeService;
import com.plannaplan.services.GroupService; import com.plannaplan.services.GroupService;
import com.plannaplan.services.UserService; import com.plannaplan.services.UserService;
@ -57,6 +59,9 @@ public class ExchangeControllerTest extends AbstractControllerTest {
@Autowired @Autowired
private ExchangeService exchangeService; private ExchangeService exchangeService;
@Autowired
private CourseService courseService;
@Test @Test
@DirtiesContext(methodMode = MethodMode.BEFORE_METHOD) @DirtiesContext(methodMode = MethodMode.BEFORE_METHOD)
public void shouldGetAllUsersExchanges() throws Exception { public void shouldGetAllUsersExchanges() throws Exception {
@ -70,8 +75,8 @@ public class ExchangeControllerTest extends AbstractControllerTest {
this.exchangeService.save(new Exchange(assignment, groupDesired)); this.exchangeService.save(new Exchange(assignment, groupDesired));
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build(); MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(get(EXCHANGE_ENDPOINT + "/all").header("Authorization", "Bearer " + mockMvc.perform(get(EXCHANGE_ENDPOINT + "/all").header("Authorization", "Bearer " + token))
token)).andExpect(status().isOk()); .andExpect(status().isOk());
} }
@Test @Test
@ -82,14 +87,15 @@ public class ExchangeControllerTest extends AbstractControllerTest {
final String token = this.userService.login(user).getToken(); 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(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 @Test
@DirtiesContext(methodMode = MethodMode.BEFORE_METHOD) @DirtiesContext(methodMode = MethodMode.BEFORE_METHOD)
public void shouldGetSingleExchange() throws Exception{ public void shouldGetSingleExchange() throws Exception {
final User user = this.userService.save(new User(null, null, final User user = this.userService.save(new User(null, null, "shouldGetSingleExchange@ExchangeController.test",
"shouldGetSingleExchange@ExchangeController.test", "11111", UserRoles.STUDENT, 320)); "11111", UserRoles.STUDENT, 320));
final String token = this.userService.login(user).getToken(); 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 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 Groups groupDesired = this.groupService.save(new Groups(216, "A2-3", null, 530, WeekDay.MONDAY, null));
@ -98,12 +104,12 @@ public class ExchangeControllerTest extends AbstractControllerTest {
final Exchange exchange = this.exchangeService.save(new Exchange(assignment, groupDesired)); final Exchange exchange = this.exchangeService.save(new Exchange(assignment, groupDesired));
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build(); MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(get(EXCHANGE_ENDPOINT + "/" + exchange.getId()).header("Authorization", "Bearer " + mockMvc.perform(get(EXCHANGE_ENDPOINT + "/" + exchange.getId()).header("Authorization", "Bearer " + token))
token)).andExpect(status().isOk()); .andExpect(status().isOk());
} }
@Test @Test
public void shouldFailGettingExchangeDueToPermission() throws Exception{ public void shouldFailGettingExchangeDueToPermission() throws Exception {
final User user = this.userService.save(new User(null, null, 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 group = this.groupService.save(new Groups(215, "A2-2", null, 520, WeekDay.TUESDAY, null));
@ -117,28 +123,29 @@ public class ExchangeControllerTest extends AbstractControllerTest {
final String token2 = this.userService.login(user2).getToken(); final String token2 = this.userService.login(user2).getToken();
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build(); MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(post(EXCHANGE_ENDPOINT + "/" + exchange.getId()).header("Authorization", "Bearer " + mockMvc.perform(post(EXCHANGE_ENDPOINT + "/" + exchange.getId()).header("Authorization", "Bearer " + token2))
token2)).andExpect(status().is4xxClientError()); .andExpect(status().is4xxClientError());
} }
@Test @Test
public void shouldFailPostDueToAssignmentNotFound() throws Exception{ public void shouldFailPostDueToAssignmentNotFound() throws Exception {
final User user = this.userService.save(new User(null, null, 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 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 group = this.groupService.save(new Groups(212, "A2-1", null, 420, WeekDay.WEDNESDAY, null));
MockMvc mockMvc = MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build(); mockMvc.perform(
mockMvc.perform(post(EXCHANGE_ENDPOINT).header("Authorization", "Bearer " + post(EXCHANGE_ENDPOINT).header("Authorization", "Bearer " + token).contentType(APPLICATION_JSON_UTF8)
token).contentType(APPLICATION_JSON_UTF8).content("{\"assignment\": "+ user.getId() +", \"group\": "+ group.getId() +" }")).andExpect(status().isBadRequest()); .content("{\"assignment\": " + user.getId() + ", \"group\": " + group.getId() + " }"))
.andExpect(status().isBadRequest());
} }
@Test @Test
@DirtiesContext(methodMode = MethodMode.BEFORE_METHOD) @DirtiesContext(methodMode = MethodMode.BEFORE_METHOD)
public void shouldInsertExchange() throws Exception{ public void shouldInsertExchange() throws Exception {
final User user = this.userService.save(new User(null, null, final User user = this.userService.save(
"shouldInsertExchange@ExchangeController.test", "11111", UserRoles.STUDENT, 320)); new User(null, null, "shouldInsertExchange@ExchangeController.test", "11111", UserRoles.STUDENT, 320));
final String token = this.userService.login(user).getToken(); 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 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 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(); this.assignmentService.callAcceptAlgorythm();
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build(); MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(post(EXCHANGE_ENDPOINT).header("Authorization", "Bearer " + mockMvc.perform(post(EXCHANGE_ENDPOINT).header("Authorization", "Bearer " + token)
token).contentType(APPLICATION_JSON_UTF8).content("{\"assignment\": "+ assignment.getId() +", \"group\": "+ groupDesired.getId() +" }")).andExpect(status().isOk()); .contentType(APPLICATION_JSON_UTF8)
.content("{\"assignment\": " + assignment.getId() + ", \"group\": " + groupDesired.getId() + " }"))
.andExpect(status().isOk());
} }
@Test @Test
public void shouldFailInsertExchangeDueToMissingGroup() throws Exception{ public void shouldFailInsertExchangeDueToMissingGroup() throws Exception {
final User user = this.userService.save(new User(null, null, 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(); 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 " + mockMvc.perform(post(EXCHANGE_ENDPOINT).header("Authorization", "Bearer " + token)
token).contentType(APPLICATION_JSON_UTF8).content("{\"assignment\": "+ user.getId() +" }")).andExpect(status().isBadRequest()); .contentType(APPLICATION_JSON_UTF8).content("{\"assignment\": " + user.getId() + " }"))
.andExpect(status().isBadRequest());
} }
@Test @Test
public void shouldFailInsertExchangeDueToMissingAssignment() throws Exception{ public void shouldFailInsertExchangeDueToMissingAssignment() throws Exception {
final User user = this.userService.save(new User(null, null, 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 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 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 " + mockMvc.perform(post(EXCHANGE_ENDPOINT).header("Authorization", "Bearer " + token)
token).contentType(APPLICATION_JSON_UTF8).content("{\"group\": "+ group.getId() +" }")).andExpect(status().isBadRequest()); .contentType(APPLICATION_JSON_UTF8).content("{\"group\": " + group.getId() + " }"))
.andExpect(status().isBadRequest());
} }
@Test @Test
public void shouldFailInsertExchangeDueToMissingParam() throws Exception{ public void shouldFailInsertExchangeDueToMissingParam() throws Exception {
final User user = this.userService.save(new User(null, null, 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(); 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 " + mockMvc.perform(
token).contentType(APPLICATION_JSON_UTF8)).andExpect(status().isBadRequest()); post(EXCHANGE_ENDPOINT).header("Authorization", "Bearer " + token).contentType(APPLICATION_JSON_UTF8))
.andExpect(status().isBadRequest());
} }
@Test @Test
@DirtiesContext(methodMode = MethodMode.BEFORE_METHOD) @DirtiesContext(methodMode = MethodMode.BEFORE_METHOD)
public void shouldDenyExchangeDueToAssigmentOverlapping() throws Exception{ public void shouldDenyExchangeDueToAssigmentOverlapping() throws Exception {
final User user = this.userService.save(new User(null, null, final User user = this.userService
"shouldDenyExchangeDueToAssigmentOverlapping@ExchangeController.test", "11111", UserRoles.STUDENT, 320)); .save(new User(null, null, "shouldDenyExchangeDueToAssigmentOverlapping@ExchangeController.test",
"11111", UserRoles.STUDENT, 320));
final String token = this.userService.login(user).getToken(); 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 group = this.groupService.save(new Groups(215, "A2-2", null, 520, WeekDay.TUESDAY, null));
final Commision commision = this.commisionService.save(new Commision(user)); 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)); this.exchangeService.save(new Exchange(assignment, group));
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build(); MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(post(EXCHANGE_ENDPOINT).header("Authorization", "Bearer " + mockMvc.perform(
token).contentType(APPLICATION_JSON_UTF8).content("{\"assignment\": "+ user.getId() +", \"group\": "+ group.getId() +" }")).andExpect(status().isBadRequest()); post(EXCHANGE_ENDPOINT).header("Authorization", "Bearer " + token).contentType(APPLICATION_JSON_UTF8)
.content("{\"assignment\": " + user.getId() + ", \"group\": " + group.getId() + " }"))
.andExpect(status().isBadRequest());
} }
@Test @Test
public void shouldDenyPostDueToAssignmentNotAccepted() throws Exception{ public void shouldDenyPostDueToAssignmentNotAccepted() throws Exception {
final User user = this.userService.save(new User(null, null, 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 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 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 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 Commision commision = this.commisionService.save(new Commision(user));
final Assignment assignment = this.assignmentService.save(new Assignment(group,commision)); 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 " + mockMvc.perform(
token).contentType(APPLICATION_JSON_UTF8).content("{\"assignment\": "+ assignment.getId() +", \"group\": "+ group2.getId() +" }")).andExpect(status().isBadRequest()); post(EXCHANGE_ENDPOINT).header("Authorization", "Bearer " + token).contentType(APPLICATION_JSON_UTF8)
.content("{\"assignment\": " + assignment.getId() + ", \"group\": " + group2.getId() + " }"))
.andExpect(status().isBadRequest());
} }
@Test @Test
@DirtiesContext(methodMode = MethodMode.BEFORE_METHOD) @DirtiesContext(methodMode = MethodMode.BEFORE_METHOD)
public void shouldDeleteExchange() throws Exception{ public void shouldDeleteExchange() throws Exception {
final User user = this.userService.save(new User(null, null, final User user = this.userService.save(
"shouldDeleteExchange@ExchangeController.test", "11111", UserRoles.STUDENT, 320)); new User(null, null, "shouldDeleteExchange@ExchangeController.test", "11111", UserRoles.STUDENT, 320));
final String token = this.userService.login(user).getToken(); 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 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 Groups groupDesired = this.groupService.save(new Groups(216, "A2-3", null, 530, WeekDay.MONDAY, null));
@ -230,12 +247,12 @@ public class ExchangeControllerTest extends AbstractControllerTest {
final Exchange exchange = this.exchangeService.save(new Exchange(assignment, groupDesired)); final Exchange exchange = this.exchangeService.save(new Exchange(assignment, groupDesired));
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build(); MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(delete(EXCHANGE_ENDPOINT + "/" + exchange.getId()).header("Authorization", "Bearer " + mockMvc.perform(delete(EXCHANGE_ENDPOINT + "/" + exchange.getId()).header("Authorization", "Bearer " + token))
token)).andExpect(status().isOk()); .andExpect(status().isOk());
} }
@Test @Test
public void shouldFailDeleteDueToWrongPermissions() throws Exception{ public void shouldFailDeleteDueToWrongPermissions() throws Exception {
final User user = this.userService.save(new User(null, null, 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 group = this.groupService.save(new Groups(215, "A2-2", null, 520, WeekDay.TUESDAY, null));
@ -249,35 +266,37 @@ public class ExchangeControllerTest extends AbstractControllerTest {
final String token2 = this.userService.login(user2).getToken(); final String token2 = this.userService.login(user2).getToken();
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build(); MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(get(EXCHANGE_ENDPOINT + "/" + exchange.getId()).header("Authorization", "Bearer " + mockMvc.perform(get(EXCHANGE_ENDPOINT + "/" + exchange.getId()).header("Authorization", "Bearer " + token2))
token2)).andExpect(status().isBadRequest()); .andExpect(status().isBadRequest());
} }
@Test @Test
public void shouldFailDeleteDueToMissingParam() throws Exception{ public void shouldFailDeleteDueToMissingParam() throws Exception {
final User user = this.userService.save(new User(null, null, final User user = this.userService.save(
"shouldFailDeleteDueToMissingParam@ExchangeController.test", UserRoles.STUDENT)); new User(null, null, "shouldFailDeleteDueToMissingParam@ExchangeController.test", UserRoles.STUDENT));
final String token = this.userService.login(user).getToken(); 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(delete(EXCHANGE_ENDPOINT).header("Authorization", "Bearer " + token)).andExpect(status().is4xxClientError()); mockMvc.perform(delete(EXCHANGE_ENDPOINT).header("Authorization", "Bearer " + token))
.andExpect(status().is4xxClientError());
} }
@Test @Test
public void shouldFailDeleteDueToExchangeNotFound() throws Exception{ public void shouldFailDeleteDueToExchangeNotFound() throws Exception {
final User user = this.userService.save(new User(null, null, 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(); 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(delete(EXCHANGE_ENDPOINT + "/" + user.getId()).header("Authorization", "Bearer " + mockMvc.perform(delete(EXCHANGE_ENDPOINT + "/" + user.getId()).header("Authorization", "Bearer " + token))
token)).andExpect(status().isBadRequest()); .andExpect(status().isBadRequest());
} }
@Test @Test
@DirtiesContext(methodMode = MethodMode.BEFORE_METHOD) @DirtiesContext(methodMode = MethodMode.BEFORE_METHOD)
public void shouldFailPostDueToGroupAlreadyAccepted() throws Exception{ public void shouldFailPostDueToGroupAlreadyAccepted() throws Exception {
final User user = this.userService.save(new User(null, null, "shouldFailPostDueToGroupAlreadyAccepted@ExchangeController.test", "11111", UserRoles.STUDENT, 320)); 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 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 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)); 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)); this.exchangeService.save(new Exchange(assignment, group2));
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build(); MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(post(EXCHANGE_ENDPOINT).header("Authorization", "Bearer " + mockMvc.perform(
token).contentType(APPLICATION_JSON_UTF8).content("{\"assignment\": "+ assignment.getId() +", \"group\": "+ group2.getId() +" }")).andExpect(status().isBadRequest()); 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());
} }
} }