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 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())
&& assignmentInstance.isAccepted())) {
return new ResponseEntity<>(

View File

@ -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,6 +59,9 @@ public class ExchangeControllerTest extends AbstractControllerTest {
@Autowired
private ExchangeService exchangeService;
@Autowired
private CourseService courseService;
@Test
@DirtiesContext(methodMode = MethodMode.BEFORE_METHOD)
public void shouldGetAllUsersExchanges() throws Exception {
@ -70,8 +75,8 @@ 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
@ -82,14 +87,15 @@ public class ExchangeControllerTest extends AbstractControllerTest {
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,12 +104,12 @@ 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));
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();
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));
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));
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));
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));
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));
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 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,12 +247,12 @@ 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));
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();
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));
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());
}
}