diff --git a/inventory-service/src/test/java/com/petclinic/inventoryservice/businesslayer/ProductInventoryServiceUnitTest.java b/inventory-service/src/test/java/com/petclinic/inventoryservice/businesslayer/ProductInventoryServiceUnitTest.java index 14bdf096f9..2ff91d77b4 100644 --- a/inventory-service/src/test/java/com/petclinic/inventoryservice/businesslayer/ProductInventoryServiceUnitTest.java +++ b/inventory-service/src/test/java/com/petclinic/inventoryservice/businesslayer/ProductInventoryServiceUnitTest.java @@ -14,12 +14,15 @@ import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.data.domain.PageRequest; +import org.springframework.data.domain.Pageable; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; import reactor.test.StepVerifier; import java.util.Optional; import java.util.UUID; +import java.util.regex.Pattern; + import static org.junit.jupiter.api.Assertions.*; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyString; @@ -867,6 +870,166 @@ >>>>>>> d1e75733 (fixing a bit of code because of the quodana requirement) */ + //search inventories + + @Test + void searchInventories_withAllParams_shouldReturnResults() { + Pageable page = PageRequest.of(0, 5); // Example pageable + String name = "Benzodiazepines"; + String type = "Internal"; + String description = "Medication for procedures"; + + when(inventoryRepository.findAllByInventoryNameAndInventoryTypeAndInventoryDescription(name, type, description)) + .thenReturn(Flux.just(inventory)); + + Flux result = productInventoryService.searchInventories(page, name, type, description); + + StepVerifier.create(result) + .expectNextCount(1) + .verifyComplete(); + } + + @Test + void searchInventories_withTypeAndDescription_shouldReturnResults() { + Pageable page = PageRequest.of(0, 5); + String type = "Internal"; + String description = "Medication for procedures"; + + when(inventoryRepository.findAllByInventoryTypeAndInventoryDescription(type, description)) + .thenReturn(Flux.just(inventory)); + + Flux result = productInventoryService.searchInventories(page, null, type, description); + + StepVerifier.create(result) + .expectNextCount(1) + .verifyComplete(); + } + + // Similarly, create other test methods for different combinations of parameters and scenarios... + + @Test + void searchInventories_withOnlyName_shouldReturnResults() { + Pageable page = PageRequest.of(0, 5); + String name = "Benzodiazepines"; + + when(inventoryRepository.findByInventoryNameRegex(anyString())) + .thenReturn(Flux.just(inventory)); + + Flux result = productInventoryService.searchInventories(page, name, null, null); + + StepVerifier.create(result) + .expectNextCount(1) + .verifyComplete(); + } + @Test + void searchInventories_withNameLengthOne_shouldReturnResults() { + Pageable page = PageRequest.of(0, 5); + String name = "B"; // Assuming 'B' is the starting letter for some inventory names + + when(inventoryRepository.findByInventoryNameRegex(anyString())) + .thenReturn(Flux.just(inventory)); + + Flux result = productInventoryService.searchInventories(page, name, null, null); + + StepVerifier.create(result) + .expectNextCount(1) + .verifyComplete(); + } + + @Test + void searchInventories_withDescriptionLengthOne_shouldReturnResults() { + Pageable page = PageRequest.of(0, 5); + String description = "M"; // Assuming 'M' is the starting letter for some inventory descriptions + + when(inventoryRepository.findByInventoryDescriptionRegex(anyString())) + .thenReturn(Flux.just(inventory)); + + Flux result = productInventoryService.searchInventories(page, null, null, description); + + StepVerifier.create(result) + .expectNextCount(1) + .verifyComplete(); + } + + @Test + void searchInventories_withDescriptionLongerThanOne_shouldReturnResults() { + Pageable page = PageRequest.of(0, 5); + String description = "Medication"; // Longer than one character + + when(inventoryRepository.findByInventoryDescriptionRegex(anyString())) + .thenReturn(Flux.just(inventory)); + + Flux result = productInventoryService.searchInventories(page, null, null, description); + + StepVerifier.create(result) + .expectNextCount(1) + .verifyComplete(); + } + + + @Test + void searchInventories_withDescriptionNotExisting_shouldThrowError() { + Pageable page = PageRequest.of(0, 5); + String description = "NonExistingDescription"; + + when(inventoryRepository.findByInventoryDescriptionRegex(anyString())) + .thenReturn(Flux.empty()); // No inventory found + + Flux result =productInventoryService.searchInventories(page, null, null, description); + + StepVerifier.create(result) + .expectError(NotFoundException.class) + .verify(); + } + @Test + void getProductsByInventoryIdAndProductName_withValidFields_shouldSucceed(){ + String inventoryId = "1"; + String productName = "B"; + String regexPattern = "(?i)^" + Pattern.quote(productName) + ".*"; + + when(productRepository + .findAllProductsByInventoryIdAndProductNameRegex( + inventoryId, + regexPattern)) + .thenReturn(Flux.just(product)); + + Flux productResponseDTOMono = productInventoryService + .getProductsInInventoryByInventoryIdAndProductsField( + inventoryId, + productName, + null, + null); + + StepVerifier + .create(productResponseDTOMono) + .expectNextCount(1) + .verifyComplete(); + } + + @Test + void getAllProductsByInventoryId_withValidFields_shouldSucceed(){ + String inventoryId = "1"; + + when(productRepository + .findAllProductsByInventoryId(inventoryId)) + .thenReturn(Flux.just(product)); + + Flux productResponseDTOMono = productInventoryService + .getProductsInInventoryByInventoryIdAndProductsField( + inventoryId, + null, + null, + null); + + StepVerifier + .create(productResponseDTOMono) + .expectNextCount(1) + .verifyComplete(); + } } + + + + diff --git a/inventory-service/src/test/java/com/petclinic/inventoryservice/datalayer/Inventory/InventoryRepositoryTest.java b/inventory-service/src/test/java/com/petclinic/inventoryservice/datalayer/Inventory/InventoryRepositoryTest.java index 0a0ab1bb27..e732d1b58e 100644 --- a/inventory-service/src/test/java/com/petclinic/inventoryservice/datalayer/Inventory/InventoryRepositoryTest.java +++ b/inventory-service/src/test/java/com/petclinic/inventoryservice/datalayer/Inventory/InventoryRepositoryTest.java @@ -5,6 +5,7 @@ import org.reactivestreams.Publisher; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.data.mongo.DataMongoTest; +import reactor.core.publisher.Flux; import reactor.test.StepVerifier; import java.util.UUID; @@ -104,6 +105,40 @@ public void shouldFindInventoryByName() { + @Test + public void shouldFindInventoryByNameRegex() { + // Arrange + Inventory inventory = buildInventory("inventoryId_4", "Benzodiazepines", "Internal", "SomeDescription"); + inventoryRepository.save(inventory).block(); + + String regexPattern = "(?i)^B.*"; // Just for this example since we know the name starts with 'B' + + // Act & Assert + StepVerifier + .create(inventoryRepository.findByInventoryNameRegex(regexPattern)) + .expectNextMatches(result -> result.getInventoryId().equals("inventoryId_4")) + .verifyComplete(); + } + + @Test + public void shouldFindInventoryByDescriptionRegex() { + // Arrange + Inventory inventory = buildInventory("inventoryId_5", "SomeName", "Internal", "Medication"); + inventoryRepository.save(inventory).block(); + + String regexPattern = "(?i)^M.*"; // Since we know the description starts with 'M' + + // Act & Assert + StepVerifier + .create(inventoryRepository.findByInventoryDescriptionRegex(regexPattern)) + .expectNextMatches(result -> result.getInventoryId().equals("inventoryId_5")) + .verifyComplete(); + } + + + + + private Inventory buildInventory(String inventoryId, String inventoryName, String inventoryType, String inventoryDescription) { return Inventory.builder() .inventoryName(inventoryName) diff --git a/inventory-service/src/test/java/com/petclinic/inventoryservice/datalayer/Product/ProductRepositoryTest.java b/inventory-service/src/test/java/com/petclinic/inventoryservice/datalayer/Product/ProductRepositoryTest.java index b89107e599..d97e108d1d 100644 --- a/inventory-service/src/test/java/com/petclinic/inventoryservice/datalayer/Product/ProductRepositoryTest.java +++ b/inventory-service/src/test/java/com/petclinic/inventoryservice/datalayer/Product/ProductRepositoryTest.java @@ -150,6 +150,55 @@ public void testFindProductByProductId() { }) .verifyComplete(); } + @Test + public void shouldFindProductByInventoryIdAndOneCharNameRegex() { + + String regexPattern = "(?i)^n.*"; + Product product = buildProduct("inventoryId_1", "productId_1", "name", "Desc", 100.00, 10, 15.99); + productRepository.save(product).block(); + + // Act & Assert + StepVerifier + .create(productRepository.findAllProductsByInventoryIdAndProductNameRegex("inventoryId_1", regexPattern)) + .expectNextMatches(result -> result.getProductId().equals("productId_1")) + .verifyComplete(); + } + @Test + public void shouldFindProductByInventoryIdAndFullNameRegex() { + + String regexPattern = "(?i)^name.*"; + Product product = buildProduct("inventoryId_2", "productId_2", "name", "Desc", 100.00, 10, 15.99); + productRepository.save(product).block(); + + + StepVerifier + .create(productRepository.findAllProductsByInventoryIdAndProductNameRegex("inventoryId_2", regexPattern)) + .expectNextMatches(result -> result.getProductId().equals("productId_2")) + .verifyComplete(); + } + @Test + public void shouldFindProductByInventoryId() { + // Arrange + Product product = buildProduct("inventoryId_3", "productId_3", "AnotherName", "Desc", 100.00, 10, 15.99); + productRepository.save(product).block(); + + // Act & Assert + StepVerifier + .create(productRepository.findAllProductsByInventoryId("inventoryId_3")) + .expectNextMatches(result -> result.getProductId().equals("productId_3")) + .verifyComplete(); + } + @Test + public void shouldNotFindProductByInvalidInventoryId() { + // No products saved for this test + + // Act & Assert + StepVerifier + .create(productRepository.findAllProductsByInventoryId("non_existent_id")) + .expectNextCount(0) + .verifyComplete(); + } + private Product buildProduct(String inventoryId, String productId, String productName, String productDescription, Double productPrice, Integer productQuantity, Double productSalePrice) { return Product.builder() diff --git a/inventory-service/src/test/java/com/petclinic/inventoryservice/presentationlayer/InventoryControllerIntegrationTest.java b/inventory-service/src/test/java/com/petclinic/inventoryservice/presentationlayer/InventoryControllerIntegrationTest.java index f43b388977..4df45317b0 100644 --- a/inventory-service/src/test/java/com/petclinic/inventoryservice/presentationlayer/InventoryControllerIntegrationTest.java +++ b/inventory-service/src/test/java/com/petclinic/inventoryservice/presentationlayer/InventoryControllerIntegrationTest.java @@ -886,6 +886,14 @@ public void getAllInventoryTypes_shouldSucceed() { .expectStatus().isOk() .expectHeader().contentType(MediaType.APPLICATION_JSON); } + + //search Products by name + + + + + + /* @Test public void deleteInventoryByInventoryId_withNotFoundInventoryId_shouldNotFound() { diff --git a/inventory-service/src/test/java/com/petclinic/inventoryservice/presentationlayer/InventoryControllerUnitTest.java b/inventory-service/src/test/java/com/petclinic/inventoryservice/presentationlayer/InventoryControllerUnitTest.java index 4a38c14da5..d0153f1051 100644 --- a/inventory-service/src/test/java/com/petclinic/inventoryservice/presentationlayer/InventoryControllerUnitTest.java +++ b/inventory-service/src/test/java/com/petclinic/inventoryservice/presentationlayer/InventoryControllerUnitTest.java @@ -4,9 +4,12 @@ import com.petclinic.inventoryservice.utils.exceptions.InvalidInputException; import com.petclinic.inventoryservice.utils.exceptions.NotFoundException; import org.junit.jupiter.api.Test; +import org.mockito.Mockito; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.reactive.WebFluxTest; import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.data.domain.PageRequest; +import org.springframework.data.domain.Pageable; import org.springframework.http.MediaType; import org.springframework.test.web.reactive.server.WebTestClient; import reactor.core.publisher.Flux; @@ -758,8 +761,136 @@ public void getAllInventoryTypes_shouldSucceed(){ assertEquals(typesDTOS.size(), responseDTOs.size()); }); } + @Test + void searchInventories_WithNameOnly_ShouldReturnMatchingInventories() { + // Arrange + Pageable page = PageRequest.of(0, 10); + String inventoryName = "SampleName"; + + InventoryResponseDTO sampleResponse = new InventoryResponseDTO(); + + when(productInventoryService.searchInventories(page, inventoryName, null, null)) + .thenReturn(Flux.just(sampleResponse)); + + // Act and Assert + webTestClient + .get() + .uri(uriBuilder -> uriBuilder.path("/inventory") + .queryParam("inventoryName", inventoryName) + .build()) + .exchange() + .expectStatus().isOk() + .expectBodyList(InventoryResponseDTO.class) + .hasSize(1) + .contains(sampleResponse); + } + @Test + void searchInventories_WithOnlyType_ShouldReturnMatchingInventories() { + // Arrange + Pageable page = PageRequest.of(0, 10); + String inventoryType = "SampleType"; + InventoryResponseDTO sampleResponse = new InventoryResponseDTO(); + + when(productInventoryService.searchInventories(page, null, inventoryType, null)) + .thenReturn(Flux.just(sampleResponse)); + + // Act and Assert + webTestClient + .get() + .uri(uriBuilder -> uriBuilder.path("/inventory") + .queryParam("inventoryType", inventoryType) + .build()) + .exchange() + .expectStatus().isOk() + .expectBodyList(InventoryResponseDTO.class) + .hasSize(1) + .contains(sampleResponse); + } + + @Test + void searchInventories_WithOnlyDescription_ShouldReturnMatchingInventories() { + // Arrange + Pageable page = PageRequest.of(0, 10); + String inventoryDescription = "SampleDescription"; + InventoryResponseDTO sampleResponse = new InventoryResponseDTO(); + + when(productInventoryService.searchInventories(page, null, null, inventoryDescription)) + .thenReturn(Flux.just(sampleResponse)); + + // Act and Assert + webTestClient + .get() + .uri(uriBuilder -> uriBuilder.path("/inventory") + .queryParam("inventoryDescription", inventoryDescription) + .build()) + .exchange() + .expectStatus().isOk() + .expectBodyList(InventoryResponseDTO.class) + .hasSize(1) + .contains(sampleResponse); + } + @Test + void searchInventories_WithNoParams_ShouldReturnAllInventories() { + // Arrange + Pageable page = PageRequest.of(0, 10); + InventoryResponseDTO sampleResponse = new InventoryResponseDTO(); + + when(productInventoryService.searchInventories(page, null, null, null)) + .thenReturn(Flux.just(sampleResponse)); + + // Act and Assert + webTestClient + .get() + .uri("/inventory") + .exchange() + .expectStatus().isOk() + .expectBodyList(InventoryResponseDTO.class) + .contains(sampleResponse); + } + @Test + void getProductsByInventoryIdAndProductName_withValidFields_shouldSucceed() { + String inventoryId = "1"; + String productName = "B"; + Mockito.when(productInventoryService.getProductsInInventoryByInventoryIdAndProductsField( + anyString(), + anyString(), + Mockito.isNull(), + Mockito.isNull() + )).thenReturn(Flux.fromIterable(productResponseDTOS)); + + webTestClient.get() + .uri(uriBuilder -> uriBuilder + .path("/inventory/{inventoryId}/products") + .queryParam("productName", productName) + .build(inventoryId)) + .accept(MediaType.APPLICATION_JSON) + .exchange() + .expectStatus().isOk() + .expectBodyList(ProductResponseDTO.class) + .hasSize(2); + } + @Test + void getAllProductsByInventoryId_withValidFields_shouldSucceed() { + String inventoryId = "1"; + Mockito.when(productInventoryService.getProductsInInventoryByInventoryIdAndProductsField( + anyString(), + Mockito.isNull(), + Mockito.isNull(), + Mockito.isNull() + )).thenReturn(Flux.fromIterable(productResponseDTOS)); + webTestClient.get() + .uri("/inventory/{inventoryId}/products", inventoryId) + .accept(MediaType.APPLICATION_JSON) + .exchange() + .expectStatus().isOk() + .expectBodyList(ProductResponseDTO.class) + .hasSize(2); + } } + + +