From b539556ed93f19834c749923082e6a88ecacadea Mon Sep 17 00:00:00 2001 From: Raja Kolli Date: Tue, 5 Sep 2023 02:37:17 +0000 Subject: [PATCH] feat: reduce transfering extra data from database --- .../repositories/OrderRepository.java | 5 ++ .../orderservice/services/OrderService.java | 8 +-- .../repositories/OrderRepositoryTest.java | 66 +++++++++++++++++++ 3 files changed, 74 insertions(+), 5 deletions(-) create mode 100644 order-service/src/test/java/com/example/orderservice/repositories/OrderRepositoryTest.java diff --git a/order-service/src/main/java/com/example/orderservice/repositories/OrderRepository.java b/order-service/src/main/java/com/example/orderservice/repositories/OrderRepository.java index 5f165d5c..8a5f38f6 100644 --- a/order-service/src/main/java/com/example/orderservice/repositories/OrderRepository.java +++ b/order-service/src/main/java/com/example/orderservice/repositories/OrderRepository.java @@ -9,6 +9,8 @@ Licensed under MIT License Copyright (c) 2021-2023 Raja Kolli. import com.example.orderservice.entities.Order; import java.util.List; import java.util.Optional; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.EntityGraph; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Modifying; @@ -32,4 +34,7 @@ int updateOrderStatusAndSourceById( @Param("id") Long orderId, @Param("status") String status, @Param("source") String source); + + @Query("select o.id from Order o") + Page findAllOrders(Pageable pageable); } diff --git a/order-service/src/main/java/com/example/orderservice/services/OrderService.java b/order-service/src/main/java/com/example/orderservice/services/OrderService.java index 16fe567a..29058573 100644 --- a/order-service/src/main/java/com/example/orderservice/services/OrderService.java +++ b/order-service/src/main/java/com/example/orderservice/services/OrderService.java @@ -47,12 +47,10 @@ public PagedResult findAllOrders( // create Pageable instance Pageable pageable = PageRequest.of(pageNo, pageSize, sort); - // Fetches only ParentEntities - Page page = orderRepository.findAll(pageable); - // Get orderIds matching pagination - List orderIds = page.getContent().stream().map(Order::getId).toList(); + // Fetches only ParentEntities ids + Page page = orderRepository.findAllOrders(pageable); // fetching parentAlongWithChildEntries - List ordersWithOrderItems = orderRepository.findByIdIn(orderIds); + List ordersWithOrderItems = orderRepository.findByIdIn(page.getContent()); // Mapping Order to OrderDTO CompletableFuture List> completableFutureList = ordersWithOrderItems.stream() diff --git a/order-service/src/test/java/com/example/orderservice/repositories/OrderRepositoryTest.java b/order-service/src/test/java/com/example/orderservice/repositories/OrderRepositoryTest.java new file mode 100644 index 00000000..2d603c44 --- /dev/null +++ b/order-service/src/test/java/com/example/orderservice/repositories/OrderRepositoryTest.java @@ -0,0 +1,66 @@ +/*** +

+ Licensed under MIT License Copyright (c) 2023 Raja Kolli. +

+***/ + +package com.example.orderservice.repositories; + +import static org.assertj.core.api.Assertions.assertThat; + +import com.example.orderservice.common.PostGreSQLContainer; +import com.example.orderservice.entities.Order; +import com.example.orderservice.util.TestData; +import java.util.ArrayList; +import java.util.List; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase; +import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; +import org.springframework.boot.testcontainers.context.ImportTestcontainers; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageRequest; +import org.springframework.data.domain.Pageable; +import org.springframework.data.domain.Sort; + +@ImportTestcontainers(PostGreSQLContainer.class) +@DataJpaTest(properties = "application.catalogServiceUrl=http://dummy") +@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE) +class OrderRepositoryTest { + + @Autowired private OrderRepository orderRepository; + @Autowired private OrderItemRepository orderItemRepository; + + @AfterEach + void tearDown() { + this.orderItemRepository.deleteAllInBatch(); + this.orderRepository.deleteAllInBatch(); + } + + @Test + void findAllOrders() { + + List orderList = new ArrayList<>(); + for (int i = 0; i <= 15; i++) { + orderList.add(TestData.getOrder()); + } + this.orderRepository.saveAll(orderList); + + // create Pageable instance + Pageable pageable = PageRequest.of(1, 5, Sort.by("id").ascending()); + + Page page = this.orderRepository.findAllOrders(pageable); + + assertThat(page).isNotNull(); + assertThat(page.getTotalElements()).isEqualTo(16); + assertThat(page.getTotalPages()).isEqualTo(4); + assertThat(page.getNumber()).isEqualTo(1); + assertThat(page.isFirst()).isEqualTo(false); + assertThat(page.isLast()).isEqualTo(false); + assertThat(page.hasNext()).isEqualTo(true); + assertThat(page.hasPrevious()).isEqualTo(true); + assertThat(page.getNumberOfElements()).isEqualTo(5); + assertThat(page.getContent()).isNotEmpty().hasSize(5); + } +}