use of com.ncedu.fooddelivery.api.v1.errors.notfound.NotFoundEx in project 2021-msk-food-delivery by netcracker-edu.
the class ClientServiceTest method getClientDTOByIdError.
@Test
public void getClientDTOByIdError() {
Long userId = 0L;
when(clientRepoMock.findById(userId)).thenReturn(Optional.ofNullable(null));
Exception exception = assertThrows(NotFoundEx.class, () -> {
clientService.getClientDTOById(userId);
});
String resultMessage = exception.getMessage();
String perfectMessage = new NotFoundEx(userId.toString()).getMessage();
verify(clientRepoMock, times(1)).findById(userId);
assertEquals(perfectMessage, resultMessage);
}
use of com.ncedu.fooddelivery.api.v1.errors.notfound.NotFoundEx in project 2021-msk-food-delivery by netcracker-edu.
the class ClientServiceTest method changeClientInfoError.
@Test
public void changeClientInfoError() {
Long userID = 0L;
Client client = ClientUtils.createPennyTeller(userID);
when(clientRepoMock.findById(userID)).thenReturn(Optional.ofNullable(null));
when(clientRepoMock.save(client)).thenReturn(null);
UserChangeInfoDTO userChangeInfoDTO = new UserChangeInfoDTO();
Exception exception = assertThrows(NotFoundEx.class, () -> {
clientService.changeClientInfo(userID, userChangeInfoDTO);
});
String resultMessage = exception.getMessage();
String perfectMessage = new NotFoundEx(userID.toString()).getMessage();
verify(clientRepoMock, times(1)).findById(userID);
verify(clientRepoMock, never()).save(client);
assertEquals(perfectMessage, resultMessage);
}
use of com.ncedu.fooddelivery.api.v1.errors.notfound.NotFoundEx in project 2021-msk-food-delivery by netcracker-edu.
the class ProductPositionController method nullifyProductPosition.
@PatchMapping("/api/v1/productPosition/{id}/currentAmount")
@PreAuthorize("hasAnyAuthority('ADMIN', 'MODERATOR')")
public ResponseEntity<?> nullifyProductPosition(@Min(value = 1) @Max(value = Long.MAX_VALUE) @PathVariable Long id, @AuthenticationPrincipal User user) {
ProductPosition productPositionToNullify = productPositionService.getProductPosition(id);
if (productPositionToNullify == null)
throw new NotFoundEx(String.valueOf(id));
if (Role.isMODERATOR(user.getRole().toString())) {
if (!user.getModerator().getWarehouseId().equals(productPositionToNullify.getWarehouse().getId())) {
throw new CustomAccessDeniedException();
}
}
productPositionService.nullifyProductPosition(id);
return new ResponseEntity<>(HttpStatus.OK);
}
use of com.ncedu.fooddelivery.api.v1.errors.notfound.NotFoundEx in project 2021-msk-food-delivery by netcracker-edu.
the class ProductPositionController method updatePaymentStatus.
@PatchMapping("/api/v1/productPositions/paymentState")
@PreAuthorize("hasAnyAuthority('ADMIN', 'MODERATOR')")
public ResponseEntity<?> updatePaymentStatus(@AuthenticationPrincipal User user, @Valid @RequestBody UpdatePaymentStatusDTO updatePaymentStatusDTO) {
List<ProductPosition> productPositionList = new ArrayList<>();
for (Long id : updatePaymentStatusDTO.getProductPositions()) {
ProductPosition productPosition = productPositionService.getProductPosition(id);
if (productPosition == null)
throw new NotFoundEx(String.valueOf(id));
productPositionList.add(productPosition);
}
if (Role.isMODERATOR(user.getRole().toString())) {
Long moderatorBindedWarehouseId = user.getModerator().getWarehouseId();
for (ProductPosition productPosition : productPositionList) {
if (!productPosition.getWarehouse().getId().equals(moderatorBindedWarehouseId))
throw new CustomAccessDeniedException();
}
}
productPositionService.updatePaymentStatus(updatePaymentStatusDTO.getProductPositions());
return new ResponseEntity<>(HttpStatus.OK);
}
use of com.ncedu.fooddelivery.api.v1.errors.notfound.NotFoundEx in project 2021-msk-food-delivery by netcracker-edu.
the class ProductPositionController method deleteProductPosition.
@DeleteMapping("/api/v1/productPosition/{id}")
@PreAuthorize("hasAnyAuthority('ADMIN', 'MODERATOR')")
public ResponseEntity<?> deleteProductPosition(@PathVariable Long id, @AuthenticationPrincipal User user) {
ProductPosition productPositionToDelete = productPositionService.getProductPosition(id);
if (productPositionToDelete == null)
throw new NotFoundEx(String.valueOf(id));
if (Role.isMODERATOR(user.getRole().toString())) {
if (!user.getModerator().getWarehouseId().equals(productPositionToDelete.getWarehouse().getId())) {
throw new CustomAccessDeniedException();
}
}
boolean deleteResult = productPositionService.deleteProductPosition(id);
if (deleteResult)
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
else
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
Aggregations