Tests works

This commit is contained in:
BuildTools 2020-10-30 14:27:13 +01:00
parent b40acf8919
commit 956b53439f
2 changed files with 122 additions and 55 deletions

View File

@ -34,56 +34,64 @@ import org.springframework.web.bind.annotation.RequestMapping;
@RequestMapping("/api/" + App.API_VERSION + "/commisions") @RequestMapping("/api/" + App.API_VERSION + "/commisions")
public class CommisionController extends TokenBasedController { public class CommisionController extends TokenBasedController {
@Autowired @Autowired
private CommisionService commisionService; private CommisionService commisionService;
@Autowired @Autowired
private GroupService groupServcicxe; private GroupService groupServcicxe;
@Autowired @Autowired
private AssignmentService assignmentService; private AssignmentService assignmentService;
public CommisionController() { public CommisionController() {
} }
@PostMapping(value = { "/add", "/add/{id}" }) @PostMapping(value = { "/add", "/add/{id}" })
public ResponseEntity<String> addCommision(@RequestBody List<Long> groups, public ResponseEntity<String> addCommision(@RequestBody List<Long> groups,
@PathVariable(name = "id", required = false) Long userId) @PathVariable(name = "id", required = false) Long userId) {
throws UserNotFoundException, IllegalArgumentException {
final User asker = this.getCurrentUser().orElseThrow(() -> new UserNotFoundException("Invalid token")); try {
final User user = userId != null final User asker = this.getCurrentUser()
? userService.getById(userId).orElseThrow(() -> new UserNotFoundException("Given user id not exist")) .orElseThrow(() -> new UserNotFoundException("Invalid token"));
: asker;
Assert.isTrue( final User user = userId != null
(asker.getRole() == UserRoles.DEANERY && user.getRole() == UserRoles.STUDENT ? userService.getById(userId).orElseThrow(
|| (asker.getId() == user.getId() && user.getRole() == UserRoles.STUDENT)), () -> new UserNotFoundException("Given user id not exist"))
"Incorrect attempt to change plan"); : asker;
Optional<Long> notExistingGroup = this.groupServcicxe.findNotExistingGroup(groups); Assert.isTrue((asker.getRole() == UserRoles.DEANERY && user.getRole() == UserRoles.STUDENT
Assert.isTrue(!notExistingGroup.isPresent(), || (asker.getId() == user.getId() && user.getRole() == UserRoles.STUDENT)),
"Group " + notExistingGroup.orElse(Long.MIN_VALUE).toString() + "doesn't exist"); "Incorrect attempt to change plan");
final Commision com = new Commision(user); Optional<Long> notExistingGroup = this.groupServcicxe.findNotExistingGroup(groups);
this.commisionService.save(com); Assert.isTrue(!notExistingGroup.isPresent(), "Group "
+ notExistingGroup.orElse(Long.MIN_VALUE).toString() + "doesn't exist");
groups.stream().forEach((groupId) -> { final Commision com = new Commision(user);
Groups group = this.groupServcicxe.getGroupById(groupId).orElseThrow(() -> new NullPointerException()); this.commisionService.save(com);
Assignment a = new Assignment(group, com);
this.assignmentService.save(a);
});
return new ResponseEntity<>("Succes", HttpStatus.OK); groups.stream().forEach((groupId) -> {
} Groups group = this.groupServcicxe.getGroupById(groupId)
.orElseThrow(() -> new NullPointerException());
Assignment a = new Assignment(group, com);
this.assignmentService.save(a);
});
@GetMapping("/getAllCommisions") return new ResponseEntity<>("Succes", HttpStatus.OK);
public ResponseEntity<List<CommisionResponse>> getAlCommisions() throws UserNotFoundException { } catch (UserNotFoundException exception) {
User user = this.getCurrentUser().orElseThrow(() -> new NullPointerException()); return new ResponseEntity<>(exception.getMessage(), HttpStatus.NOT_FOUND);
List<CommisionResponse> result = CommisionResponseMappers } catch (IllegalArgumentException exception) {
.mapToResponse(this.commisionService.getUsersCommisions(user)); return new ResponseEntity<>(exception.getMessage(), HttpStatus.BAD_REQUEST);
return new ResponseEntity<>(result, HttpStatus.OK); }
} }
@GetMapping("/getAllCommisions")
public ResponseEntity<List<CommisionResponse>> getAlCommisions() throws UserNotFoundException {
User user = this.getCurrentUser().orElseThrow(() -> new NullPointerException());
List<CommisionResponse> result = CommisionResponseMappers
.mapToResponse(this.commisionService.getUsersCommisions(user));
return new ResponseEntity<>(result, HttpStatus.OK);
}
} }

