From 216748b6a7c607ce18300023796d35f379b65f7d Mon Sep 17 00:00:00 2001 From: Nong AnuchitO Date: Thu, 21 Mar 2024 13:03:55 +0700 Subject: [PATCH] formatting --- .../kbazaar/product/ProductController.java | 36 +++++++++---------- .../kbazaar/product/ProductRepository.java | 3 +- .../kbazaar/product/ProductResponse.java | 2 +- .../kbazaar/product/ProductService.java | 20 ++++++----- .../kbazaar/architecture/CodingRuleTest.java | 7 ++-- .../kbazaar/product/ProductServiceTest.java | 31 ++++++++-------- 6 files changed, 49 insertions(+), 50 deletions(-) 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 32f68a2..230bd13 100644 --- a/kbazaar/src/main/java/com/kampus/kbazaar/product/ProductController.java +++ b/kbazaar/src/main/java/com/kampus/kbazaar/product/ProductController.java @@ -5,11 +5,7 @@ import io.swagger.v3.oas.annotations.media.Schema; import io.swagger.v3.oas.annotations.responses.ApiResponse; import io.swagger.v3.oas.annotations.responses.ApiResponses; - import java.util.List; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; @@ -19,7 +15,6 @@ @RequestMapping("/api/v1") public class ProductController { - private ProductService productService; public ProductController(ProductService productService) { @@ -27,21 +22,26 @@ public ProductController(ProductService productService) { } @ApiResponses({ - @ApiResponse( - responseCode = "200", - description = "list all products", - content = { - @Content( - mediaType = "application/json", - array = @ArraySchema(schema = @Schema(implementation = ProductResponse.class))) - }), - @ApiResponse( - responseCode = "500", - description = "internal server error", - content = + @ApiResponse( + responseCode = "200", + description = "list all products", + content = { @Content( mediaType = "application/json", - schema = @Schema(implementation = Error.class))) + array = + @ArraySchema( + schema = + @Schema( + implementation = + ProductResponse.class))) + }), + @ApiResponse( + responseCode = "500", + description = "internal server error", + content = + @Content( + mediaType = "application/json", + schema = @Schema(implementation = Error.class))) }) @GetMapping("/products") public List getProducts() { 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 43fbf2b..04c5ad9 100644 --- a/kbazaar/src/main/java/com/kampus/kbazaar/product/ProductRepository.java +++ b/kbazaar/src/main/java/com/kampus/kbazaar/product/ProductRepository.java @@ -1,10 +1,9 @@ package com.kampus.kbazaar.product; +import java.util.Optional; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; -import java.util.Optional; - @Repository public interface ProductRepository extends JpaRepository { diff --git a/kbazaar/src/main/java/com/kampus/kbazaar/product/ProductResponse.java b/kbazaar/src/main/java/com/kampus/kbazaar/product/ProductResponse.java index 27031a8..5d04695 100644 --- a/kbazaar/src/main/java/com/kampus/kbazaar/product/ProductResponse.java +++ b/kbazaar/src/main/java/com/kampus/kbazaar/product/ProductResponse.java @@ -1,3 +1,3 @@ package com.kampus.kbazaar.product; -public record ProductResponse(Long id, String name, String sku, double price, int quantity) {} \ No newline at end of file +public record ProductResponse(Long id, String name, String sku, double price, int quantity) {} 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 365c745..8c3bbdd 100644 --- a/kbazaar/src/main/java/com/kampus/kbazaar/product/ProductService.java +++ b/kbazaar/src/main/java/com/kampus/kbazaar/product/ProductService.java @@ -1,9 +1,8 @@ package com.kampus.kbazaar.product; -import org.springframework.stereotype.Service; - import java.util.List; import java.util.Optional; +import org.springframework.stereotype.Service; @Service public class ProductService { @@ -15,18 +14,23 @@ public ProductService(ProductRepository productRepository) { } public List getAll() { - return productRepository - .findAll() - .stream() - .map(p -> new ProductResponse(p.getId(), p.getName(), p.getSku(), p.getPrice(), p.getQuantity())) + return productRepository.findAll().stream() + .map( + p -> + new ProductResponse( + p.getId(), + p.getName(), + p.getSku(), + p.getPrice(), + p.getQuantity())) .toList(); } public ProductResponse getBySku(String sku) { Optional product = productRepository.findBySku(sku); if (product.isEmpty()) { -// throw new ProductNotFoundException("Product not found"); -// TODO: handle exception + // throw new ProductNotFoundException("Product not found"); + // TODO: handle exception return null; } diff --git a/kbazaar/src/test/java/com/kampus/kbazaar/architecture/CodingRuleTest.java b/kbazaar/src/test/java/com/kampus/kbazaar/architecture/CodingRuleTest.java index f34de6c..c3ec7cd 100644 --- a/kbazaar/src/test/java/com/kampus/kbazaar/architecture/CodingRuleTest.java +++ b/kbazaar/src/test/java/com/kampus/kbazaar/architecture/CodingRuleTest.java @@ -1,17 +1,16 @@ package com.kampus.kbazaar.architecture; +import static com.tngtech.archunit.library.GeneralCodingRules.NO_CLASSES_SHOULD_USE_FIELD_INJECTION; + import com.tngtech.archunit.core.importer.ImportOption; import com.tngtech.archunit.junit.AnalyzeClasses; import com.tngtech.archunit.junit.ArchTest; import com.tngtech.archunit.lang.ArchRule; -import static com.tngtech.archunit.library.GeneralCodingRules.NO_CLASSES_SHOULD_USE_FIELD_INJECTION; - @AnalyzeClasses( packages = "com.kampus.kbazaar", importOptions = ImportOption.DoNotIncludeTests.class) public class CodingRuleTest { - @ArchTest - private final ArchRule no_field_injection = NO_CLASSES_SHOULD_USE_FIELD_INJECTION; + @ArchTest private final ArchRule no_field_injection = NO_CLASSES_SHOULD_USE_FIELD_INJECTION; } diff --git a/kbazaar/src/test/java/com/kampus/kbazaar/product/ProductServiceTest.java b/kbazaar/src/test/java/com/kampus/kbazaar/product/ProductServiceTest.java index e31dc31..02e04cb 100644 --- a/kbazaar/src/test/java/com/kampus/kbazaar/product/ProductServiceTest.java +++ b/kbazaar/src/test/java/com/kampus/kbazaar/product/ProductServiceTest.java @@ -1,28 +1,23 @@ package com.kampus.kbazaar.product; -import jdk.jfr.Name; -import org.junit.jupiter.api.AfterEach; +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.when; + +import java.util.Arrays; +import java.util.List; +import java.util.Optional; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.MockitoAnnotations; -import java.util.Arrays; -import java.util.List; -import java.util.Optional; - -import static org.junit.jupiter.api.Assertions.*; -import static org.mockito.ArgumentMatchers.anyString; -import static org.mockito.Mockito.when; - class ProductServiceTest { - @Mock - private ProductRepository productRepository; + @Mock private ProductRepository productRepository; - @InjectMocks - private ProductService productService; + @InjectMocks private ProductService productService; @BeforeEach void setUp() { @@ -32,7 +27,8 @@ void setUp() { @Test void testShouldBeAbleToGetAllProducts() { // Mock data - Product product1 = new Product(1L, "Google Pixel 5", "MOBILE-GOOGLE-PIXEL-5", 12990.00, 100); + Product product1 = + new Product(1L, "Google Pixel 5", "MOBILE-GOOGLE-PIXEL-5", 12990.00, 100); Product product2 = new Product(2L, "Coca-Cola", "BEV-COCA-COLA", 20.00, 150); List productList = Arrays.asList(product1, product2); @@ -45,7 +41,7 @@ void testShouldBeAbleToGetAllProducts() { // Assertions assertEquals(2, result.size()); assertEquals("Google Pixel 5", result.get(0).name()); - assertEquals("MOBILE-GOOGLE-PIXEL-5", result.get(1).sku()); + assertEquals("BEV-COCA-COLA", result.get(1).sku()); } @Test @@ -66,7 +62,8 @@ void testShouldBeAbleToGetProductBySku() { Product product = new Product(1L, "Pens", "STATIONERY-PEN-BIC-BALLPOINT", 14.99, 100); // Mock repository method - when(productRepository.findBySku("STATIONERY-PEN-BIC-BALLPOINT")).thenReturn(Optional.of(product)); + when(productRepository.findBySku("STATIONERY-PEN-BIC-BALLPOINT")) + .thenReturn(Optional.of(product)); // Call service method ProductResponse result = productService.getBySku("STATIONERY-PEN-BIC-BALLPOINT");