Skip to content

Commit

Permalink
update user story-3
Browse files Browse the repository at this point in the history
  • Loading branch information
Zhuuuun committed Mar 23, 2024
1 parent f0ab1e8 commit fbe3ac7
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 50 deletions.
2 changes: 1 addition & 1 deletion kbazaar/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ services:
POSTGRES_PASSWORD: postgres
POSTGRES_DB: postgres
ports:
- '5432:5432'
- '5433:5432'
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand Down Expand Up @@ -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<ProductResponse> 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<ProductResponse> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ public interface ProductRepository extends JpaRepository<Product, Long> {
Optional<Product> findBySku(String sku);

@Query(value = "SELECT * FROM product LIMIT :limit OFFSET :page", nativeQuery = true)
List<Product> findAllByPage(int limit,int page);
List<Product> findAllByPage(int limit, int page);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -29,8 +27,9 @@ public ProductResponse getBySku(String sku) {
return product.get().toResponse();
}

public List<ProductResponse> listAllProductByPage(int limit,int page) {
return productRepository.findAllByPage(limit,page).stream().map(Product::toResponse).toList();

public List<ProductResponse> listAllProductByPage(int limit, int page) {
return productRepository.findAllByPage(limit, page).stream()
.map(Product::toResponse)
.toList();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.kampus.kbazaar.product;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;

Expand Down
6 changes: 3 additions & 3 deletions kbazaar/src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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.maximumPoolSize=1

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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)
Expand All @@ -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() {
Expand Down Expand Up @@ -87,24 +84,62 @@ public void shouldReturnProduct() throws Exception {
}

@Test
@DisplayName("should return product with pagination")
void testGetProductsWithPage() {
// Mocking productService's behavior
List<ProductResponse> 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<ProductResponse> 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);
}

}

0 comments on commit fbe3ac7

Please sign in to comment.