Skip to content

Commit

Permalink
Merge branch 'main' into feat/VETS-CPC-935_Display_Image_Next_to_Vet_…
Browse files Browse the repository at this point in the history
…Profile_Picture_Based_on_Average_Rating_of_Vet
  • Loading branch information
liu-cal authored Oct 9, 2023
2 parents 8c21336 + ed3cecf commit 495eac4
Show file tree
Hide file tree
Showing 5 changed files with 386 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<InventoryResponseDTO> 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<InventoryResponseDTO> 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<InventoryResponseDTO> 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<InventoryResponseDTO> 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<InventoryResponseDTO> 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<InventoryResponseDTO> 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<InventoryResponseDTO> 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<ProductResponseDTO> 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<ProductResponseDTO> productResponseDTOMono = productInventoryService
.getProductsInInventoryByInventoryIdAndProductsField(
inventoryId,
null,
null,
null);

StepVerifier
.create(productResponseDTOMono)
.expectNextCount(1)
.verifyComplete();
}


}




Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Loading

0 comments on commit 495eac4

Please sign in to comment.