View File

@ -19,7 +19,6 @@ import com.plannaplan.entities.User;
import com.plannaplan.services.UserService; import com.plannaplan.services.UserService;
import com.plannaplan.types.UserRoles; import com.plannaplan.types.UserRoles;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.*; import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
@ -36,10 +35,18 @@ public class CommisionControllerTest {
private UserService service; private UserService service;
private static User user; private static User user;
private static User otherUser;
private static User asker;
private static User otherAsker;
private static final String TEST_COMMISIONS_STUDENT_EMAIL = "commisions.student@notexisting.domain";
private static final String TEST_COMMISIONS_OTHER_STUDENT_EMAIL = "commisions.student2@notexisting.domain";
private static final String TEST_COMMISIONS_DEANERY_EMAIL = "commisions.deanery@notexisting.domain";
private static final String TEST_COMMISIONS_OTHER_DEANERY_EMAIL = "commisions.deanery2@notexisting.domain";
private static final String ADD_COMMISION_ENDPOINT = "/api/v1/commisions/add"; private static final String ADD_COMMISION_ENDPOINT = "/api/v1/commisions/add";
private static final String GET_COMMISIONS_ENDPOINT = "/api/v1/commisions/getAllCommisions"; private static final String GET_COMMISIONS_ENDPOINT = "/api/v1/commisions/getAllCommisions";
private static final String TEST_COMMISIONS_EMAIL = "commisions@notexisting.domain";
private static final MediaType APPLICATION_JSON_UTF8 = new MediaType(MediaType.APPLICATION_JSON.getType(), private static final MediaType APPLICATION_JSON_UTF8 = new MediaType(MediaType.APPLICATION_JSON.getType(),
MediaType.APPLICATION_JSON.getSubtype(), Charset.forName("utf8")); MediaType.APPLICATION_JSON.getSubtype(), Charset.forName("utf8"));
@ -52,8 +59,8 @@ public class CommisionControllerTest {
@Test @Test
public void shouldFailedAddingCommisionDueToNoArgs() throws Exception { public void shouldFailedAddingCommisionDueToNoArgs() throws Exception {
this.checkUser(); this.checkUsers();
final String token = this.service.login(TEST_COMMISIONS_EMAIL); final String token = this.service.login(TEST_COMMISIONS_STUDENT_EMAIL);
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build(); MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(post(ADD_COMMISION_ENDPOINT).header("Authorization", "Bearer " + token)) mockMvc.perform(post(ADD_COMMISION_ENDPOINT).header("Authorization", "Bearer " + token))
@ -62,8 +69,8 @@ public class CommisionControllerTest {
@Test @Test
public void shouldReturnOkAddingCommision() throws Exception { public void shouldReturnOkAddingCommision() throws Exception {
this.checkUser(); this.checkUsers();
final String token = this.service.login(TEST_COMMISIONS_EMAIL); final String token = this.service.login(TEST_COMMISIONS_STUDENT_EMAIL);
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build(); MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(post(ADD_COMMISION_ENDPOINT).header("Authorization", "Bearer " + token) mockMvc.perform(post(ADD_COMMISION_ENDPOINT).header("Authorization", "Bearer " + token)
@ -78,8 +85,8 @@ public class CommisionControllerTest {
@Test @Test
public void shouldReturnOkGettingAllCommisions() throws Exception { public void shouldReturnOkGettingAllCommisions() throws Exception {
this.checkUser(); this.checkUsers();
final String token = this.service.login(TEST_COMMISIONS_EMAIL); final String token = this.service.login(TEST_COMMISIONS_STUDENT_EMAIL);
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build(); MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(get(GET_COMMISIONS_ENDPOINT).header("Authorization", "Bearer " + token)) mockMvc.perform(get(GET_COMMISIONS_ENDPOINT).header("Authorization", "Bearer " + token))
@ -88,38 +95,90 @@ public class CommisionControllerTest {
@Test @Test
public void shouldAddCommisionWithSelfIdPrivided() throws Exception { public void shouldAddCommisionWithSelfIdPrivided() throws Exception {
assertTrue(false); this.checkUsers();
final String token = this.service.login(TEST_COMMISIONS_STUDENT_EMAIL);
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(post(ADD_COMMISION_ENDPOINT + "/" + CommisionControllerTest.user.getId().toString())
.header("Authorization", "Bearer " + token).contentType(APPLICATION_JSON_UTF8).content("[]"))
.andExpect(status().isOk());
} }
@Test @Test
public void shouldFailCommisionWithSomeoneIdPrividedAsStudent() throws Exception { public void shouldFailCommisionWithSomeoneIdPrividedAsStudent() throws Exception {
assertTrue(false); this.checkUsers();
final String token = this.service.login(TEST_COMMISIONS_STUDENT_EMAIL);
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(post(ADD_COMMISION_ENDPOINT + "/" + CommisionControllerTest.otherUser.getId().toString())
.header("Authorization", "Bearer " + token).contentType(APPLICATION_JSON_UTF8).content("[]"))
.andExpect(status().is4xxClientError());
} }
@Test @Test
public void shouldFailCommisionAsDeanaryWithNoId() throws Exception { public void shouldFailCommisionAsDeanaryWithNoId() throws Exception {
assertTrue(false); this.checkUsers();
final String token = this.service.login(TEST_COMMISIONS_DEANERY_EMAIL);
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(post(ADD_COMMISION_ENDPOINT).header("Authorization", "Bearer " + token)
.contentType(APPLICATION_JSON_UTF8).content("[]")).andExpect(status().is4xxClientError());
} }
@Test @Test
public void shouldFailCommisionWithSelfIdPrividedAsDeanary() throws Exception { public void shouldFailCommisionWithSelfIdPrividedAsDeanary() throws Exception {
assertTrue(false); this.checkUsers();
final String token = this.service.login(TEST_COMMISIONS_DEANERY_EMAIL);
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(post(ADD_COMMISION_ENDPOINT + "/" + CommisionControllerTest.asker.getId().toString())
.header("Authorization", "Bearer " + token).contentType(APPLICATION_JSON_UTF8).content("[]"))
.andExpect(status().is4xxClientError());
} }
@Test @Test
public void shouldAddCommisionWithSomeoneIdPrividedAsDeanary() throws Exception { public void shouldAddCommisionWithSomeoneIdPrividedAsDeanary() throws Exception {
assertTrue(false); this.checkUsers();
final String token = this.service.login(TEST_COMMISIONS_DEANERY_EMAIL);
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(post(ADD_COMMISION_ENDPOINT + "/" + CommisionControllerTest.otherUser.getId().toString())
.header("Authorization", "Bearer " + token).contentType(APPLICATION_JSON_UTF8).content("[]"))
.andExpect(status().isOk());
} }
@Test @Test
public void shouldFailCommisionWithOtherDeanaryIdPrividedAsDeanary() throws Exception { public void shouldFailCommisionWithOtherDeanaryIdPrividedAsDeanary() throws Exception {
assertTrue(false); this.checkUsers();
final String token = this.service.login(TEST_COMMISIONS_DEANERY_EMAIL);
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(post(ADD_COMMISION_ENDPOINT + "/" + CommisionControllerTest.otherAsker.getId().toString())
.header("Authorization", "Bearer " + token).contentType(APPLICATION_JSON_UTF8).content("[]"))
.andExpect(status().is4xxClientError());
} }
private void checkUser() { private void checkUsers() {
if (CommisionControllerTest.user == null) { if (CommisionControllerTest.user == null) {
CommisionControllerTest.user = new User(null, null, TEST_COMMISIONS_EMAIL, UserRoles.TEST_USER); CommisionControllerTest.user = new User(null, null, TEST_COMMISIONS_STUDENT_EMAIL, UserRoles.STUDENT);
this.service.save(user); this.service.save(user);
} }
if (CommisionControllerTest.otherUser == null) {
CommisionControllerTest.otherUser = new User(null, null, TEST_COMMISIONS_OTHER_STUDENT_EMAIL,
UserRoles.STUDENT);
this.service.save(otherUser);
}
if (CommisionControllerTest.asker == null) {
CommisionControllerTest.asker = new User(null, null, TEST_COMMISIONS_DEANERY_EMAIL, UserRoles.DEANERY);
this.service.save(asker);
}
if (CommisionControllerTest.otherAsker == null) {
CommisionControllerTest.otherAsker = new User(null, null, TEST_COMMISIONS_OTHER_DEANERY_EMAIL,
UserRoles.DEANERY);
this.service.save(otherAsker);
}
} }
} }