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));
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,8 +104,8 @@ 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
@ -117,8 +123,8 @@ 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
@ -128,17 +134,18 @@ 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.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));
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,8 +155,10 @@ 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
@ -159,8 +168,9 @@ public class ExchangeControllerTest extends AbstractControllerTest {
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
@ -171,8 +181,9 @@ public class ExchangeControllerTest extends AbstractControllerTest {
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
@ -182,15 +193,17 @@ public class ExchangeControllerTest extends AbstractControllerTest {
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));
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,8 +211,10 @@ 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
@ -213,15 +228,17 @@ public class ExchangeControllerTest extends AbstractControllerTest {
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));
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,8 +247,8 @@ 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
@ -249,18 +266,19 @@ 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));
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
@ -270,14 +288,15 @@ public class ExchangeControllerTest extends AbstractControllerTest {
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));
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());
}
}