Skip to content

Commit

Permalink
formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
AnuchitO committed Mar 21, 2024
1 parent 7bb8f4a commit 216748b
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -19,29 +15,33 @@
@RequestMapping("/api/v1")
public class ProductController {


private ProductService productService;

public ProductController(ProductService productService) {
this.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<ProductResponse> getProducts() {
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Product, Long> {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package com.kampus.kbazaar.product;

public record ProductResponse(Long id, String name, String sku, double price, int quantity) {}
public record ProductResponse(Long id, String name, String sku, double price, int quantity) {}
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -15,18 +14,23 @@ public ProductService(ProductRepository productRepository) {
}

public List<ProductResponse> 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> 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;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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() {
Expand All @@ -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<Product> productList = Arrays.asList(product1, product2);

Expand All @@ -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
Expand All @@ -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");
Expand Down

0 comments on commit 216748b

Please sign in to comment.