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")
public class CommisionController extends TokenBasedController {
@Autowired
private CommisionService commisionService;
@Autowired
private CommisionService commisionService;
@Autowired
private GroupService groupServcicxe;
@Autowired
private GroupService groupServcicxe;
@Autowired
private AssignmentService assignmentService;
@Autowired
private AssignmentService assignmentService;
public CommisionController() {
}
public CommisionController() {
}
@PostMapping(value = { "/add", "/add/{id}" })
public ResponseEntity<String> addCommision(@RequestBody List<Long> groups,
@PathVariable(name = "id", required = false) Long userId)
throws UserNotFoundException, IllegalArgumentException {
@PostMapping(value = { "/add", "/add/{id}" })
public ResponseEntity<String> addCommision(@RequestBody List<Long> groups,
@PathVariable(name = "id", required = false) Long userId) {
final User asker = this.getCurrentUser().orElseThrow(() -> new UserNotFoundException("Invalid token"));
try {
final User user = userId != null
? userService.getById(userId).orElseThrow(() -> new UserNotFoundException("Given user id not exist"))
: asker;
final User asker = this.getCurrentUser()
.orElseThrow(() -> new UserNotFoundException("Invalid token"));
Assert.isTrue(
(asker.getRole() == UserRoles.DEANERY && user.getRole() == UserRoles.STUDENT
|| (asker.getId() == user.getId() && user.getRole() == UserRoles.STUDENT)),
"Incorrect attempt to change plan");
final User user = userId != null
? userService.getById(userId).orElseThrow(
() -> new UserNotFoundException("Given user id not exist"))
: asker;
Optional<Long> notExistingGroup = this.groupServcicxe.findNotExistingGroup(groups);
Assert.isTrue(!notExistingGroup.isPresent(),
"Group " + notExistingGroup.orElse(Long.MIN_VALUE).toString() + "doesn't exist");
Assert.isTrue((asker.getRole() == UserRoles.DEANERY && user.getRole() == UserRoles.STUDENT
|| (asker.getId() == user.getId() && user.getRole() == UserRoles.STUDENT)),
"Incorrect attempt to change plan");
final Commision com = new Commision(user);
this.commisionService.save(com);
Optional<Long> notExistingGroup = this.groupServcicxe.findNotExistingGroup(groups);
Assert.isTrue(!notExistingGroup.isPresent(), "Group "
+ notExistingGroup.orElse(Long.MIN_VALUE).toString() + "doesn't exist");
groups.stream().forEach((groupId) -> {
Groups group = this.groupServcicxe.getGroupById(groupId).orElseThrow(() -> new NullPointerException());
Assignment a = new Assignment(group, com);
this.assignmentService.save(a);
});
final Commision com = new Commision(user);
this.commisionService.save(com);
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")
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);
}
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);
}
}