Merge origin/deanery-plan-changing to master: With fixed restservice/.../App.java
Signed-off-by: Marcin Woźniak <y0rune@aol.com>
This commit is contained in:
commit
9bff6460d6
@ -41,4 +41,14 @@ public class GroupService {
|
|||||||
public Optional<Groups> getGroupById(Long id) {
|
public Optional<Groups> getGroupById(Long id) {
|
||||||
return this.repo.findById(id);
|
return this.repo.findById(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Optional<Long> findNotExistingGroup(List<Long> ids) {
|
||||||
|
for (Long oneId : ids) {
|
||||||
|
if (this.repo.existsById(oneId) == false) {
|
||||||
|
return Optional.of(oneId);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return Optional.empty();
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
@ -49,4 +49,8 @@ public class UserService {
|
|||||||
return this.repo.searchForUsers(query, UserRoles.STUDENT);
|
return this.repo.searchForUsers(query, UserRoles.STUDENT);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Optional<User> getById(Long userId) {
|
||||||
|
return this.repo.findById(userId);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
@ -2,9 +2,11 @@ package com.plannaplan.controllers;
|
|||||||
|
|
||||||
import org.springframework.web.bind.annotation.CrossOrigin;
|
import org.springframework.web.bind.annotation.CrossOrigin;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.PathVariable;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
import com.plannaplan.App;
|
import com.plannaplan.App;
|
||||||
import com.plannaplan.entities.Assignment;
|
import com.plannaplan.entities.Assignment;
|
||||||
@ -17,10 +19,12 @@ import com.plannaplan.responses.models.CommisionResponse;
|
|||||||
import com.plannaplan.services.AssignmentService;
|
import com.plannaplan.services.AssignmentService;
|
||||||
import com.plannaplan.services.CommisionService;
|
import com.plannaplan.services.CommisionService;
|
||||||
import com.plannaplan.services.GroupService;
|
import com.plannaplan.services.GroupService;
|
||||||
|
import com.plannaplan.types.UserRoles;
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.util.Assert;
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestBody;
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
@ -30,40 +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("/add")
|
@PostMapping(value = { "/add", "/add/{id}" })
|
||||||
public ResponseEntity<String> addCommision(@RequestBody List<Long> groups) throws UserNotFoundException {
|
public ResponseEntity<String> addCommision(@RequestBody List<Long> groups,
|
||||||
|
@PathVariable(name = "id", required = false) Long userId) {
|
||||||
|
|
||||||
User user = this.getCurrentUser().orElseThrow(() -> new NullPointerException());
|
try {
|
||||||
Commision com = new Commision(user);
|
|
||||||
this.commisionService.save(com);
|
|
||||||
|
|
||||||
groups.stream().forEach((groupId) -> {
|
final User asker = this.getCurrentUser()
|
||||||
Groups group = this.groupServcicxe.getGroupById(groupId).orElseThrow(() -> new NullPointerException());
|
.orElseThrow(() -> new UserNotFoundException("Invalid token"));
|
||||||
Assignment a = new Assignment(group, com);
|
|
||||||
this.assignmentService.save(a);
|
|
||||||
});
|
|
||||||
|
|
||||||
return new ResponseEntity<>("Succes", HttpStatus.OK);
|
final User user = userId != null
|
||||||
}
|
? userService.getById(userId).orElseThrow(
|
||||||
|
() -> new UserNotFoundException("Given user id not exist"))
|
||||||
|
: asker;
|
||||||
|
|
||||||
@GetMapping("/getAllCommisions")
|
Assert.isTrue((asker.getRole() == UserRoles.DEANERY && user.getRole() == UserRoles.STUDENT
|
||||||
public ResponseEntity<List<CommisionResponse>> getAlCommisions() throws UserNotFoundException {
|
|| (asker.getId() == user.getId() && user.getRole() == UserRoles.STUDENT)),
|
||||||
User user = this.getCurrentUser().orElseThrow(() -> new NullPointerException());
|
"Incorrect attempt to change plan");
|
||||||
List<CommisionResponse> result = CommisionResponseMappers
|
|
||||||
.mapToResponse(this.commisionService.getUsersCommisions(user));
|
Optional<Long> notExistingGroup = this.groupServcicxe.findNotExistingGroup(groups);
|
||||||
return new ResponseEntity<>(result, HttpStatus.OK);
|
Assert.isTrue(!notExistingGroup.isPresent(), "Group "
|
||||||
}
|
+ notExistingGroup.orElse(Long.MIN_VALUE).toString() + "doesn't exist");
|
||||||
|
|
||||||
|
final Commision com = new Commision(user);
|
||||||
|
this.commisionService.save(com);
|
||||||
|
|
||||||
|
groups.stream().forEach((groupId) -> {
|
||||||
|
Groups group = this.groupServcicxe.getGroupById(groupId)
|
||||||
|
.orElseThrow(() -> new NullPointerException());
|
||||||
|
Assignment a = new Assignment(group, com);
|
||||||
|
this.assignmentService.save(a);
|
||||||
|
});
|
||||||
|
|
||||||
|
return new ResponseEntity<>("Succes", HttpStatus.OK);
|
||||||
|
} catch (UserNotFoundException exception) {
|
||||||
|
return new ResponseEntity<>(exception.getMessage(), HttpStatus.NOT_FOUND);
|
||||||
|
} catch (IllegalArgumentException exception) {
|
||||||
|
return new ResponseEntity<>(exception.getMessage(), HttpStatus.BAD_REQUEST);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@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);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -35,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"));
|
||||||
|
|
||||||
@ -51,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))
|
||||||
@ -61,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)
|
||||||
@ -77,18 +85,100 @@ 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))
|
||||||
.andExpect(status().isOk());
|
.andExpect(status().isOk());
|
||||||
}
|
}
|
||||||
|
|
||||||
private void checkUser() {
|
@Test
|
||||||
|
public void shouldAddCommisionWithSelfIdPrivided() throws Exception {
|
||||||
|
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
|
||||||
|
public void shouldFailCommisionWithSomeoneIdPrividedAsStudent() throws Exception {
|
||||||
|
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
|
||||||
|
public void shouldFailCommisionAsDeanaryWithNoId() throws Exception {
|
||||||
|
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
|
||||||
|
public void shouldFailCommisionWithSelfIdPrividedAsDeanary() throws Exception {
|
||||||
|
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
|
||||||
|
public void shouldAddCommisionWithSomeoneIdPrividedAsDeanary() throws Exception {
|
||||||
|
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
|
||||||
|
public void shouldFailCommisionWithOtherDeanaryIdPrividedAsDeanary() throws Exception {
|
||||||
|
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 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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user