From 832f6258947ce59a9a299a3b6b170d7df3c825a8 Mon Sep 17 00:00:00 2001 From: berki0 <107307903+berki0@users.noreply.github.com> Date: Sun, 24 Mar 2024 19:56:34 +0200 Subject: [PATCH 01/33] [feature-70-BE-Expose-Organization-destruction] - changes after review --- .../ProductInOrganizationController.java | 24 ++++++++++++++++++ .../ProductsInOrganizationResponseDto.java | 17 +++++++++++++ .../mapper/ProductInOrganizationMapper.java | 25 +++++++++++++++++++ .../service/ProductInOrganizationService.java | 24 ++++++++++++++++++ .../inventory/service/ProductService.java | 7 +++++- 5 files changed, 96 insertions(+), 1 deletion(-) create mode 100644 src/main/java/jewellery/inventory/controller/ProductInOrganizationController.java create mode 100644 src/main/java/jewellery/inventory/dto/response/ProductsInOrganizationResponseDto.java create mode 100644 src/main/java/jewellery/inventory/mapper/ProductInOrganizationMapper.java create mode 100644 src/main/java/jewellery/inventory/service/ProductInOrganizationService.java diff --git a/src/main/java/jewellery/inventory/controller/ProductInOrganizationController.java b/src/main/java/jewellery/inventory/controller/ProductInOrganizationController.java new file mode 100644 index 00000000..02fea71f --- /dev/null +++ b/src/main/java/jewellery/inventory/controller/ProductInOrganizationController.java @@ -0,0 +1,24 @@ +package jewellery.inventory.controller; + +import io.swagger.v3.oas.annotations.Operation; +import java.util.UUID; +import jewellery.inventory.dto.response.ProductsInOrganizationResponseDto; +import jewellery.inventory.service.ProductInOrganizationService; +import lombok.AllArgsConstructor; +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.*; + +@RestController +@RequestMapping("/organizations") +@AllArgsConstructor +public class ProductInOrganizationController { + private final ProductInOrganizationService productInOrganizationService; + + @Operation(summary = "Get all products in organization") + @ResponseStatus(HttpStatus.OK) + @GetMapping("/{organizationId}/products") + public ProductsInOrganizationResponseDto getAllProductsInOrganization( + @PathVariable UUID organizationId) { + return productInOrganizationService.getProductsInOrganization(organizationId); + } +} diff --git a/src/main/java/jewellery/inventory/dto/response/ProductsInOrganizationResponseDto.java b/src/main/java/jewellery/inventory/dto/response/ProductsInOrganizationResponseDto.java new file mode 100644 index 00000000..6bbaae95 --- /dev/null +++ b/src/main/java/jewellery/inventory/dto/response/ProductsInOrganizationResponseDto.java @@ -0,0 +1,17 @@ +package jewellery.inventory.dto.response; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; +import lombok.experimental.SuperBuilder; + +import java.util.List; + +@Data +@SuperBuilder +@AllArgsConstructor +@NoArgsConstructor +public class ProductsInOrganizationResponseDto { + private OrganizationResponseDto organization; + private List products; +} diff --git a/src/main/java/jewellery/inventory/mapper/ProductInOrganizationMapper.java b/src/main/java/jewellery/inventory/mapper/ProductInOrganizationMapper.java new file mode 100644 index 00000000..ad6e81c8 --- /dev/null +++ b/src/main/java/jewellery/inventory/mapper/ProductInOrganizationMapper.java @@ -0,0 +1,25 @@ +package jewellery.inventory.mapper; + +import jewellery.inventory.dto.response.ProductResponseDto; +import jewellery.inventory.dto.response.ProductsInOrganizationResponseDto; +import jewellery.inventory.model.Organization; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Component; + +import java.util.List; + +@Component +@RequiredArgsConstructor +public class ProductInOrganizationMapper { + private final OrganizationMapper organizationMapper; + + public ProductsInOrganizationResponseDto mapToProductResponseDto( + Organization organization, List products) { + ProductsInOrganizationResponseDto productsInOrganizationResponseDto = + new ProductsInOrganizationResponseDto(); + + productsInOrganizationResponseDto.setOrganization(organizationMapper.toResponse(organization)); + productsInOrganizationResponseDto.setProducts(products); + return productsInOrganizationResponseDto; + } +} diff --git a/src/main/java/jewellery/inventory/service/ProductInOrganizationService.java b/src/main/java/jewellery/inventory/service/ProductInOrganizationService.java new file mode 100644 index 00000000..7630b313 --- /dev/null +++ b/src/main/java/jewellery/inventory/service/ProductInOrganizationService.java @@ -0,0 +1,24 @@ +package jewellery.inventory.service; + +import java.util.UUID; +import jewellery.inventory.dto.response.ProductsInOrganizationResponseDto; +import jewellery.inventory.mapper.ProductInOrganizationMapper; +import jewellery.inventory.model.Organization; +import lombok.AllArgsConstructor; +import org.springframework.stereotype.Service; + +@Service +@AllArgsConstructor +public class ProductInOrganizationService { + private final OrganizationService organizationService; + private final ProductService productService; + private final ProductInOrganizationMapper mapper; + + public ProductsInOrganizationResponseDto getProductsInOrganization(UUID organizationId) { + Organization organization = organizationService.getOrganization(organizationId); + organizationService.validateUserInOrganization(organization); + + return mapper.mapToProductResponseDto( + organization, productService.getProductsResponse(organization.getProductsOwned())); + } +} diff --git a/src/main/java/jewellery/inventory/service/ProductService.java b/src/main/java/jewellery/inventory/service/ProductService.java index b6d8e6c6..f2066c63 100644 --- a/src/main/java/jewellery/inventory/service/ProductService.java +++ b/src/main/java/jewellery/inventory/service/ProductService.java @@ -86,6 +86,10 @@ public List getAllProducts() { return products.stream().map(productMapper::mapToProductResponseDto).toList(); } + public List getProductsResponse(List products) { + return products.stream().map(productMapper::mapToProductResponseDto).toList(); + } + public List getByOwner(UUID ownerId) { List products = productRepository.findAllByOwnerId(ownerId); logger.info("Get product by owner with ID: {}", ownerId); @@ -383,7 +387,8 @@ private ResourceInProduct transferSingleResourceQuantityFromUserToProduct( owner.getId(), product.getId()); - ResourceInUser resourceInUser = getResourceInUser(owner, incomingResourceInProduct.getResourceId()); + ResourceInUser resourceInUser = + getResourceInUser(owner, incomingResourceInProduct.getResourceId()); resourceInUserService.removeQuantityFromResourceNoLog( owner.getId(), resourceInUser.getResource().getId(), From f90569ea9aef12b298d91d79eca365b3c34f1061 Mon Sep 17 00:00:00 2001 From: berki0 <107307903+berki0@users.noreply.github.com> Date: Mon, 25 Mar 2024 02:35:13 +0200 Subject: [PATCH 02/33] [feature-72-BE-products-in-organization] CRUD --- .../ProductInOrganizationController.java | 8 + .../exception/GlobalExceptionHandler.java | 3 +- .../OrganizationNotOwnerException.java | 10 ++ .../jewellery/inventory/model/EventType.java | 3 +- .../service/ProductInOrganizationService.java | 156 +++++++++++++++++- .../inventory/service/ProductService.java | 12 +- .../ResourceInOrganizationService.java | 2 +- 7 files changed, 185 insertions(+), 9 deletions(-) create mode 100644 src/main/java/jewellery/inventory/exception/organization/OrganizationNotOwnerException.java diff --git a/src/main/java/jewellery/inventory/controller/ProductInOrganizationController.java b/src/main/java/jewellery/inventory/controller/ProductInOrganizationController.java index 02fea71f..53d86782 100644 --- a/src/main/java/jewellery/inventory/controller/ProductInOrganizationController.java +++ b/src/main/java/jewellery/inventory/controller/ProductInOrganizationController.java @@ -1,7 +1,9 @@ package jewellery.inventory.controller; import io.swagger.v3.oas.annotations.Operation; +import jakarta.validation.Valid; import java.util.UUID; +import jewellery.inventory.dto.request.ProductRequestDto; import jewellery.inventory.dto.response.ProductsInOrganizationResponseDto; import jewellery.inventory.service.ProductInOrganizationService; import lombok.AllArgsConstructor; @@ -21,4 +23,10 @@ public ProductsInOrganizationResponseDto getAllProductsInOrganization( @PathVariable UUID organizationId) { return productInOrganizationService.getProductsInOrganization(organizationId); } + @Operation(summary = "Create a new product in organization") + @ResponseStatus(HttpStatus.CREATED) + @PostMapping("/products") + public ProductsInOrganizationResponseDto createProductInOrganization(@RequestBody @Valid ProductRequestDto productRequestDto) { + return productInOrganizationService.createProductInOrganization(productRequestDto); + } } diff --git a/src/main/java/jewellery/inventory/exception/GlobalExceptionHandler.java b/src/main/java/jewellery/inventory/exception/GlobalExceptionHandler.java index dead3fe7..f4d78c15 100644 --- a/src/main/java/jewellery/inventory/exception/GlobalExceptionHandler.java +++ b/src/main/java/jewellery/inventory/exception/GlobalExceptionHandler.java @@ -89,7 +89,8 @@ public ResponseEntity handleBadDataExceptions(RuntimeException ex) { UserIsNotPartOfOrganizationException.class, UserIsPartOfOrganizationException.class, OrphanResourcesInOrganizationException.class, - OrphanProductsInOrganizationException.class + OrphanProductsInOrganizationException.class, + OrganizationNotOwnerException.class }) public ResponseEntity handleEntityConstraintConflict(RuntimeException ex) { return createErrorResponse(HttpStatus.CONFLICT, ex.getMessage(), ex); diff --git a/src/main/java/jewellery/inventory/exception/organization/OrganizationNotOwnerException.java b/src/main/java/jewellery/inventory/exception/organization/OrganizationNotOwnerException.java new file mode 100644 index 00000000..ad77d404 --- /dev/null +++ b/src/main/java/jewellery/inventory/exception/organization/OrganizationNotOwnerException.java @@ -0,0 +1,10 @@ +package jewellery.inventory.exception.organization; + + +import java.util.UUID; + +public class OrganizationNotOwnerException extends RuntimeException { + public OrganizationNotOwnerException(UUID organizationId, UUID productId) { + super("Organization with id " + organizationId + " is not the owner of a product with id " + productId); + } + } diff --git a/src/main/java/jewellery/inventory/model/EventType.java b/src/main/java/jewellery/inventory/model/EventType.java index 5883e097..3888ed5e 100644 --- a/src/main/java/jewellery/inventory/model/EventType.java +++ b/src/main/java/jewellery/inventory/model/EventType.java @@ -23,5 +23,6 @@ public enum EventType { ORGANIZATION_USER_DELETE, ORGANIZATION_USER_UPDATE, ORGANIZATION_ADD_RESOURCE_QUANTITY, - ORGANIZATION_REMOVE_RESOURCE_QUANTITY + ORGANIZATION_REMOVE_RESOURCE_QUANTITY, + ORGANIZATION_PRODUCT_CREATE } diff --git a/src/main/java/jewellery/inventory/service/ProductInOrganizationService.java b/src/main/java/jewellery/inventory/service/ProductInOrganizationService.java index 7630b313..bbc52bee 100644 --- a/src/main/java/jewellery/inventory/service/ProductInOrganizationService.java +++ b/src/main/java/jewellery/inventory/service/ProductInOrganizationService.java @@ -1,11 +1,21 @@ package jewellery.inventory.service; +import java.util.ArrayList; +import java.util.List; import java.util.UUID; +import jewellery.inventory.aspect.annotation.LogCreateEvent; +import jewellery.inventory.dto.request.ProductRequestDto; +import jewellery.inventory.dto.request.resource.ResourceQuantityRequestDto; import jewellery.inventory.dto.response.ProductsInOrganizationResponseDto; +import jewellery.inventory.exception.invalid_resource_quantity.InsufficientResourceQuantityException; +import jewellery.inventory.exception.organization.OrganizationNotOwnerException; import jewellery.inventory.mapper.ProductInOrganizationMapper; -import jewellery.inventory.model.Organization; +import jewellery.inventory.model.*; +import jewellery.inventory.model.resource.Resource; +import jewellery.inventory.model.resource.ResourceInProduct; import lombok.AllArgsConstructor; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; @Service @AllArgsConstructor @@ -13,12 +23,154 @@ public class ProductInOrganizationService { private final OrganizationService organizationService; private final ProductService productService; private final ProductInOrganizationMapper mapper; + private final ResourceInOrganizationService resourceInOrganizationService; public ProductsInOrganizationResponseDto getProductsInOrganization(UUID organizationId) { Organization organization = organizationService.getOrganization(organizationId); organizationService.validateUserInOrganization(organization); - + return mapper.mapToProductResponseDto( organization, productService.getProductsResponse(organization.getProductsOwned())); } + + @Transactional + @LogCreateEvent(eventType = EventType.ORGANIZATION_PRODUCT_CREATE) + public ProductsInOrganizationResponseDto createProductInOrganization( + ProductRequestDto productRequestDto) { + Organization organization = organizationService.getOrganization(productRequestDto.getOwnerId()); + + organizationService.validateCurrentUserPermission( + organization, OrganizationPermission.CREATE_PRODUCT); + + Product product = persistProductWithoutResourcesAndProducts(productRequestDto, organization); + addProductsContentToProduct(productRequestDto, product); + addResourcesToProduct(productRequestDto, organization, product); + + return mapper.mapToProductResponseDto( + organization, productService.getProductsResponse(List.of(product))); + } + + private Product persistProductWithoutResourcesAndProducts( + ProductRequestDto productRequestDto, Organization organization) { + Product product = getProductWithoutResourcesAndProduct(productRequestDto, organization); + productService.saveProduct(product); + return product; + } + + private Product getProductWithoutResourcesAndProduct( + ProductRequestDto productRequestDto, Organization organization) { + Product product = new Product(); + setProductFields(productRequestDto, organization, product); + return product; + } + + private void setProductFields( + ProductRequestDto productRequestDto, Organization organization, Product product) { + product.setOwner(null); + product.setOrganization(organization); + product.setAuthors(productService.getAuthors(productRequestDto)); + product.setPartOfSale(null); + product.setDescription(productRequestDto.getDescription()); + product.setProductionNumber(productRequestDto.getProductionNumber()); + product.setCatalogNumber(productRequestDto.getCatalogNumber()); + product.setAdditionalPrice(productRequestDto.getAdditionalPrice()); + product.setProductsContent(new ArrayList<>()); + product.setResourcesContent(new ArrayList<>()); + } + + private void addProductsContentToProduct(ProductRequestDto productRequestDto, Product product) { + if (productRequestDto.getProductsContent() != null) { + product.setProductsContent( + getProductsInProduct(productRequestDto.getProductsContent(), product)); + productService.saveProduct(product); + } + } + + private List getProductsInProduct( + List productsIdInRequest, Product parentProduct) { + List products = new ArrayList<>(); + if (productsIdInRequest != null) { + productsIdInRequest.forEach( + productId -> { + Product product = productService.getProduct(productId); + productService.throwExceptionIfProductIsPartOfItself(product, parentProduct.getId()); + productService.throwExceptionIfProductIsSold(product); + productService.throwExceptionIfProductIsPartOfAnotherProduct(productId, product); + if ((product.getOrganization() != null) + && parentProduct + .getOrganization() + .getId() + .equals(product.getOrganization().getId())) { + product.setContentOf(parentProduct); + products.add(product); + } else { + throw new OrganizationNotOwnerException( + parentProduct.getOrganization().getId(), product.getId()); + } + }); + } + return products; + } + + private void addResourcesToProduct( + ProductRequestDto productRequestDto, Organization organization, Product product) { + + List resourcesInProducts = + transferResourcesQuantitiesFromOrganizationToProduct( + organization, productRequestDto.getResourcesContent(), product); + product.setResourcesContent(resourcesInProducts); + } + + private List transferResourcesQuantitiesFromOrganizationToProduct( + Organization organization, + List incomingResourceInProductList, + Product product) { + List resourcesForAdd = new ArrayList<>(); + + for (ResourceQuantityRequestDto resourceQuantity : incomingResourceInProductList) { + resourcesForAdd.add( + transferSingleResourceQuantityFromOrganizationToProduct( + organization, resourceQuantity, product)); + } + return resourcesForAdd; + } + + private ResourceInProduct transferSingleResourceQuantityFromOrganizationToProduct( + Organization organization, + ResourceQuantityRequestDto incomingResourceInProduct, + Product product) { + + ResourceInOrganization resourceInOrganization = + resourceInOrganizationService.findResourceInOrganizationOrThrow( + organization, incomingResourceInProduct.getResourceId()); + + checkResourceAvailability(resourceInOrganization, incomingResourceInProduct); + + resourceInOrganizationService.removeQuantityFromResource( + organization.getId(), + incomingResourceInProduct.getResourceId(), + incomingResourceInProduct.getQuantity()); + + return createResourceInProduct( + incomingResourceInProduct, resourceInOrganization.getResource(), product); + } + + private ResourceInProduct createResourceInProduct( + ResourceQuantityRequestDto incomingResourceInProduct, Resource resource, Product product) { + ResourceInProduct resourceInProduct = new ResourceInProduct(); + resourceInProduct.setResource(resource); + resourceInProduct.setQuantity(incomingResourceInProduct.getQuantity()); + resourceInProduct.setProduct(product); + return resourceInProduct; + } + + private void checkResourceAvailability( + ResourceInOrganization resourceInOrganization, + ResourceQuantityRequestDto incomingResourceInProduct) { + if (resourceInOrganization.getQuantity().compareTo(incomingResourceInProduct.getQuantity()) + < 0) { + throw new InsufficientResourceQuantityException( + incomingResourceInProduct.getQuantity(), resourceInOrganization.getQuantity()); + } + } } diff --git a/src/main/java/jewellery/inventory/service/ProductService.java b/src/main/java/jewellery/inventory/service/ProductService.java index f2066c63..1d7017dc 100644 --- a/src/main/java/jewellery/inventory/service/ProductService.java +++ b/src/main/java/jewellery/inventory/service/ProductService.java @@ -106,6 +106,10 @@ public ProductResponseDto getProductResponse(UUID id) { return productMapper.mapToProductResponseDto(getProduct(id)); } + public Product saveProduct(Product product) { + return productRepository.save(product); + } + public void updateProductOwnerAndSale(Product product, User newOwner, Sale sale) { updateProductOwnerRecursively(product, newOwner); if (sale == null) { @@ -174,7 +178,7 @@ private void deleteImageWhenAttached(UUID id, Product product) throws IOExceptio } } - private void throwExceptionIfProductIsPartOfAnotherProduct(UUID id, Product product) { + public void throwExceptionIfProductIsPartOfAnotherProduct(UUID id, Product product) { if (product.getContentOf() != null) { throw new ProductIsContentException(id); } @@ -186,7 +190,7 @@ private void throwExceptionIfProductOwnerEqualsRecipient(Product product, UUID r } } - private void throwExceptionIfProductIsSold(Product product) { + public void throwExceptionIfProductIsSold(Product product) { if (product.getPartOfSale() != null) { throw new ProductIsSoldException(product.getId()); } @@ -284,7 +288,7 @@ private List getProductsInProduct( return products; } - private void throwExceptionIfProductIsPartOfItself(Product product, UUID parentId) { + public void throwExceptionIfProductIsPartOfItself(Product product, UUID parentId) { if (product.getId().equals(parentId)) { throw new ProductPartOfItselfException(); } @@ -325,7 +329,7 @@ private void setProductFields(ProductRequestDto productRequestDto, User user, Pr product.setResourcesContent(new ArrayList<>()); } - private List getAuthors(ProductRequestDto productRequestDto) { + public List getAuthors(ProductRequestDto productRequestDto) { logger.debug("Getting authors for product."); List authorsIds = productRequestDto.getAuthors(); List authors = new ArrayList<>(); diff --git a/src/main/java/jewellery/inventory/service/ResourceInOrganizationService.java b/src/main/java/jewellery/inventory/service/ResourceInOrganizationService.java index 9e84ded3..5f9418eb 100644 --- a/src/main/java/jewellery/inventory/service/ResourceInOrganizationService.java +++ b/src/main/java/jewellery/inventory/service/ResourceInOrganizationService.java @@ -103,7 +103,7 @@ private void removeQuantityFromResource( logger.debug("ResourceInOrganization after quantity removal: {}", resourceInOrganization); } - private ResourceInOrganization findResourceInOrganizationOrThrow( + public ResourceInOrganization findResourceInOrganizationOrThrow( Organization previousOwner, UUID resourceId) { return findResourceInOrganization(previousOwner, resourceId) .orElseThrow( From 3281bb8749d481c5e625da6f6e5b6b4986a68aa6 Mon Sep 17 00:00:00 2001 From: berki0 <107307903+berki0@users.noreply.github.com> Date: Mon, 25 Mar 2024 10:51:32 +0200 Subject: [PATCH 03/33] [feature-72-BE-products-in-organization] add deleting method --- .../ProductInOrganizationController.java | 6 ++ .../service/ProductInOrganizationService.java | 62 +++++++++++++++++-- .../ResourceInOrganizationService.java | 5 +- 3 files changed, 67 insertions(+), 6 deletions(-) diff --git a/src/main/java/jewellery/inventory/controller/ProductInOrganizationController.java b/src/main/java/jewellery/inventory/controller/ProductInOrganizationController.java index 53d86782..06e2668e 100644 --- a/src/main/java/jewellery/inventory/controller/ProductInOrganizationController.java +++ b/src/main/java/jewellery/inventory/controller/ProductInOrganizationController.java @@ -29,4 +29,10 @@ public ProductsInOrganizationResponseDto getAllProductsInOrganization( public ProductsInOrganizationResponseDto createProductInOrganization(@RequestBody @Valid ProductRequestDto productRequestDto) { return productInOrganizationService.createProductInOrganization(productRequestDto); } + @Operation(summary = "Delete a new product in organization") + @ResponseStatus(HttpStatus.NO_CONTENT) + @DeleteMapping("/{organizationId}/products/{productId}") + public void deleteProductInOrganization(@PathVariable UUID organizationId,@PathVariable UUID productId) { + productInOrganizationService.deleteProductInOrganization(organizationId,productId); + } } diff --git a/src/main/java/jewellery/inventory/service/ProductInOrganizationService.java b/src/main/java/jewellery/inventory/service/ProductInOrganizationService.java index bbc52bee..455f8dd5 100644 --- a/src/main/java/jewellery/inventory/service/ProductInOrganizationService.java +++ b/src/main/java/jewellery/inventory/service/ProductInOrganizationService.java @@ -1,5 +1,6 @@ package jewellery.inventory.service; +import java.math.BigDecimal; import java.util.ArrayList; import java.util.List; import java.util.UUID; @@ -50,6 +51,23 @@ public ProductsInOrganizationResponseDto createProductInOrganization( organization, productService.getProductsResponse(List.of(product))); } + public void deleteProductInOrganization(UUID organizationId, UUID productId) { + organizationService.validateCurrentUserPermission( + organizationService.getOrganization(organizationId), + OrganizationPermission.DISASSEMBLE_PRODUCT); + + Product forDeleteProduct = productService.getProduct(productId); + + List subProducts = + getProductsInProduct( + forDeleteProduct.getProductsContent().stream().map(Product::getId).toList(), + forDeleteProduct); + + removeProductsContentFromProduct(subProducts); + removeResourcesFromProduct( + forDeleteProduct, organizationService.getOrganization(organizationId)); + } + private Product persistProductWithoutResourcesAndProducts( ProductRequestDto productRequestDto, Organization organization) { Product product = getProductWithoutResourcesAndProduct(productRequestDto, organization); @@ -86,6 +104,43 @@ private void addProductsContentToProduct(ProductRequestDto productRequestDto, Pr } } + private void removeProductsContentFromProduct(List subProducts) { + if (!subProducts.isEmpty()) { + for (Product subProduct : subProducts) { + subProduct.setContentOf(null); + productService.saveProduct(subProduct); + } + } + } + + private void removeResourcesFromProduct(Product product, Organization organization) { + List resourceInProductList = new ArrayList<>(); + for (int i = 0; i < product.getResourcesContent().size(); i++) { + ResourceInProduct resourceInProduct = + createResourceInProduct( + product.getResourcesContent().get(i).getQuantity(), + product.getResourcesContent().get(i).getResource(), + product); + resourceInProductList.add(resourceInProduct); + product.setResourcesContent(null); + } + addResourcesToOrganization(resourceInProductList, organization); + } + + private void addResourcesToOrganization( + List resourceInProductList, Organization organization) { + for (ResourceInProduct resourceInProduct : resourceInProductList) { + getResourceInOrganization( + organization, resourceInProduct.getResource(), resourceInProduct.getQuantity()); + } + } + + private ResourceInOrganization getResourceInOrganization( + Organization organization, Resource resource, BigDecimal quantity) { + return resourceInOrganizationService.addResourceToOrganization( + organization, resource, quantity, BigDecimal.ZERO); + } + private List getProductsInProduct( List productsIdInRequest, Product parentProduct) { List products = new ArrayList<>(); @@ -95,7 +150,6 @@ private List getProductsInProduct( Product product = productService.getProduct(productId); productService.throwExceptionIfProductIsPartOfItself(product, parentProduct.getId()); productService.throwExceptionIfProductIsSold(product); - productService.throwExceptionIfProductIsPartOfAnotherProduct(productId, product); if ((product.getOrganization() != null) && parentProduct .getOrganization() @@ -152,14 +206,14 @@ private ResourceInProduct transferSingleResourceQuantityFromOrganizationToProduc incomingResourceInProduct.getQuantity()); return createResourceInProduct( - incomingResourceInProduct, resourceInOrganization.getResource(), product); + incomingResourceInProduct.getQuantity(), resourceInOrganization.getResource(), product); } private ResourceInProduct createResourceInProduct( - ResourceQuantityRequestDto incomingResourceInProduct, Resource resource, Product product) { + BigDecimal quantity, Resource resource, Product product) { ResourceInProduct resourceInProduct = new ResourceInProduct(); resourceInProduct.setResource(resource); - resourceInProduct.setQuantity(incomingResourceInProduct.getQuantity()); + resourceInProduct.setQuantity(quantity); resourceInProduct.setProduct(product); return resourceInProduct; } diff --git a/src/main/java/jewellery/inventory/service/ResourceInOrganizationService.java b/src/main/java/jewellery/inventory/service/ResourceInOrganizationService.java index 5f9418eb..90f34765 100644 --- a/src/main/java/jewellery/inventory/service/ResourceInOrganizationService.java +++ b/src/main/java/jewellery/inventory/service/ResourceInOrganizationService.java @@ -39,6 +39,7 @@ public ResourcesInOrganizationResponseDto addResourceToOrganization( ResourceInOrganizationRequestDto resourceInOrganizationRequestDto) { Organization organization = organizationService.getOrganization(resourceInOrganizationRequestDto.getOrganizationId()); + organizationService.validateCurrentUserPermission( organization, OrganizationPermission.ADD_RESOURCE_QUANTITY); @@ -141,7 +142,7 @@ private ResourceInOrganization createAndAddNewResourceInOrganization( return resourceInOrganization; } - private ResourceInOrganization getResourceInOrganization( + public ResourceInOrganization getResourceInOrganization( Organization organization, Resource resource) { logger.debug( "Getting resource in organization. Organization: {}, Resource: {}", organization, resource); @@ -150,7 +151,7 @@ private ResourceInOrganization getResourceInOrganization( () -> createAndAddNewResourceInOrganization(organization, resource, BigDecimal.ZERO)); } - private ResourceInOrganization addResourceToOrganization( + public ResourceInOrganization addResourceToOrganization( Organization organization, Resource resource, BigDecimal quantity, BigDecimal dealPrice) { logger.info( "Adding resource to organization. Organization: {}, Resource: {}, Quantity: {}", From b2af6457f9f1dc020b17c4d9db7bdd11354b3db6 Mon Sep 17 00:00:00 2001 From: berki0 <107307903+berki0@users.noreply.github.com> Date: Mon, 25 Mar 2024 11:46:50 +0200 Subject: [PATCH 04/33] [feature-72-BE-products-in-organization] add deleting method --- .../inventory/service/ProductInOrganizationService.java | 1 + src/main/java/jewellery/inventory/service/ProductService.java | 3 +++ 2 files changed, 4 insertions(+) diff --git a/src/main/java/jewellery/inventory/service/ProductInOrganizationService.java b/src/main/java/jewellery/inventory/service/ProductInOrganizationService.java index 455f8dd5..e24b8317 100644 --- a/src/main/java/jewellery/inventory/service/ProductInOrganizationService.java +++ b/src/main/java/jewellery/inventory/service/ProductInOrganizationService.java @@ -66,6 +66,7 @@ public void deleteProductInOrganization(UUID organizationId, UUID productId) { removeProductsContentFromProduct(subProducts); removeResourcesFromProduct( forDeleteProduct, organizationService.getOrganization(organizationId)); + productService.deleteProductById(productId); } private Product persistProductWithoutResourcesAndProducts( diff --git a/src/main/java/jewellery/inventory/service/ProductService.java b/src/main/java/jewellery/inventory/service/ProductService.java index 1d7017dc..1527d4e8 100644 --- a/src/main/java/jewellery/inventory/service/ProductService.java +++ b/src/main/java/jewellery/inventory/service/ProductService.java @@ -161,6 +161,9 @@ public void deleteProduct(UUID id) throws IOException { productRepository.deleteById(id); logger.info("Deleted product by ID: {}", id); } + public void deleteProductById(UUID productId){ + productRepository.deleteById(productId); + } @LogUpdateEvent(eventType = EventType.PRODUCT_TRANSFER) public ProductResponseDto transferProduct(UUID productId, UUID recipientId) { From f6ff5c1a4b790fc5df92d8ad3fc6fe9e8d091ae9 Mon Sep 17 00:00:00 2001 From: ignatIgnatov Date: Tue, 26 Mar 2024 09:22:52 +0200 Subject: [PATCH 05/33] update product, entity fetcher --- .../ProductInOrganizationController.java | 19 ++++- .../jewellery/inventory/model/EventType.java | 4 +- .../service/ProductInOrganizationService.java | 70 ++++++++++++++++++- .../inventory/service/ProductService.java | 2 +- 4 files changed, 89 insertions(+), 6 deletions(-) diff --git a/src/main/java/jewellery/inventory/controller/ProductInOrganizationController.java b/src/main/java/jewellery/inventory/controller/ProductInOrganizationController.java index 06e2668e..395121e4 100644 --- a/src/main/java/jewellery/inventory/controller/ProductInOrganizationController.java +++ b/src/main/java/jewellery/inventory/controller/ProductInOrganizationController.java @@ -23,16 +23,29 @@ public ProductsInOrganizationResponseDto getAllProductsInOrganization( @PathVariable UUID organizationId) { return productInOrganizationService.getProductsInOrganization(organizationId); } + @Operation(summary = "Create a new product in organization") @ResponseStatus(HttpStatus.CREATED) @PostMapping("/products") - public ProductsInOrganizationResponseDto createProductInOrganization(@RequestBody @Valid ProductRequestDto productRequestDto) { + public ProductsInOrganizationResponseDto createProductInOrganization( + @RequestBody @Valid ProductRequestDto productRequestDto) { return productInOrganizationService.createProductInOrganization(productRequestDto); } + @Operation(summary = "Delete a new product in organization") @ResponseStatus(HttpStatus.NO_CONTENT) @DeleteMapping("/{organizationId}/products/{productId}") - public void deleteProductInOrganization(@PathVariable UUID organizationId,@PathVariable UUID productId) { - productInOrganizationService.deleteProductInOrganization(organizationId,productId); + public void deleteProductInOrganization( + @PathVariable UUID organizationId, @PathVariable UUID productId) { + productInOrganizationService.deleteProductInOrganization(organizationId, productId); + } + + @Operation(summary = "Update a product in organization") + @ResponseStatus(HttpStatus.OK) + @PutMapping("/products/{productId}") + public ProductsInOrganizationResponseDto updateProduct( + @PathVariable("productId") UUID productId, + @Valid @RequestBody ProductRequestDto productRequestDto) { + return productInOrganizationService.updateProduct(productId, productRequestDto); } } diff --git a/src/main/java/jewellery/inventory/model/EventType.java b/src/main/java/jewellery/inventory/model/EventType.java index 3888ed5e..9e0f9446 100644 --- a/src/main/java/jewellery/inventory/model/EventType.java +++ b/src/main/java/jewellery/inventory/model/EventType.java @@ -24,5 +24,7 @@ public enum EventType { ORGANIZATION_USER_UPDATE, ORGANIZATION_ADD_RESOURCE_QUANTITY, ORGANIZATION_REMOVE_RESOURCE_QUANTITY, - ORGANIZATION_PRODUCT_CREATE + ORGANIZATION_PRODUCT_CREATE, + ORGANIZATION_PRODUCT_UPDATE, + ORGANIZATION_PRODUCT_DISASSEMBLY } diff --git a/src/main/java/jewellery/inventory/service/ProductInOrganizationService.java b/src/main/java/jewellery/inventory/service/ProductInOrganizationService.java index e24b8317..606f4063 100644 --- a/src/main/java/jewellery/inventory/service/ProductInOrganizationService.java +++ b/src/main/java/jewellery/inventory/service/ProductInOrganizationService.java @@ -4,27 +4,62 @@ import java.util.ArrayList; import java.util.List; import java.util.UUID; +import jewellery.inventory.aspect.EntityFetcher; import jewellery.inventory.aspect.annotation.LogCreateEvent; +import jewellery.inventory.aspect.annotation.LogDeleteEvent; +import jewellery.inventory.aspect.annotation.LogUpdateEvent; import jewellery.inventory.dto.request.ProductRequestDto; +import jewellery.inventory.dto.request.ResourceInOrganizationRequestDto; import jewellery.inventory.dto.request.resource.ResourceQuantityRequestDto; import jewellery.inventory.dto.response.ProductsInOrganizationResponseDto; import jewellery.inventory.exception.invalid_resource_quantity.InsufficientResourceQuantityException; import jewellery.inventory.exception.organization.OrganizationNotOwnerException; import jewellery.inventory.mapper.ProductInOrganizationMapper; +import jewellery.inventory.mapper.ProductMapper; import jewellery.inventory.model.*; import jewellery.inventory.model.resource.Resource; import jewellery.inventory.model.resource.ResourceInProduct; +import jewellery.inventory.repository.ProductRepository; +import jewellery.inventory.repository.ResourceInProductRepository; import lombok.AllArgsConstructor; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @Service @AllArgsConstructor -public class ProductInOrganizationService { +public class ProductInOrganizationService implements EntityFetcher { private final OrganizationService organizationService; private final ProductService productService; private final ProductInOrganizationMapper mapper; private final ResourceInOrganizationService resourceInOrganizationService; + private final ResourceInProductRepository resourceInProductRepository; + private final ProductRepository productRepository; + private final ProductMapper productMapper; + + @Transactional + @LogUpdateEvent(eventType = EventType.ORGANIZATION_PRODUCT_UPDATE) + public ProductsInOrganizationResponseDto updateProduct( + UUID productId, ProductRequestDto productRequestDto) { + + Organization organization = organizationService.getOrganization(productRequestDto.getOwnerId()); + organizationService.validateUserInOrganization(organization); + + organizationService.validateCurrentUserPermission( + organization, OrganizationPermission.EDIT_PRODUCT); + + Product product = productService.getProduct(productId); + productService.throwExceptionIfProductIsSold(product); + moveQuantityFromResourcesInProductToResourcesInOrganization(product); + productService.disassembleProductContent(product); + + setProductFields(productRequestDto, organization, product); + productRepository.save(product); + + addProductsContentToProduct(productRequestDto, product); + addResourcesToProduct(productRequestDto, organization, product); + return mapper.mapToProductResponseDto( + organization, List.of(productMapper.mapToProductResponseDto(product))); + } public ProductsInOrganizationResponseDto getProductsInOrganization(UUID organizationId) { Organization organization = organizationService.getOrganization(organizationId); @@ -51,6 +86,8 @@ public ProductsInOrganizationResponseDto createProductInOrganization( organization, productService.getProductsResponse(List.of(product))); } + @Transactional + @LogDeleteEvent(eventType = EventType.ORGANIZATION_PRODUCT_DISASSEMBLY) public void deleteProductInOrganization(UUID organizationId, UUID productId) { organizationService.validateCurrentUserPermission( organizationService.getOrganization(organizationId), @@ -228,4 +265,35 @@ private void checkResourceAvailability( incomingResourceInProduct.getQuantity(), resourceInOrganization.getQuantity()); } } + + private ResourceInOrganizationRequestDto getResourceInOrganizationRequest( + Organization owner, ResourceInProduct resourceInProduct) { + + return ResourceInOrganizationRequestDto.builder() + .organizationId(owner.getId()) + .resourceId(resourceInProduct.getResource().getId()) + .quantity(resourceInProduct.getQuantity()) + .build(); + } + + private void moveQuantityFromResourcesInProductToResourcesInOrganization(Product product) { + List resourcesInProduct = product.getResourcesContent(); + resourcesInProduct.forEach( + resourceInProduct -> { + resourceInOrganizationService.addResourceToOrganization( + getResourceInOrganizationRequest(product.getOrganization(), resourceInProduct)); + resourceInProductRepository.delete(resourceInProduct); + }); + product.setResourcesContent(null); + } + + @Override + public Object fetchEntity(Object... ids) { + Product product = productRepository.findById((UUID) ids[0]).orElse(null); + if (product == null) { + return null; + } + return mapper.mapToProductResponseDto( + product.getOrganization(), List.of(productMapper.mapToProductResponseDto(product))); + } } diff --git a/src/main/java/jewellery/inventory/service/ProductService.java b/src/main/java/jewellery/inventory/service/ProductService.java index 1527d4e8..c80f0181 100644 --- a/src/main/java/jewellery/inventory/service/ProductService.java +++ b/src/main/java/jewellery/inventory/service/ProductService.java @@ -199,7 +199,7 @@ public void throwExceptionIfProductIsSold(Product product) { } } - private void disassembleProductContent(Product product) { + public void disassembleProductContent(Product product) { if (product.getProductsContent() != null) { logger.debug("Disassembling product content for product with ID: {}", product.getId()); From 4f6732ed05498dd5745e8c03d6ae10d92a5bb02c Mon Sep 17 00:00:00 2001 From: berki0 <107307903+berki0@users.noreply.github.com> Date: Wed, 27 Mar 2024 00:01:00 +0200 Subject: [PATCH 06/33] [feature-72-BE-products-in-organization] add tests --- .../ProductInOrganizationController.java | 2 +- .../service/ProductInOrganizationService.java | 34 +-- .../ResourceInOrganizationService.java | 3 +- .../inventory/helper/ProductTestHelper.java | 11 + ...ductInOrganizationCrudIntegrationTest.java | 248 ++++++++++++++++++ 5 files changed, 265 insertions(+), 33 deletions(-) create mode 100644 src/test/java/jewellery/inventory/integration/ProductInOrganizationCrudIntegrationTest.java diff --git a/src/main/java/jewellery/inventory/controller/ProductInOrganizationController.java b/src/main/java/jewellery/inventory/controller/ProductInOrganizationController.java index 395121e4..7e86e160 100644 --- a/src/main/java/jewellery/inventory/controller/ProductInOrganizationController.java +++ b/src/main/java/jewellery/inventory/controller/ProductInOrganizationController.java @@ -36,7 +36,7 @@ public ProductsInOrganizationResponseDto createProductInOrganization( @ResponseStatus(HttpStatus.NO_CONTENT) @DeleteMapping("/{organizationId}/products/{productId}") public void deleteProductInOrganization( - @PathVariable UUID organizationId, @PathVariable UUID productId) { + @PathVariable("organizationId") UUID organizationId, @PathVariable("productId") UUID productId) { productInOrganizationService.deleteProductInOrganization(organizationId, productId); } diff --git a/src/main/java/jewellery/inventory/service/ProductInOrganizationService.java b/src/main/java/jewellery/inventory/service/ProductInOrganizationService.java index 606f4063..46d4a7b1 100644 --- a/src/main/java/jewellery/inventory/service/ProductInOrganizationService.java +++ b/src/main/java/jewellery/inventory/service/ProductInOrganizationService.java @@ -95,14 +95,16 @@ public void deleteProductInOrganization(UUID organizationId, UUID productId) { Product forDeleteProduct = productService.getProduct(productId); + productService.throwExceptionIfProductIsSold(forDeleteProduct); + productService.throwExceptionIfProductIsPartOfAnotherProduct(productId, forDeleteProduct); + List subProducts = getProductsInProduct( forDeleteProduct.getProductsContent().stream().map(Product::getId).toList(), forDeleteProduct); removeProductsContentFromProduct(subProducts); - removeResourcesFromProduct( - forDeleteProduct, organizationService.getOrganization(organizationId)); + moveQuantityFromResourcesInProductToResourcesInOrganization(forDeleteProduct); productService.deleteProductById(productId); } @@ -151,34 +153,6 @@ private void removeProductsContentFromProduct(List subProducts) { } } - private void removeResourcesFromProduct(Product product, Organization organization) { - List resourceInProductList = new ArrayList<>(); - for (int i = 0; i < product.getResourcesContent().size(); i++) { - ResourceInProduct resourceInProduct = - createResourceInProduct( - product.getResourcesContent().get(i).getQuantity(), - product.getResourcesContent().get(i).getResource(), - product); - resourceInProductList.add(resourceInProduct); - product.setResourcesContent(null); - } - addResourcesToOrganization(resourceInProductList, organization); - } - - private void addResourcesToOrganization( - List resourceInProductList, Organization organization) { - for (ResourceInProduct resourceInProduct : resourceInProductList) { - getResourceInOrganization( - organization, resourceInProduct.getResource(), resourceInProduct.getQuantity()); - } - } - - private ResourceInOrganization getResourceInOrganization( - Organization organization, Resource resource, BigDecimal quantity) { - return resourceInOrganizationService.addResourceToOrganization( - organization, resource, quantity, BigDecimal.ZERO); - } - private List getProductsInProduct( List productsIdInRequest, Product parentProduct) { List products = new ArrayList<>(); diff --git a/src/main/java/jewellery/inventory/service/ResourceInOrganizationService.java b/src/main/java/jewellery/inventory/service/ResourceInOrganizationService.java index 90f34765..3e070903 100644 --- a/src/main/java/jewellery/inventory/service/ResourceInOrganizationService.java +++ b/src/main/java/jewellery/inventory/service/ResourceInOrganizationService.java @@ -26,7 +26,6 @@ public class ResourceInOrganizationService implements EntityFetcher { private static final Logger logger = LogManager.getLogger(ResourceInOrganizationService.class); private final ResourceInOrganizationRepository resourceInOrganizationRepository; - private final UserInOrganizationService userInOrganizationService; private final OrganizationService organizationService; private final ResourceService resourceService; private final ResourceInOrganizationMapper resourceInOrganizationMapper; @@ -142,7 +141,7 @@ private ResourceInOrganization createAndAddNewResourceInOrganization( return resourceInOrganization; } - public ResourceInOrganization getResourceInOrganization( + private ResourceInOrganization getResourceInOrganization( Organization organization, Resource resource) { logger.debug( "Getting resource in organization. Organization: {}, Resource: {}", organization, resource); diff --git a/src/test/java/jewellery/inventory/helper/ProductTestHelper.java b/src/test/java/jewellery/inventory/helper/ProductTestHelper.java index f9f5bbb1..33a74d69 100644 --- a/src/test/java/jewellery/inventory/helper/ProductTestHelper.java +++ b/src/test/java/jewellery/inventory/helper/ProductTestHelper.java @@ -39,6 +39,17 @@ public static ProductRequestDto getProductRequestDto( return productRequestDto; } + public static ProductRequestDto getBaseProductRequestDtoForOrganization( + User author) { + ProductRequestDto productRequestDto = new ProductRequestDto(); + productRequestDto.setProductionNumber("1234"); + productRequestDto.setCatalogNumber("1"); + productRequestDto.setAuthors(List.of(author.getId())); + productRequestDto.setDescription("This is test product"); + productRequestDto.setAdditionalPrice(BigDecimal.ZERO); + return productRequestDto; + } + @NotNull public static ProductRequestDto getProductRequestDto( ResourcesInUserResponseDto resourcesInUser, User user) { diff --git a/src/test/java/jewellery/inventory/integration/ProductInOrganizationCrudIntegrationTest.java b/src/test/java/jewellery/inventory/integration/ProductInOrganizationCrudIntegrationTest.java new file mode 100644 index 00000000..a4648b80 --- /dev/null +++ b/src/test/java/jewellery/inventory/integration/ProductInOrganizationCrudIntegrationTest.java @@ -0,0 +1,248 @@ +package jewellery.inventory.integration; + +import static jewellery.inventory.helper.ResourceTestHelper.getPearlRequestDto; +import static jewellery.inventory.utils.BigDecimalUtil.getBigDecimal; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +import java.math.BigDecimal; +import java.util.List; +import java.util.Objects; +import java.util.UUID; +import jewellery.inventory.dto.request.*; +import jewellery.inventory.dto.request.resource.ResourceQuantityRequestDto; +import jewellery.inventory.dto.request.resource.ResourceRequestDto; +import jewellery.inventory.dto.response.*; +import jewellery.inventory.dto.response.resource.ResourceResponseDto; +import jewellery.inventory.helper.*; +import jewellery.inventory.model.ResourceInOrganization; +import jewellery.inventory.model.User; +import jewellery.inventory.model.resource.PreciousStone; +import jewellery.inventory.model.resource.ResourceInProduct; +import jewellery.inventory.repository.ResourceInProductRepository; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.http.HttpMethod; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; + +class ProductInOrganizationCrudIntegrationTest extends AuthenticatedIntegrationTestBase { + private static final BigDecimal RESOURCE_QUANTITY = getBigDecimal("100"); + private static final BigDecimal RESOURCE_PRICE = getBigDecimal("105"); + + private String getBaseResourceAvailabilityUrl() { + return buildUrl("organizations", "resources-availability"); + } + + private String getBaseResourceUrl() { + return buildUrl("resources"); + } + + private String buildUrl(String... paths) { + return "/" + String.join("/", paths); + } + + private String getBaseOrganizationUrl() { + return buildUrl("organizations"); + } + + private String getOrganizationProductsUrl(String organizationId) { + return buildUrl("organizations", organizationId, "products"); + } + + private String getOrganizationProductsWithIdUrl(String organizationId, String productId) { + return buildUrl("organizations", organizationId, "products", productId); + } + + private PreciousStone preciousStone; + + private ProductRequestDto productRequestDto; + private OrganizationResponseDto organization; + private User user; + + @BeforeEach + void setUp() { + user = createUserInDatabase(UserTestHelper.createTestUserRequest()); + organization = createOrganization(); + preciousStone = createPreciousStoneInDatabase(); + productRequestDto = ProductTestHelper.getBaseProductRequestDtoForOrganization(user); + } + + @Test + void getAllProductsFromOrganizationSuccessfully() { + OrganizationResponseDto organizationResponseDto = createOrganization(); + + ResponseEntity response = + this.testRestTemplate.exchange( + getOrganizationProductsUrl(organizationResponseDto.getId().toString()), + HttpMethod.GET, + null, + ProductsInOrganizationResponseDto.class); + + assertEquals(HttpStatus.OK, response.getStatusCode()); + } + + @Test + void createProductInOrganizationSuccessfully() { + OrganizationResponseDto organizationResponseDto = createOrganization(); + ResourceResponseDto resourceResponse = createResourceResponse(); + + ResourceInOrganizationRequestDto resourceInOrganizationRequest = + ResourceInOrganizationTestHelper.createResourceInOrganizationRequestDto( + organizationResponseDto.getId(), + resourceResponse.getId(), + RESOURCE_QUANTITY, + RESOURCE_PRICE); + + ResponseEntity resource = + sendResourceToOrganization(resourceInOrganizationRequest); + + ResponseEntity getAllProductsInOrgResponse = + this.testRestTemplate.exchange( + getOrganizationProductsUrl(organizationResponseDto.getId().toString()), + HttpMethod.GET, + null, + ProductsInOrganizationResponseDto.class); + + assertEquals(getAllProductsInOrgResponse.getBody().getProducts().size(), 0); + + ResponseEntity productInOrganizationResponse = + createProduct( + setOwnerAndResourceToProductRequest( + productRequestDto, + organizationResponseDto.getId(), + resourceResponse.getId(), + RESOURCE_QUANTITY)); + + assertNotNull(productInOrganizationResponse.getBody()); + assertEquals(productInOrganizationResponse.getBody().getProducts().size(), 1); + assertEquals(HttpStatus.CREATED, productInOrganizationResponse.getStatusCode()); + } + + @Test + void deleteProductInOrganizationSuccessfully() { + OrganizationResponseDto organizationResponseDto = createOrganization(); + ResourceResponseDto resourceResponse = createResourceResponse(); + + ResourceInOrganizationRequestDto resourceInOrganizationRequest = + ResourceInOrganizationTestHelper.createResourceInOrganizationRequestDto( + organizationResponseDto.getId(), + resourceResponse.getId(), + RESOURCE_QUANTITY, + RESOURCE_PRICE); + + ResponseEntity resource = + sendResourceToOrganization(resourceInOrganizationRequest); + + ResponseEntity getAllProductsInOrgResponse = + this.testRestTemplate.exchange( + getOrganizationProductsUrl(organizationResponseDto.getId().toString()), + HttpMethod.GET, + null, + ProductsInOrganizationResponseDto.class); + + assertEquals(getAllProductsInOrgResponse.getBody().getProducts().size(), 0); + + ResponseEntity productInOrganizationResponse = + createProduct( + setOwnerAndResourceToProductRequest( + productRequestDto, + organizationResponseDto.getId(), + resourceResponse.getId(), + BigDecimal.valueOf(99))); + + assertEquals(productInOrganizationResponse.getBody().getProducts().size(), 1); + + ResponseEntity response = + this.testRestTemplate.exchange( + getOrganizationProductsWithIdUrl( + organizationResponseDto.getId().toString(), + productInOrganizationResponse.getBody().getProducts().get(0).getId().toString()), + HttpMethod.DELETE, + null, + Void.class); + + assertEquals(HttpStatus.NO_CONTENT, response.getStatusCode()); + } + + private OrganizationResponseDto createOrganization() { + OrganizationRequestDto organizationRequestDto = + OrganizationTestHelper.getTestOrganizationRequest(); + ResponseEntity response = + this.testRestTemplate.postForEntity( + getBaseOrganizationUrl(), organizationRequestDto, OrganizationResponseDto.class); + + OrganizationResponseDto organizationResponseDto = response.getBody(); + assertNotNull(organizationResponseDto); + return organizationResponseDto; + } + + private ResourceResponseDto createResourceResponse() { + ResourceRequestDto resourceRequest = getPearlRequestDto(); + ResponseEntity resourceResponseEntity = + this.testRestTemplate.postForEntity( + getBaseResourceUrl(), resourceRequest, ResourceResponseDto.class); + + assertEquals(HttpStatus.CREATED, resourceResponseEntity.getStatusCode()); + + ResourceResponseDto createdResource = resourceResponseEntity.getBody(); + assertNotNull(createdResource); + assertNotNull(createdResource.getId()); + return createdResource; + } + + private ResponseEntity sendResourceToOrganization( + ResourceInOrganizationRequestDto resourceInOrganizationRequest) { + return this.testRestTemplate.postForEntity( + getBaseResourceAvailabilityUrl(), + resourceInOrganizationRequest, + ResourcesInOrganizationResponseDto.class); + } + + @Nullable + private ResponseEntity createProduct( + ProductRequestDto productRequestDto) { + return this.testRestTemplate.postForEntity( + getBaseOrganizationUrl() + "/products", + productRequestDto, + ProductsInOrganizationResponseDto.class); + } + + @Nullable + private PreciousStone createPreciousStoneInDatabase() { + ResourceRequestDto resourceRequest = ResourceTestHelper.getPreciousStoneRequestDto(); + ResponseEntity createResource = + this.testRestTemplate.postForEntity( + getBaseResourceUrl(), resourceRequest, PreciousStone.class); + + return createResource.getBody(); + } + + @Nullable + private User createUserInDatabase(UserRequestDto userRequestDto) { + ResponseEntity createUser = + this.testRestTemplate.postForEntity("/users", userRequestDto, User.class); + return createUser.getBody(); + } + + private ProductRequestDto setOwnerAndResourceToProductRequest( + ProductRequestDto productRequestDto, + UUID organizationId, + UUID resourceId, + BigDecimal quantity) { + productRequestDto.setOwnerId(organizationId); + ResourceQuantityRequestDto resourceQuantityRequestDto = new ResourceQuantityRequestDto(); + resourceQuantityRequestDto.setResourceId(resourceId); + resourceQuantityRequestDto.setQuantity(quantity); + productRequestDto.setResourcesContent(List.of(resourceQuantityRequestDto)); + return productRequestDto; + } + + @NotNull + private String getDeleteResourceUrl(UUID organizationId, UUID productId) { + return buildUrl( + "organizations", "resources-availability", organizationId.toString(), productId.toString()); + } +} From f42a518fd2c46c67a02723e955679ba2c363167b Mon Sep 17 00:00:00 2001 From: berki0 <107307903+berki0@users.noreply.github.com> Date: Wed, 27 Mar 2024 17:06:40 +0200 Subject: [PATCH 07/33] [feature-72-BE-products-in-organization] add tests --- .../helper/OrganizationTestHelper.java | 14 +- ...ductInOrganizationCrudIntegrationTest.java | 45 +++--- .../ProductInOrganizationServiceTest.java | 132 ++++++++++++++++++ 3 files changed, 158 insertions(+), 33 deletions(-) create mode 100644 src/test/java/jewellery/inventory/unit/service/ProductInOrganizationServiceTest.java diff --git a/src/test/java/jewellery/inventory/helper/OrganizationTestHelper.java b/src/test/java/jewellery/inventory/helper/OrganizationTestHelper.java index 73c22a89..54da36c9 100644 --- a/src/test/java/jewellery/inventory/helper/OrganizationTestHelper.java +++ b/src/test/java/jewellery/inventory/helper/OrganizationTestHelper.java @@ -5,10 +5,7 @@ import jewellery.inventory.dto.request.OrganizationRequestDto; import jewellery.inventory.dto.request.UserInOrganizationRequestDto; import jewellery.inventory.dto.response.*; -import jewellery.inventory.model.Organization; -import jewellery.inventory.model.OrganizationPermission; -import jewellery.inventory.model.User; -import jewellery.inventory.model.UserInOrganization; +import jewellery.inventory.model.*; public class OrganizationTestHelper { private static final String ORGANIZATION_NAME = "Test Name"; @@ -24,7 +21,14 @@ public static Organization getTestOrganization() { return organization; } - public static Organization getOrganizationWithUserWithNoPermissions(Organization organization, User user) { + public static Organization setProductToOrganization(Organization organization, Product product) { + product.setOrganization(organization); + organization.setProductsOwned(List.of(product)); + return organization; + } + + public static Organization getOrganizationWithUserWithNoPermissions( + Organization organization, User user) { organization.setUsersInOrganization( List.of(new UserInOrganization(UUID.randomUUID(), user, organization, new ArrayList<>()))); return organization; diff --git a/src/test/java/jewellery/inventory/integration/ProductInOrganizationCrudIntegrationTest.java b/src/test/java/jewellery/inventory/integration/ProductInOrganizationCrudIntegrationTest.java index a4648b80..89d8fc92 100644 --- a/src/test/java/jewellery/inventory/integration/ProductInOrganizationCrudIntegrationTest.java +++ b/src/test/java/jewellery/inventory/integration/ProductInOrganizationCrudIntegrationTest.java @@ -7,7 +7,6 @@ import java.math.BigDecimal; import java.util.List; -import java.util.Objects; import java.util.UUID; import jewellery.inventory.dto.request.*; import jewellery.inventory.dto.request.resource.ResourceQuantityRequestDto; @@ -15,12 +14,8 @@ import jewellery.inventory.dto.response.*; import jewellery.inventory.dto.response.resource.ResourceResponseDto; import jewellery.inventory.helper.*; -import jewellery.inventory.model.ResourceInOrganization; import jewellery.inventory.model.User; import jewellery.inventory.model.resource.PreciousStone; -import jewellery.inventory.model.resource.ResourceInProduct; -import jewellery.inventory.repository.ResourceInProductRepository; -import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -127,31 +122,31 @@ void deleteProductInOrganizationSuccessfully() { ResourceResponseDto resourceResponse = createResourceResponse(); ResourceInOrganizationRequestDto resourceInOrganizationRequest = - ResourceInOrganizationTestHelper.createResourceInOrganizationRequestDto( - organizationResponseDto.getId(), - resourceResponse.getId(), - RESOURCE_QUANTITY, - RESOURCE_PRICE); + ResourceInOrganizationTestHelper.createResourceInOrganizationRequestDto( + organizationResponseDto.getId(), + resourceResponse.getId(), + RESOURCE_QUANTITY, + RESOURCE_PRICE); ResponseEntity resource = - sendResourceToOrganization(resourceInOrganizationRequest); + sendResourceToOrganization(resourceInOrganizationRequest); ResponseEntity getAllProductsInOrgResponse = - this.testRestTemplate.exchange( - getOrganizationProductsUrl(organizationResponseDto.getId().toString()), - HttpMethod.GET, - null, - ProductsInOrganizationResponseDto.class); + this.testRestTemplate.exchange( + getOrganizationProductsUrl(organizationResponseDto.getId().toString()), + HttpMethod.GET, + null, + ProductsInOrganizationResponseDto.class); assertEquals(getAllProductsInOrgResponse.getBody().getProducts().size(), 0); ResponseEntity productInOrganizationResponse = - createProduct( - setOwnerAndResourceToProductRequest( - productRequestDto, - organizationResponseDto.getId(), - resourceResponse.getId(), - BigDecimal.valueOf(99))); + createProduct( + setOwnerAndResourceToProductRequest( + productRequestDto, + organizationResponseDto.getId(), + resourceResponse.getId(), + RESOURCE_QUANTITY)); assertEquals(productInOrganizationResponse.getBody().getProducts().size(), 1); @@ -239,10 +234,4 @@ private ProductRequestDto setOwnerAndResourceToProductRequest( productRequestDto.setResourcesContent(List.of(resourceQuantityRequestDto)); return productRequestDto; } - - @NotNull - private String getDeleteResourceUrl(UUID organizationId, UUID productId) { - return buildUrl( - "organizations", "resources-availability", organizationId.toString(), productId.toString()); - } } diff --git a/src/test/java/jewellery/inventory/unit/service/ProductInOrganizationServiceTest.java b/src/test/java/jewellery/inventory/unit/service/ProductInOrganizationServiceTest.java new file mode 100644 index 00000000..7f6632e0 --- /dev/null +++ b/src/test/java/jewellery/inventory/unit/service/ProductInOrganizationServiceTest.java @@ -0,0 +1,132 @@ +package jewellery.inventory.unit.service; + +import static jewellery.inventory.helper.OrganizationTestHelper.getTestOrganizationResponseDto; +import static jewellery.inventory.helper.OrganizationTestHelper.setProductToOrganization; +import static jewellery.inventory.helper.ProductTestHelper.getTestProduct; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.mockito.Mockito.when; + +import java.math.BigDecimal; +import java.util.ArrayList; +import java.util.List; +import java.util.UUID; +import jewellery.inventory.dto.request.ProductRequestDto; +import jewellery.inventory.dto.request.ResourceInOrganizationRequestDto; +import jewellery.inventory.dto.request.resource.ResourceQuantityRequestDto; +import jewellery.inventory.dto.response.OrganizationResponseDto; +import jewellery.inventory.dto.response.ProductResponseDto; +import jewellery.inventory.dto.response.ProductsInOrganizationResponseDto; +import jewellery.inventory.helper.*; +import jewellery.inventory.mapper.ProductInOrganizationMapper; +import jewellery.inventory.mapper.ProductMapper; +import jewellery.inventory.model.Organization; +import jewellery.inventory.model.Product; +import jewellery.inventory.model.ResourceInOrganization; +import jewellery.inventory.model.User; +import jewellery.inventory.model.resource.Resource; +import jewellery.inventory.repository.ProductRepository; +import jewellery.inventory.repository.ResourceInProductRepository; +import jewellery.inventory.service.OrganizationService; +import jewellery.inventory.service.ProductInOrganizationService; +import jewellery.inventory.service.ProductService; +import jewellery.inventory.service.ResourceInOrganizationService; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +@ExtendWith(MockitoExtension.class) + class ProductInOrganizationServiceTest { + @InjectMocks private ProductInOrganizationService productInOrganizationService; + @Mock private OrganizationService organizationService; + @Mock private ProductService productService; + @Mock private ProductInOrganizationMapper mapper; + @Mock private ResourceInOrganizationService resourceInOrganizationService; + @Mock private ResourceInProductRepository resourceInProductRepository; + @Mock private ProductRepository productRepository; + @Mock private ProductMapper productMapper; + private Organization organization; + private Organization organizationWithProduct; + private OrganizationResponseDto organizationResponseDto; + + private Resource resource; + private ResourceInOrganizationRequestDto resourceInOrganizationRequestDto; + private ResourceInOrganization resourceInOrganization; + private ProductRequestDto productRequestDto; + private User user; + private Product product; + private ProductResponseDto productResponseDto; + private static final BigDecimal QUANTITY = BigDecimal.ONE; + private static final BigDecimal NEGATIVE_QUANTITY = BigDecimal.valueOf(-5); + private static final BigDecimal BIG_QUANTITY = BigDecimal.valueOf(30); + private static final BigDecimal DEAL_PRICE = BigDecimal.TEN; + private ProductsInOrganizationResponseDto productsInOrganizationResponseDto; + + @BeforeEach + void setUp() { + user = UserTestHelper.createSecondTestUser(); + organization = OrganizationTestHelper.getTestOrganization(); + resource = ResourceTestHelper.getPearl(); + resourceInOrganizationRequestDto = + ResourceInOrganizationTestHelper.createResourceInOrganizationRequestDto( + organization.getId(), resource.getId(), QUANTITY, DEAL_PRICE); + resourceInOrganization = + ResourceInOrganizationTestHelper.createResourceInOrganization(organization, resource); + organization.setResourceInOrganization(List.of(resourceInOrganization)); + productRequestDto = ProductTestHelper.getBaseProductRequestDtoForOrganization(user); + productRequestDto = + setOwnerAndResourceToProductRequest( + productRequestDto, organization.getId(), user.getId(), BIG_QUANTITY); + product = getTestProduct(user, resource); + organizationWithProduct = setProductToOrganization(organization, product); + productResponseDto= productToResponse(product); + organizationResponseDto = getTestOrganizationResponseDto(organization); + productsInOrganizationResponseDto = new ProductsInOrganizationResponseDto(organizationResponseDto,List.of(productResponseDto)); + + + + } + + @Test + void getAllProductsInOrganizationSuccessfully() { + when(organizationService.getOrganization(organizationWithProduct.getId())).thenReturn(organizationWithProduct); + when(mapper.mapToProductResponseDto(organizationWithProduct, new ArrayList<>())) + .thenReturn(productsInOrganizationResponseDto); + + ProductsInOrganizationResponseDto products = + productInOrganizationService.getProductsInOrganization(organizationWithProduct.getId()); + assertNotNull(products); + assertEquals(1, products.getProducts().size()); + } + + @Test + void createProductInOrganizationSuccessfully() { + when(organizationService.getOrganization(organizationWithProduct.getId())).thenReturn(organizationWithProduct); + + ProductsInOrganizationResponseDto products = + productInOrganizationService.createProductInOrganization(productRequestDto); + assertNotNull(products); + + } + + private ProductRequestDto setOwnerAndResourceToProductRequest( + ProductRequestDto productRequestDto, + UUID organizationId, + UUID resourceId, + BigDecimal quantity) { + productRequestDto.setOwnerId(organizationId); + ResourceQuantityRequestDto resourceQuantityRequestDto = new ResourceQuantityRequestDto(); + resourceQuantityRequestDto.setResourceId(resourceId); + resourceQuantityRequestDto.setQuantity(quantity); + productRequestDto.setResourcesContent(List.of(resourceQuantityRequestDto)); + return productRequestDto; + } + private ProductResponseDto productToResponse(Product product){ + ProductResponseDto productResponseDto=new ProductResponseDto(); + productResponseDto.setId(product.getId()); + return productResponseDto; + } +} From b108aae707f9c79abe1ed97deb2fe0f33083fe3d Mon Sep 17 00:00:00 2001 From: berki0 <107307903+berki0@users.noreply.github.com> Date: Wed, 27 Mar 2024 17:28:24 +0200 Subject: [PATCH 08/33] [feature-72-BE-products-in-organization] add tests --- .../helper/OrganizationTestHelper.java | 3 ++- .../ResourceInOrganizationTestHelper.java | 2 +- .../ProductInOrganizationServiceTest.java | 18 ++++++++++++++---- 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/src/test/java/jewellery/inventory/helper/OrganizationTestHelper.java b/src/test/java/jewellery/inventory/helper/OrganizationTestHelper.java index 54da36c9..4349f86a 100644 --- a/src/test/java/jewellery/inventory/helper/OrganizationTestHelper.java +++ b/src/test/java/jewellery/inventory/helper/OrganizationTestHelper.java @@ -21,9 +21,10 @@ public static Organization getTestOrganization() { return organization; } - public static Organization setProductToOrganization(Organization organization, Product product) { + public static Organization setProductAndRsourcesToOrganization(Organization organization, Product product,ResourceInOrganization resourceInOrganization) { product.setOrganization(organization); organization.setProductsOwned(List.of(product)); + organization.setResourceInOrganization(List.of(resourceInOrganization)); return organization; } diff --git a/src/test/java/jewellery/inventory/helper/ResourceInOrganizationTestHelper.java b/src/test/java/jewellery/inventory/helper/ResourceInOrganizationTestHelper.java index 7c64ea71..a22deca2 100644 --- a/src/test/java/jewellery/inventory/helper/ResourceInOrganizationTestHelper.java +++ b/src/test/java/jewellery/inventory/helper/ResourceInOrganizationTestHelper.java @@ -25,7 +25,7 @@ public static ResourceInOrganization createResourceInOrganization( .id(UUID.randomUUID()) .organization(organization) .resource(resource) - .quantity(BigDecimal.TEN) + .quantity(BigDecimal.valueOf(100)) .dealPrice(BigDecimal.ONE) .build(); } diff --git a/src/test/java/jewellery/inventory/unit/service/ProductInOrganizationServiceTest.java b/src/test/java/jewellery/inventory/unit/service/ProductInOrganizationServiceTest.java index 7f6632e0..88ac1cf5 100644 --- a/src/test/java/jewellery/inventory/unit/service/ProductInOrganizationServiceTest.java +++ b/src/test/java/jewellery/inventory/unit/service/ProductInOrganizationServiceTest.java @@ -1,7 +1,6 @@ package jewellery.inventory.unit.service; -import static jewellery.inventory.helper.OrganizationTestHelper.getTestOrganizationResponseDto; -import static jewellery.inventory.helper.OrganizationTestHelper.setProductToOrganization; +import static jewellery.inventory.helper.OrganizationTestHelper.*; import static jewellery.inventory.helper.ProductTestHelper.getTestProduct; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; @@ -81,7 +80,7 @@ void setUp() { setOwnerAndResourceToProductRequest( productRequestDto, organization.getId(), user.getId(), BIG_QUANTITY); product = getTestProduct(user, resource); - organizationWithProduct = setProductToOrganization(organization, product); + organizationWithProduct = setProductAndRsourcesToOrganization(organization, product,resourceInOrganization); productResponseDto= productToResponse(product); organizationResponseDto = getTestOrganizationResponseDto(organization); productsInOrganizationResponseDto = new ProductsInOrganizationResponseDto(organizationResponseDto,List.of(productResponseDto)); @@ -104,11 +103,22 @@ void getAllProductsInOrganizationSuccessfully() { @Test void createProductInOrganizationSuccessfully() { - when(organizationService.getOrganization(organizationWithProduct.getId())).thenReturn(organizationWithProduct); + when(organizationService.getOrganization(organization.getId())).thenReturn(organization); + resourceInOrganizationService.addResourceToOrganization(resourceInOrganizationRequestDto); + + when(resourceInOrganizationService.findResourceInOrganizationOrThrow( + organization, productRequestDto.getResourcesContent().get(0).getResourceId())).thenReturn(resourceInOrganization); + + + when(mapper.mapToProductResponseDto(organization,new ArrayList<>())) + .thenReturn(productsInOrganizationResponseDto); ProductsInOrganizationResponseDto products = productInOrganizationService.createProductInOrganization(productRequestDto); assertNotNull(products); + assertEquals(1, products.getProducts().size()); + assertEquals(products.getOrganization().getId(),organization.getId()); + assertEquals(products.getProducts().get(0).getId(),organization.getProductsOwned().get(0).getId()); } From 76b0db80adc753628967fd17aeef3a1031af58c3 Mon Sep 17 00:00:00 2001 From: berki0 <107307903+berki0@users.noreply.github.com> Date: Wed, 27 Mar 2024 17:40:36 +0200 Subject: [PATCH 09/33] [feature-72-BE-products-in-organization] add tests --- ...ductInOrganizationCrudIntegrationTest.java | 90 +++++++++---------- .../ResourceInOrganizationServiceTest.java | 2 +- 2 files changed, 46 insertions(+), 46 deletions(-) diff --git a/src/test/java/jewellery/inventory/integration/ProductInOrganizationCrudIntegrationTest.java b/src/test/java/jewellery/inventory/integration/ProductInOrganizationCrudIntegrationTest.java index 89d8fc92..5b3f4290 100644 --- a/src/test/java/jewellery/inventory/integration/ProductInOrganizationCrudIntegrationTest.java +++ b/src/test/java/jewellery/inventory/integration/ProductInOrganizationCrudIntegrationTest.java @@ -116,51 +116,51 @@ void createProductInOrganizationSuccessfully() { assertEquals(HttpStatus.CREATED, productInOrganizationResponse.getStatusCode()); } - @Test - void deleteProductInOrganizationSuccessfully() { - OrganizationResponseDto organizationResponseDto = createOrganization(); - ResourceResponseDto resourceResponse = createResourceResponse(); - - ResourceInOrganizationRequestDto resourceInOrganizationRequest = - ResourceInOrganizationTestHelper.createResourceInOrganizationRequestDto( - organizationResponseDto.getId(), - resourceResponse.getId(), - RESOURCE_QUANTITY, - RESOURCE_PRICE); - - ResponseEntity resource = - sendResourceToOrganization(resourceInOrganizationRequest); - - ResponseEntity getAllProductsInOrgResponse = - this.testRestTemplate.exchange( - getOrganizationProductsUrl(organizationResponseDto.getId().toString()), - HttpMethod.GET, - null, - ProductsInOrganizationResponseDto.class); - - assertEquals(getAllProductsInOrgResponse.getBody().getProducts().size(), 0); - - ResponseEntity productInOrganizationResponse = - createProduct( - setOwnerAndResourceToProductRequest( - productRequestDto, - organizationResponseDto.getId(), - resourceResponse.getId(), - RESOURCE_QUANTITY)); - - assertEquals(productInOrganizationResponse.getBody().getProducts().size(), 1); - - ResponseEntity response = - this.testRestTemplate.exchange( - getOrganizationProductsWithIdUrl( - organizationResponseDto.getId().toString(), - productInOrganizationResponse.getBody().getProducts().get(0).getId().toString()), - HttpMethod.DELETE, - null, - Void.class); - - assertEquals(HttpStatus.NO_CONTENT, response.getStatusCode()); - } +// @Test +// void deleteProductInOrganizationSuccessfully() { +// OrganizationResponseDto organizationResponseDto = createOrganization(); +// ResourceResponseDto resourceResponse = createResourceResponse(); +// +// ResourceInOrganizationRequestDto resourceInOrganizationRequest = +// ResourceInOrganizationTestHelper.createResourceInOrganizationRequestDto( +// organizationResponseDto.getId(), +// resourceResponse.getId(), +// RESOURCE_QUANTITY, +// RESOURCE_PRICE); +// +// ResponseEntity resource = +// sendResourceToOrganization(resourceInOrganizationRequest); +// +// ResponseEntity getAllProductsInOrgResponse = +// this.testRestTemplate.exchange( +// getOrganizationProductsUrl(organizationResponseDto.getId().toString()), +// HttpMethod.GET, +// null, +// ProductsInOrganizationResponseDto.class); +// +// assertEquals(getAllProductsInOrgResponse.getBody().getProducts().size(), 0); +// +// ResponseEntity productInOrganizationResponse = +// createProduct( +// setOwnerAndResourceToProductRequest( +// productRequestDto, +// organizationResponseDto.getId(), +// resourceResponse.getId(), +// RESOURCE_QUANTITY)); +// +// assertEquals(productInOrganizationResponse.getBody().getProducts().size(), 1); +// +// ResponseEntity response = +// this.testRestTemplate.exchange( +// getOrganizationProductsWithIdUrl( +// organizationResponseDto.getId().toString(), +// productInOrganizationResponse.getBody().getProducts().get(0).getId().toString()), +// HttpMethod.DELETE, +// null, +// Void.class); +// +// assertEquals(HttpStatus.NO_CONTENT, response.getStatusCode()); +// } private OrganizationResponseDto createOrganization() { OrganizationRequestDto organizationRequestDto = diff --git a/src/test/java/jewellery/inventory/unit/service/ResourceInOrganizationServiceTest.java b/src/test/java/jewellery/inventory/unit/service/ResourceInOrganizationServiceTest.java index a53b573c..f0e27d67 100644 --- a/src/test/java/jewellery/inventory/unit/service/ResourceInOrganizationServiceTest.java +++ b/src/test/java/jewellery/inventory/unit/service/ResourceInOrganizationServiceTest.java @@ -43,7 +43,7 @@ class ResourceInOrganizationServiceTest { private ResourceInOrganization resourceInOrganization; private static final BigDecimal QUANTITY = BigDecimal.ONE; private static final BigDecimal NEGATIVE_QUANTITY = BigDecimal.valueOf(-5); - private static final BigDecimal BIG_QUANTITY = BigDecimal.valueOf(30); + private static final BigDecimal BIG_QUANTITY = BigDecimal.valueOf(1000); private static final BigDecimal DEAL_PRICE = BigDecimal.TEN; @BeforeEach From 3f9fc7251160aaa6c94475364a6593a74242b67c Mon Sep 17 00:00:00 2001 From: berki0 <107307903+berki0@users.noreply.github.com> Date: Wed, 27 Mar 2024 18:21:42 +0200 Subject: [PATCH 10/33] [feature-72-BE-products-in-organization] add tests --- .../service/ProductInOrganizationService.java | 13 +++-- .../inventory/helper/ProductTestHelper.java | 2 +- .../ProductInOrganizationServiceTest.java | 53 +++++++++++-------- 3 files changed, 41 insertions(+), 27 deletions(-) diff --git a/src/main/java/jewellery/inventory/service/ProductInOrganizationService.java b/src/main/java/jewellery/inventory/service/ProductInOrganizationService.java index 46d4a7b1..f0fe5124 100644 --- a/src/main/java/jewellery/inventory/service/ProductInOrganizationService.java +++ b/src/main/java/jewellery/inventory/service/ProductInOrganizationService.java @@ -98,12 +98,15 @@ public void deleteProductInOrganization(UUID organizationId, UUID productId) { productService.throwExceptionIfProductIsSold(forDeleteProduct); productService.throwExceptionIfProductIsPartOfAnotherProduct(productId, forDeleteProduct); - List subProducts = - getProductsInProduct( - forDeleteProduct.getProductsContent().stream().map(Product::getId).toList(), - forDeleteProduct); + if (!forDeleteProduct.getProductsContent().isEmpty()) { + List subProducts = + getProductsInProduct( + forDeleteProduct.getProductsContent().stream().map(Product::getId).toList(), + forDeleteProduct); + + removeProductsContentFromProduct(subProducts); + } - removeProductsContentFromProduct(subProducts); moveQuantityFromResourcesInProductToResourcesInOrganization(forDeleteProduct); productService.deleteProductById(productId); } diff --git a/src/test/java/jewellery/inventory/helper/ProductTestHelper.java b/src/test/java/jewellery/inventory/helper/ProductTestHelper.java index 33a74d69..9f9431f4 100644 --- a/src/test/java/jewellery/inventory/helper/ProductTestHelper.java +++ b/src/test/java/jewellery/inventory/helper/ProductTestHelper.java @@ -85,7 +85,7 @@ public static Product getTestProduct(User user, Resource pearl) { testProduct.setOwner(user); testProduct.setDescription("This is Test Product"); testProduct.setResourcesContent(List.of(getResourceInProduct(pearl))); - testProduct.setProductsContent(null); + testProduct.setProductsContent(new ArrayList<>()); testProduct.setContentOf(null); testProduct.setAdditionalPrice(BigDecimal.TEN); return testProduct; diff --git a/src/test/java/jewellery/inventory/unit/service/ProductInOrganizationServiceTest.java b/src/test/java/jewellery/inventory/unit/service/ProductInOrganizationServiceTest.java index 88ac1cf5..2a1b87bc 100644 --- a/src/test/java/jewellery/inventory/unit/service/ProductInOrganizationServiceTest.java +++ b/src/test/java/jewellery/inventory/unit/service/ProductInOrganizationServiceTest.java @@ -2,9 +2,8 @@ import static jewellery.inventory.helper.OrganizationTestHelper.*; import static jewellery.inventory.helper.ProductTestHelper.getTestProduct; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.mockito.Mockito.when; +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.*; import java.math.BigDecimal; import java.util.ArrayList; @@ -38,7 +37,7 @@ import org.mockito.junit.jupiter.MockitoExtension; @ExtendWith(MockitoExtension.class) - class ProductInOrganizationServiceTest { +class ProductInOrganizationServiceTest { @InjectMocks private ProductInOrganizationService productInOrganizationService; @Mock private OrganizationService organizationService; @Mock private ProductService productService; @@ -62,7 +61,7 @@ class ProductInOrganizationServiceTest { private static final BigDecimal NEGATIVE_QUANTITY = BigDecimal.valueOf(-5); private static final BigDecimal BIG_QUANTITY = BigDecimal.valueOf(30); private static final BigDecimal DEAL_PRICE = BigDecimal.TEN; - private ProductsInOrganizationResponseDto productsInOrganizationResponseDto; + private ProductsInOrganizationResponseDto productsInOrganizationResponseDto; @BeforeEach void setUp() { @@ -80,18 +79,18 @@ void setUp() { setOwnerAndResourceToProductRequest( productRequestDto, organization.getId(), user.getId(), BIG_QUANTITY); product = getTestProduct(user, resource); - organizationWithProduct = setProductAndRsourcesToOrganization(organization, product,resourceInOrganization); - productResponseDto= productToResponse(product); + organizationWithProduct = + setProductAndRsourcesToOrganization(organization, product, resourceInOrganization); + productResponseDto = productToResponse(product); organizationResponseDto = getTestOrganizationResponseDto(organization); - productsInOrganizationResponseDto = new ProductsInOrganizationResponseDto(organizationResponseDto,List.of(productResponseDto)); - - - + productsInOrganizationResponseDto = + new ProductsInOrganizationResponseDto(organizationResponseDto, List.of(productResponseDto)); } @Test void getAllProductsInOrganizationSuccessfully() { - when(organizationService.getOrganization(organizationWithProduct.getId())).thenReturn(organizationWithProduct); + when(organizationService.getOrganization(organizationWithProduct.getId())) + .thenReturn(organizationWithProduct); when(mapper.mapToProductResponseDto(organizationWithProduct, new ArrayList<>())) .thenReturn(productsInOrganizationResponseDto); @@ -107,19 +106,30 @@ void createProductInOrganizationSuccessfully() { resourceInOrganizationService.addResourceToOrganization(resourceInOrganizationRequestDto); when(resourceInOrganizationService.findResourceInOrganizationOrThrow( - organization, productRequestDto.getResourcesContent().get(0).getResourceId())).thenReturn(resourceInOrganization); + organization, productRequestDto.getResourcesContent().get(0).getResourceId())) + .thenReturn(resourceInOrganization); - - when(mapper.mapToProductResponseDto(organization,new ArrayList<>())) - .thenReturn(productsInOrganizationResponseDto); + when(mapper.mapToProductResponseDto(organization, new ArrayList<>())) + .thenReturn(productsInOrganizationResponseDto); ProductsInOrganizationResponseDto products = - productInOrganizationService.createProductInOrganization(productRequestDto); + productInOrganizationService.createProductInOrganization(productRequestDto); assertNotNull(products); assertEquals(1, products.getProducts().size()); - assertEquals(products.getOrganization().getId(),organization.getId()); - assertEquals(products.getProducts().get(0).getId(),organization.getProductsOwned().get(0).getId()); + assertEquals(products.getOrganization().getId(), organization.getId()); + assertEquals( + products.getProducts().get(0).getId(), organization.getProductsOwned().get(0).getId()); + } + @Test + void deleteProductInOrganizationSuccessfully() { + when(organizationService.getOrganization(organization.getId())).thenReturn(organization); + resourceInOrganizationService.addResourceToOrganization(resourceInOrganizationRequestDto); + + when(productService.getProduct(product.getId())).thenReturn(product); + + productInOrganizationService.deleteProductInOrganization(organization.getId(), product.getId()); + verify(productService, times(1)).deleteProductById(product.getId()); } private ProductRequestDto setOwnerAndResourceToProductRequest( @@ -134,8 +144,9 @@ private ProductRequestDto setOwnerAndResourceToProductRequest( productRequestDto.setResourcesContent(List.of(resourceQuantityRequestDto)); return productRequestDto; } - private ProductResponseDto productToResponse(Product product){ - ProductResponseDto productResponseDto=new ProductResponseDto(); + + private ProductResponseDto productToResponse(Product product) { + ProductResponseDto productResponseDto = new ProductResponseDto(); productResponseDto.setId(product.getId()); return productResponseDto; } From 283f2bb6759014de0fa157e516512d8c071f5092 Mon Sep 17 00:00:00 2001 From: berki0 <107307903+berki0@users.noreply.github.com> Date: Wed, 27 Mar 2024 18:28:05 +0200 Subject: [PATCH 11/33] [feature-72-BE-products-in-organization] add tests --- .../service/ProductInOrganizationService.java | 13 +++++-------- .../inventory/unit/service/ProductServiceTest.java | 2 +- 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/src/main/java/jewellery/inventory/service/ProductInOrganizationService.java b/src/main/java/jewellery/inventory/service/ProductInOrganizationService.java index f0fe5124..46d4a7b1 100644 --- a/src/main/java/jewellery/inventory/service/ProductInOrganizationService.java +++ b/src/main/java/jewellery/inventory/service/ProductInOrganizationService.java @@ -98,15 +98,12 @@ public void deleteProductInOrganization(UUID organizationId, UUID productId) { productService.throwExceptionIfProductIsSold(forDeleteProduct); productService.throwExceptionIfProductIsPartOfAnotherProduct(productId, forDeleteProduct); - if (!forDeleteProduct.getProductsContent().isEmpty()) { - List subProducts = - getProductsInProduct( - forDeleteProduct.getProductsContent().stream().map(Product::getId).toList(), - forDeleteProduct); - - removeProductsContentFromProduct(subProducts); - } + List subProducts = + getProductsInProduct( + forDeleteProduct.getProductsContent().stream().map(Product::getId).toList(), + forDeleteProduct); + removeProductsContentFromProduct(subProducts); moveQuantityFromResourcesInProductToResourcesInOrganization(forDeleteProduct); productService.deleteProductById(productId); } diff --git a/src/test/java/jewellery/inventory/unit/service/ProductServiceTest.java b/src/test/java/jewellery/inventory/unit/service/ProductServiceTest.java index 313c5652..f9e2fcfb 100644 --- a/src/test/java/jewellery/inventory/unit/service/ProductServiceTest.java +++ b/src/test/java/jewellery/inventory/unit/service/ProductServiceTest.java @@ -330,7 +330,7 @@ void testUpdateProductSuccessfullyWhenProductIsPartOfAnotherProduct() { verify(userRepository,times(1)).findById(user.getId()); verify(resourceInUserRepository, times(1)) .findByResourceIdAndOwnerId(pearl.getId(), user.getId()); - verify(productRepository, times(1)).save(innerProduct); + verify(productRepository, times(2)).save(innerProduct); } @Test From c7df9935b016fcee310f6e4276798995e75391ed Mon Sep 17 00:00:00 2001 From: berki0 <107307903+berki0@users.noreply.github.com> Date: Wed, 27 Mar 2024 20:31:37 +0200 Subject: [PATCH 12/33] [feature-72-BE-products-in-organization] add tests --- .../helper/OrganizationTestHelper.java | 2 +- .../inventory/helper/ProductTestHelper.java | 10 ++- ...ductInOrganizationCrudIntegrationTest.java | 11 +--- .../ProductInOrganizationServiceTest.java | 64 +++++-------------- 4 files changed, 29 insertions(+), 58 deletions(-) diff --git a/src/test/java/jewellery/inventory/helper/OrganizationTestHelper.java b/src/test/java/jewellery/inventory/helper/OrganizationTestHelper.java index 4349f86a..97185fff 100644 --- a/src/test/java/jewellery/inventory/helper/OrganizationTestHelper.java +++ b/src/test/java/jewellery/inventory/helper/OrganizationTestHelper.java @@ -21,7 +21,7 @@ public static Organization getTestOrganization() { return organization; } - public static Organization setProductAndRsourcesToOrganization(Organization organization, Product product,ResourceInOrganization resourceInOrganization) { + public static Organization setProductAndResourcesToOrganization(Organization organization, Product product, ResourceInOrganization resourceInOrganization) { product.setOrganization(organization); organization.setProductsOwned(List.of(product)); organization.setResourceInOrganization(List.of(resourceInOrganization)); diff --git a/src/test/java/jewellery/inventory/helper/ProductTestHelper.java b/src/test/java/jewellery/inventory/helper/ProductTestHelper.java index 9f9431f4..211c98cd 100644 --- a/src/test/java/jewellery/inventory/helper/ProductTestHelper.java +++ b/src/test/java/jewellery/inventory/helper/ProductTestHelper.java @@ -39,14 +39,20 @@ public static ProductRequestDto getProductRequestDto( return productRequestDto; } - public static ProductRequestDto getBaseProductRequestDtoForOrganization( - User author) { + public static ProductRequestDto getProductRequestDtoForOrganization( + User author,UUID organizationId,UUID resourceId, + BigDecimal quantity) { ProductRequestDto productRequestDto = new ProductRequestDto(); + productRequestDto.setOwnerId(organizationId); productRequestDto.setProductionNumber("1234"); productRequestDto.setCatalogNumber("1"); productRequestDto.setAuthors(List.of(author.getId())); productRequestDto.setDescription("This is test product"); productRequestDto.setAdditionalPrice(BigDecimal.ZERO); + ResourceQuantityRequestDto resourceQuantityRequestDto = new ResourceQuantityRequestDto(); + resourceQuantityRequestDto.setResourceId(resourceId); + resourceQuantityRequestDto.setQuantity(quantity); + productRequestDto.setResourcesContent(List.of(resourceQuantityRequestDto)); return productRequestDto; } diff --git a/src/test/java/jewellery/inventory/integration/ProductInOrganizationCrudIntegrationTest.java b/src/test/java/jewellery/inventory/integration/ProductInOrganizationCrudIntegrationTest.java index 5b3f4290..ab9aae50 100644 --- a/src/test/java/jewellery/inventory/integration/ProductInOrganizationCrudIntegrationTest.java +++ b/src/test/java/jewellery/inventory/integration/ProductInOrganizationCrudIntegrationTest.java @@ -47,12 +47,7 @@ private String getOrganizationProductsUrl(String organizationId) { return buildUrl("organizations", organizationId, "products"); } - private String getOrganizationProductsWithIdUrl(String organizationId, String productId) { - return buildUrl("organizations", organizationId, "products", productId); - } - private PreciousStone preciousStone; - private ProductRequestDto productRequestDto; private OrganizationResponseDto organization; private User user; @@ -62,7 +57,7 @@ void setUp() { user = createUserInDatabase(UserTestHelper.createTestUserRequest()); organization = createOrganization(); preciousStone = createPreciousStoneInDatabase(); - productRequestDto = ProductTestHelper.getBaseProductRequestDtoForOrganization(user); + productRequestDto = ProductTestHelper.getProductRequestDtoForOrganization(user,organization.getId(),preciousStone.getId(),RESOURCE_QUANTITY); } @Test @@ -101,7 +96,7 @@ void createProductInOrganizationSuccessfully() { null, ProductsInOrganizationResponseDto.class); - assertEquals(getAllProductsInOrgResponse.getBody().getProducts().size(), 0); + assertEquals(0, getAllProductsInOrgResponse.getBody().getProducts().size()); ResponseEntity productInOrganizationResponse = createProduct( @@ -112,7 +107,7 @@ void createProductInOrganizationSuccessfully() { RESOURCE_QUANTITY)); assertNotNull(productInOrganizationResponse.getBody()); - assertEquals(productInOrganizationResponse.getBody().getProducts().size(), 1); + assertEquals(1, productInOrganizationResponse.getBody().getProducts().size()); assertEquals(HttpStatus.CREATED, productInOrganizationResponse.getStatusCode()); } diff --git a/src/test/java/jewellery/inventory/unit/service/ProductInOrganizationServiceTest.java b/src/test/java/jewellery/inventory/unit/service/ProductInOrganizationServiceTest.java index 2a1b87bc..571da634 100644 --- a/src/test/java/jewellery/inventory/unit/service/ProductInOrganizationServiceTest.java +++ b/src/test/java/jewellery/inventory/unit/service/ProductInOrganizationServiceTest.java @@ -8,10 +8,8 @@ import java.math.BigDecimal; import java.util.ArrayList; import java.util.List; -import java.util.UUID; import jewellery.inventory.dto.request.ProductRequestDto; import jewellery.inventory.dto.request.ResourceInOrganizationRequestDto; -import jewellery.inventory.dto.request.resource.ResourceQuantityRequestDto; import jewellery.inventory.dto.response.OrganizationResponseDto; import jewellery.inventory.dto.response.ProductResponseDto; import jewellery.inventory.dto.response.ProductsInOrganizationResponseDto; @@ -44,47 +42,33 @@ class ProductInOrganizationServiceTest { @Mock private ProductInOrganizationMapper mapper; @Mock private ResourceInOrganizationService resourceInOrganizationService; @Mock private ResourceInProductRepository resourceInProductRepository; - @Mock private ProductRepository productRepository; - @Mock private ProductMapper productMapper; + private static final BigDecimal BIG_QUANTITY = BigDecimal.valueOf(30); + private Organization organization; private Organization organizationWithProduct; - private OrganizationResponseDto organizationResponseDto; - - private Resource resource; - private ResourceInOrganizationRequestDto resourceInOrganizationRequestDto; private ResourceInOrganization resourceInOrganization; private ProductRequestDto productRequestDto; - private User user; private Product product; - private ProductResponseDto productResponseDto; - private static final BigDecimal QUANTITY = BigDecimal.ONE; - private static final BigDecimal NEGATIVE_QUANTITY = BigDecimal.valueOf(-5); - private static final BigDecimal BIG_QUANTITY = BigDecimal.valueOf(30); - private static final BigDecimal DEAL_PRICE = BigDecimal.TEN; private ProductsInOrganizationResponseDto productsInOrganizationResponseDto; @BeforeEach void setUp() { - user = UserTestHelper.createSecondTestUser(); + + User user = UserTestHelper.createSecondTestUser(); organization = OrganizationTestHelper.getTestOrganization(); - resource = ResourceTestHelper.getPearl(); - resourceInOrganizationRequestDto = - ResourceInOrganizationTestHelper.createResourceInOrganizationRequestDto( - organization.getId(), resource.getId(), QUANTITY, DEAL_PRICE); + Resource resource = ResourceTestHelper.getPearl(); + product = getTestProduct(user, resource); resourceInOrganization = ResourceInOrganizationTestHelper.createResourceInOrganization(organization, resource); organization.setResourceInOrganization(List.of(resourceInOrganization)); - productRequestDto = ProductTestHelper.getBaseProductRequestDtoForOrganization(user); productRequestDto = - setOwnerAndResourceToProductRequest( - productRequestDto, organization.getId(), user.getId(), BIG_QUANTITY); - product = getTestProduct(user, resource); + ProductTestHelper.getProductRequestDtoForOrganization( + user, organization.getId(), user.getId(), BIG_QUANTITY); organizationWithProduct = - setProductAndRsourcesToOrganization(organization, product, resourceInOrganization); - productResponseDto = productToResponse(product); - organizationResponseDto = getTestOrganizationResponseDto(organization); + setProductAndResourcesToOrganization(organization, product, resourceInOrganization); productsInOrganizationResponseDto = - new ProductsInOrganizationResponseDto(organizationResponseDto, List.of(productResponseDto)); + new ProductsInOrganizationResponseDto( + getTestOrganizationResponseDto(organization), List.of(productToResponse(product))); } @Test @@ -103,7 +87,6 @@ void getAllProductsInOrganizationSuccessfully() { @Test void createProductInOrganizationSuccessfully() { when(organizationService.getOrganization(organization.getId())).thenReturn(organization); - resourceInOrganizationService.addResourceToOrganization(resourceInOrganizationRequestDto); when(resourceInOrganizationService.findResourceInOrganizationOrThrow( organization, productRequestDto.getResourcesContent().get(0).getResourceId())) @@ -112,19 +95,19 @@ void createProductInOrganizationSuccessfully() { when(mapper.mapToProductResponseDto(organization, new ArrayList<>())) .thenReturn(productsInOrganizationResponseDto); - ProductsInOrganizationResponseDto products = + ProductsInOrganizationResponseDto productsInOrganizationResponse = productInOrganizationService.createProductInOrganization(productRequestDto); - assertNotNull(products); - assertEquals(1, products.getProducts().size()); - assertEquals(products.getOrganization().getId(), organization.getId()); + assertNotNull(productsInOrganizationResponse); + assertEquals(1, productsInOrganizationResponse.getProducts().size()); + assertEquals(productsInOrganizationResponse.getOrganization().getId(), organization.getId()); assertEquals( - products.getProducts().get(0).getId(), organization.getProductsOwned().get(0).getId()); + productsInOrganizationResponse.getProducts().get(0).getId(), + organization.getProductsOwned().get(0).getId()); } @Test void deleteProductInOrganizationSuccessfully() { when(organizationService.getOrganization(organization.getId())).thenReturn(organization); - resourceInOrganizationService.addResourceToOrganization(resourceInOrganizationRequestDto); when(productService.getProduct(product.getId())).thenReturn(product); @@ -132,19 +115,6 @@ void deleteProductInOrganizationSuccessfully() { verify(productService, times(1)).deleteProductById(product.getId()); } - private ProductRequestDto setOwnerAndResourceToProductRequest( - ProductRequestDto productRequestDto, - UUID organizationId, - UUID resourceId, - BigDecimal quantity) { - productRequestDto.setOwnerId(organizationId); - ResourceQuantityRequestDto resourceQuantityRequestDto = new ResourceQuantityRequestDto(); - resourceQuantityRequestDto.setResourceId(resourceId); - resourceQuantityRequestDto.setQuantity(quantity); - productRequestDto.setResourcesContent(List.of(resourceQuantityRequestDto)); - return productRequestDto; - } - private ProductResponseDto productToResponse(Product product) { ProductResponseDto productResponseDto = new ProductResponseDto(); productResponseDto.setId(product.getId()); From 9d1a46e7366593c132298ed37b3ef619e0fd9c09 Mon Sep 17 00:00:00 2001 From: berki0 <107307903+berki0@users.noreply.github.com> Date: Wed, 27 Mar 2024 21:13:56 +0200 Subject: [PATCH 13/33] [feature-72-BE-products-in-organization] add tests --- .../service/ProductInOrganizationService.java | 7 +++++ ...ductInOrganizationCrudIntegrationTest.java | 22 +++++---------- .../ProductInOrganizationServiceTest.java | 27 ++++++++++++------- 3 files changed, 30 insertions(+), 26 deletions(-) diff --git a/src/main/java/jewellery/inventory/service/ProductInOrganizationService.java b/src/main/java/jewellery/inventory/service/ProductInOrganizationService.java index 46d4a7b1..bf7d3190 100644 --- a/src/main/java/jewellery/inventory/service/ProductInOrganizationService.java +++ b/src/main/java/jewellery/inventory/service/ProductInOrganizationService.java @@ -95,6 +95,7 @@ public void deleteProductInOrganization(UUID organizationId, UUID productId) { Product forDeleteProduct = productService.getProduct(productId); + throwExceptionIfOrganizationNotOwner(organizationId, forDeleteProduct); productService.throwExceptionIfProductIsSold(forDeleteProduct); productService.throwExceptionIfProductIsPartOfAnotherProduct(productId, forDeleteProduct); @@ -240,6 +241,12 @@ private void checkResourceAvailability( } } + private void throwExceptionIfOrganizationNotOwner(UUID organizationId, Product product) { + if (organizationId==product.getOrganization().getId()) { + throw new OrganizationNotOwnerException(organizationId, product.getId()); + } + } + private ResourceInOrganizationRequestDto getResourceInOrganizationRequest( Organization owner, ResourceInProduct resourceInProduct) { diff --git a/src/test/java/jewellery/inventory/integration/ProductInOrganizationCrudIntegrationTest.java b/src/test/java/jewellery/inventory/integration/ProductInOrganizationCrudIntegrationTest.java index ab9aae50..a0cf248e 100644 --- a/src/test/java/jewellery/inventory/integration/ProductInOrganizationCrudIntegrationTest.java +++ b/src/test/java/jewellery/inventory/integration/ProductInOrganizationCrudIntegrationTest.java @@ -47,6 +47,10 @@ private String getOrganizationProductsUrl(String organizationId) { return buildUrl("organizations", organizationId, "products"); } + private String getOrganizationProductsWithIdUrl(String organizationId, String productId) { + return buildUrl("organizations", organizationId, "products", productId); + } + private PreciousStone preciousStone; private ProductRequestDto productRequestDto; private OrganizationResponseDto organization; @@ -116,24 +120,12 @@ void createProductInOrganizationSuccessfully() { // OrganizationResponseDto organizationResponseDto = createOrganization(); // ResourceResponseDto resourceResponse = createResourceResponse(); // -// ResourceInOrganizationRequestDto resourceInOrganizationRequest = +// sendResourceToOrganization( // ResourceInOrganizationTestHelper.createResourceInOrganizationRequestDto( // organizationResponseDto.getId(), // resourceResponse.getId(), // RESOURCE_QUANTITY, -// RESOURCE_PRICE); -// -// ResponseEntity resource = -// sendResourceToOrganization(resourceInOrganizationRequest); -// -// ResponseEntity getAllProductsInOrgResponse = -// this.testRestTemplate.exchange( -// getOrganizationProductsUrl(organizationResponseDto.getId().toString()), -// HttpMethod.GET, -// null, -// ProductsInOrganizationResponseDto.class); -// -// assertEquals(getAllProductsInOrgResponse.getBody().getProducts().size(), 0); +// RESOURCE_PRICE)); // // ResponseEntity productInOrganizationResponse = // createProduct( @@ -143,8 +135,6 @@ void createProductInOrganizationSuccessfully() { // resourceResponse.getId(), // RESOURCE_QUANTITY)); // -// assertEquals(productInOrganizationResponse.getBody().getProducts().size(), 1); -// // ResponseEntity response = // this.testRestTemplate.exchange( // getOrganizationProductsWithIdUrl( diff --git a/src/test/java/jewellery/inventory/unit/service/ProductInOrganizationServiceTest.java b/src/test/java/jewellery/inventory/unit/service/ProductInOrganizationServiceTest.java index 571da634..d34a7353 100644 --- a/src/test/java/jewellery/inventory/unit/service/ProductInOrganizationServiceTest.java +++ b/src/test/java/jewellery/inventory/unit/service/ProductInOrganizationServiceTest.java @@ -9,24 +9,22 @@ import java.util.ArrayList; import java.util.List; import jewellery.inventory.dto.request.ProductRequestDto; -import jewellery.inventory.dto.request.ResourceInOrganizationRequestDto; -import jewellery.inventory.dto.response.OrganizationResponseDto; import jewellery.inventory.dto.response.ProductResponseDto; import jewellery.inventory.dto.response.ProductsInOrganizationResponseDto; +import jewellery.inventory.exception.organization.OrganizationNotOwnerException; import jewellery.inventory.helper.*; import jewellery.inventory.mapper.ProductInOrganizationMapper; -import jewellery.inventory.mapper.ProductMapper; import jewellery.inventory.model.Organization; import jewellery.inventory.model.Product; import jewellery.inventory.model.ResourceInOrganization; import jewellery.inventory.model.User; import jewellery.inventory.model.resource.Resource; -import jewellery.inventory.repository.ProductRepository; import jewellery.inventory.repository.ResourceInProductRepository; import jewellery.inventory.service.OrganizationService; import jewellery.inventory.service.ProductInOrganizationService; import jewellery.inventory.service.ProductService; import jewellery.inventory.service.ResourceInOrganizationService; +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @@ -65,10 +63,10 @@ void setUp() { ProductTestHelper.getProductRequestDtoForOrganization( user, organization.getId(), user.getId(), BIG_QUANTITY); organizationWithProduct = - setProductAndResourcesToOrganization(organization, product, resourceInOrganization); + setProductAndResourcesToOrganization(OrganizationTestHelper.getTestOrganization(), product, resourceInOrganization); productsInOrganizationResponseDto = new ProductsInOrganizationResponseDto( - getTestOrganizationResponseDto(organization), List.of(productToResponse(product))); + getTestOrganizationResponseDto(OrganizationTestHelper.getTestOrganization()), List.of(productToResponse(product))); } @Test @@ -99,10 +97,6 @@ void createProductInOrganizationSuccessfully() { productInOrganizationService.createProductInOrganization(productRequestDto); assertNotNull(productsInOrganizationResponse); assertEquals(1, productsInOrganizationResponse.getProducts().size()); - assertEquals(productsInOrganizationResponse.getOrganization().getId(), organization.getId()); - assertEquals( - productsInOrganizationResponse.getProducts().get(0).getId(), - organization.getProductsOwned().get(0).getId()); } @Test @@ -113,6 +107,19 @@ void deleteProductInOrganizationSuccessfully() { productInOrganizationService.deleteProductInOrganization(organization.getId(), product.getId()); verify(productService, times(1)).deleteProductById(product.getId()); + + } + @Test + void deleteProductInOrganizationThrowOrganizationNotOwnerException() { + when(organizationService.getOrganization(organizationWithProduct.getId())).thenReturn(organizationWithProduct); + + when(productService.getProduct(product.getId())).thenReturn(product); + + Assertions.assertThrows( + OrganizationNotOwnerException.class, + () -> + productInOrganizationService.deleteProductInOrganization( + organizationWithProduct.getId(), product.getId())); } private ProductResponseDto productToResponse(Product product) { From 5842c842f1836b4713becb8b52b994929c5d8ca5 Mon Sep 17 00:00:00 2001 From: berki0 <107307903+berki0@users.noreply.github.com> Date: Wed, 27 Mar 2024 21:53:15 +0200 Subject: [PATCH 14/33] [feature-72-BE-products-in-organization] add tests --- .../mapper/ProductInOrganizationMapper.java | 2 +- .../service/ProductInOrganizationService.java | 9 ++-- .../inventory/helper/ProductTestHelper.java | 1 + .../ProductInOrganizationServiceTest.java | 45 ++++++++++++++----- 4 files changed, 42 insertions(+), 15 deletions(-) diff --git a/src/main/java/jewellery/inventory/mapper/ProductInOrganizationMapper.java b/src/main/java/jewellery/inventory/mapper/ProductInOrganizationMapper.java index ad6e81c8..de8e502f 100644 --- a/src/main/java/jewellery/inventory/mapper/ProductInOrganizationMapper.java +++ b/src/main/java/jewellery/inventory/mapper/ProductInOrganizationMapper.java @@ -13,7 +13,7 @@ public class ProductInOrganizationMapper { private final OrganizationMapper organizationMapper; - public ProductsInOrganizationResponseDto mapToProductResponseDto( + public ProductsInOrganizationResponseDto mapToProductsInOrganizationResponseDto( Organization organization, List products) { ProductsInOrganizationResponseDto productsInOrganizationResponseDto = new ProductsInOrganizationResponseDto(); diff --git a/src/main/java/jewellery/inventory/service/ProductInOrganizationService.java b/src/main/java/jewellery/inventory/service/ProductInOrganizationService.java index bf7d3190..3c5d186a 100644 --- a/src/main/java/jewellery/inventory/service/ProductInOrganizationService.java +++ b/src/main/java/jewellery/inventory/service/ProductInOrganizationService.java @@ -57,7 +57,8 @@ public ProductsInOrganizationResponseDto updateProduct( addProductsContentToProduct(productRequestDto, product); addResourcesToProduct(productRequestDto, organization, product); - return mapper.mapToProductResponseDto( + + return mapper.mapToProductsInOrganizationResponseDto( organization, List.of(productMapper.mapToProductResponseDto(product))); } @@ -65,7 +66,7 @@ public ProductsInOrganizationResponseDto getProductsInOrganization(UUID organiza Organization organization = organizationService.getOrganization(organizationId); organizationService.validateUserInOrganization(organization); - return mapper.mapToProductResponseDto( + return mapper.mapToProductsInOrganizationResponseDto( organization, productService.getProductsResponse(organization.getProductsOwned())); } @@ -82,7 +83,7 @@ public ProductsInOrganizationResponseDto createProductInOrganization( addProductsContentToProduct(productRequestDto, product); addResourcesToProduct(productRequestDto, organization, product); - return mapper.mapToProductResponseDto( + return mapper.mapToProductsInOrganizationResponseDto( organization, productService.getProductsResponse(List.of(product))); } @@ -274,7 +275,7 @@ public Object fetchEntity(Object... ids) { if (product == null) { return null; } - return mapper.mapToProductResponseDto( + return mapper.mapToProductsInOrganizationResponseDto( product.getOrganization(), List.of(productMapper.mapToProductResponseDto(product))); } } diff --git a/src/test/java/jewellery/inventory/helper/ProductTestHelper.java b/src/test/java/jewellery/inventory/helper/ProductTestHelper.java index 211c98cd..5a24c7a8 100644 --- a/src/test/java/jewellery/inventory/helper/ProductTestHelper.java +++ b/src/test/java/jewellery/inventory/helper/ProductTestHelper.java @@ -93,6 +93,7 @@ public static Product getTestProduct(User user, Resource pearl) { testProduct.setResourcesContent(List.of(getResourceInProduct(pearl))); testProduct.setProductsContent(new ArrayList<>()); testProduct.setContentOf(null); + testProduct.setResourcesContent(new ArrayList<>()); testProduct.setAdditionalPrice(BigDecimal.TEN); return testProduct; } diff --git a/src/test/java/jewellery/inventory/unit/service/ProductInOrganizationServiceTest.java b/src/test/java/jewellery/inventory/unit/service/ProductInOrganizationServiceTest.java index d34a7353..a88e169c 100644 --- a/src/test/java/jewellery/inventory/unit/service/ProductInOrganizationServiceTest.java +++ b/src/test/java/jewellery/inventory/unit/service/ProductInOrganizationServiceTest.java @@ -1,6 +1,7 @@ package jewellery.inventory.unit.service; import static jewellery.inventory.helper.OrganizationTestHelper.*; +import static jewellery.inventory.helper.ProductTestHelper.getProductRequestDtoForOrganization; import static jewellery.inventory.helper.ProductTestHelper.getTestProduct; import static org.junit.jupiter.api.Assertions.*; import static org.mockito.Mockito.*; @@ -14,11 +15,13 @@ import jewellery.inventory.exception.organization.OrganizationNotOwnerException; import jewellery.inventory.helper.*; import jewellery.inventory.mapper.ProductInOrganizationMapper; +import jewellery.inventory.mapper.ProductMapper; import jewellery.inventory.model.Organization; import jewellery.inventory.model.Product; import jewellery.inventory.model.ResourceInOrganization; import jewellery.inventory.model.User; import jewellery.inventory.model.resource.Resource; +import jewellery.inventory.repository.ProductRepository; import jewellery.inventory.repository.ResourceInProductRepository; import jewellery.inventory.service.OrganizationService; import jewellery.inventory.service.ProductInOrganizationService; @@ -37,6 +40,8 @@ class ProductInOrganizationServiceTest { @InjectMocks private ProductInOrganizationService productInOrganizationService; @Mock private OrganizationService organizationService; @Mock private ProductService productService; + @Mock private ProductRepository productRepository; + @Mock private ProductMapper productMapper; @Mock private ProductInOrganizationMapper mapper; @Mock private ResourceInOrganizationService resourceInOrganizationService; @Mock private ResourceInProductRepository resourceInProductRepository; @@ -60,20 +65,21 @@ void setUp() { ResourceInOrganizationTestHelper.createResourceInOrganization(organization, resource); organization.setResourceInOrganization(List.of(resourceInOrganization)); productRequestDto = - ProductTestHelper.getProductRequestDtoForOrganization( - user, organization.getId(), user.getId(), BIG_QUANTITY); + getProductRequestDtoForOrganization(user, organization.getId(), user.getId(), BIG_QUANTITY); organizationWithProduct = - setProductAndResourcesToOrganization(OrganizationTestHelper.getTestOrganization(), product, resourceInOrganization); + setProductAndResourcesToOrganization( + OrganizationTestHelper.getTestOrganization(), product, resourceInOrganization); productsInOrganizationResponseDto = new ProductsInOrganizationResponseDto( - getTestOrganizationResponseDto(OrganizationTestHelper.getTestOrganization()), List.of(productToResponse(product))); + getTestOrganizationResponseDto(OrganizationTestHelper.getTestOrganization()), + List.of(productToResponse(product))); } @Test void getAllProductsInOrganizationSuccessfully() { when(organizationService.getOrganization(organizationWithProduct.getId())) .thenReturn(organizationWithProduct); - when(mapper.mapToProductResponseDto(organizationWithProduct, new ArrayList<>())) + when(mapper.mapToProductsInOrganizationResponseDto(organizationWithProduct, new ArrayList<>())) .thenReturn(productsInOrganizationResponseDto); ProductsInOrganizationResponseDto products = @@ -90,7 +96,7 @@ void createProductInOrganizationSuccessfully() { organization, productRequestDto.getResourcesContent().get(0).getResourceId())) .thenReturn(resourceInOrganization); - when(mapper.mapToProductResponseDto(organization, new ArrayList<>())) + when(mapper.mapToProductsInOrganizationResponseDto(organization, new ArrayList<>())) .thenReturn(productsInOrganizationResponseDto); ProductsInOrganizationResponseDto productsInOrganizationResponse = @@ -99,6 +105,24 @@ void createProductInOrganizationSuccessfully() { assertEquals(1, productsInOrganizationResponse.getProducts().size()); } + @Test + void updateProductInOrganizationSuccessfully() { + when(organizationService.getOrganization(organization.getId())).thenReturn(organization); + when(productService.getProduct(product.getId())).thenReturn(product); + + when(resourceInOrganizationService.findResourceInOrganizationOrThrow( + organization, productRequestDto.getResourcesContent().get(0).getResourceId())) + .thenReturn(resourceInOrganization); + + when(mapper.mapToProductsInOrganizationResponseDto(eq(organization), anyList())) + .thenReturn(productsInOrganizationResponseDto); + when(productMapper.mapToProductResponseDto(product)).thenReturn(productToResponse(product)); + + ProductsInOrganizationResponseDto productsInOrganizationResponse = + productInOrganizationService.updateProduct(product.getId(), productRequestDto); + assertNotNull(productsInOrganizationResponse); + } + @Test void deleteProductInOrganizationSuccessfully() { when(organizationService.getOrganization(organization.getId())).thenReturn(organization); @@ -107,19 +131,20 @@ void deleteProductInOrganizationSuccessfully() { productInOrganizationService.deleteProductInOrganization(organization.getId(), product.getId()); verify(productService, times(1)).deleteProductById(product.getId()); - } + @Test void deleteProductInOrganizationThrowOrganizationNotOwnerException() { - when(organizationService.getOrganization(organizationWithProduct.getId())).thenReturn(organizationWithProduct); + when(organizationService.getOrganization(organizationWithProduct.getId())) + .thenReturn(organizationWithProduct); when(productService.getProduct(product.getId())).thenReturn(product); - Assertions.assertThrows( + Assertions.assertThrows( OrganizationNotOwnerException.class, () -> productInOrganizationService.deleteProductInOrganization( - organizationWithProduct.getId(), product.getId())); + organizationWithProduct.getId(), product.getId())); } private ProductResponseDto productToResponse(Product product) { From 34484b6d0a8cdcf6bf0233a2f2072f964febbd51 Mon Sep 17 00:00:00 2001 From: berki0 <107307903+berki0@users.noreply.github.com> Date: Wed, 27 Mar 2024 22:32:55 +0200 Subject: [PATCH 15/33] [feature-72-BE-products-in-organization] add tests --- ...ductInOrganizationCrudIntegrationTest.java | 115 +++++++++++++----- 1 file changed, 83 insertions(+), 32 deletions(-) diff --git a/src/test/java/jewellery/inventory/integration/ProductInOrganizationCrudIntegrationTest.java b/src/test/java/jewellery/inventory/integration/ProductInOrganizationCrudIntegrationTest.java index a0cf248e..dc54fcc9 100644 --- a/src/test/java/jewellery/inventory/integration/ProductInOrganizationCrudIntegrationTest.java +++ b/src/test/java/jewellery/inventory/integration/ProductInOrganizationCrudIntegrationTest.java @@ -19,6 +19,7 @@ import org.jetbrains.annotations.Nullable; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.springframework.http.HttpEntity; import org.springframework.http.HttpMethod; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; @@ -61,7 +62,9 @@ void setUp() { user = createUserInDatabase(UserTestHelper.createTestUserRequest()); organization = createOrganization(); preciousStone = createPreciousStoneInDatabase(); - productRequestDto = ProductTestHelper.getProductRequestDtoForOrganization(user,organization.getId(),preciousStone.getId(),RESOURCE_QUANTITY); + productRequestDto = + ProductTestHelper.getProductRequestDtoForOrganization( + user, organization.getId(), preciousStone.getId(), RESOURCE_QUANTITY); } @Test @@ -115,37 +118,72 @@ void createProductInOrganizationSuccessfully() { assertEquals(HttpStatus.CREATED, productInOrganizationResponse.getStatusCode()); } -// @Test -// void deleteProductInOrganizationSuccessfully() { -// OrganizationResponseDto organizationResponseDto = createOrganization(); -// ResourceResponseDto resourceResponse = createResourceResponse(); -// -// sendResourceToOrganization( -// ResourceInOrganizationTestHelper.createResourceInOrganizationRequestDto( -// organizationResponseDto.getId(), -// resourceResponse.getId(), -// RESOURCE_QUANTITY, -// RESOURCE_PRICE)); -// -// ResponseEntity productInOrganizationResponse = -// createProduct( -// setOwnerAndResourceToProductRequest( -// productRequestDto, -// organizationResponseDto.getId(), -// resourceResponse.getId(), -// RESOURCE_QUANTITY)); -// -// ResponseEntity response = -// this.testRestTemplate.exchange( -// getOrganizationProductsWithIdUrl( -// organizationResponseDto.getId().toString(), -// productInOrganizationResponse.getBody().getProducts().get(0).getId().toString()), -// HttpMethod.DELETE, -// null, -// Void.class); -// -// assertEquals(HttpStatus.NO_CONTENT, response.getStatusCode()); -// } + @Test + void updateProductInOrganizationSuccessfully() { + OrganizationResponseDto organizationResponseDto = createOrganization(); + ResourceResponseDto resourceResponse = createResourceResponse(); + + ResourceInOrganizationRequestDto resourceInOrganizationRequest = + ResourceInOrganizationTestHelper.createResourceInOrganizationRequestDto( + organizationResponseDto.getId(), + resourceResponse.getId(), + RESOURCE_QUANTITY, + RESOURCE_PRICE); + + ResponseEntity resource = + sendResourceToOrganization(resourceInOrganizationRequest); + + ResponseEntity resource2 = + sendResourceToOrganization(resourceInOrganizationRequest); + + + ResponseEntity productInOrganizationResponse = + createProduct( + setOwnerAndResourceToProductRequest( + productRequestDto, + organizationResponseDto.getId(), + resourceResponse.getId(), + RESOURCE_QUANTITY)); + + ResponseEntity updatedProductInOrganizationResponse = + updateProduct( + productRequestDto, + productInOrganizationResponse.getBody().getProducts().get(0).getId().toString()); + assertEquals(HttpStatus.OK,updatedProductInOrganizationResponse.getStatusCode()); + } + + // @Test + // void deleteProductInOrganizationSuccessfully() { + // OrganizationResponseDto organizationResponseDto = createOrganization(); + // ResourceResponseDto resourceResponse = createResourceResponse(); + // + // sendResourceToOrganization( + // ResourceInOrganizationTestHelper.createResourceInOrganizationRequestDto( + // organizationResponseDto.getId(), + // resourceResponse.getId(), + // RESOURCE_QUANTITY, + // RESOURCE_PRICE)); + // + // ResponseEntity productInOrganizationResponse = + // createProduct( + // setOwnerAndResourceToProductRequest( + // productRequestDto, + // organizationResponseDto.getId(), + // resourceResponse.getId(), + // RESOURCE_QUANTITY)); + // + // ResponseEntity response = + // this.testRestTemplate.exchange( + // getOrganizationProductsWithIdUrl( + // organizationResponseDto.getId().toString(), + // + // productInOrganizationResponse.getBody().getProducts().get(0).getId().toString()), + // HttpMethod.DELETE, + // null, + // Void.class); + // + // assertEquals(HttpStatus.NO_CONTENT, response.getStatusCode()); + // } private OrganizationResponseDto createOrganization() { OrganizationRequestDto organizationRequestDto = @@ -190,6 +228,19 @@ private ResponseEntity createProduct( ProductsInOrganizationResponseDto.class); } + @Nullable + private ResponseEntity updateProduct( + ProductRequestDto productRequestDto, String productId) { + + HttpEntity requestEntity = new HttpEntity<>(productRequestDto, headers); + + return this.testRestTemplate.exchange( + getBaseOrganizationUrl() + "/products/" + productId, + HttpMethod.PUT, + requestEntity, + ProductsInOrganizationResponseDto.class); + } + @Nullable private PreciousStone createPreciousStoneInDatabase() { ResourceRequestDto resourceRequest = ResourceTestHelper.getPreciousStoneRequestDto(); From 89c3c5a29836f777cfd939ac43136a9e3eda957e Mon Sep 17 00:00:00 2001 From: berki0 <107307903+berki0@users.noreply.github.com> Date: Wed, 27 Mar 2024 22:42:46 +0200 Subject: [PATCH 16/33] [feature-72-BE-products-in-organization] add tests --- ...ductInOrganizationCrudIntegrationTest.java | 59 +++++++++---------- 1 file changed, 29 insertions(+), 30 deletions(-) diff --git a/src/test/java/jewellery/inventory/integration/ProductInOrganizationCrudIntegrationTest.java b/src/test/java/jewellery/inventory/integration/ProductInOrganizationCrudIntegrationTest.java index dc54fcc9..86c16eae 100644 --- a/src/test/java/jewellery/inventory/integration/ProductInOrganizationCrudIntegrationTest.java +++ b/src/test/java/jewellery/inventory/integration/ProductInOrganizationCrudIntegrationTest.java @@ -134,8 +134,7 @@ void updateProductInOrganizationSuccessfully() { sendResourceToOrganization(resourceInOrganizationRequest); ResponseEntity resource2 = - sendResourceToOrganization(resourceInOrganizationRequest); - + sendResourceToOrganization(resourceInOrganizationRequest); ResponseEntity productInOrganizationResponse = createProduct( @@ -149,41 +148,41 @@ void updateProductInOrganizationSuccessfully() { updateProduct( productRequestDto, productInOrganizationResponse.getBody().getProducts().get(0).getId().toString()); - assertEquals(HttpStatus.OK,updatedProductInOrganizationResponse.getStatusCode()); + assertEquals(HttpStatus.OK, updatedProductInOrganizationResponse.getStatusCode()); } - // @Test - // void deleteProductInOrganizationSuccessfully() { - // OrganizationResponseDto organizationResponseDto = createOrganization(); - // ResourceResponseDto resourceResponse = createResourceResponse(); + // @Test + // void deleteProductInOrganizationSuccessfully() { + // OrganizationResponseDto organizationResponseDto = createOrganization(); + // ResourceResponseDto resourceResponse = createResourceResponse(); // - // sendResourceToOrganization( - // ResourceInOrganizationTestHelper.createResourceInOrganizationRequestDto( - // organizationResponseDto.getId(), - // resourceResponse.getId(), - // RESOURCE_QUANTITY, - // RESOURCE_PRICE)); + // sendResourceToOrganization( + // ResourceInOrganizationTestHelper.createResourceInOrganizationRequestDto( + // organizationResponseDto.getId(), + // resourceResponse.getId(), + // RESOURCE_QUANTITY, + // RESOURCE_PRICE)); // - // ResponseEntity productInOrganizationResponse = - // createProduct( - // setOwnerAndResourceToProductRequest( - // productRequestDto, - // organizationResponseDto.getId(), - // resourceResponse.getId(), - // RESOURCE_QUANTITY)); + // ResponseEntity productInOrganizationResponse = + // createProduct( + // setOwnerAndResourceToProductRequest( + // productRequestDto, + // organizationResponseDto.getId(), + // resourceResponse.getId(), + // RESOURCE_QUANTITY)); // - // ResponseEntity response = - // this.testRestTemplate.exchange( - // getOrganizationProductsWithIdUrl( - // organizationResponseDto.getId().toString(), + // ResponseEntity response = + // this.testRestTemplate.exchange( + // getOrganizationProductsWithIdUrl( + // organizationResponseDto.getId().toString(), // - // productInOrganizationResponse.getBody().getProducts().get(0).getId().toString()), - // HttpMethod.DELETE, - // null, - // Void.class); + // productInOrganizationResponse.getBody().getProducts().get(0).getId().toString()), + // HttpMethod.DELETE, + // null, + // Void.class); // - // assertEquals(HttpStatus.NO_CONTENT, response.getStatusCode()); - // } + // assertEquals(HttpStatus.NO_CONTENT, response.getStatusCode()); + // } private OrganizationResponseDto createOrganization() { OrganizationRequestDto organizationRequestDto = From 851ccf096d1f9a20c0b280d19b3ad45092548db7 Mon Sep 17 00:00:00 2001 From: berki0 <107307903+berki0@users.noreply.github.com> Date: Thu, 28 Mar 2024 11:34:42 +0200 Subject: [PATCH 17/33] [feature-72-BE-products-in-organization] add tests --- .../service/ProductInOrganizationService.java | 2 +- ...ductInOrganizationCrudIntegrationTest.java | 64 +++++++++---------- .../ProductInOrganizationServiceTest.java | 30 ++++++++- 3 files changed, 62 insertions(+), 34 deletions(-) diff --git a/src/main/java/jewellery/inventory/service/ProductInOrganizationService.java b/src/main/java/jewellery/inventory/service/ProductInOrganizationService.java index 3c5d186a..fb7f2356 100644 --- a/src/main/java/jewellery/inventory/service/ProductInOrganizationService.java +++ b/src/main/java/jewellery/inventory/service/ProductInOrganizationService.java @@ -243,7 +243,7 @@ private void checkResourceAvailability( } private void throwExceptionIfOrganizationNotOwner(UUID organizationId, Product product) { - if (organizationId==product.getOrganization().getId()) { + if (organizationId!=product.getOrganization().getId()) { throw new OrganizationNotOwnerException(organizationId, product.getId()); } } diff --git a/src/test/java/jewellery/inventory/integration/ProductInOrganizationCrudIntegrationTest.java b/src/test/java/jewellery/inventory/integration/ProductInOrganizationCrudIntegrationTest.java index 86c16eae..7cbebfe0 100644 --- a/src/test/java/jewellery/inventory/integration/ProductInOrganizationCrudIntegrationTest.java +++ b/src/test/java/jewellery/inventory/integration/ProductInOrganizationCrudIntegrationTest.java @@ -151,38 +151,38 @@ void updateProductInOrganizationSuccessfully() { assertEquals(HttpStatus.OK, updatedProductInOrganizationResponse.getStatusCode()); } - // @Test - // void deleteProductInOrganizationSuccessfully() { - // OrganizationResponseDto organizationResponseDto = createOrganization(); - // ResourceResponseDto resourceResponse = createResourceResponse(); - // - // sendResourceToOrganization( - // ResourceInOrganizationTestHelper.createResourceInOrganizationRequestDto( - // organizationResponseDto.getId(), - // resourceResponse.getId(), - // RESOURCE_QUANTITY, - // RESOURCE_PRICE)); - // - // ResponseEntity productInOrganizationResponse = - // createProduct( - // setOwnerAndResourceToProductRequest( - // productRequestDto, - // organizationResponseDto.getId(), - // resourceResponse.getId(), - // RESOURCE_QUANTITY)); - // - // ResponseEntity response = - // this.testRestTemplate.exchange( - // getOrganizationProductsWithIdUrl( - // organizationResponseDto.getId().toString(), - // - // productInOrganizationResponse.getBody().getProducts().get(0).getId().toString()), - // HttpMethod.DELETE, - // null, - // Void.class); - // - // assertEquals(HttpStatus.NO_CONTENT, response.getStatusCode()); - // } +// @Test +// void deleteProductInOrganizationSuccessfully() { +// OrganizationResponseDto organizationResponseDto = createOrganization(); +// ResourceResponseDto resourceResponse = createResourceResponse(); +// +// sendResourceToOrganization( +// ResourceInOrganizationTestHelper.createResourceInOrganizationRequestDto( +// organizationResponseDto.getId(), +// resourceResponse.getId(), +// RESOURCE_QUANTITY, +// RESOURCE_PRICE)); +// +// ResponseEntity productInOrganizationResponse = +// createProduct( +// setOwnerAndResourceToProductRequest( +// productRequestDto, +// organizationResponseDto.getId(), +// resourceResponse.getId(), +// RESOURCE_QUANTITY)); +// +// ResponseEntity response = +// this.testRestTemplate.exchange( +// getOrganizationProductsWithIdUrl( +// organizationResponseDto.getId().toString(), +// productInOrganizationResponse.getBody().getProducts().get(0).getId().toString()), +// HttpMethod.DELETE, +// null, +// Void.class); +// +// assertEquals(HttpStatus.CREATED, productInOrganizationResponse.getStatusCode()); +// assertEquals(HttpStatus.NO_CONTENT, response.getStatusCode()); +// } private OrganizationResponseDto createOrganization() { OrganizationRequestDto organizationRequestDto = diff --git a/src/test/java/jewellery/inventory/unit/service/ProductInOrganizationServiceTest.java b/src/test/java/jewellery/inventory/unit/service/ProductInOrganizationServiceTest.java index a88e169c..efbccc38 100644 --- a/src/test/java/jewellery/inventory/unit/service/ProductInOrganizationServiceTest.java +++ b/src/test/java/jewellery/inventory/unit/service/ProductInOrganizationServiceTest.java @@ -9,6 +9,8 @@ import java.math.BigDecimal; import java.util.ArrayList; import java.util.List; +import java.util.UUID; + import jewellery.inventory.dto.request.ProductRequestDto; import jewellery.inventory.dto.response.ProductResponseDto; import jewellery.inventory.dto.response.ProductsInOrganizationResponseDto; @@ -51,6 +53,7 @@ class ProductInOrganizationServiceTest { private Organization organizationWithProduct; private ResourceInOrganization resourceInOrganization; private ProductRequestDto productRequestDto; + private ProductRequestDto productWithProductRequestDto; private Product product; private ProductsInOrganizationResponseDto productsInOrganizationResponseDto; @@ -66,6 +69,9 @@ void setUp() { organization.setResourceInOrganization(List.of(resourceInOrganization)); productRequestDto = getProductRequestDtoForOrganization(user, organization.getId(), user.getId(), BIG_QUANTITY); + productWithProductRequestDto = + getProductRequestDtoForOrganization(user, organization.getId(), user.getId(), BIG_QUANTITY); + productWithProductRequestDto.setProductsContent(List.of(product.getId())); organizationWithProduct = setProductAndResourcesToOrganization( OrganizationTestHelper.getTestOrganization(), product, resourceInOrganization); @@ -105,6 +111,27 @@ void createProductInOrganizationSuccessfully() { assertEquals(1, productsInOrganizationResponse.getProducts().size()); } + @Test + void createProductWithProductInOrganizationSuccessfully() { + when(organizationService.getOrganization(organization.getId())).thenReturn(organization); + + when(resourceInOrganizationService.findResourceInOrganizationOrThrow( + organization, + productWithProductRequestDto.getResourcesContent().get(0).getResourceId())) + .thenReturn(resourceInOrganization); + + product.setOrganization(organization); + + when(mapper.mapToProductsInOrganizationResponseDto(organization, new ArrayList<>())) + .thenReturn(productsInOrganizationResponseDto); + when(productService.getProduct(any(UUID.class))).thenReturn(product); + + ProductsInOrganizationResponseDto productsInOrganizationResponse = + productInOrganizationService.createProductInOrganization(productWithProductRequestDto); + assertNotNull(productsInOrganizationResponse); + assertEquals(1, productsInOrganizationResponse.getProducts().size()); + } + @Test void updateProductInOrganizationSuccessfully() { when(organizationService.getOrganization(organization.getId())).thenReturn(organization); @@ -126,7 +153,7 @@ void updateProductInOrganizationSuccessfully() { @Test void deleteProductInOrganizationSuccessfully() { when(organizationService.getOrganization(organization.getId())).thenReturn(organization); - + product.setOrganization(organization); when(productService.getProduct(product.getId())).thenReturn(product); productInOrganizationService.deleteProductInOrganization(organization.getId(), product.getId()); @@ -137,6 +164,7 @@ void deleteProductInOrganizationSuccessfully() { void deleteProductInOrganizationThrowOrganizationNotOwnerException() { when(organizationService.getOrganization(organizationWithProduct.getId())) .thenReturn(organizationWithProduct); + product.setOrganization(new Organization()); when(productService.getProduct(product.getId())).thenReturn(product); From 2577835f2d995af627dfdf8f1ae4becf7e7e252c Mon Sep 17 00:00:00 2001 From: berki0 <107307903+berki0@users.noreply.github.com> Date: Thu, 28 Mar 2024 13:32:04 +0200 Subject: [PATCH 18/33] [feature-72-BE-products-in-organization] add tests --- .../ProductInOrganizationController.java | 2 +- .../service/ProductInOrganizationService.java | 4 +- ...ductInOrganizationCrudIntegrationTest.java | 87 ++++++++++++------- .../ProductInOrganizationServiceTest.java | 4 +- 4 files changed, 59 insertions(+), 38 deletions(-) diff --git a/src/main/java/jewellery/inventory/controller/ProductInOrganizationController.java b/src/main/java/jewellery/inventory/controller/ProductInOrganizationController.java index 7e86e160..240de74b 100644 --- a/src/main/java/jewellery/inventory/controller/ProductInOrganizationController.java +++ b/src/main/java/jewellery/inventory/controller/ProductInOrganizationController.java @@ -36,7 +36,7 @@ public ProductsInOrganizationResponseDto createProductInOrganization( @ResponseStatus(HttpStatus.NO_CONTENT) @DeleteMapping("/{organizationId}/products/{productId}") public void deleteProductInOrganization( - @PathVariable("organizationId") UUID organizationId, @PathVariable("productId") UUID productId) { + @PathVariable("productId") UUID productId, @PathVariable("organizationId") UUID organizationId) { productInOrganizationService.deleteProductInOrganization(organizationId, productId); } diff --git a/src/main/java/jewellery/inventory/service/ProductInOrganizationService.java b/src/main/java/jewellery/inventory/service/ProductInOrganizationService.java index fb7f2356..5cf02583 100644 --- a/src/main/java/jewellery/inventory/service/ProductInOrganizationService.java +++ b/src/main/java/jewellery/inventory/service/ProductInOrganizationService.java @@ -89,7 +89,7 @@ public ProductsInOrganizationResponseDto createProductInOrganization( @Transactional @LogDeleteEvent(eventType = EventType.ORGANIZATION_PRODUCT_DISASSEMBLY) - public void deleteProductInOrganization(UUID organizationId, UUID productId) { + public void deleteProductInOrganization(UUID productId,UUID organizationId) { organizationService.validateCurrentUserPermission( organizationService.getOrganization(organizationId), OrganizationPermission.DISASSEMBLE_PRODUCT); @@ -243,7 +243,7 @@ private void checkResourceAvailability( } private void throwExceptionIfOrganizationNotOwner(UUID organizationId, Product product) { - if (organizationId!=product.getOrganization().getId()) { + if (!organizationId.equals(product.getOrganization().getId())) { throw new OrganizationNotOwnerException(organizationId, product.getId()); } } diff --git a/src/test/java/jewellery/inventory/integration/ProductInOrganizationCrudIntegrationTest.java b/src/test/java/jewellery/inventory/integration/ProductInOrganizationCrudIntegrationTest.java index 7cbebfe0..9ab65622 100644 --- a/src/test/java/jewellery/inventory/integration/ProductInOrganizationCrudIntegrationTest.java +++ b/src/test/java/jewellery/inventory/integration/ProductInOrganizationCrudIntegrationTest.java @@ -1,10 +1,12 @@ package jewellery.inventory.integration; import static jewellery.inventory.helper.ResourceTestHelper.getPearlRequestDto; +import static jewellery.inventory.model.EventType.*; import static jewellery.inventory.utils.BigDecimalUtil.getBigDecimal; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; +import com.fasterxml.jackson.core.JsonProcessingException; import java.math.BigDecimal; import java.util.List; import java.util.UUID; @@ -82,7 +84,7 @@ void getAllProductsFromOrganizationSuccessfully() { } @Test - void createProductInOrganizationSuccessfully() { + void createProductInOrganizationSuccessfully() throws JsonProcessingException { OrganizationResponseDto organizationResponseDto = createOrganization(); ResourceResponseDto resourceResponse = createResourceResponse(); @@ -116,6 +118,14 @@ void createProductInOrganizationSuccessfully() { assertNotNull(productInOrganizationResponse.getBody()); assertEquals(1, productInOrganizationResponse.getBody().getProducts().size()); assertEquals(HttpStatus.CREATED, productInOrganizationResponse.getStatusCode()); + + // Map expectedEventPayload = + // + // getCreateOrDeleteEventPayload(productInOrganizationResponse.getBody().getProducts().get(0), + // objectMapper); + // + // systemEventTestHelper.assertEventWasLogged(ORGANIZATION_PRODUCT_CREATE, + // expectedEventPayload); } @Test @@ -149,40 +159,51 @@ void updateProductInOrganizationSuccessfully() { productRequestDto, productInOrganizationResponse.getBody().getProducts().get(0).getId().toString()); assertEquals(HttpStatus.OK, updatedProductInOrganizationResponse.getStatusCode()); + + // Map expectedEventPayload = + // getUpdateEventPayload( + // productInOrganizationResponse.getBody().getProducts().get(0), + // + // Objects.requireNonNull(updatedProductInOrganizationResponse.getBody()).getProducts().get(0), + // objectMapper); + // + // systemEventTestHelper.assertEventWasLogged(ORGANIZATION_PRODUCT_UPDATE, + // expectedEventPayload); } -// @Test -// void deleteProductInOrganizationSuccessfully() { -// OrganizationResponseDto organizationResponseDto = createOrganization(); -// ResourceResponseDto resourceResponse = createResourceResponse(); -// -// sendResourceToOrganization( -// ResourceInOrganizationTestHelper.createResourceInOrganizationRequestDto( -// organizationResponseDto.getId(), -// resourceResponse.getId(), -// RESOURCE_QUANTITY, -// RESOURCE_PRICE)); -// -// ResponseEntity productInOrganizationResponse = -// createProduct( -// setOwnerAndResourceToProductRequest( -// productRequestDto, -// organizationResponseDto.getId(), -// resourceResponse.getId(), -// RESOURCE_QUANTITY)); -// -// ResponseEntity response = -// this.testRestTemplate.exchange( -// getOrganizationProductsWithIdUrl( -// organizationResponseDto.getId().toString(), -// productInOrganizationResponse.getBody().getProducts().get(0).getId().toString()), -// HttpMethod.DELETE, -// null, -// Void.class); -// -// assertEquals(HttpStatus.CREATED, productInOrganizationResponse.getStatusCode()); -// assertEquals(HttpStatus.NO_CONTENT, response.getStatusCode()); -// } + // @Test + // void deleteProductInOrganizationSuccessfully() { + // OrganizationResponseDto organizationResponseDto = createOrganization(); + // ResourceResponseDto resourceResponse = createResourceResponse(); + // + // sendResourceToOrganization( + // ResourceInOrganizationTestHelper.createResourceInOrganizationRequestDto( + // organizationResponseDto.getId(), + // resourceResponse.getId(), + // RESOURCE_QUANTITY, + // RESOURCE_PRICE)); + // + // ResponseEntity productInOrganizationResponse = + // createProduct( + // setOwnerAndResourceToProductRequest( + // productRequestDto, + // organizationResponseDto.getId(), + // resourceResponse.getId(), + // RESOURCE_QUANTITY)); + // + // ResponseEntity response = + // this.testRestTemplate.exchange( + // getOrganizationProductsWithIdUrl( + // organizationResponseDto.getId().toString(), + // + // productInOrganizationResponse.getBody().getProducts().get(0).getId().toString()), + // HttpMethod.DELETE, + // null, + // Void.class); + // + // assertEquals(HttpStatus.CREATED, productInOrganizationResponse.getStatusCode()); + // assertEquals(HttpStatus.NO_CONTENT, response.getStatusCode()); + // } private OrganizationResponseDto createOrganization() { OrganizationRequestDto organizationRequestDto = diff --git a/src/test/java/jewellery/inventory/unit/service/ProductInOrganizationServiceTest.java b/src/test/java/jewellery/inventory/unit/service/ProductInOrganizationServiceTest.java index efbccc38..fb4f5af8 100644 --- a/src/test/java/jewellery/inventory/unit/service/ProductInOrganizationServiceTest.java +++ b/src/test/java/jewellery/inventory/unit/service/ProductInOrganizationServiceTest.java @@ -156,7 +156,7 @@ void deleteProductInOrganizationSuccessfully() { product.setOrganization(organization); when(productService.getProduct(product.getId())).thenReturn(product); - productInOrganizationService.deleteProductInOrganization(organization.getId(), product.getId()); + productInOrganizationService.deleteProductInOrganization(product.getId(), organization.getId()); verify(productService, times(1)).deleteProductById(product.getId()); } @@ -172,7 +172,7 @@ void deleteProductInOrganizationThrowOrganizationNotOwnerException() { OrganizationNotOwnerException.class, () -> productInOrganizationService.deleteProductInOrganization( - organizationWithProduct.getId(), product.getId())); + product.getId(), organizationWithProduct.getId())); } private ProductResponseDto productToResponse(Product product) { From 18f7185c9787398abd4f14ff1ab8dc6080993ba5 Mon Sep 17 00:00:00 2001 From: berki0 <107307903+berki0@users.noreply.github.com> Date: Thu, 28 Mar 2024 16:28:44 +0200 Subject: [PATCH 19/33] [feature-72-BE-products-in-organization] add tests --- .../inventory/service/ProductInOrganizationService.java | 1 + .../unit/service/ProductInOrganizationServiceTest.java | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main/java/jewellery/inventory/service/ProductInOrganizationService.java b/src/main/java/jewellery/inventory/service/ProductInOrganizationService.java index 5cf02583..b7f3f4d5 100644 --- a/src/main/java/jewellery/inventory/service/ProductInOrganizationService.java +++ b/src/main/java/jewellery/inventory/service/ProductInOrganizationService.java @@ -48,6 +48,7 @@ public ProductsInOrganizationResponseDto updateProduct( organization, OrganizationPermission.EDIT_PRODUCT); Product product = productService.getProduct(productId); + throwExceptionIfOrganizationNotOwner(organization.getId(),product); productService.throwExceptionIfProductIsSold(product); moveQuantityFromResourcesInProductToResourcesInOrganization(product); productService.disassembleProductContent(product); diff --git a/src/test/java/jewellery/inventory/unit/service/ProductInOrganizationServiceTest.java b/src/test/java/jewellery/inventory/unit/service/ProductInOrganizationServiceTest.java index fb4f5af8..36501090 100644 --- a/src/test/java/jewellery/inventory/unit/service/ProductInOrganizationServiceTest.java +++ b/src/test/java/jewellery/inventory/unit/service/ProductInOrganizationServiceTest.java @@ -135,6 +135,7 @@ void createProductWithProductInOrganizationSuccessfully() { @Test void updateProductInOrganizationSuccessfully() { when(organizationService.getOrganization(organization.getId())).thenReturn(organization); + product.setOrganization(organization); when(productService.getProduct(product.getId())).thenReturn(product); when(resourceInOrganizationService.findResourceInOrganizationOrThrow( @@ -172,7 +173,7 @@ void deleteProductInOrganizationThrowOrganizationNotOwnerException() { OrganizationNotOwnerException.class, () -> productInOrganizationService.deleteProductInOrganization( - product.getId(), organizationWithProduct.getId())); + product.getId(), organizationWithProduct.getId())); } private ProductResponseDto productToResponse(Product product) { From 60259747d30f333c7a6b2dfa9dbe98506a8142c7 Mon Sep 17 00:00:00 2001 From: berki0 <107307903+berki0@users.noreply.github.com> Date: Thu, 28 Mar 2024 16:48:52 +0200 Subject: [PATCH 20/33] [feature-72-BE-products-in-organization] add tests --- .../ProductInOrganizationController.java | 6 +++--- .../service/ProductInOrganizationService.java | 14 +++++++------- .../ProductInOrganizationServiceTest.java | 17 +---------------- 3 files changed, 11 insertions(+), 26 deletions(-) diff --git a/src/main/java/jewellery/inventory/controller/ProductInOrganizationController.java b/src/main/java/jewellery/inventory/controller/ProductInOrganizationController.java index 240de74b..bc23c8ea 100644 --- a/src/main/java/jewellery/inventory/controller/ProductInOrganizationController.java +++ b/src/main/java/jewellery/inventory/controller/ProductInOrganizationController.java @@ -34,10 +34,10 @@ public ProductsInOrganizationResponseDto createProductInOrganization( @Operation(summary = "Delete a new product in organization") @ResponseStatus(HttpStatus.NO_CONTENT) - @DeleteMapping("/{organizationId}/products/{productId}") + @DeleteMapping("/products/{productId}") public void deleteProductInOrganization( - @PathVariable("productId") UUID productId, @PathVariable("organizationId") UUID organizationId) { - productInOrganizationService.deleteProductInOrganization(organizationId, productId); + @PathVariable("productId") UUID productId) { + productInOrganizationService.deleteProductInOrganization(productId); } @Operation(summary = "Update a product in organization") diff --git a/src/main/java/jewellery/inventory/service/ProductInOrganizationService.java b/src/main/java/jewellery/inventory/service/ProductInOrganizationService.java index b7f3f4d5..4446b5fb 100644 --- a/src/main/java/jewellery/inventory/service/ProductInOrganizationService.java +++ b/src/main/java/jewellery/inventory/service/ProductInOrganizationService.java @@ -48,7 +48,7 @@ public ProductsInOrganizationResponseDto updateProduct( organization, OrganizationPermission.EDIT_PRODUCT); Product product = productService.getProduct(productId); - throwExceptionIfOrganizationNotOwner(organization.getId(),product); + throwExceptionIfOrganizationNotOwner(organization.getId(), product); productService.throwExceptionIfProductIsSold(product); moveQuantityFromResourcesInProductToResourcesInOrganization(product); productService.disassembleProductContent(product); @@ -90,14 +90,14 @@ public ProductsInOrganizationResponseDto createProductInOrganization( @Transactional @LogDeleteEvent(eventType = EventType.ORGANIZATION_PRODUCT_DISASSEMBLY) - public void deleteProductInOrganization(UUID productId,UUID organizationId) { - organizationService.validateCurrentUserPermission( - organizationService.getOrganization(organizationId), - OrganizationPermission.DISASSEMBLE_PRODUCT); - + public void deleteProductInOrganization(UUID productId) { Product forDeleteProduct = productService.getProduct(productId); + Organization organization = + organizationService.getOrganization(forDeleteProduct.getOrganization().getId()); + + organizationService.validateCurrentUserPermission( + organization, OrganizationPermission.DISASSEMBLE_PRODUCT); - throwExceptionIfOrganizationNotOwner(organizationId, forDeleteProduct); productService.throwExceptionIfProductIsSold(forDeleteProduct); productService.throwExceptionIfProductIsPartOfAnotherProduct(productId, forDeleteProduct); diff --git a/src/test/java/jewellery/inventory/unit/service/ProductInOrganizationServiceTest.java b/src/test/java/jewellery/inventory/unit/service/ProductInOrganizationServiceTest.java index 36501090..5b90b879 100644 --- a/src/test/java/jewellery/inventory/unit/service/ProductInOrganizationServiceTest.java +++ b/src/test/java/jewellery/inventory/unit/service/ProductInOrganizationServiceTest.java @@ -157,25 +157,10 @@ void deleteProductInOrganizationSuccessfully() { product.setOrganization(organization); when(productService.getProduct(product.getId())).thenReturn(product); - productInOrganizationService.deleteProductInOrganization(product.getId(), organization.getId()); + productInOrganizationService.deleteProductInOrganization(product.getId()); verify(productService, times(1)).deleteProductById(product.getId()); } - @Test - void deleteProductInOrganizationThrowOrganizationNotOwnerException() { - when(organizationService.getOrganization(organizationWithProduct.getId())) - .thenReturn(organizationWithProduct); - product.setOrganization(new Organization()); - - when(productService.getProduct(product.getId())).thenReturn(product); - - Assertions.assertThrows( - OrganizationNotOwnerException.class, - () -> - productInOrganizationService.deleteProductInOrganization( - product.getId(), organizationWithProduct.getId())); - } - private ProductResponseDto productToResponse(Product product) { ProductResponseDto productResponseDto = new ProductResponseDto(); productResponseDto.setId(product.getId()); From a434678edd0dcaf856a3c68ae9fb55afa6db420c Mon Sep 17 00:00:00 2001 From: ignatIgnatov Date: Fri, 29 Mar 2024 14:16:20 +0200 Subject: [PATCH 21/33] working create and update integration tests --- .../service/ProductInOrganizationService.java | 5 +- ...ductInOrganizationCrudIntegrationTest.java | 110 ++++++++++-------- 2 files changed, 62 insertions(+), 53 deletions(-) diff --git a/src/main/java/jewellery/inventory/service/ProductInOrganizationService.java b/src/main/java/jewellery/inventory/service/ProductInOrganizationService.java index 4446b5fb..56ad8575 100644 --- a/src/main/java/jewellery/inventory/service/ProductInOrganizationService.java +++ b/src/main/java/jewellery/inventory/service/ProductInOrganizationService.java @@ -34,7 +34,6 @@ public class ProductInOrganizationService implements EntityFetcher { private final ResourceInOrganizationService resourceInOrganizationService; private final ResourceInProductRepository resourceInProductRepository; private final ProductRepository productRepository; - private final ProductMapper productMapper; @Transactional @LogUpdateEvent(eventType = EventType.ORGANIZATION_PRODUCT_UPDATE) @@ -60,7 +59,7 @@ public ProductsInOrganizationResponseDto updateProduct( addResourcesToProduct(productRequestDto, organization, product); return mapper.mapToProductsInOrganizationResponseDto( - organization, List.of(productMapper.mapToProductResponseDto(product))); + organization, productService.getProductsResponse(List.of(product))); } public ProductsInOrganizationResponseDto getProductsInOrganization(UUID organizationId) { @@ -277,6 +276,6 @@ public Object fetchEntity(Object... ids) { return null; } return mapper.mapToProductsInOrganizationResponseDto( - product.getOrganization(), List.of(productMapper.mapToProductResponseDto(product))); + product.getOrganization(), productService.getProductsResponse(List.of(product))); } } diff --git a/src/test/java/jewellery/inventory/integration/ProductInOrganizationCrudIntegrationTest.java b/src/test/java/jewellery/inventory/integration/ProductInOrganizationCrudIntegrationTest.java index 9ab65622..9881ab3a 100644 --- a/src/test/java/jewellery/inventory/integration/ProductInOrganizationCrudIntegrationTest.java +++ b/src/test/java/jewellery/inventory/integration/ProductInOrganizationCrudIntegrationTest.java @@ -1,6 +1,8 @@ package jewellery.inventory.integration; import static jewellery.inventory.helper.ResourceTestHelper.getPearlRequestDto; +import static jewellery.inventory.helper.SystemEventTestHelper.getCreateOrDeleteEventPayload; +import static jewellery.inventory.helper.SystemEventTestHelper.getUpdateEventPayload; import static jewellery.inventory.model.EventType.*; import static jewellery.inventory.utils.BigDecimalUtil.getBigDecimal; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -9,6 +11,8 @@ import com.fasterxml.jackson.core.JsonProcessingException; import java.math.BigDecimal; import java.util.List; +import java.util.Map; +import java.util.Objects; import java.util.UUID; import jewellery.inventory.dto.request.*; import jewellery.inventory.dto.request.resource.ResourceQuantityRequestDto; @@ -119,17 +123,15 @@ void createProductInOrganizationSuccessfully() throws JsonProcessingException { assertEquals(1, productInOrganizationResponse.getBody().getProducts().size()); assertEquals(HttpStatus.CREATED, productInOrganizationResponse.getStatusCode()); - // Map expectedEventPayload = - // - // getCreateOrDeleteEventPayload(productInOrganizationResponse.getBody().getProducts().get(0), - // objectMapper); - // - // systemEventTestHelper.assertEventWasLogged(ORGANIZATION_PRODUCT_CREATE, - // expectedEventPayload); + Map expectedEventPayload = + getCreateOrDeleteEventPayload( + productInOrganizationResponse.getBody(), objectMapper); + + systemEventTestHelper.assertEventWasLogged(ORGANIZATION_PRODUCT_CREATE, expectedEventPayload); } @Test - void updateProductInOrganizationSuccessfully() { + void updateProductInOrganizationSuccessfully() throws JsonProcessingException { OrganizationResponseDto organizationResponseDto = createOrganization(); ResourceResponseDto resourceResponse = createResourceResponse(); @@ -160,50 +162,58 @@ void updateProductInOrganizationSuccessfully() { productInOrganizationResponse.getBody().getProducts().get(0).getId().toString()); assertEquals(HttpStatus.OK, updatedProductInOrganizationResponse.getStatusCode()); - // Map expectedEventPayload = - // getUpdateEventPayload( - // productInOrganizationResponse.getBody().getProducts().get(0), - // - // Objects.requireNonNull(updatedProductInOrganizationResponse.getBody()).getProducts().get(0), - // objectMapper); - // - // systemEventTestHelper.assertEventWasLogged(ORGANIZATION_PRODUCT_UPDATE, - // expectedEventPayload); + Map expectedEventPayload = + getUpdateEventPayload( + productInOrganizationResponse.getBody(), + Objects.requireNonNull(updatedProductInOrganizationResponse.getBody()), + objectMapper); + + systemEventTestHelper.assertEventWasLogged(ORGANIZATION_PRODUCT_UPDATE, expectedEventPayload); } - // @Test - // void deleteProductInOrganizationSuccessfully() { - // OrganizationResponseDto organizationResponseDto = createOrganization(); - // ResourceResponseDto resourceResponse = createResourceResponse(); - // - // sendResourceToOrganization( - // ResourceInOrganizationTestHelper.createResourceInOrganizationRequestDto( - // organizationResponseDto.getId(), - // resourceResponse.getId(), - // RESOURCE_QUANTITY, - // RESOURCE_PRICE)); - // - // ResponseEntity productInOrganizationResponse = - // createProduct( - // setOwnerAndResourceToProductRequest( - // productRequestDto, - // organizationResponseDto.getId(), - // resourceResponse.getId(), - // RESOURCE_QUANTITY)); - // - // ResponseEntity response = - // this.testRestTemplate.exchange( - // getOrganizationProductsWithIdUrl( - // organizationResponseDto.getId().toString(), - // - // productInOrganizationResponse.getBody().getProducts().get(0).getId().toString()), - // HttpMethod.DELETE, - // null, - // Void.class); - // - // assertEquals(HttpStatus.CREATED, productInOrganizationResponse.getStatusCode()); - // assertEquals(HttpStatus.NO_CONTENT, response.getStatusCode()); - // } + @Test + void deleteProductInOrganizationSuccessfully() { + OrganizationResponseDto organizationResponseDto = createOrganization(); + ResourceResponseDto resourceResponse = createResourceResponse(); + + sendResourceToOrganization( + ResourceInOrganizationTestHelper.createResourceInOrganizationRequestDto( + organizationResponseDto.getId(), + resourceResponse.getId(), + RESOURCE_QUANTITY, + RESOURCE_PRICE)); + + ResponseEntity productInOrganizationResponse = + createProduct( + setOwnerAndResourceToProductRequest( + productRequestDto, + organizationResponseDto.getId(), + resourceResponse.getId(), + RESOURCE_QUANTITY)); + + ResponseEntity response = + this.testRestTemplate.exchange( + getBaseOrganizationUrl() + "/products/" + productInOrganizationResponse.getBody().getProducts().get(0).getId(), + HttpMethod.DELETE, + null, + HttpStatus.class); + + assertEquals(HttpStatus.NO_CONTENT, response.getStatusCode()); + + +// ResponseEntity response = +// this.testRestTemplate.exchange( +// getOrganizationProductsWithIdUrl( +// organizationResponseDto.getId().toString(), +// +// productInOrganizationResponse.getBody().getProducts().get(0).getId().toString()), +// HttpMethod.DELETE, +// null, +// Void.class); +// +// assertEquals(HttpStatus.CREATED, productInOrganizationResponse.getStatusCode()); +// assertEquals(HttpStatus.NO_CONTENT, response.getStatusCode()); + } private OrganizationResponseDto createOrganization() { OrganizationRequestDto organizationRequestDto = From d47aede449aee5d64731f467bd6b04554761ec6b Mon Sep 17 00:00:00 2001 From: ignatIgnatov Date: Fri, 29 Mar 2024 16:15:57 +0200 Subject: [PATCH 22/33] refactoring --- .../service/ProductInOrganizationService.java | 27 ++---- ...ductInOrganizationCrudIntegrationTest.java | 87 +++++++++---------- .../ProductInOrganizationServiceTest.java | 6 -- 3 files changed, 49 insertions(+), 71 deletions(-) diff --git a/src/main/java/jewellery/inventory/service/ProductInOrganizationService.java b/src/main/java/jewellery/inventory/service/ProductInOrganizationService.java index 56ad8575..846d88a3 100644 --- a/src/main/java/jewellery/inventory/service/ProductInOrganizationService.java +++ b/src/main/java/jewellery/inventory/service/ProductInOrganizationService.java @@ -15,7 +15,6 @@ import jewellery.inventory.exception.invalid_resource_quantity.InsufficientResourceQuantityException; import jewellery.inventory.exception.organization.OrganizationNotOwnerException; import jewellery.inventory.mapper.ProductInOrganizationMapper; -import jewellery.inventory.mapper.ProductMapper; import jewellery.inventory.model.*; import jewellery.inventory.model.resource.Resource; import jewellery.inventory.model.resource.ResourceInProduct; @@ -90,23 +89,18 @@ public ProductsInOrganizationResponseDto createProductInOrganization( @Transactional @LogDeleteEvent(eventType = EventType.ORGANIZATION_PRODUCT_DISASSEMBLY) public void deleteProductInOrganization(UUID productId) { - Product forDeleteProduct = productService.getProduct(productId); + Product product = productService.getProduct(productId); Organization organization = - organizationService.getOrganization(forDeleteProduct.getOrganization().getId()); + organizationService.getOrganization(product.getOrganization().getId()); organizationService.validateCurrentUserPermission( organization, OrganizationPermission.DISASSEMBLE_PRODUCT); - productService.throwExceptionIfProductIsSold(forDeleteProduct); - productService.throwExceptionIfProductIsPartOfAnotherProduct(productId, forDeleteProduct); - - List subProducts = - getProductsInProduct( - forDeleteProduct.getProductsContent().stream().map(Product::getId).toList(), - forDeleteProduct); + productService.throwExceptionIfProductIsSold(product); + productService.throwExceptionIfProductIsPartOfAnotherProduct(productId, product); - removeProductsContentFromProduct(subProducts); - moveQuantityFromResourcesInProductToResourcesInOrganization(forDeleteProduct); + moveQuantityFromResourcesInProductToResourcesInOrganization(product); + productService.disassembleProductContent(product); productService.deleteProductById(productId); } @@ -146,15 +140,6 @@ private void addProductsContentToProduct(ProductRequestDto productRequestDto, Pr } } - private void removeProductsContentFromProduct(List subProducts) { - if (!subProducts.isEmpty()) { - for (Product subProduct : subProducts) { - subProduct.setContentOf(null); - productService.saveProduct(subProduct); - } - } - } - private List getProductsInProduct( List productsIdInRequest, Product parentProduct) { List products = new ArrayList<>(); diff --git a/src/test/java/jewellery/inventory/integration/ProductInOrganizationCrudIntegrationTest.java b/src/test/java/jewellery/inventory/integration/ProductInOrganizationCrudIntegrationTest.java index 9881ab3a..a04bf0e6 100644 --- a/src/test/java/jewellery/inventory/integration/ProductInOrganizationCrudIntegrationTest.java +++ b/src/test/java/jewellery/inventory/integration/ProductInOrganizationCrudIntegrationTest.java @@ -50,6 +50,10 @@ private String getBaseOrganizationUrl() { return buildUrl("organizations"); } + private String getProductUrl(UUID id) { + return "/products/" + id; + } + private String getOrganizationProductsUrl(String organizationId) { return buildUrl("organizations", organizationId, "products"); } @@ -124,8 +128,7 @@ void createProductInOrganizationSuccessfully() throws JsonProcessingException { assertEquals(HttpStatus.CREATED, productInOrganizationResponse.getStatusCode()); Map expectedEventPayload = - getCreateOrDeleteEventPayload( - productInOrganizationResponse.getBody(), objectMapper); + getCreateOrDeleteEventPayload(productInOrganizationResponse.getBody(), objectMapper); systemEventTestHelper.assertEventWasLogged(ORGANIZATION_PRODUCT_CREATE, expectedEventPayload); } @@ -171,49 +174,45 @@ void updateProductInOrganizationSuccessfully() throws JsonProcessingException { systemEventTestHelper.assertEventWasLogged(ORGANIZATION_PRODUCT_UPDATE, expectedEventPayload); } - @Test - void deleteProductInOrganizationSuccessfully() { - OrganizationResponseDto organizationResponseDto = createOrganization(); - ResourceResponseDto resourceResponse = createResourceResponse(); - - sendResourceToOrganization( - ResourceInOrganizationTestHelper.createResourceInOrganizationRequestDto( - organizationResponseDto.getId(), - resourceResponse.getId(), - RESOURCE_QUANTITY, - RESOURCE_PRICE)); - - ResponseEntity productInOrganizationResponse = - createProduct( - setOwnerAndResourceToProductRequest( - productRequestDto, - organizationResponseDto.getId(), - resourceResponse.getId(), - RESOURCE_QUANTITY)); - - ResponseEntity response = - this.testRestTemplate.exchange( - getBaseOrganizationUrl() + "/products/" + productInOrganizationResponse.getBody().getProducts().get(0).getId(), - HttpMethod.DELETE, - null, - HttpStatus.class); - - assertEquals(HttpStatus.NO_CONTENT, response.getStatusCode()); - - -// ResponseEntity response = -// this.testRestTemplate.exchange( -// getOrganizationProductsWithIdUrl( -// organizationResponseDto.getId().toString(), -// -// productInOrganizationResponse.getBody().getProducts().get(0).getId().toString()), -// HttpMethod.DELETE, -// null, -// Void.class); + @Test + void deleteProductInOrganizationSuccessfully() throws JsonProcessingException { + sendResourceToOrganization( + ResourceInOrganizationTestHelper.createResourceInOrganizationRequestDto( + organization.getId(), preciousStone.getId(), RESOURCE_QUANTITY, RESOURCE_PRICE)); + + ResponseEntity productInOrganizationResponse = + createProduct( + setOwnerAndResourceToProductRequest( + productRequestDto, organization.getId(), preciousStone.getId(), RESOURCE_QUANTITY)); + + assertEquals(HttpStatus.CREATED, productInOrganizationResponse.getStatusCode()); + assertEquals(1, productInOrganizationResponse.getBody().getProducts().size()); + + UUID productId = productInOrganizationResponse.getBody().getProducts().get(0).getId(); + + ResponseEntity response = + this.testRestTemplate.exchange( + getBaseOrganizationUrl() + getProductUrl(productId), + HttpMethod.DELETE, + null, + String.class); + + assertEquals(HttpStatus.NO_CONTENT, response.getStatusCode()); + + ResponseEntity afterDeleteResponse = + this.testRestTemplate.exchange( + getOrganizationProductsUrl(organization.getId().toString()), + HttpMethod.GET, + null, + ProductsInOrganizationResponseDto.class); + + assertEquals(0, afterDeleteResponse.getBody().getProducts().size()); + +// Map expectedEventPayload = +// getCreateOrDeleteEventPayload(productInOrganizationResponse.getBody(), objectMapper); // -// assertEquals(HttpStatus.CREATED, productInOrganizationResponse.getStatusCode()); -// assertEquals(HttpStatus.NO_CONTENT, response.getStatusCode()); - } +// systemEventTestHelper.assertEventWasLogged(ORGANIZATION_PRODUCT_DISASSEMBLY, expectedEventPayload); + } private OrganizationResponseDto createOrganization() { OrganizationRequestDto organizationRequestDto = diff --git a/src/test/java/jewellery/inventory/unit/service/ProductInOrganizationServiceTest.java b/src/test/java/jewellery/inventory/unit/service/ProductInOrganizationServiceTest.java index 5b90b879..3877030b 100644 --- a/src/test/java/jewellery/inventory/unit/service/ProductInOrganizationServiceTest.java +++ b/src/test/java/jewellery/inventory/unit/service/ProductInOrganizationServiceTest.java @@ -10,14 +10,11 @@ import java.util.ArrayList; import java.util.List; import java.util.UUID; - import jewellery.inventory.dto.request.ProductRequestDto; import jewellery.inventory.dto.response.ProductResponseDto; import jewellery.inventory.dto.response.ProductsInOrganizationResponseDto; -import jewellery.inventory.exception.organization.OrganizationNotOwnerException; import jewellery.inventory.helper.*; import jewellery.inventory.mapper.ProductInOrganizationMapper; -import jewellery.inventory.mapper.ProductMapper; import jewellery.inventory.model.Organization; import jewellery.inventory.model.Product; import jewellery.inventory.model.ResourceInOrganization; @@ -29,7 +26,6 @@ import jewellery.inventory.service.ProductInOrganizationService; import jewellery.inventory.service.ProductService; import jewellery.inventory.service.ResourceInOrganizationService; -import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @@ -43,7 +39,6 @@ class ProductInOrganizationServiceTest { @Mock private OrganizationService organizationService; @Mock private ProductService productService; @Mock private ProductRepository productRepository; - @Mock private ProductMapper productMapper; @Mock private ProductInOrganizationMapper mapper; @Mock private ResourceInOrganizationService resourceInOrganizationService; @Mock private ResourceInProductRepository resourceInProductRepository; @@ -144,7 +139,6 @@ void updateProductInOrganizationSuccessfully() { when(mapper.mapToProductsInOrganizationResponseDto(eq(organization), anyList())) .thenReturn(productsInOrganizationResponseDto); - when(productMapper.mapToProductResponseDto(product)).thenReturn(productToResponse(product)); ProductsInOrganizationResponseDto productsInOrganizationResponse = productInOrganizationService.updateProduct(product.getId(), productRequestDto); From 935639a6f7f5640c8de9b9a9b60f4594448e9285 Mon Sep 17 00:00:00 2001 From: ignatIgnatov Date: Fri, 29 Mar 2024 21:19:48 +0200 Subject: [PATCH 23/33] working delete integration test --- .../model/resource/ResourceInProduct.java | 8 +++----- .../service/ProductInOrganizationService.java | 1 + .../service/ResourceInOrganizationService.java | 2 +- ...oductInOrganizationCrudIntegrationTest.java | 18 +++--------------- 4 files changed, 8 insertions(+), 21 deletions(-) diff --git a/src/main/java/jewellery/inventory/model/resource/ResourceInProduct.java b/src/main/java/jewellery/inventory/model/resource/ResourceInProduct.java index d925e2ad..361c0281 100644 --- a/src/main/java/jewellery/inventory/model/resource/ResourceInProduct.java +++ b/src/main/java/jewellery/inventory/model/resource/ResourceInProduct.java @@ -1,9 +1,7 @@ package jewellery.inventory.model.resource; -import jakarta.persistence.Entity; -import jakarta.persistence.GeneratedValue; -import jakarta.persistence.Id; -import jakarta.persistence.ManyToOne; +import jakarta.persistence.*; + import java.math.BigDecimal; import java.util.UUID; import jewellery.inventory.model.Product; @@ -20,5 +18,5 @@ public class ResourceInProduct { private BigDecimal quantity; - @ManyToOne private Product product; + @ManyToOne(cascade = CascadeType.PERSIST) private Product product; } diff --git a/src/main/java/jewellery/inventory/service/ProductInOrganizationService.java b/src/main/java/jewellery/inventory/service/ProductInOrganizationService.java index 846d88a3..3c6f80cb 100644 --- a/src/main/java/jewellery/inventory/service/ProductInOrganizationService.java +++ b/src/main/java/jewellery/inventory/service/ProductInOrganizationService.java @@ -100,6 +100,7 @@ public void deleteProductInOrganization(UUID productId) { productService.throwExceptionIfProductIsPartOfAnotherProduct(productId, product); moveQuantityFromResourcesInProductToResourcesInOrganization(product); + productService.disassembleProductContent(product); productService.deleteProductById(productId); } diff --git a/src/main/java/jewellery/inventory/service/ResourceInOrganizationService.java b/src/main/java/jewellery/inventory/service/ResourceInOrganizationService.java index 3e070903..b9df2264 100644 --- a/src/main/java/jewellery/inventory/service/ResourceInOrganizationService.java +++ b/src/main/java/jewellery/inventory/service/ResourceInOrganizationService.java @@ -150,7 +150,7 @@ private ResourceInOrganization getResourceInOrganization( () -> createAndAddNewResourceInOrganization(organization, resource, BigDecimal.ZERO)); } - public ResourceInOrganization addResourceToOrganization( + private ResourceInOrganization addResourceToOrganization( Organization organization, Resource resource, BigDecimal quantity, BigDecimal dealPrice) { logger.info( "Adding resource to organization. Organization: {}, Resource: {}, Quantity: {}", diff --git a/src/test/java/jewellery/inventory/integration/ProductInOrganizationCrudIntegrationTest.java b/src/test/java/jewellery/inventory/integration/ProductInOrganizationCrudIntegrationTest.java index a04bf0e6..891e308b 100644 --- a/src/test/java/jewellery/inventory/integration/ProductInOrganizationCrudIntegrationTest.java +++ b/src/test/java/jewellery/inventory/integration/ProductInOrganizationCrudIntegrationTest.java @@ -185,9 +185,6 @@ void deleteProductInOrganizationSuccessfully() throws JsonProcessingException { setOwnerAndResourceToProductRequest( productRequestDto, organization.getId(), preciousStone.getId(), RESOURCE_QUANTITY)); - assertEquals(HttpStatus.CREATED, productInOrganizationResponse.getStatusCode()); - assertEquals(1, productInOrganizationResponse.getBody().getProducts().size()); - UUID productId = productInOrganizationResponse.getBody().getProducts().get(0).getId(); ResponseEntity response = @@ -199,19 +196,10 @@ void deleteProductInOrganizationSuccessfully() throws JsonProcessingException { assertEquals(HttpStatus.NO_CONTENT, response.getStatusCode()); - ResponseEntity afterDeleteResponse = - this.testRestTemplate.exchange( - getOrganizationProductsUrl(organization.getId().toString()), - HttpMethod.GET, - null, - ProductsInOrganizationResponseDto.class); - - assertEquals(0, afterDeleteResponse.getBody().getProducts().size()); + Map expectedEventPayload = + getCreateOrDeleteEventPayload(productInOrganizationResponse.getBody(), objectMapper); -// Map expectedEventPayload = -// getCreateOrDeleteEventPayload(productInOrganizationResponse.getBody(), objectMapper); -// -// systemEventTestHelper.assertEventWasLogged(ORGANIZATION_PRODUCT_DISASSEMBLY, expectedEventPayload); + systemEventTestHelper.assertEventWasLogged(ORGANIZATION_PRODUCT_DISASSEMBLY, expectedEventPayload); } private OrganizationResponseDto createOrganization() { From 0d9e0899cfef64c8380ff07cab04474241b93408 Mon Sep 17 00:00:00 2001 From: berki0 <107307903+berki0@users.noreply.github.com> Date: Mon, 1 Apr 2024 08:37:44 +0300 Subject: [PATCH 24/33] [feature-72-BE-products-in-organization] add tests --- .../inventory/service/ProductInOrganizationService.java | 2 +- .../integration/ProductInOrganizationCrudIntegrationTest.java | 4 ---- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/src/main/java/jewellery/inventory/service/ProductInOrganizationService.java b/src/main/java/jewellery/inventory/service/ProductInOrganizationService.java index 3c6f80cb..269c767d 100644 --- a/src/main/java/jewellery/inventory/service/ProductInOrganizationService.java +++ b/src/main/java/jewellery/inventory/service/ProductInOrganizationService.java @@ -86,12 +86,12 @@ public ProductsInOrganizationResponseDto createProductInOrganization( organization, productService.getProductsResponse(List.of(product))); } - @Transactional @LogDeleteEvent(eventType = EventType.ORGANIZATION_PRODUCT_DISASSEMBLY) public void deleteProductInOrganization(UUID productId) { Product product = productService.getProduct(productId); Organization organization = organizationService.getOrganization(product.getOrganization().getId()); + throwExceptionIfOrganizationNotOwner(organization.getId(),product); organizationService.validateCurrentUserPermission( organization, OrganizationPermission.DISASSEMBLE_PRODUCT); diff --git a/src/test/java/jewellery/inventory/integration/ProductInOrganizationCrudIntegrationTest.java b/src/test/java/jewellery/inventory/integration/ProductInOrganizationCrudIntegrationTest.java index 891e308b..4e630ffd 100644 --- a/src/test/java/jewellery/inventory/integration/ProductInOrganizationCrudIntegrationTest.java +++ b/src/test/java/jewellery/inventory/integration/ProductInOrganizationCrudIntegrationTest.java @@ -58,10 +58,6 @@ private String getOrganizationProductsUrl(String organizationId) { return buildUrl("organizations", organizationId, "products"); } - private String getOrganizationProductsWithIdUrl(String organizationId, String productId) { - return buildUrl("organizations", organizationId, "products", productId); - } - private PreciousStone preciousStone; private ProductRequestDto productRequestDto; private OrganizationResponseDto organization; From b8d86065bf3d5ae578b878d5e2f62b45d26409dc Mon Sep 17 00:00:00 2001 From: berki0 <107307903+berki0@users.noreply.github.com> Date: Tue, 2 Apr 2024 13:29:11 +0300 Subject: [PATCH 25/33] [feature-72-BE-products-in-organization] changes after review --- .../service/ProductInOrganizationService.java | 39 ++++++++++--------- ...ductInOrganizationCrudIntegrationTest.java | 6 +-- .../ProductInOrganizationServiceTest.java | 24 ++++++------ 3 files changed, 35 insertions(+), 34 deletions(-) diff --git a/src/main/java/jewellery/inventory/service/ProductInOrganizationService.java b/src/main/java/jewellery/inventory/service/ProductInOrganizationService.java index 269c767d..0966aff3 100644 --- a/src/main/java/jewellery/inventory/service/ProductInOrganizationService.java +++ b/src/main/java/jewellery/inventory/service/ProductInOrganizationService.java @@ -34,13 +34,20 @@ public class ProductInOrganizationService implements EntityFetcher { private final ResourceInProductRepository resourceInProductRepository; private final ProductRepository productRepository; + public ProductsInOrganizationResponseDto getProductsInOrganization(UUID organizationId) { + Organization organization = organizationService.getOrganization(organizationId); + organizationService.validateUserInOrganization(organization); + + return mapper.mapToProductsInOrganizationResponseDto( + organization, productService.getProductsResponse(organization.getProductsOwned())); + } + @Transactional @LogUpdateEvent(eventType = EventType.ORGANIZATION_PRODUCT_UPDATE) public ProductsInOrganizationResponseDto updateProduct( UUID productId, ProductRequestDto productRequestDto) { Organization organization = organizationService.getOrganization(productRequestDto.getOwnerId()); - organizationService.validateUserInOrganization(organization); organizationService.validateCurrentUserPermission( organization, OrganizationPermission.EDIT_PRODUCT); @@ -54,19 +61,7 @@ public ProductsInOrganizationResponseDto updateProduct( setProductFields(productRequestDto, organization, product); productRepository.save(product); - addProductsContentToProduct(productRequestDto, product); - addResourcesToProduct(productRequestDto, organization, product); - - return mapper.mapToProductsInOrganizationResponseDto( - organization, productService.getProductsResponse(List.of(product))); - } - - public ProductsInOrganizationResponseDto getProductsInOrganization(UUID organizationId) { - Organization organization = organizationService.getOrganization(organizationId); - organizationService.validateUserInOrganization(organization); - - return mapper.mapToProductsInOrganizationResponseDto( - organization, productService.getProductsResponse(organization.getProductsOwned())); + return addProductContents(organization, productRequestDto, product); } @Transactional @@ -79,11 +74,8 @@ public ProductsInOrganizationResponseDto createProductInOrganization( organization, OrganizationPermission.CREATE_PRODUCT); Product product = persistProductWithoutResourcesAndProducts(productRequestDto, organization); - addProductsContentToProduct(productRequestDto, product); - addResourcesToProduct(productRequestDto, organization, product); - return mapper.mapToProductsInOrganizationResponseDto( - organization, productService.getProductsResponse(List.of(product))); + return addProductContents(organization, productRequestDto, product); } @LogDeleteEvent(eventType = EventType.ORGANIZATION_PRODUCT_DISASSEMBLY) @@ -91,7 +83,7 @@ public void deleteProductInOrganization(UUID productId) { Product product = productService.getProduct(productId); Organization organization = organizationService.getOrganization(product.getOrganization().getId()); - throwExceptionIfOrganizationNotOwner(organization.getId(),product); + throwExceptionIfOrganizationNotOwner(organization.getId(), product); organizationService.validateCurrentUserPermission( organization, OrganizationPermission.DISASSEMBLE_PRODUCT); @@ -105,6 +97,15 @@ public void deleteProductInOrganization(UUID productId) { productService.deleteProductById(productId); } + private ProductsInOrganizationResponseDto addProductContents( + Organization organization, ProductRequestDto productRequestDto, Product product) { + addProductsContentToProduct(productRequestDto, product); + addResourcesToProduct(productRequestDto, organization, product); + + return mapper.mapToProductsInOrganizationResponseDto( + organization, productService.getProductsResponse(List.of(product))); + } + private Product persistProductWithoutResourcesAndProducts( ProductRequestDto productRequestDto, Organization organization) { Product product = getProductWithoutResourcesAndProduct(productRequestDto, organization); diff --git a/src/test/java/jewellery/inventory/integration/ProductInOrganizationCrudIntegrationTest.java b/src/test/java/jewellery/inventory/integration/ProductInOrganizationCrudIntegrationTest.java index 4e630ffd..bfb44b9f 100644 --- a/src/test/java/jewellery/inventory/integration/ProductInOrganizationCrudIntegrationTest.java +++ b/src/test/java/jewellery/inventory/integration/ProductInOrganizationCrudIntegrationTest.java @@ -90,7 +90,7 @@ void getAllProductsFromOrganizationSuccessfully() { @Test void createProductInOrganizationSuccessfully() throws JsonProcessingException { OrganizationResponseDto organizationResponseDto = createOrganization(); - ResourceResponseDto resourceResponse = createResourceResponse(); + ResourceResponseDto resourceResponse = sendCreateResourceRequest(); ResourceInOrganizationRequestDto resourceInOrganizationRequest = ResourceInOrganizationTestHelper.createResourceInOrganizationRequestDto( @@ -132,7 +132,7 @@ void createProductInOrganizationSuccessfully() throws JsonProcessingException { @Test void updateProductInOrganizationSuccessfully() throws JsonProcessingException { OrganizationResponseDto organizationResponseDto = createOrganization(); - ResourceResponseDto resourceResponse = createResourceResponse(); + ResourceResponseDto resourceResponse = sendCreateResourceRequest(); ResourceInOrganizationRequestDto resourceInOrganizationRequest = ResourceInOrganizationTestHelper.createResourceInOrganizationRequestDto( @@ -210,7 +210,7 @@ private OrganizationResponseDto createOrganization() { return organizationResponseDto; } - private ResourceResponseDto createResourceResponse() { + private ResourceResponseDto sendCreateResourceRequest() { ResourceRequestDto resourceRequest = getPearlRequestDto(); ResponseEntity resourceResponseEntity = this.testRestTemplate.postForEntity( diff --git a/src/test/java/jewellery/inventory/unit/service/ProductInOrganizationServiceTest.java b/src/test/java/jewellery/inventory/unit/service/ProductInOrganizationServiceTest.java index 3877030b..66f4d37f 100644 --- a/src/test/java/jewellery/inventory/unit/service/ProductInOrganizationServiceTest.java +++ b/src/test/java/jewellery/inventory/unit/service/ProductInOrganizationServiceTest.java @@ -4,6 +4,7 @@ import static jewellery.inventory.helper.ProductTestHelper.getProductRequestDtoForOrganization; import static jewellery.inventory.helper.ProductTestHelper.getTestProduct; import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.mockito.Mockito.*; import java.math.BigDecimal; @@ -42,7 +43,7 @@ class ProductInOrganizationServiceTest { @Mock private ProductInOrganizationMapper mapper; @Mock private ResourceInOrganizationService resourceInOrganizationService; @Mock private ResourceInProductRepository resourceInProductRepository; - private static final BigDecimal BIG_QUANTITY = BigDecimal.valueOf(30); + private static final BigDecimal QUANTITY = BigDecimal.valueOf(30); private Organization organization; private Organization organizationWithProduct; @@ -63,9 +64,9 @@ void setUp() { ResourceInOrganizationTestHelper.createResourceInOrganization(organization, resource); organization.setResourceInOrganization(List.of(resourceInOrganization)); productRequestDto = - getProductRequestDtoForOrganization(user, organization.getId(), user.getId(), BIG_QUANTITY); + getProductRequestDtoForOrganization(user, organization.getId(), user.getId(), QUANTITY); productWithProductRequestDto = - getProductRequestDtoForOrganization(user, organization.getId(), user.getId(), BIG_QUANTITY); + getProductRequestDtoForOrganization(user, organization.getId(), user.getId(), QUANTITY); productWithProductRequestDto.setProductsContent(List.of(product.getId())); organizationWithProduct = setProductAndResourcesToOrganization( @@ -85,6 +86,7 @@ void getAllProductsInOrganizationSuccessfully() { ProductsInOrganizationResponseDto products = productInOrganizationService.getProductsInOrganization(organizationWithProduct.getId()); + assertNotNull(products); assertEquals(1, products.getProducts().size()); } @@ -92,16 +94,15 @@ void getAllProductsInOrganizationSuccessfully() { @Test void createProductInOrganizationSuccessfully() { when(organizationService.getOrganization(organization.getId())).thenReturn(organization); - when(resourceInOrganizationService.findResourceInOrganizationOrThrow( organization, productRequestDto.getResourcesContent().get(0).getResourceId())) .thenReturn(resourceInOrganization); - when(mapper.mapToProductsInOrganizationResponseDto(organization, new ArrayList<>())) .thenReturn(productsInOrganizationResponseDto); ProductsInOrganizationResponseDto productsInOrganizationResponse = productInOrganizationService.createProductInOrganization(productRequestDto); + assertNotNull(productsInOrganizationResponse); assertEquals(1, productsInOrganizationResponse.getProducts().size()); } @@ -109,20 +110,18 @@ void createProductInOrganizationSuccessfully() { @Test void createProductWithProductInOrganizationSuccessfully() { when(organizationService.getOrganization(organization.getId())).thenReturn(organization); - when(resourceInOrganizationService.findResourceInOrganizationOrThrow( organization, productWithProductRequestDto.getResourcesContent().get(0).getResourceId())) .thenReturn(resourceInOrganization); - product.setOrganization(organization); - when(mapper.mapToProductsInOrganizationResponseDto(organization, new ArrayList<>())) .thenReturn(productsInOrganizationResponseDto); when(productService.getProduct(any(UUID.class))).thenReturn(product); ProductsInOrganizationResponseDto productsInOrganizationResponse = productInOrganizationService.createProductInOrganization(productWithProductRequestDto); + assertNotNull(productsInOrganizationResponse); assertEquals(1, productsInOrganizationResponse.getProducts().size()); } @@ -132,26 +131,27 @@ void updateProductInOrganizationSuccessfully() { when(organizationService.getOrganization(organization.getId())).thenReturn(organization); product.setOrganization(organization); when(productService.getProduct(product.getId())).thenReturn(product); - when(resourceInOrganizationService.findResourceInOrganizationOrThrow( organization, productRequestDto.getResourcesContent().get(0).getResourceId())) .thenReturn(resourceInOrganization); - when(mapper.mapToProductsInOrganizationResponseDto(eq(organization), anyList())) .thenReturn(productsInOrganizationResponseDto); ProductsInOrganizationResponseDto productsInOrganizationResponse = productInOrganizationService.updateProduct(product.getId(), productRequestDto); + assertNotNull(productsInOrganizationResponse); + assertEquals(1, productsInOrganizationResponse.getProducts().size()); } @Test void deleteProductInOrganizationSuccessfully() { - when(organizationService.getOrganization(organization.getId())).thenReturn(organization); - product.setOrganization(organization); + when(organizationService.getOrganization(organizationWithProduct.getId())) + .thenReturn(organizationWithProduct); when(productService.getProduct(product.getId())).thenReturn(product); productInOrganizationService.deleteProductInOrganization(product.getId()); + verify(productService, times(1)).deleteProductById(product.getId()); } From fb0fb2d6a61b351be53b2c802df580bf8949afa9 Mon Sep 17 00:00:00 2001 From: berki0 <107307903+berki0@users.noreply.github.com> Date: Tue, 2 Apr 2024 14:37:33 +0300 Subject: [PATCH 26/33] [feature-72-BE-products-in-organization] changes after review --- .../service/ProductInOrganizationService.java | 16 ++-------------- .../service/ResourceInOrganizationService.java | 2 +- 2 files changed, 3 insertions(+), 15 deletions(-) diff --git a/src/main/java/jewellery/inventory/service/ProductInOrganizationService.java b/src/main/java/jewellery/inventory/service/ProductInOrganizationService.java index 0966aff3..3194d3de 100644 --- a/src/main/java/jewellery/inventory/service/ProductInOrganizationService.java +++ b/src/main/java/jewellery/inventory/service/ProductInOrganizationService.java @@ -9,7 +9,6 @@ import jewellery.inventory.aspect.annotation.LogDeleteEvent; import jewellery.inventory.aspect.annotation.LogUpdateEvent; import jewellery.inventory.dto.request.ProductRequestDto; -import jewellery.inventory.dto.request.ResourceInOrganizationRequestDto; import jewellery.inventory.dto.request.resource.ResourceQuantityRequestDto; import jewellery.inventory.dto.response.ProductsInOrganizationResponseDto; import jewellery.inventory.exception.invalid_resource_quantity.InsufficientResourceQuantityException; @@ -94,6 +93,7 @@ public void deleteProductInOrganization(UUID productId) { moveQuantityFromResourcesInProductToResourcesInOrganization(product); productService.disassembleProductContent(product); + product.setOrganization(null); productService.deleteProductById(productId); } @@ -234,23 +234,11 @@ private void throwExceptionIfOrganizationNotOwner(UUID organizationId, Product p throw new OrganizationNotOwnerException(organizationId, product.getId()); } } - - private ResourceInOrganizationRequestDto getResourceInOrganizationRequest( - Organization owner, ResourceInProduct resourceInProduct) { - - return ResourceInOrganizationRequestDto.builder() - .organizationId(owner.getId()) - .resourceId(resourceInProduct.getResource().getId()) - .quantity(resourceInProduct.getQuantity()) - .build(); - } - private void moveQuantityFromResourcesInProductToResourcesInOrganization(Product product) { List resourcesInProduct = product.getResourcesContent(); resourcesInProduct.forEach( resourceInProduct -> { - resourceInOrganizationService.addResourceToOrganization( - getResourceInOrganizationRequest(product.getOrganization(), resourceInProduct)); + resourceInOrganizationService.addResourceToOrganization(product.getOrganization(),resourceInProduct.getResource(),resourceInProduct.getQuantity(),BigDecimal.ZERO); resourceInProductRepository.delete(resourceInProduct); }); product.setResourcesContent(null); diff --git a/src/main/java/jewellery/inventory/service/ResourceInOrganizationService.java b/src/main/java/jewellery/inventory/service/ResourceInOrganizationService.java index b9df2264..3e070903 100644 --- a/src/main/java/jewellery/inventory/service/ResourceInOrganizationService.java +++ b/src/main/java/jewellery/inventory/service/ResourceInOrganizationService.java @@ -150,7 +150,7 @@ private ResourceInOrganization getResourceInOrganization( () -> createAndAddNewResourceInOrganization(organization, resource, BigDecimal.ZERO)); } - private ResourceInOrganization addResourceToOrganization( + public ResourceInOrganization addResourceToOrganization( Organization organization, Resource resource, BigDecimal quantity, BigDecimal dealPrice) { logger.info( "Adding resource to organization. Organization: {}, Resource: {}, Quantity: {}", From 9e9bbe6a3da36e4320da0c6b759c02c194848c57 Mon Sep 17 00:00:00 2001 From: berki0 <107307903+berki0@users.noreply.github.com> Date: Tue, 2 Apr 2024 14:48:51 +0300 Subject: [PATCH 27/33] [feature-72-BE-products-in-organization] changes after review --- .../ProductInOrganizationCrudIntegrationTest.java | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/test/java/jewellery/inventory/integration/ProductInOrganizationCrudIntegrationTest.java b/src/test/java/jewellery/inventory/integration/ProductInOrganizationCrudIntegrationTest.java index bfb44b9f..edf1d574 100644 --- a/src/test/java/jewellery/inventory/integration/ProductInOrganizationCrudIntegrationTest.java +++ b/src/test/java/jewellery/inventory/integration/ProductInOrganizationCrudIntegrationTest.java @@ -196,6 +196,15 @@ void deleteProductInOrganizationSuccessfully() throws JsonProcessingException { getCreateOrDeleteEventPayload(productInOrganizationResponse.getBody(), objectMapper); systemEventTestHelper.assertEventWasLogged(ORGANIZATION_PRODUCT_DISASSEMBLY, expectedEventPayload); + + ResponseEntity organizationProductsResponse = + this.testRestTemplate.exchange( + getOrganizationProductsUrl(productInOrganizationResponse.getBody().getOrganization().getId().toString()), + HttpMethod.GET, + null, + ProductsInOrganizationResponseDto.class); + assertEquals(0,organizationProductsResponse.getBody().getProducts().size()); + } private OrganizationResponseDto createOrganization() { From 4346fad2642dc64a644711bc9acc47f550b3af09 Mon Sep 17 00:00:00 2001 From: berki0 <107307903+berki0@users.noreply.github.com> Date: Tue, 2 Apr 2024 16:42:35 +0300 Subject: [PATCH 28/33] [feature-72-BE-products-in-organization] changes after review --- .../service/ProductInOrganizationService.java | 45 ++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/src/main/java/jewellery/inventory/service/ProductInOrganizationService.java b/src/main/java/jewellery/inventory/service/ProductInOrganizationService.java index 3194d3de..d9ed285a 100644 --- a/src/main/java/jewellery/inventory/service/ProductInOrganizationService.java +++ b/src/main/java/jewellery/inventory/service/ProductInOrganizationService.java @@ -20,12 +20,15 @@ import jewellery.inventory.repository.ProductRepository; import jewellery.inventory.repository.ResourceInProductRepository; import lombok.AllArgsConstructor; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @Service @AllArgsConstructor public class ProductInOrganizationService implements EntityFetcher { + private static final Logger logger = LogManager.getLogger(ProductInOrganizationService.class); private final OrganizationService organizationService; private final ProductService productService; private final ProductInOrganizationMapper mapper; @@ -115,6 +118,7 @@ private Product persistProductWithoutResourcesAndProducts( private Product getProductWithoutResourcesAndProduct( ProductRequestDto productRequestDto, Organization organization) { + logger.debug("Creating product without resources and products"); Product product = new Product(); setProductFields(productRequestDto, organization, product); return product; @@ -132,12 +136,14 @@ private void setProductFields( product.setAdditionalPrice(productRequestDto.getAdditionalPrice()); product.setProductsContent(new ArrayList<>()); product.setResourcesContent(new ArrayList<>()); + logger.debug("Product fields have been set successfully for product: {}", product); } private void addProductsContentToProduct(ProductRequestDto productRequestDto, Product product) { if (productRequestDto.getProductsContent() != null) { product.setProductsContent( getProductsInProduct(productRequestDto.getProductsContent(), product)); + logger.debug("Products content added successfully to product: {}", product); productService.saveProduct(product); } } @@ -158,12 +164,18 @@ private List getProductsInProduct( .equals(product.getOrganization().getId())) { product.setContentOf(parentProduct); products.add(product); + logger.debug("Added product '{}' to the list.", product); } else { + logger.error( + "Organization with ID '{}' is not the owner of product with ID '{}'.", + parentProduct.getOrganization().getId(), + product.getId()); throw new OrganizationNotOwnerException( parentProduct.getOrganization().getId(), product.getId()); } }); } + logger.debug("Products in product retrieved successfully."); return products; } @@ -173,6 +185,8 @@ private void addResourcesToProduct( List resourcesInProducts = transferResourcesQuantitiesFromOrganizationToProduct( organization, productRequestDto.getResourcesContent(), product); + logger.debug("Resources added successfully to product: {}", product); + product.setResourcesContent(resourcesInProducts); } @@ -187,6 +201,7 @@ private List transferResourcesQuantitiesFromOrganizationToPro transferSingleResourceQuantityFromOrganizationToProduct( organization, resourceQuantity, product)); } + logger.debug("Resources quantities transferred successfully from organization to product."); return resourcesForAdd; } @@ -194,6 +209,9 @@ private ResourceInProduct transferSingleResourceQuantityFromOrganizationToProduc Organization organization, ResourceQuantityRequestDto incomingResourceInProduct, Product product) { + logger.debug( + "Transferring single resource quantity from organization to product for product: {}", + product); ResourceInOrganization resourceInOrganization = resourceInOrganizationService.findResourceInOrganizationOrThrow( @@ -206,6 +224,12 @@ private ResourceInProduct transferSingleResourceQuantityFromOrganizationToProduc incomingResourceInProduct.getResourceId(), incomingResourceInProduct.getQuantity()); + logger.debug( + "Quantity '{}' of resource '{}' removed from organization '{}'.", + incomingResourceInProduct.getQuantity(), + resourceInOrganization.getResource(), + organization.getName()); + return createResourceInProduct( incomingResourceInProduct.getQuantity(), resourceInOrganization.getResource(), product); } @@ -216,6 +240,7 @@ private ResourceInProduct createResourceInProduct( resourceInProduct.setResource(resource); resourceInProduct.setQuantity(quantity); resourceInProduct.setProduct(product); + logger.debug("ResourceInProduct created successfully for product: {}", product); return resourceInProduct; } @@ -224,6 +249,12 @@ private void checkResourceAvailability( ResourceQuantityRequestDto incomingResourceInProduct) { if (resourceInOrganization.getQuantity().compareTo(incomingResourceInProduct.getQuantity()) < 0) { + logger.error( + "Insufficient quantity of resource '{}' in organization. Requested: {}, Available: {}", + resourceInOrganization.getResource(), + incomingResourceInProduct.getQuantity(), + resourceInOrganization.getQuantity()); + throw new InsufficientResourceQuantityException( incomingResourceInProduct.getQuantity(), resourceInOrganization.getQuantity()); } @@ -231,15 +262,27 @@ private void checkResourceAvailability( private void throwExceptionIfOrganizationNotOwner(UUID organizationId, Product product) { if (!organizationId.equals(product.getOrganization().getId())) { + logger.error("Organization with ID '{}' is not the owner of product with ID '{}'.", organizationId, product.getId()); throw new OrganizationNotOwnerException(organizationId, product.getId()); } } + private void moveQuantityFromResourcesInProductToResourcesInOrganization(Product product) { List resourcesInProduct = product.getResourcesContent(); resourcesInProduct.forEach( resourceInProduct -> { - resourceInOrganizationService.addResourceToOrganization(product.getOrganization(),resourceInProduct.getResource(),resourceInProduct.getQuantity(),BigDecimal.ZERO); + logger.info( + "Adding resource: {} to organization: {} with quantity {}", + resourceInProduct.getResource(), + product.getOrganization(), + resourceInProduct.getQuantity()); + resourceInOrganizationService.addResourceToOrganization( + product.getOrganization(), + resourceInProduct.getResource(), + resourceInProduct.getQuantity(), + BigDecimal.ZERO); resourceInProductRepository.delete(resourceInProduct); + logger.info("Resource deleted from product: {}", product); }); product.setResourcesContent(null); } From 2063c212373fdd186fe0860a199b7e7ea802c289 Mon Sep 17 00:00:00 2001 From: berki0 <107307903+berki0@users.noreply.github.com> Date: Wed, 3 Apr 2024 09:59:38 +0300 Subject: [PATCH 29/33] [feature-72-BE-products-in-organization] changes after review --- .../integration/ProductInOrganizationCrudIntegrationTest.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/test/java/jewellery/inventory/integration/ProductInOrganizationCrudIntegrationTest.java b/src/test/java/jewellery/inventory/integration/ProductInOrganizationCrudIntegrationTest.java index edf1d574..35fc109a 100644 --- a/src/test/java/jewellery/inventory/integration/ProductInOrganizationCrudIntegrationTest.java +++ b/src/test/java/jewellery/inventory/integration/ProductInOrganizationCrudIntegrationTest.java @@ -203,6 +203,7 @@ void deleteProductInOrganizationSuccessfully() throws JsonProcessingException { HttpMethod.GET, null, ProductsInOrganizationResponseDto.class); + assertEquals(0,organizationProductsResponse.getBody().getProducts().size()); } From a43b3d9b967e806638a85a784cf20ab67e665615 Mon Sep 17 00:00:00 2001 From: berki0 <107307903+berki0@users.noreply.github.com> Date: Wed, 3 Apr 2024 12:54:46 +0300 Subject: [PATCH 30/33] fix delete method --- .../inventory/service/ProductInOrganizationService.java | 7 ++++++- .../jewellery/inventory/helper/OrganizationTestHelper.java | 3 ++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/main/java/jewellery/inventory/service/ProductInOrganizationService.java b/src/main/java/jewellery/inventory/service/ProductInOrganizationService.java index d9ed285a..6a058f08 100644 --- a/src/main/java/jewellery/inventory/service/ProductInOrganizationService.java +++ b/src/main/java/jewellery/inventory/service/ProductInOrganizationService.java @@ -80,6 +80,7 @@ public ProductsInOrganizationResponseDto createProductInOrganization( return addProductContents(organization, productRequestDto, product); } + @Transactional @LogDeleteEvent(eventType = EventType.ORGANIZATION_PRODUCT_DISASSEMBLY) public void deleteProductInOrganization(UUID productId) { Product product = productService.getProduct(productId); @@ -97,6 +98,7 @@ public void deleteProductInOrganization(UUID productId) { productService.disassembleProductContent(product); product.setOrganization(null); + organization.getProductsOwned().remove(product); productService.deleteProductById(productId); } @@ -262,7 +264,10 @@ private void checkResourceAvailability( private void throwExceptionIfOrganizationNotOwner(UUID organizationId, Product product) { if (!organizationId.equals(product.getOrganization().getId())) { - logger.error("Organization with ID '{}' is not the owner of product with ID '{}'.", organizationId, product.getId()); + logger.error( + "Organization with ID '{}' is not the owner of product with ID '{}'.", + organizationId, + product.getId()); throw new OrganizationNotOwnerException(organizationId, product.getId()); } } diff --git a/src/test/java/jewellery/inventory/helper/OrganizationTestHelper.java b/src/test/java/jewellery/inventory/helper/OrganizationTestHelper.java index 97185fff..0a3b6201 100644 --- a/src/test/java/jewellery/inventory/helper/OrganizationTestHelper.java +++ b/src/test/java/jewellery/inventory/helper/OrganizationTestHelper.java @@ -23,7 +23,8 @@ public static Organization getTestOrganization() { public static Organization setProductAndResourcesToOrganization(Organization organization, Product product, ResourceInOrganization resourceInOrganization) { product.setOrganization(organization); - organization.setProductsOwned(List.of(product)); + organization.setProductsOwned(new ArrayList<>()); + organization.getProductsOwned().add(product); organization.setResourceInOrganization(List.of(resourceInOrganization)); return organization; } From ecc5794da59cdb9f5bd4c93bc5ae939f03a1ed43 Mon Sep 17 00:00:00 2001 From: berki0 <107307903+berki0@users.noreply.github.com> Date: Wed, 3 Apr 2024 13:06:32 +0300 Subject: [PATCH 31/33] fix delete method --- ...ductInOrganizationCrudIntegrationTest.java | 39 ++++++++++--------- 1 file changed, 20 insertions(+), 19 deletions(-) diff --git a/src/test/java/jewellery/inventory/integration/ProductInOrganizationCrudIntegrationTest.java b/src/test/java/jewellery/inventory/integration/ProductInOrganizationCrudIntegrationTest.java index 35fc109a..72fb130a 100644 --- a/src/test/java/jewellery/inventory/integration/ProductInOrganizationCrudIntegrationTest.java +++ b/src/test/java/jewellery/inventory/integration/ProductInOrganizationCrudIntegrationTest.java @@ -61,16 +61,15 @@ private String getOrganizationProductsUrl(String organizationId) { private PreciousStone preciousStone; private ProductRequestDto productRequestDto; private OrganizationResponseDto organization; - private User user; @BeforeEach void setUp() { - user = createUserInDatabase(UserTestHelper.createTestUserRequest()); + User user = createUserInDatabase(UserTestHelper.createTestUserRequest()); organization = createOrganization(); preciousStone = createPreciousStoneInDatabase(); productRequestDto = ProductTestHelper.getProductRequestDtoForOrganization( - user, organization.getId(), preciousStone.getId(), RESOURCE_QUANTITY); + user, organization.getId(), preciousStone.getId(), RESOURCE_QUANTITY); } @Test @@ -102,14 +101,7 @@ void createProductInOrganizationSuccessfully() throws JsonProcessingException { ResponseEntity resource = sendResourceToOrganization(resourceInOrganizationRequest); - ResponseEntity getAllProductsInOrgResponse = - this.testRestTemplate.exchange( - getOrganizationProductsUrl(organizationResponseDto.getId().toString()), - HttpMethod.GET, - null, - ProductsInOrganizationResponseDto.class); - - assertEquals(0, getAllProductsInOrgResponse.getBody().getProducts().size()); + assertProductsInOrganizationSize(organizationResponseDto.getId().toString(), 0); ResponseEntity productInOrganizationResponse = createProduct( @@ -127,6 +119,8 @@ void createProductInOrganizationSuccessfully() throws JsonProcessingException { getCreateOrDeleteEventPayload(productInOrganizationResponse.getBody(), objectMapper); systemEventTestHelper.assertEventWasLogged(ORGANIZATION_PRODUCT_CREATE, expectedEventPayload); + + assertProductsInOrganizationSize(organizationResponseDto.getId().toString(), 1); } @Test @@ -168,6 +162,8 @@ void updateProductInOrganizationSuccessfully() throws JsonProcessingException { objectMapper); systemEventTestHelper.assertEventWasLogged(ORGANIZATION_PRODUCT_UPDATE, expectedEventPayload); + + assertProductsInOrganizationSize(organizationResponseDto.getId().toString(), 1); } @Test @@ -195,17 +191,22 @@ void deleteProductInOrganizationSuccessfully() throws JsonProcessingException { Map expectedEventPayload = getCreateOrDeleteEventPayload(productInOrganizationResponse.getBody(), objectMapper); - systemEventTestHelper.assertEventWasLogged(ORGANIZATION_PRODUCT_DISASSEMBLY, expectedEventPayload); + systemEventTestHelper.assertEventWasLogged( + ORGANIZATION_PRODUCT_DISASSEMBLY, expectedEventPayload); - ResponseEntity organizationProductsResponse = - this.testRestTemplate.exchange( - getOrganizationProductsUrl(productInOrganizationResponse.getBody().getOrganization().getId().toString()), - HttpMethod.GET, - null, - ProductsInOrganizationResponseDto.class); + assertProductsInOrganizationSize( + productInOrganizationResponse.getBody().getOrganization().getId().toString(), 0); + } - assertEquals(0,organizationProductsResponse.getBody().getProducts().size()); + private void assertProductsInOrganizationSize(String organizationId, int assertSize) { + ResponseEntity organizationProductsResponse = + this.testRestTemplate.exchange( + getOrganizationProductsUrl(organizationId), + HttpMethod.GET, + null, + ProductsInOrganizationResponseDto.class); + assertEquals(assertSize, organizationProductsResponse.getBody().getProducts().size()); } private OrganizationResponseDto createOrganization() { From 650aacd3907271291af2315b4211c5da82bae983 Mon Sep 17 00:00:00 2001 From: berki0 <107307903+berki0@users.noreply.github.com> Date: Wed, 3 Apr 2024 15:44:59 +0300 Subject: [PATCH 32/33] fix delete method --- .../exception/GlobalExceptionHandler.java | 5 +++-- ...roductIsNotPartOfOrganizationException.java | 9 +++++++++ .../model/resource/ResourceInProduct.java | 2 +- .../service/ProductInOrganizationService.java | 18 ++++++++++++------ .../ProductInOrganizationServiceTest.java | 2 -- 5 files changed, 25 insertions(+), 11 deletions(-) create mode 100644 src/main/java/jewellery/inventory/exception/organization/ProductIsNotPartOfOrganizationException.java diff --git a/src/main/java/jewellery/inventory/exception/GlobalExceptionHandler.java b/src/main/java/jewellery/inventory/exception/GlobalExceptionHandler.java index f4d78c15..921f4848 100644 --- a/src/main/java/jewellery/inventory/exception/GlobalExceptionHandler.java +++ b/src/main/java/jewellery/inventory/exception/GlobalExceptionHandler.java @@ -90,7 +90,8 @@ public ResponseEntity handleBadDataExceptions(RuntimeException ex) { UserIsPartOfOrganizationException.class, OrphanResourcesInOrganizationException.class, OrphanProductsInOrganizationException.class, - OrganizationNotOwnerException.class + OrganizationNotOwnerException.class, + ProductIsNotPartOfOrganizationException.class }) public ResponseEntity handleEntityConstraintConflict(RuntimeException ex) { return createErrorResponse(HttpStatus.CONFLICT, ex.getMessage(), ex); @@ -112,7 +113,7 @@ private ResponseEntity createErrorResponse( String date = LocalDateTime.now().format(DateTimeFormatter.ofPattern(DATE_TIME_FORMAT_PATTERN)); body.put(TIMESTAMP_KEY, date); body.put(ERROR_KEY, error); - logger.error("Error occurred: " + ex.getMessage(), ex); + logger.error("Error occurred: {} " , ex.getMessage(), ex); return new ResponseEntity<>(body, status); } diff --git a/src/main/java/jewellery/inventory/exception/organization/ProductIsNotPartOfOrganizationException.java b/src/main/java/jewellery/inventory/exception/organization/ProductIsNotPartOfOrganizationException.java new file mode 100644 index 00000000..8a5308a0 --- /dev/null +++ b/src/main/java/jewellery/inventory/exception/organization/ProductIsNotPartOfOrganizationException.java @@ -0,0 +1,9 @@ +package jewellery.inventory.exception.organization; + +import java.util.UUID; + +public class ProductIsNotPartOfOrganizationException extends RuntimeException { + public ProductIsNotPartOfOrganizationException(UUID productId) { + super("The Product with id " + productId + "is not owned of Organization"); + } +} diff --git a/src/main/java/jewellery/inventory/model/resource/ResourceInProduct.java b/src/main/java/jewellery/inventory/model/resource/ResourceInProduct.java index 361c0281..90df8c95 100644 --- a/src/main/java/jewellery/inventory/model/resource/ResourceInProduct.java +++ b/src/main/java/jewellery/inventory/model/resource/ResourceInProduct.java @@ -18,5 +18,5 @@ public class ResourceInProduct { private BigDecimal quantity; - @ManyToOne(cascade = CascadeType.PERSIST) private Product product; + @ManyToOne private Product product; } diff --git a/src/main/java/jewellery/inventory/service/ProductInOrganizationService.java b/src/main/java/jewellery/inventory/service/ProductInOrganizationService.java index 6a058f08..e7e75ee8 100644 --- a/src/main/java/jewellery/inventory/service/ProductInOrganizationService.java +++ b/src/main/java/jewellery/inventory/service/ProductInOrganizationService.java @@ -13,6 +13,7 @@ import jewellery.inventory.dto.response.ProductsInOrganizationResponseDto; import jewellery.inventory.exception.invalid_resource_quantity.InsufficientResourceQuantityException; import jewellery.inventory.exception.organization.OrganizationNotOwnerException; +import jewellery.inventory.exception.organization.ProductIsNotPartOfOrganizationException; import jewellery.inventory.mapper.ProductInOrganizationMapper; import jewellery.inventory.model.*; import jewellery.inventory.model.resource.Resource; @@ -84,9 +85,8 @@ public ProductsInOrganizationResponseDto createProductInOrganization( @LogDeleteEvent(eventType = EventType.ORGANIZATION_PRODUCT_DISASSEMBLY) public void deleteProductInOrganization(UUID productId) { Product product = productService.getProduct(productId); - Organization organization = - organizationService.getOrganization(product.getOrganization().getId()); - throwExceptionIfOrganizationNotOwner(organization.getId(), product); + validateProductIsOwnedOrganization(product); + Organization organization = product.getOrganization(); organizationService.validateCurrentUserPermission( organization, OrganizationPermission.DISASSEMBLE_PRODUCT); @@ -97,11 +97,15 @@ public void deleteProductInOrganization(UUID productId) { moveQuantityFromResourcesInProductToResourcesInOrganization(product); productService.disassembleProductContent(product); - product.setOrganization(null); - organization.getProductsOwned().remove(product); productService.deleteProductById(productId); } + private void validateProductIsOwnedOrganization(Product product) { + if (product.getOrganization() == null) { + throw new ProductIsNotPartOfOrganizationException(product.getId()); + } + } + private ProductsInOrganizationResponseDto addProductContents( Organization organization, ProductRequestDto productRequestDto, Product product) { addProductsContentToProduct(productRequestDto, product); @@ -286,6 +290,8 @@ private void moveQuantityFromResourcesInProductToResourcesInOrganization(Product resourceInProduct.getResource(), resourceInProduct.getQuantity(), BigDecimal.ZERO); + + resourceInProduct.setProduct(null); resourceInProductRepository.delete(resourceInProduct); logger.info("Resource deleted from product: {}", product); }); @@ -295,7 +301,7 @@ private void moveQuantityFromResourcesInProductToResourcesInOrganization(Product @Override public Object fetchEntity(Object... ids) { Product product = productRepository.findById((UUID) ids[0]).orElse(null); - if (product == null) { + if (product == null || product.getOrganization() == null) { return null; } return mapper.mapToProductsInOrganizationResponseDto( diff --git a/src/test/java/jewellery/inventory/unit/service/ProductInOrganizationServiceTest.java b/src/test/java/jewellery/inventory/unit/service/ProductInOrganizationServiceTest.java index 66f4d37f..a508e8b3 100644 --- a/src/test/java/jewellery/inventory/unit/service/ProductInOrganizationServiceTest.java +++ b/src/test/java/jewellery/inventory/unit/service/ProductInOrganizationServiceTest.java @@ -146,8 +146,6 @@ void updateProductInOrganizationSuccessfully() { @Test void deleteProductInOrganizationSuccessfully() { - when(organizationService.getOrganization(organizationWithProduct.getId())) - .thenReturn(organizationWithProduct); when(productService.getProduct(product.getId())).thenReturn(product); productInOrganizationService.deleteProductInOrganization(product.getId()); From 6cf44edafa9d4284292bd7a0e7d92bdb99bf5782 Mon Sep 17 00:00:00 2001 From: berki0 <107307903+berki0@users.noreply.github.com> Date: Wed, 3 Apr 2024 19:11:05 +0300 Subject: [PATCH 33/33] [feature-72-BE-products-in-organization] changes after review --- .../model/resource/ResourceInProduct.java | 13 ++++++++---- ...ductInOrganizationCrudIntegrationTest.java | 21 ++----------------- 2 files changed, 11 insertions(+), 23 deletions(-) diff --git a/src/main/java/jewellery/inventory/model/resource/ResourceInProduct.java b/src/main/java/jewellery/inventory/model/resource/ResourceInProduct.java index 90df8c95..6262d188 100644 --- a/src/main/java/jewellery/inventory/model/resource/ResourceInProduct.java +++ b/src/main/java/jewellery/inventory/model/resource/ResourceInProduct.java @@ -1,9 +1,11 @@ package jewellery.inventory.model.resource; -import jakarta.persistence.*; - import java.math.BigDecimal; import java.util.UUID; +import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.Id; +import jakarta.persistence.ManyToOne; import jewellery.inventory.model.Product; import lombok.Getter; import lombok.Setter; @@ -12,9 +14,12 @@ @Getter @Setter public class ResourceInProduct { - @Id @GeneratedValue private UUID id; + @Id + @GeneratedValue + private UUID id; - @ManyToOne private Resource resource; + @ManyToOne + private Resource resource; private BigDecimal quantity; diff --git a/src/test/java/jewellery/inventory/integration/ProductInOrganizationCrudIntegrationTest.java b/src/test/java/jewellery/inventory/integration/ProductInOrganizationCrudIntegrationTest.java index 72fb130a..d08e55d9 100644 --- a/src/test/java/jewellery/inventory/integration/ProductInOrganizationCrudIntegrationTest.java +++ b/src/test/java/jewellery/inventory/integration/ProductInOrganizationCrudIntegrationTest.java @@ -69,7 +69,7 @@ void setUp() { preciousStone = createPreciousStoneInDatabase(); productRequestDto = ProductTestHelper.getProductRequestDtoForOrganization( - user, organization.getId(), preciousStone.getId(), RESOURCE_QUANTITY); + user, organization.getId(), preciousStone.getId(), RESOURCE_QUANTITY); } @Test @@ -90,17 +90,14 @@ void getAllProductsFromOrganizationSuccessfully() { void createProductInOrganizationSuccessfully() throws JsonProcessingException { OrganizationResponseDto organizationResponseDto = createOrganization(); ResourceResponseDto resourceResponse = sendCreateResourceRequest(); - ResourceInOrganizationRequestDto resourceInOrganizationRequest = ResourceInOrganizationTestHelper.createResourceInOrganizationRequestDto( organizationResponseDto.getId(), resourceResponse.getId(), RESOURCE_QUANTITY, RESOURCE_PRICE); - ResponseEntity resource = sendResourceToOrganization(resourceInOrganizationRequest); - assertProductsInOrganizationSize(organizationResponseDto.getId().toString(), 0); ResponseEntity productInOrganizationResponse = @@ -114,12 +111,9 @@ void createProductInOrganizationSuccessfully() throws JsonProcessingException { assertNotNull(productInOrganizationResponse.getBody()); assertEquals(1, productInOrganizationResponse.getBody().getProducts().size()); assertEquals(HttpStatus.CREATED, productInOrganizationResponse.getStatusCode()); - Map expectedEventPayload = getCreateOrDeleteEventPayload(productInOrganizationResponse.getBody(), objectMapper); - systemEventTestHelper.assertEventWasLogged(ORGANIZATION_PRODUCT_CREATE, expectedEventPayload); - assertProductsInOrganizationSize(organizationResponseDto.getId().toString(), 1); } @@ -127,20 +121,16 @@ void createProductInOrganizationSuccessfully() throws JsonProcessingException { void updateProductInOrganizationSuccessfully() throws JsonProcessingException { OrganizationResponseDto organizationResponseDto = createOrganization(); ResourceResponseDto resourceResponse = sendCreateResourceRequest(); - ResourceInOrganizationRequestDto resourceInOrganizationRequest = ResourceInOrganizationTestHelper.createResourceInOrganizationRequestDto( organizationResponseDto.getId(), resourceResponse.getId(), RESOURCE_QUANTITY, RESOURCE_PRICE); - ResponseEntity resource = sendResourceToOrganization(resourceInOrganizationRequest); - ResponseEntity resource2 = sendResourceToOrganization(resourceInOrganizationRequest); - ResponseEntity productInOrganizationResponse = createProduct( setOwnerAndResourceToProductRequest( @@ -153,16 +143,14 @@ void updateProductInOrganizationSuccessfully() throws JsonProcessingException { updateProduct( productRequestDto, productInOrganizationResponse.getBody().getProducts().get(0).getId().toString()); - assertEquals(HttpStatus.OK, updatedProductInOrganizationResponse.getStatusCode()); + assertEquals(HttpStatus.OK, updatedProductInOrganizationResponse.getStatusCode()); Map expectedEventPayload = getUpdateEventPayload( productInOrganizationResponse.getBody(), Objects.requireNonNull(updatedProductInOrganizationResponse.getBody()), objectMapper); - systemEventTestHelper.assertEventWasLogged(ORGANIZATION_PRODUCT_UPDATE, expectedEventPayload); - assertProductsInOrganizationSize(organizationResponseDto.getId().toString(), 1); } @@ -171,12 +159,10 @@ void deleteProductInOrganizationSuccessfully() throws JsonProcessingException { sendResourceToOrganization( ResourceInOrganizationTestHelper.createResourceInOrganizationRequestDto( organization.getId(), preciousStone.getId(), RESOURCE_QUANTITY, RESOURCE_PRICE)); - ResponseEntity productInOrganizationResponse = createProduct( setOwnerAndResourceToProductRequest( productRequestDto, organization.getId(), preciousStone.getId(), RESOURCE_QUANTITY)); - UUID productId = productInOrganizationResponse.getBody().getProducts().get(0).getId(); ResponseEntity response = @@ -187,13 +173,10 @@ void deleteProductInOrganizationSuccessfully() throws JsonProcessingException { String.class); assertEquals(HttpStatus.NO_CONTENT, response.getStatusCode()); - Map expectedEventPayload = getCreateOrDeleteEventPayload(productInOrganizationResponse.getBody(), objectMapper); - systemEventTestHelper.assertEventWasLogged( ORGANIZATION_PRODUCT_DISASSEMBLY, expectedEventPayload); - assertProductsInOrganizationSize( productInOrganizationResponse.getBody().getOrganization().getId().toString(), 0); }