From 20e163e9fd1446fe95e5c6177f975d26abf7611a Mon Sep 17 00:00:00 2001 From: nashtech-tuannguyenhuu1 <105196741+nashtech-tuannguyenhuu1@users.noreply.github.com> Date: Tue, 8 Oct 2024 17:26:58 +0700 Subject: [PATCH] #1128 [BACKOFFICE] [Bugs] Add some boxes in the homepage of the backoffice (#1138) --- .../com/yas/order/service/OrderServiceIT.java | 28 ++++- order/src/it/resources/application.properties | 3 +- .../yas/order/controller/OrderController.java | 6 + .../yas/order/repository/OrderRepository.java | 3 + .../com/yas/order/service/OrderService.java | 18 +++ .../order/controller/OrderControllerTest.java | 15 +++ .../yas/product/service/ProductServiceIT.java | 56 ++++++++- .../src/it/resources/application.properties | 1 - .../product/controller/ProductController.java | 7 ++ .../product/repository/ProductRepository.java | 3 + .../yas/product/service/ProductService.java | 18 +++ .../controller/ProductControllerTest.java | 26 +++- .../rating/controller/RatingController.java | 6 + .../rating/repository/RatingRepository.java | 3 + .../com/yas/rating/service/RatingService.java | 19 +++ .../controller/RatingControllerTest.java | 45 ++++--- .../yas/rating/service/RatingServiceTest.java | 113 ++++++++++++------ 17 files changed, 308 insertions(+), 62 deletions(-) diff --git a/order/src/it/java/com/yas/order/service/OrderServiceIT.java b/order/src/it/java/com/yas/order/service/OrderServiceIT.java index 097bf14335..ccf84409b2 100644 --- a/order/src/it/java/com/yas/order/service/OrderServiceIT.java +++ b/order/src/it/java/com/yas/order/service/OrderServiceIT.java @@ -6,14 +6,15 @@ import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; +import com.yas.commonlibrary.IntegrationTestConfiguration; import com.yas.commonlibrary.exception.NotFoundException; import com.yas.order.OrderApplication; -import com.yas.commonlibrary.IntegrationTestConfiguration; import com.yas.order.model.Order; import com.yas.order.model.enumeration.OrderStatus; import com.yas.order.model.enumeration.PaymentStatus; import com.yas.order.repository.OrderItemRepository; import com.yas.order.repository.OrderRepository; +import com.yas.order.viewmodel.order.OrderBriefVm; import com.yas.order.viewmodel.order.OrderItemPostVm; import com.yas.order.viewmodel.order.OrderListVm; import com.yas.order.viewmodel.order.OrderPostVm; @@ -281,4 +282,29 @@ void testAcceptOrder_whenNotFound_throwNotFoundException() { } + @Test + void testGetLatestOrders_WhenHasListOrderListVm_returnListOrderListVm() { + orderService.createOrder(orderPostVm); + List orderList = orderService.getLatestOrders(1); + assertNotNull(orderList); + } + + @Test + void testGetLatestOrders_WhenCountLessThen1_returnEmpty() { + List newResponse = orderService.getLatestOrders(-1); + assertEquals(0, newResponse.size()); + } + + @Test + void testGetLatestOrders_WhenCountIs0_returnEmpty() { + List newResponse = orderService.getLatestOrders(0); + assertEquals(0, newResponse.size()); + } + + @Test + void testGetLatestOrders_WhenProductsEmpty_returnEmpty() { + List newResponse = orderService.getLatestOrders(5); + assertEquals(0, newResponse.size()); + } + } diff --git a/order/src/it/resources/application.properties b/order/src/it/resources/application.properties index 85b7e61a07..e05e9ca4c5 100644 --- a/order/src/it/resources/application.properties +++ b/order/src/it/resources/application.properties @@ -10,4 +10,5 @@ spring.liquibase.enabled=false spring.security.oauth2.resourceserver.jwt.issuer-uri=test springdoc.oauthflow.authorization-url=test -springdoc.oauthflow.token-url=test \ No newline at end of file +springdoc.oauthflow.token-url=test +spring.jpa.open-in-view=true \ No newline at end of file diff --git a/order/src/main/java/com/yas/order/controller/OrderController.java b/order/src/main/java/com/yas/order/controller/OrderController.java index d0bcf07257..34672dc54b 100644 --- a/order/src/main/java/com/yas/order/controller/OrderController.java +++ b/order/src/main/java/com/yas/order/controller/OrderController.java @@ -2,6 +2,7 @@ import com.yas.order.model.enumeration.OrderStatus; import com.yas.order.service.OrderService; +import com.yas.order.viewmodel.order.OrderBriefVm; import com.yas.order.viewmodel.order.OrderExistsByProductAndUserGetVm; import com.yas.order.viewmodel.order.OrderGetVm; import com.yas.order.viewmodel.order.OrderListVm; @@ -85,4 +86,9 @@ public ResponseEntity getOrders( pageNo, pageSize)); } + + @GetMapping("/backoffice/orders/latest/{count}") + public ResponseEntity> getLatestOrders(@PathVariable int count) { + return ResponseEntity.ok(orderService.getLatestOrders(count)); + } } diff --git a/order/src/main/java/com/yas/order/repository/OrderRepository.java b/order/src/main/java/com/yas/order/repository/OrderRepository.java index 21aac53454..2972cea7f2 100644 --- a/order/src/main/java/com/yas/order/repository/OrderRepository.java +++ b/order/src/main/java/com/yas/order/repository/OrderRepository.java @@ -52,4 +52,7 @@ Page findOrderByWithMulCriteria( Pageable pageable); Optional findByCheckoutId(String checkoutId); + + @Query("SELECT o FROM Order o ORDER BY o.createdOn DESC") + List getLatestOrders(Pageable pageable); } diff --git a/order/src/main/java/com/yas/order/service/OrderService.java b/order/src/main/java/com/yas/order/service/OrderService.java index 001992ceb4..c5a6720ffd 100644 --- a/order/src/main/java/com/yas/order/service/OrderService.java +++ b/order/src/main/java/com/yas/order/service/OrderService.java @@ -163,6 +163,24 @@ public OrderListVm getAllOrder(ZonedDateTime createdFrom, return new OrderListVm(orderVms, orderPage.getTotalElements(), orderPage.getTotalPages()); } + public List getLatestOrders(int count) { + + if (count <= 0) { + return List.of(); + } + + Pageable pageable = PageRequest.of(0, count); + List orders = orderRepository.getLatestOrders(pageable); + + if (CollectionUtils.isEmpty(orders)) { + return List.of(); + } + + return orders.stream() + .map(OrderBriefVm::fromModel) + .toList(); + } + public OrderExistsByProductAndUserGetVm isOrderCompletedWithUserIdAndProductId(final Long productId) { String userId = AuthenticationUtils.getCurrentUserId(); diff --git a/order/src/test/java/com/yas/order/controller/OrderControllerTest.java b/order/src/test/java/com/yas/order/controller/OrderControllerTest.java index 51da0746b4..0e31edac80 100644 --- a/order/src/test/java/com/yas/order/controller/OrderControllerTest.java +++ b/order/src/test/java/com/yas/order/controller/OrderControllerTest.java @@ -19,6 +19,7 @@ import com.yas.order.model.enumeration.PaymentMethod; import com.yas.order.model.enumeration.PaymentStatus; import com.yas.order.service.OrderService; +import com.yas.order.viewmodel.order.OrderBriefVm; import com.yas.order.viewmodel.order.OrderExistsByProductAndUserGetVm; import com.yas.order.viewmodel.order.OrderGetVm; import com.yas.order.viewmodel.order.OrderItemPostVm; @@ -31,6 +32,7 @@ import com.yas.order.viewmodel.orderaddress.OrderAddressVm; import java.math.BigDecimal; import java.time.ZonedDateTime; +import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Set; @@ -205,6 +207,19 @@ void testGetOrders_whenRequestIsValid_thenReturnOrderListVm() throws Exception { .json(objectWriter.writeValueAsString(orderListVm))); } + @Test + void testGetLatestOrders_whenRequestIsValid_thenReturnOrderListVm() throws Exception { + + List list = new ArrayList<>(); + when(orderService.getLatestOrders(1)).thenReturn(list); + + mockMvc.perform(get("/backoffice/orders/latest/1") + .accept(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()) + .andExpect(MockMvcResultMatchers.content() + .json(objectWriter.writeValueAsString(list))); + } + private OrderVm getOrderVm() { OrderAddressVm shippingAddress = new OrderAddressVm( diff --git a/product/src/it/java/com/yas/product/service/ProductServiceIT.java b/product/src/it/java/com/yas/product/service/ProductServiceIT.java index 531f6fe268..e8921fcdab 100644 --- a/product/src/it/java/com/yas/product/service/ProductServiceIT.java +++ b/product/src/it/java/com/yas/product/service/ProductServiceIT.java @@ -31,6 +31,7 @@ import com.yas.product.viewmodel.product.ProductThumbnailGetVm; import com.yas.product.viewmodel.product.ProductThumbnailVm; import com.yas.product.viewmodel.product.ProductsGetVm; +import java.time.ZonedDateTime; import java.util.ArrayList; import java.util.List; import java.util.Map; @@ -50,7 +51,7 @@ @Import(IntegrationTestConfiguration.class) @AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE) class ProductServiceIT { - + private final ZonedDateTime CREATED_ON = ZonedDateTime.now(); @Autowired private ProductRepository productRepository; @Autowired @@ -130,6 +131,7 @@ private void generateTestData() { product.setBrand(brand1); product.setPrice(5.0); } + product.setCreatedOn(CREATED_ON.minusDays(i)); products.add(product); } productRepository.saveAll(products); @@ -360,8 +362,8 @@ void getProductsByMultiQuery_WhenFilterByBrandNameAndProductName_ThenSuccess() { Double startPrice = 1.0; Double endPrice = 10.0; String productName = "product2"; - ProductsGetVm result - = productService.getProductsByMultiQuery(pageNo, pageSize, productName, category2.getSlug(), startPrice, endPrice); + ProductsGetVm result = productService.getProductsByMultiQuery(pageNo, + pageSize, productName, category2.getSlug(), startPrice, endPrice); // Assert result assertEquals(1, result.productContent().size()); @@ -391,4 +393,52 @@ void getProductsByBrandIds_WhenFindAllByBrandIds_ThenSuccess() { assertEquals("product1", actualResponse.getFirst().name()); assertEquals("slug1", actualResponse.getFirst().slug()); } + + + @Test + void testGetLatestProducts_WhenHasListProductListVm_returnListProductListVm() { + + List actualResponse = productRepository.findAll(); + + assertEquals(10, actualResponse.size()); + + actualResponse.getFirst().setCreatedOn(CREATED_ON.minusDays(1)); + actualResponse.get(1).setCreatedOn(CREATED_ON.minusDays(2)); + actualResponse.get(2).setCreatedOn(CREATED_ON.minusDays(3)); + actualResponse.get(3).setCreatedOn(CREATED_ON.minusDays(4)); + actualResponse.get(4).setCreatedOn(CREATED_ON.minusDays(5)); + actualResponse.get(5).setCreatedOn(CREATED_ON.minusDays(6)); + actualResponse.get(6).setCreatedOn(CREATED_ON.minusDays(7)); + actualResponse.get(7).setCreatedOn(CREATED_ON.minusDays(8)); + actualResponse.get(8).setCreatedOn(CREATED_ON.minusDays(9)); + productRepository.saveAll(actualResponse); + + List newResponse = productService.getLatestProducts(5); + assertEquals(5, newResponse.size()); + assertEquals("product10", newResponse.getFirst().name()); + assertEquals("product1", newResponse.get(1).name()); + assertEquals("product2", newResponse.get(2).name()); + assertEquals("product3", newResponse.get(3).name()); + assertEquals("product4", newResponse.get(4).name()); + } + + @Test + void testGetLatestProducts_WhenCountLessThen1_returnEmpty() { + List newResponse = productService.getLatestProducts(-1); + assertEquals(0, newResponse.size()); + } + + @Test + void testGetLatestProducts_WhenCountIs0_returnEmpty() { + List newResponse = productService.getLatestProducts(0); + assertEquals(0, newResponse.size()); + } + + @Test + void testGetLatestProducts_WhenProductsEmpty_returnEmpty() { + tearDown(); + List newResponse = productService.getLatestProducts(5); + assertEquals(0, newResponse.size()); + } + } diff --git a/product/src/it/resources/application.properties b/product/src/it/resources/application.properties index f537761743..77b7298317 100644 --- a/product/src/it/resources/application.properties +++ b/product/src/it/resources/application.properties @@ -11,6 +11,5 @@ spring.liquibase.enabled=false spring.security.oauth2.resourceserver.jwt.issuer-uri=test springdoc.oauthflow.authorization-url=test springdoc.oauthflow.token-url=test -spring.jpa.open-in-view=true cors.allowed-origins=* \ No newline at end of file diff --git a/product/src/main/java/com/yas/product/controller/ProductController.java b/product/src/main/java/com/yas/product/controller/ProductController.java index 143d9b5e8a..1189fd70f3 100644 --- a/product/src/main/java/com/yas/product/controller/ProductController.java +++ b/product/src/main/java/com/yas/product/controller/ProductController.java @@ -265,4 +265,11 @@ public ResponseEntity> getProductByBrands( @RequestParam("ids") List brandIds) { return ResponseEntity.ok(productService.getProductByBrandIds(brandIds)); } + + @GetMapping("/backoffice/products/latest/{count}") + public ResponseEntity> getLatestProducts(@PathVariable int count) { + return ResponseEntity.ok(productService.getLatestProducts(count)); + } + + } diff --git a/product/src/main/java/com/yas/product/repository/ProductRepository.java b/product/src/main/java/com/yas/product/repository/ProductRepository.java index 6aa48e8c57..1cca987df3 100644 --- a/product/src/main/java/com/yas/product/repository/ProductRepository.java +++ b/product/src/main/java/com/yas/product/repository/ProductRepository.java @@ -74,4 +74,7 @@ List findProductForWarehouse(@Param("name") String name, @Param("sku") @Query("SELECT p FROM Product p JOIN p.brand b WHERE b.id IN :brandIds ORDER BY p.id ASC") List findByBrandIdsIn(@Param("brandIds") List brandIds); + + @Query("SELECT p FROM Product p ORDER BY p.createdOn DESC") + List getLatestProducts(Pageable pageable); } diff --git a/product/src/main/java/com/yas/product/service/ProductService.java b/product/src/main/java/com/yas/product/service/ProductService.java index a50d9c9ad7..396679ebd1 100644 --- a/product/src/main/java/com/yas/product/service/ProductService.java +++ b/product/src/main/java/com/yas/product/service/ProductService.java @@ -678,6 +678,24 @@ public ProductDetailVm getProductById(long productId) { ); } + public List getLatestProducts(int count) { + + if (count <= 0) { + return List.of(); + } + + Pageable pageable = PageRequest.of(0, count); + List products = productRepository.getLatestProducts(pageable); + + if (CollectionUtils.isEmpty(products)) { + return List.of(); + } + + return products.stream() + .map(ProductListVm::fromModel) + .toList(); + } + public List getProductsByBrand(String brandSlug) { List productThumbnailVms = new ArrayList<>(); Brand brand = brandRepository diff --git a/product/src/test/java/com/yas/product/controller/ProductControllerTest.java b/product/src/test/java/com/yas/product/controller/ProductControllerTest.java index db293acddf..1144c7ed71 100644 --- a/product/src/test/java/com/yas/product/controller/ProductControllerTest.java +++ b/product/src/test/java/com/yas/product/controller/ProductControllerTest.java @@ -15,6 +15,7 @@ import com.yas.product.viewmodel.product.ProductPutVm; import com.yas.product.viewmodel.product.ProductQuantityPutVm; import java.time.ZonedDateTime; +import java.util.List; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.beans.factory.annotation.Autowired; @@ -26,11 +27,8 @@ import org.springframework.test.context.junit.jupiter.SpringExtension; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; -import org.springframework.test.web.servlet.result.MockMvcResultMatchers; import org.testcontainers.shaded.com.fasterxml.jackson.databind.ObjectMapper; -import java.util.List; - @ExtendWith(SpringExtension.class) @WebMvcTest(controllers = ProductController.class) @ContextConfiguration(classes = ProductApplication.class) @@ -258,4 +256,26 @@ void testGetProductByBrands_Success() throws Exception { // Verify interaction with the service verify(productService, times(1)).getProductByBrandIds(anyList()); } + + @Test + void testGetLatestProducts_Success() throws Exception { + + List mockProductList = List.of( + new ProductListVm(3L, "Product 3", "product3", true, true, + false, true, 100.0, ZonedDateTime.now(), 1L), + new ProductListVm(4L, "Product 4", "product4", true, true, + false, true, 200.0, ZonedDateTime.now(), 1L) + ); + when(productService.getLatestProducts(1)).thenReturn(mockProductList); + + mockMvc.perform(MockMvcRequestBuilders.get("/backoffice/products/latest/1") + .contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()) + .andExpect(jsonPath("$[0].id").value(3L)) + .andExpect(jsonPath("$[0].name").value("Product 3")) + .andExpect(jsonPath("$[1].id").value(4L)) + .andExpect(jsonPath("$[1].name").value("Product 4")); + + verify(productService, times(1)).getLatestProducts(1); + } } diff --git a/rating/src/main/java/com/yas/rating/controller/RatingController.java b/rating/src/main/java/com/yas/rating/controller/RatingController.java index ac42f8a413..599c38e8bd 100644 --- a/rating/src/main/java/com/yas/rating/controller/RatingController.java +++ b/rating/src/main/java/com/yas/rating/controller/RatingController.java @@ -7,6 +7,7 @@ import com.yas.rating.viewmodel.ResponeStatusVm; import jakarta.validation.Valid; import java.time.ZonedDateTime; +import java.util.List; import org.springframework.format.annotation.DateTimeFormat; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.DeleteMapping; @@ -46,6 +47,11 @@ public ResponseEntity getRatingListWithFilter( pageNo, pageSize)); } + @GetMapping("/backoffice/ratings/latest/{count}") + public ResponseEntity> getLatestRatings(@PathVariable int count) { + return ResponseEntity.ok(ratingService.getLatestRatings(count)); + } + @DeleteMapping("/backoffice/ratings/{id}") public ResponseEntity deleteRating(@PathVariable Long id) { return ResponseEntity.ok(ratingService.deleteRating(id)); diff --git a/rating/src/main/java/com/yas/rating/repository/RatingRepository.java b/rating/src/main/java/com/yas/rating/repository/RatingRepository.java index 0dae72fb94..56d45608fe 100644 --- a/rating/src/main/java/com/yas/rating/repository/RatingRepository.java +++ b/rating/src/main/java/com/yas/rating/repository/RatingRepository.java @@ -31,4 +31,7 @@ Page getRatingListWithFilter( List getTotalStarsAndTotalRatings(@Param("productId") long productId); boolean existsByCreatedByAndProductId(String createdBy, Long productId); + + @Query("SELECT r FROM Rating r ORDER BY r.createdOn DESC") + List getLatestRatings(Pageable pageable); } diff --git a/rating/src/main/java/com/yas/rating/service/RatingService.java b/rating/src/main/java/com/yas/rating/service/RatingService.java index 701b2e42ae..f903f3002c 100644 --- a/rating/src/main/java/com/yas/rating/service/RatingService.java +++ b/rating/src/main/java/com/yas/rating/service/RatingService.java @@ -23,6 +23,7 @@ import org.springframework.http.HttpStatus; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import org.springframework.util.CollectionUtils; import org.springframework.util.ObjectUtils; @Slf4j @@ -72,6 +73,24 @@ public RatingListVm getRatingListWithFilter(String proName, String cusName, return new RatingListVm(ratingVmList, ratings.getTotalElements(), ratings.getTotalPages()); } + public List getLatestRatings(int count) { + + if (count <= 0) { + return List.of(); + } + + Pageable pageable = PageRequest.of(0, count); + List ratings = ratingRepository.getLatestRatings(pageable); + + if (CollectionUtils.isEmpty(ratings)) { + return List.of(); + } + + return ratings.stream() + .map(RatingVm::fromModel) + .toList(); + } + public RatingVm createRating(RatingPostVm ratingPostVm) { String userId = AuthenticationUtils.extractUserId(); diff --git a/rating/src/test/java/com/yas/rating/controller/RatingControllerTest.java b/rating/src/test/java/com/yas/rating/controller/RatingControllerTest.java index 2c1b20de88..935fd9aceb 100644 --- a/rating/src/test/java/com/yas/rating/controller/RatingControllerTest.java +++ b/rating/src/test/java/com/yas/rating/controller/RatingControllerTest.java @@ -1,5 +1,18 @@ package com.yas.rating.controller; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyInt; +import static org.mockito.ArgumentMatchers.anyLong; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectWriter; import com.yas.commonlibrary.exception.NotFoundException; @@ -10,6 +23,11 @@ import com.yas.rating.viewmodel.RatingPostVm; import com.yas.rating.viewmodel.RatingVm; import com.yas.rating.viewmodel.ResponeStatusVm; +import java.time.LocalDate; +import java.time.LocalTime; +import java.time.ZoneId; +import java.time.ZonedDateTime; +import java.util.List; import org.hamcrest.Matchers; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -25,18 +43,6 @@ import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.result.MockMvcResultMatchers; -import java.time.LocalDate; -import java.time.LocalTime; -import java.time.ZoneId; -import java.time.ZonedDateTime; -import java.util.List; - -import static org.mockito.ArgumentMatchers.*; -import static org.mockito.Mockito.*; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; - @ExtendWith(SpringExtension.class) @WebMvcTest(controllers = RatingController.class) @ContextConfiguration(classes = RatingApplication.class) @@ -137,7 +143,7 @@ void testStorefrontGetRatingList_ShouldReturnList() throws Exception { } @Test - void TestCreateRating_WhenValid_ShouldSuccess() throws Exception { + void testCreateRating_WhenValid_ShouldSuccess() throws Exception { RatingPostVm vm = new RatingPostVm("rating1", 5, 1L, "product1"); when(ratingService.createRating(any(RatingPostVm.class))).thenReturn(ratingVm); @@ -151,7 +157,7 @@ void TestCreateRating_WhenValid_ShouldSuccess() throws Exception { } @Test - void TestGetAverageStarOfProduct_ShouldReturnSuccess() throws Exception { + void testGetAverageStarOfProduct_ShouldReturnSuccess() throws Exception { when(ratingService.calculateAverageStar(anyLong())).thenReturn(0.0D); this.mockMvc.perform(get("/storefront/ratings/product/{productId}/average-star", 1L) .contentType(MediaType.APPLICATION_JSON) @@ -160,4 +166,15 @@ void TestGetAverageStarOfProduct_ShouldReturnSuccess() throws Exception { .andExpect(content().string("0.0")); } + @Test + void testGetLatestRatings_WhenDataIsExisted_returnListRatingVm() throws Exception { + + when(ratingService.getLatestRatings(1)) + .thenReturn(List.of()); + + this.mockMvc.perform(get("/backoffice/ratings/latest/1") + .contentType(MediaType.APPLICATION_JSON) + ).andExpect(status().isOk()); + } + } diff --git a/rating/src/test/java/com/yas/rating/service/RatingServiceTest.java b/rating/src/test/java/com/yas/rating/service/RatingServiceTest.java index 9d245eea50..23fd4c868f 100644 --- a/rating/src/test/java/com/yas/rating/service/RatingServiceTest.java +++ b/rating/src/test/java/com/yas/rating/service/RatingServiceTest.java @@ -1,12 +1,26 @@ package com.yas.rating.service; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.ArgumentMatchers.anyLong; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + import com.yas.commonlibrary.exception.AccessDeniedException; import com.yas.commonlibrary.exception.NotFoundException; -import com.yas.rating.RatingApplication; import com.yas.commonlibrary.exception.ResourceExistedException; +import com.yas.rating.RatingApplication; import com.yas.rating.model.Rating; import com.yas.rating.repository.RatingRepository; -import com.yas.rating.viewmodel.*; +import com.yas.rating.viewmodel.CustomerVm; +import com.yas.rating.viewmodel.OrderExistsByProductAndUserGetVm; +import com.yas.rating.viewmodel.RatingListVm; +import com.yas.rating.viewmodel.RatingPostVm; +import com.yas.rating.viewmodel.RatingVm; +import java.time.ZonedDateTime; +import java.util.List; +import java.util.Optional; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -17,17 +31,6 @@ import org.springframework.security.oauth2.jwt.Jwt; import org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationToken; -import java.time.ZonedDateTime; -import java.util.List; -import java.util.Optional; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertThrows; -import static org.mockito.ArgumentMatchers.anyLong; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - @SpringBootTest(classes = RatingApplication.class) class RatingServiceTest { @@ -40,35 +43,34 @@ class RatingServiceTest { private OrderService orderService; @Autowired private RatingService ratingService; - private List ratingList; @BeforeEach void setUp() { - ratingList = List.of( - Rating.builder() - .content("comment 1") - .ratingStar(4) - .productId(1L) - .productName("product1") - .firstName("Duy") - .lastName("Nguyen") - .build(), - Rating.builder() - .content("comment 2") - .ratingStar(2) - .productId(1L) - .productName("product1") - .firstName("Hai") - .lastName("Le") - .build(), - Rating.builder() - .content("comment 3") - .ratingStar(3) - .productId(2L) - .productName("product2") - .firstName("Cuong") - .lastName("Tran") - .build() + List ratingList = List.of( + Rating.builder() + .content("comment 1") + .ratingStar(4) + .productId(1L) + .productName("product1") + .firstName("Duy") + .lastName("Nguyen") + .build(), + Rating.builder() + .content("comment 2") + .ratingStar(2) + .productId(1L) + .productName("product1") + .firstName("Hai") + .lastName("Le") + .build(), + Rating.builder() + .content("comment 3") + .ratingStar(3) + .productId(2L) + .productName("product2") + .firstName("Cuong") + .lastName("Tran") + .build() ); ratingRepository.saveAll(ratingList); } @@ -205,4 +207,37 @@ void calculateAverageStar_InvalidProductId_ShouldReturnZero() { Double averageStar = ratingService.calculateAverageStar(0L); assertEquals(0, averageStar); } + + @Test + void testGetLatestProducts_WhenHasListProductListVm_returnListProductListVm() { + List list = ratingRepository.findAll(); + list.getFirst().setCreatedOn(ZonedDateTime.now().minusDays(3)); + list.get(1).setCreatedOn(ZonedDateTime.now().minusDays(1)); + list.get(2).setCreatedOn(ZonedDateTime.now().minusDays(2)); + ratingRepository.saveAll(list); + + List ratingList = ratingService.getLatestRatings(2); + assertEquals(2, ratingList.size()); + assertEquals("comment 2", ratingList.getFirst().content()); + assertEquals("comment 3", ratingList.get(1).content()); + } + + @Test + void testGetLatestRatings_WhenCountLessThen1_returnEmpty() { + List newResponse = ratingService.getLatestRatings(-1); + assertEquals(0, newResponse.size()); + } + + @Test + void testGetLatestRatings_WhenCountIs0_returnEmpty() { + List newResponse = ratingService.getLatestRatings(0); + assertEquals(0, newResponse.size()); + } + + @Test + void testGetLatestRatings_WhenProductsEmpty_returnEmpty() { + ratingRepository.deleteAll(); + List newResponse = ratingService.getLatestRatings(5); + assertEquals(0, newResponse.size()); + } }