diff --git a/kbazaar/docker-compose.yml b/kbazaar/docker-compose.yml index 944c354..a7b47df 100644 --- a/kbazaar/docker-compose.yml +++ b/kbazaar/docker-compose.yml @@ -6,4 +6,4 @@ services: POSTGRES_PASSWORD: postgres POSTGRES_DB: postgres ports: - - '5432:5432' + - '5433:5432' diff --git a/kbazaar/src/main/java/com/kampus/kbazaar/product/ProductController.java b/kbazaar/src/main/java/com/kampus/kbazaar/product/ProductController.java index 254d004..7ec9c9c 100644 --- a/kbazaar/src/main/java/com/kampus/kbazaar/product/ProductController.java +++ b/kbazaar/src/main/java/com/kampus/kbazaar/product/ProductController.java @@ -5,12 +5,10 @@ import io.swagger.v3.oas.annotations.media.Content; import io.swagger.v3.oas.annotations.media.Schema; import io.swagger.v3.oas.annotations.responses.ApiResponse; -import java.util.List; - -import jakarta.validation.Valid; -import jakarta.validation.constraints.Digits; import jakarta.validation.constraints.Min; -import org.springframework.beans.factory.annotation.Autowired; +import java.util.List; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; @@ -70,16 +68,20 @@ public ProductResponse getProductById(@PathVariable String sku) { @GetMapping("/getProducts") public ResponseEntity getProductsWithPage( - @RequestParam(required = false, defaultValue = "10") @Min(value = 0,message = "Limit size must be positive number") int limit, - @RequestParam(required = false, defaultValue = "0") @Min(value = 0, message = "Page size must be positive number") int page) { - List productResponses = productService.listAllProductByPage(limit,page*limit); - ResponseMsg res = new ResponseMsg(); - res.setPage(page); - res.setLimit(limit); - res.setData(productResponses); - return ResponseEntity.ok(res); - } + @RequestParam(name = "limit", required = false, defaultValue = "10") + @Min(value = 0, message = "Limit size must be positive number") + int limit, + @RequestParam(name = "page", required = false, defaultValue = "0") + @Min(value = 0, message = "Page size must be positive number") + int page) { -} + List productResponses = + productService.listAllProductByPage(limit, page * limit); + HttpHeaders headers = new HttpHeaders(); + headers.set("page", Integer.toString(page)); + headers.set("limit", Integer.toString(limit)); + return new ResponseEntity<>(productResponses, headers, HttpStatus.OK); + } +} diff --git a/kbazaar/src/main/java/com/kampus/kbazaar/product/ProductRepository.java b/kbazaar/src/main/java/com/kampus/kbazaar/product/ProductRepository.java index 358ac34..8c062b4 100644 --- a/kbazaar/src/main/java/com/kampus/kbazaar/product/ProductRepository.java +++ b/kbazaar/src/main/java/com/kampus/kbazaar/product/ProductRepository.java @@ -12,5 +12,5 @@ public interface ProductRepository extends JpaRepository { Optional findBySku(String sku); @Query(value = "SELECT * FROM product LIMIT :limit OFFSET :page", nativeQuery = true) - List findAllByPage(int limit,int page); + List findAllByPage(int limit, int page); } diff --git a/kbazaar/src/main/java/com/kampus/kbazaar/product/ProductService.java b/kbazaar/src/main/java/com/kampus/kbazaar/product/ProductService.java index f6404cd..398bfd8 100644 --- a/kbazaar/src/main/java/com/kampus/kbazaar/product/ProductService.java +++ b/kbazaar/src/main/java/com/kampus/kbazaar/product/ProductService.java @@ -3,8 +3,6 @@ import com.kampus.kbazaar.exceptions.NotFoundException; import java.util.List; import java.util.Optional; - -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service @@ -29,8 +27,9 @@ public ProductResponse getBySku(String sku) { return product.get().toResponse(); } - public List listAllProductByPage(int limit,int page) { - return productRepository.findAllByPage(limit,page).stream().map(Product::toResponse).toList(); - + public List listAllProductByPage(int limit, int page) { + return productRepository.findAllByPage(limit, page).stream() + .map(Product::toResponse) + .toList(); } } diff --git a/kbazaar/src/main/java/com/kampus/kbazaar/product/ResponseMsg.java b/kbazaar/src/main/java/com/kampus/kbazaar/product/ResponseMsg.java index 9f00d7a..1d0578c 100644 --- a/kbazaar/src/main/java/com/kampus/kbazaar/product/ResponseMsg.java +++ b/kbazaar/src/main/java/com/kampus/kbazaar/product/ResponseMsg.java @@ -1,6 +1,5 @@ package com.kampus.kbazaar.product; -import lombok.AllArgsConstructor; import lombok.Getter; import lombok.Setter; diff --git a/kbazaar/src/main/resources/application.properties b/kbazaar/src/main/resources/application.properties index 9e9dd39..a75b178 100644 --- a/kbazaar/src/main/resources/application.properties +++ b/kbazaar/src/main/resources/application.properties @@ -8,9 +8,9 @@ spring.jpa.hibernate.ddl-auto=update spring.jpa.show-sql=true spring.jpa.properties.hibernate.format_sql=true -spring.datasource.url= -spring.datasource.username= -spring.datasource.password= +spring.datasource.url=jdbc:postgresql://localhost:5433/postgres +spring.datasource.username=postgres +spring.datasource.password=postgres spring.datasource.driver-class-name=org.postgresql.Driver spring.datasource.hikari.maximum-pool-size=1 diff --git a/kbazaar/src/test/java/com/kampus/kbazaar/product/ProductControllerTest.java b/kbazaar/src/test/java/com/kampus/kbazaar/product/ProductControllerTest.java index f11bf0f..b52ee4b 100644 --- a/kbazaar/src/test/java/com/kampus/kbazaar/product/ProductControllerTest.java +++ b/kbazaar/src/test/java/com/kampus/kbazaar/product/ProductControllerTest.java @@ -1,21 +1,17 @@ package com.kampus.kbazaar.product; -import static net.bytebuddy.matcher.ElementMatchers.is; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; -import static org.springframework.test.web.client.match.MockRestRequestMatchers.jsonPath; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import com.kampus.kbazaar.security.JwtAuthFilter; - import java.math.BigDecimal; import java.util.ArrayList; import java.util.List; - import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @@ -27,11 +23,12 @@ import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.FilterType; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.test.context.junit.jupiter.SpringExtension; import org.springframework.test.web.servlet.MockMvc; -import org.springframework.test.web.servlet.ResultMatcher; @ExtendWith(SpringExtension.class) @AutoConfigureMockMvc(addFilters = false) @@ -49,8 +46,8 @@ public class ProductControllerTest { @MockBean private ProductService productService; - private static final int LIMIT = 0; - private static final int OFFSET = 5; + private static final int LIMIT = 5; + private static final int OFFSET = 0; @BeforeEach public void setup() { @@ -87,24 +84,62 @@ public void shouldReturnProduct() throws Exception { } @Test + @DisplayName("should return product with pagination") void testGetProductsWithPage() { // Mocking productService's behavior - List mockProductResponses = new ArrayList<>(); - mockProductResponses.add(new ProductResponse(1L, "Product 1", "SKU001", new BigDecimal("10.99"), 100)); - mockProductResponses.add(new ProductResponse(2L, "Product 2", "SKU002", new BigDecimal("20.49"), 50)); - - when(productService.listAllProductByPage(anyInt(), anyInt())).thenReturn(mockProductResponses); - - // Calling the controller method - ResponseEntity responseEntity = productController.getProductsWithPage(10, 0); - - // Asserting the response - assertEquals(200, responseEntity.getStatusCodeValue()); - - ResponseMsg responseMsg = (ResponseMsg) responseEntity.getBody(); - assertEquals(0, responseMsg.getPage()); - assertEquals(10, responseMsg.getLimit()); - assertEquals(mockProductResponses, responseMsg.getData()); + List mockProducts = new ArrayList<>(); + mockProducts.add( + new Product( + 1L, + "Apple iPhone 12 Pro", + "MOBILE-APPLE-IPHONE-12-PRO", + new BigDecimal("20990.25"), + 50) + .toResponse()); + mockProducts.add( + new Product( + 2L, + "Samsung Galaxy S21 Ultra", + "MOBILE-SAMSUNG-GALAXY-S21-ULTRA", + new BigDecimal("18990.00"), + 70) + .toResponse()); + mockProducts.add( + new Product( + 3L, + "Google Pixel 5", + "MOBILE-GOOGLE-PIXEL-5", + new BigDecimal("12990.75"), + 40) + .toResponse()); + mockProducts.add( + new Product( + 4L, + "OnePlus 9 Pro", + "MOBILE-ONEPLUS-9-PRO", + new BigDecimal("14990.00"), + 60) + .toResponse()); + mockProducts.add( + new Product( + 5L, + "Xiaomi Mi 11", + "MOBILE-XIAOMI-MI-11", + new BigDecimal("8990.75"), + 80) + .toResponse()); + + when(productService.listAllProductByPage(anyInt(), anyInt())).thenReturn(mockProducts); + + // Actual + ResponseEntity actual = productController.getProductsWithPage(LIMIT, OFFSET); + + // Expected + HttpHeaders headers = new HttpHeaders(); + headers.set("page", Integer.toString(OFFSET)); + headers.set("limit", Integer.toString(LIMIT)); + ResponseEntity expected = new ResponseEntity<>(mockProducts, headers, HttpStatus.OK); + + assertEquals(expected, actual); } - }