|
| 1 | +package com.example.analytics_service.unit_tests; |
| 2 | + |
| 3 | +import com.example.analytics_service.entity.ProductView; |
| 4 | +import com.example.analytics_service.events.ProductEvent; |
| 5 | +import org.junit.jupiter.api.DisplayName; |
| 6 | +import org.junit.jupiter.api.Test; |
| 7 | +import org.mockito.Mockito; |
| 8 | +import reactor.core.publisher.Flux; |
| 9 | +import reactor.core.publisher.Mono; |
| 10 | +import reactor.test.StepVerifier; |
| 11 | + |
| 12 | +public class ProductViewServiceTests extends AbstractUnitTest{ |
| 13 | + |
| 14 | + @Test |
| 15 | + @DisplayName("Test findAll") |
| 16 | + void testFindAll_whenInvoked_ThenReturnFluxOfProducts() { |
| 17 | + //Arrange |
| 18 | + var flux = Flux.just( |
| 19 | + ProductView.builder().viewCount(25L).productCode("P111").build(), |
| 20 | + ProductView.builder().viewCount(15L).productCode("P151").build(), |
| 21 | + ProductView.builder().viewCount(5L).productCode("P113").build() |
| 22 | + ); |
| 23 | + |
| 24 | + Mockito.when(repo.findTop5ByOrderByViewCountDesc()).thenReturn(flux); |
| 25 | + |
| 26 | + //Act and Assert |
| 27 | + |
| 28 | + service.products() |
| 29 | + .collectList() |
| 30 | + .as(StepVerifier::create) |
| 31 | + .expectNextMatches(list -> list.size() == 3) |
| 32 | + .verifyComplete(); |
| 33 | + } |
| 34 | + |
| 35 | + @Test |
| 36 | + @DisplayName("Test consume() for new product") |
| 37 | + void testConsume_WhenNewProductIsConsumed_ThenItsViewCountInitializedAtOne(){ |
| 38 | + //Arrange |
| 39 | + var event = ProductEvent.View.builder().code("P111").build(); |
| 40 | + var expectedProductView = Mono.fromSupplier(() -> |
| 41 | + ProductView.builder().viewCount(1L).productCode("P111").build()); |
| 42 | + |
| 43 | + Mockito.when(repo.findByProductCode(Mockito.anyString())).thenReturn(Mono.empty()); |
| 44 | + Mockito.when(repo.save(Mockito.any(ProductView.class))).thenReturn(expectedProductView); |
| 45 | + |
| 46 | + //Act |
| 47 | + service.consume().apply(event) |
| 48 | + .as(StepVerifier::create) |
| 49 | + .verifyComplete(); |
| 50 | + |
| 51 | + //Assert |
| 52 | + Mockito.verify(repo, Mockito.times(1)).save(Mockito.argThat( |
| 53 | + x-> x.getViewCount() == 1 && x.getProductCode().equals("P111"))); |
| 54 | + |
| 55 | + } |
| 56 | + |
| 57 | + |
| 58 | + @Test |
| 59 | + @DisplayName("Test consume() for existing product") |
| 60 | + void testConsume_WhenExistingProductIsConsumed_ThenItsViewCountIncremented(){ |
| 61 | + //Arrange |
| 62 | + var event = ProductEvent.View.builder().code("P111").build(); |
| 63 | + |
| 64 | + var existingProductView = Mono.fromSupplier(() -> |
| 65 | + ProductView.builder().viewCount(17L).productCode("P111").build()); |
| 66 | + var expectedProductView = Mono.fromSupplier(() -> |
| 67 | + ProductView.builder().viewCount(18L).productCode("P111").build()); |
| 68 | + |
| 69 | + Mockito.when(repo.findByProductCode(Mockito.anyString())).thenReturn(existingProductView); |
| 70 | + Mockito.when(repo.save(Mockito.any(ProductView.class))).thenReturn(expectedProductView); |
| 71 | + |
| 72 | + //Act |
| 73 | + service.consume().apply(event) |
| 74 | + .as(StepVerifier::create) |
| 75 | + .verifyComplete(); |
| 76 | + |
| 77 | + //Assert |
| 78 | + Mockito.verify(repo, Mockito.times(1)).save(Mockito.argThat( |
| 79 | + x-> x.getViewCount() == 18L && x.getProductCode().equals("P111"))); |
| 80 | + |
| 81 | + } |
| 82 | +} |
0 commit comments