Search in sources :

Example 16 with NotFoundEx

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);
}
Also used : NotFoundEx(com.ncedu.fooddelivery.api.v1.errors.notfound.NotFoundEx) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 17 with NotFoundEx

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);
}
Also used : NotFoundEx(com.ncedu.fooddelivery.api.v1.errors.notfound.NotFoundEx) Client(com.ncedu.fooddelivery.api.v1.entities.Client) UserChangeInfoDTO(com.ncedu.fooddelivery.api.v1.dto.user.UserChangeInfoDTO) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 18 with NotFoundEx

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);
}
Also used : ResponseEntity(org.springframework.http.ResponseEntity) NotFoundEx(com.ncedu.fooddelivery.api.v1.errors.notfound.NotFoundEx) CustomAccessDeniedException(com.ncedu.fooddelivery.api.v1.errors.security.CustomAccessDeniedException) ProductPosition(com.ncedu.fooddelivery.api.v1.entities.productPosition.ProductPosition) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 19 with NotFoundEx

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);
}
Also used : ResponseEntity(org.springframework.http.ResponseEntity) NotFoundEx(com.ncedu.fooddelivery.api.v1.errors.notfound.NotFoundEx) ArrayList(java.util.ArrayList) CustomAccessDeniedException(com.ncedu.fooddelivery.api.v1.errors.security.CustomAccessDeniedException) ProductPosition(com.ncedu.fooddelivery.api.v1.entities.productPosition.ProductPosition) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 20 with NotFoundEx

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);
}
Also used : NotFoundEx(com.ncedu.fooddelivery.api.v1.errors.notfound.NotFoundEx) CustomAccessDeniedException(com.ncedu.fooddelivery.api.v1.errors.security.CustomAccessDeniedException) ProductPosition(com.ncedu.fooddelivery.api.v1.entities.productPosition.ProductPosition) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Aggregations

NotFoundEx (com.ncedu.fooddelivery.api.v1.errors.notfound.NotFoundEx)20 Test (org.junit.jupiter.api.Test)12 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)12 ProductPosition (com.ncedu.fooddelivery.api.v1.entities.productPosition.ProductPosition)6 CustomAccessDeniedException (com.ncedu.fooddelivery.api.v1.errors.security.CustomAccessDeniedException)6 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)4 OrderProductPosition (com.ncedu.fooddelivery.api.v1.entities.orderProductPosition.OrderProductPosition)3 AlreadyExistsException (com.ncedu.fooddelivery.api.v1.errors.badrequest.AlreadyExistsException)3 PasswordsMismatchException (com.ncedu.fooddelivery.api.v1.errors.badrequest.PasswordsMismatchException)3 ResponseEntity (org.springframework.http.ResponseEntity)3 WarehouseInfoDTO (com.ncedu.fooddelivery.api.v1.dto.warehouseDTOs.WarehouseInfoDTO)2 Client (com.ncedu.fooddelivery.api.v1.entities.Client)2 Order (com.ncedu.fooddelivery.api.v1.entities.order.Order)2 BadFileExtensionException (com.ncedu.fooddelivery.api.v1.errors.badrequest.BadFileExtensionException)2 ProductAvailabilityEx (com.ncedu.fooddelivery.api.v1.errors.orderRegistration.ProductAvailabilityEx)2 WarehouseCoordsBindingEx (com.ncedu.fooddelivery.api.v1.errors.orderRegistration.WarehouseCoordsBindingEx)2 IOException (java.io.IOException)2 UserChangeInfoDTO (com.ncedu.fooddelivery.api.v1.dto.user.UserChangeInfoDTO)1 File (com.ncedu.fooddelivery.api.v1.entities.File)1 Product (com.ncedu.fooddelivery.api.v1.entities.Product)1