Skip to content

Commit

Permalink
add condition for owner equals recipient
Browse files Browse the repository at this point in the history
  • Loading branch information
ignatIgnatov committed Apr 12, 2024
1 parent 96fe62a commit efd0a42
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import jewellery.inventory.exception.invalid_resource_quantity.InsufficientResourceQuantityException;
import jewellery.inventory.exception.organization.OrganizationNotOwnerException;
import jewellery.inventory.exception.organization.ProductIsNotPartOfOrganizationException;
import jewellery.inventory.exception.product.ProductOwnerEqualsRecipientException;
import jewellery.inventory.mapper.ProductInOrganizationMapper;
import jewellery.inventory.model.*;
import jewellery.inventory.model.resource.Resource;
Expand Down Expand Up @@ -50,6 +51,8 @@ public ProductsInOrganizationResponseDto transferProduct(UUID productId, UUID re
Product product = productService.getProduct(productId);
productService.throwExceptionIfProductIsSold(product);
productService.throwExceptionIfProductIsPartOfAnotherProduct(productId, product);
throwExceptionIfProductOrganizationEqualsRecipient(recipientId, product);

Organization recipient = organizationService.getOrganization(recipientId);

organizationService.validateCurrentUserPermission(
Expand Down Expand Up @@ -331,6 +334,12 @@ private void updateProductOrganizationRecursively(Product product, Organization
}
}

private static void throwExceptionIfProductOrganizationEqualsRecipient(UUID recipientId, Product product) {
if (product.getOrganization().getId().equals(recipientId)) {
throw new ProductOwnerEqualsRecipientException(product.getOrganization().getId());
}
}

@Override
public Object fetchEntity(Object... ids) {
Product product = productRepository.findById((UUID) ids[0]).orElse(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import jewellery.inventory.exception.organization.MissingOrganizationPermissionException;
import jewellery.inventory.exception.product.ProductIsContentException;
import jewellery.inventory.exception.product.ProductIsSoldException;
import jewellery.inventory.exception.product.ProductOwnerEqualsRecipientException;
import jewellery.inventory.helper.*;
import jewellery.inventory.mapper.ProductInOrganizationMapper;
import jewellery.inventory.model.*;
Expand Down Expand Up @@ -79,22 +80,37 @@ void setUp() {
List.of(productToResponse(product)));
}

@Test
void transferProductShouldThrowWhenOwnerOrganizationEqualsRecipient() {
when(productService.getProduct(product.getId())).thenReturn(product);

assertThrows(
ProductOwnerEqualsRecipientException.class,
() ->
productInOrganizationService.transferProduct(
product.getId(), product.getOrganization().getId()));
}

@Test
void transferProductShouldThrowWhenProductNotFound() {
when(productService.getProduct(any())).thenThrow(ProductNotFoundException.class);

assertThrows(ProductNotFoundException.class, () ->
productInOrganizationService.transferProduct(product.getId(), organizationWithProduct.getId()));
assertThrows(
ProductNotFoundException.class,
() ->
productInOrganizationService.transferProduct(
product.getId(), organizationWithProduct.getId()));
}

@Test
void transferProductShouldThrowWhenOrganizationNotFound() {
when(organizationService.getOrganization(any())).thenThrow(OrganizationNotFoundException.class);

assertThrows(
OrganizationNotFoundException.class, () ->
productInOrganizationService.transferProduct(
product.getId(), organizationWithProduct.getId()));
OrganizationNotFoundException.class,
() ->
productInOrganizationService.transferProduct(
product.getId(), organizationWithProduct.getId()));
}

@Test
Expand Down Expand Up @@ -133,12 +149,16 @@ void transferProductShouldThrowWhenProductIsPartOfOtherProduct() {
void transferProductShouldThrowWhenNoPermission() {
when(productService.getProduct(product.getId())).thenReturn(product);
when(organizationService.getOrganization(organizationWithProduct.getId()))
.thenReturn(organizationWithProduct);
when(productInOrganizationService.transferProduct(product.getId(), organizationWithProduct.getId()))
.thenThrow(MissingOrganizationPermissionException.class);
.thenReturn(organizationWithProduct);
when(productInOrganizationService.transferProduct(
product.getId(), organizationWithProduct.getId()))
.thenThrow(MissingOrganizationPermissionException.class);

assertThrows(MissingOrganizationPermissionException.class, () ->
productInOrganizationService.transferProduct(product.getId(), organizationWithProduct.getId()));
assertThrows(
MissingOrganizationPermissionException.class,
() ->
productInOrganizationService.transferProduct(
product.getId(), organizationWithProduct.getId()));
}

@Test
Expand Down

0 comments on commit efd0a42

Please sign in to comment.