Skip to content

Commit

Permalink
rename : code to productCode (#766)
Browse files Browse the repository at this point in the history
* rename : code to productCode

* fix : issue with starting application

* fix : issue with validation of db

* typo
  • Loading branch information
rajadilipkolli authored Jun 17, 2024
1 parent 0755a56 commit 008d5d7
Show file tree
Hide file tree
Showing 12 changed files with 91 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class Product implements Serializable {

@Id private Long id;

private String code;
private String productCode;

private String productName;

Expand All @@ -39,12 +39,12 @@ public Product setId(Long id) {
return this;
}

public String getCode() {
return code;
public String getProductCode() {
return productCode;
}

public Product setCode(String code) {
this.code = code;
public Product setProductCode(String productCode) {
this.productCode = productCode;
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public interface ProductMapper {
@Mapping(target = "id", ignore = true)
Product toEntity(ProductRequest productRequest);

@Mapping(target = "code", source = "productCode")
ProductDto toProductDto(ProductRequest productRequest);

@Mapping(target = "id", ignore = true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Licensed under MIT License Copyright (c) 2021-2023 Raja Kolli.
import jakarta.validation.constraints.Positive;

public record ProductRequest(
@NotBlank(message = "Product code can't be blank") String code,
@NotBlank(message = "Product code can't be blank") String productCode,
String productName,
String description,
String imageUrl,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Licensed under MIT License Copyright (c) 2023-2024 Raja Kolli.

public record ProductResponse(
Long id,
String code,
String productCode,
String productName,
String description,
String imageUrl,
Expand All @@ -24,7 +24,7 @@ public ProductResponse withInStock(final boolean inStock) {
? this
: new ProductResponse(
this.id,
this.code,
this.productCode,
this.productName,
this.description,
this.imageUrl,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ Licensed under MIT License Copyright (c) 2021-2023 Raja Kolli.
public interface ProductRepository
extends ReactiveCrudRepository<Product, Long>, ReactiveSortingRepository<Product, Long> {

Mono<Long> countDistinctByCodeAllIgnoreCaseIn(List<String> code);
Mono<Long> countDistinctByProductCodeAllIgnoreCaseIn(List<String> productCodeList);

Mono<Product> findByCodeAllIgnoreCase(String code);
Mono<Product> findByProductCodeAllIgnoreCase(String productCode);

Flux<Product> findAllBy(Pageable pageable);
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public Mono<PagedResult<ProductResponse>> findAllProducts(

List<String> productCodeList =
productResponseList.stream()
.map(ProductResponse::code)
.map(ProductResponse::productCode)
.toList();

return getInventoryByProductCodes(productCodeList)
Expand All @@ -118,7 +118,7 @@ private List<ProductResponse> updateProductAvailability(
.map(
productResponse -> {
int availableQuantity =
inventoriesMap.getOrDefault(productResponse.code(), 0);
inventoriesMap.getOrDefault(productResponse.productCode(), 0);
return productResponse.withInStock(availableQuantity > 0);
})
.toList();
Expand All @@ -136,7 +136,7 @@ public Mono<ProductResponse> findProductById(Long id) {
.map(productMapper::toProductResponse)
.flatMap(
productResponse ->
getInventoryByProductCode(productResponse.code())
getInventoryByProductCode(productResponse.productCode())
.map(
inventoryDto ->
productResponse.withInStock(
Expand All @@ -153,7 +153,7 @@ public Mono<ProductResponse> findProductByProductCode(
String productCode, boolean fetchInStock) {
Mono<ProductResponse> productResponseMono =
productRepository
.findByCodeAllIgnoreCase(productCode)
.findByProductCodeAllIgnoreCase(productCode)
.map(productMapper::toProductResponse)
.switchIfEmpty(Mono.error(new ProductNotFoundException(productCode)));

Expand All @@ -165,7 +165,7 @@ public Mono<ProductResponse> findProductByProductCode(

private Mono<ProductResponse> fetchInventoryAndUpdateProductResponse(
ProductResponse productResponse) {
return getInventoryByProductCode(productResponse.code())
return getInventoryByProductCode(productResponse.productCode())
.map(
inventoryResponse ->
productResponse.withInStock(
Expand All @@ -191,7 +191,8 @@ public Mono<ProductResponse> saveProduct(ProductRequest productRequest) {
e ->
// Handle unique key constraint violation here
Mono.error(
new ProductAlreadyExistsException(productRequest.code())))
new ProductAlreadyExistsException(
productRequest.productCode())))
.map(productMapper::toProductResponse);
}

Expand All @@ -204,7 +205,7 @@ public Mono<Void> deleteProductById(Long id) {
public Mono<Boolean> productExistsByProductCodes(List<String> productCodes) {
log.info("checking if products Exists :{}", productCodes);
return productRepository
.countDistinctByCodeAllIgnoreCaseIn(productCodes)
.countDistinctByProductCodeAllIgnoreCaseIn(productCodes)
.map(count -> count == productCodes.size());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ databaseChangeLog:
- column:
constraints:
nullable: 'false'
name: code
name: product_code
type: ${string.type}
- column:
constraints:
Expand All @@ -54,6 +54,6 @@ databaseChangeLog:
type: 'NUMERIC(19,2)'
tableName: products
addUniqueConstraint:
columnNames: code
constraintName: uc_products_code
columnNames: product_code
constraintName: uc_product_code
tableName: products
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ void testGenerateProducts() {
int randomPrice = ThreadLocalRandom.current().nextInt(1, 101);
return new Product()
.setId(1L)
.setCode(request.code())
.setProductCode(request.productCode())
.setProductName(request.productName())
.setDescription(request.description())
.setPrice(randomPrice);
Expand All @@ -68,7 +68,7 @@ void testGenerateProducts() {
ProductRequest request = invocation.getArgument(0);
int randomPrice = ThreadLocalRandom.current().nextInt(1, 101);
return new ProductDto(
request.code(),
request.productCode(),
request.productName(),
request.description(),
(double) randomPrice);
Expand All @@ -81,7 +81,7 @@ void testGenerateProducts() {
Product product = invocationOnMock.getArgument(0);
return new ProductResponse(
product.getId(),
product.getCode(),
product.getProductCode(),
product.getProductName(),
product.getDescription(),
null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,17 +69,17 @@ void setUp() {
List<Product> productList =
List.of(
new Product()
.setCode("P001")
.setProductCode("P001")
.setProductName("name 1")
.setDescription("description 1")
.setPrice(9.0),
new Product()
.setCode("P002")
.setProductCode("P002")
.setProductName("name 2")
.setDescription("description 2")
.setPrice(10.0),
new Product()
.setCode("P003")
.setProductCode("P003")
.setProductName("name 3")
.setDescription("description 3")
.setPrice(11.0));
Expand Down Expand Up @@ -213,7 +213,9 @@ void shouldFindProductById() throws JsonProcessingException {
Product product = savedProductList.getFirst();
Long productId = product.getId();
mockBackendEndpoint(
200, objectMapper.writeValueAsString(new InventoryResponse(product.getCode(), 0)));
200,
objectMapper.writeValueAsString(
new InventoryResponse(product.getProductCode(), 0)));
webTestClient
.get()
.uri("/api/catalog/id/{id}", productId)
Expand All @@ -225,8 +227,8 @@ void shouldFindProductById() throws JsonProcessingException {
.expectBody()
.jsonPath("$.id")
.isEqualTo(product.getId())
.jsonPath("$.code")
.isEqualTo(product.getCode())
.jsonPath("$.productCode")
.isEqualTo(product.getProductCode())
.jsonPath("$.productName")
.isEqualTo(product.getProductName())
.jsonPath("$.description")
Expand Down Expand Up @@ -256,7 +258,9 @@ void shouldRetryOnErrorAndFetchSuccessResponse() throws JsonProcessingException
Product product = savedProductList.getFirst();
Long productId = product.getId();
mockBackendEndpoint(
200, objectMapper.writeValueAsString(new InventoryResponse(product.getCode(), 10)));
200,
objectMapper.writeValueAsString(
new InventoryResponse(product.getProductCode(), 10)));

webTestClient
.get()
Expand All @@ -269,8 +273,8 @@ void shouldRetryOnErrorAndFetchSuccessResponse() throws JsonProcessingException
.expectBody()
.jsonPath("$.id")
.isEqualTo(product.getId())
.jsonPath("$.code")
.isEqualTo(product.getCode())
.jsonPath("$.productCode")
.isEqualTo(product.getProductCode())
.jsonPath("$.productName")
.isEqualTo(product.getProductName())
.jsonPath("$.description")
Expand Down Expand Up @@ -309,7 +313,9 @@ void shouldRetryOnErrorAndBreakCircuitResponse() throws JsonProcessingException
Product product = savedProductList.getFirst();
Long productId = product.getId();
mockBackendEndpoint(
200, objectMapper.writeValueAsString(new InventoryResponse(product.getCode(), 10)));
200,
objectMapper.writeValueAsString(
new InventoryResponse(product.getProductCode(), 10)));

webTestClient
.get()
Expand Down Expand Up @@ -344,7 +350,9 @@ void shouldRetryAndFailAndBreakCloseTheCircuitTest() throws JsonProcessingExcept
mockBackendEndpoint(500, "ERROR");
mockBackendEndpoint(500, "ERROR");
mockBackendEndpoint(
200, objectMapper.writeValueAsString(new InventoryResponse(product.getCode(), 10)));
200,
objectMapper.writeValueAsString(
new InventoryResponse(product.getProductCode(), 10)));
webTestClient
.get()
.uri("/api/catalog/id/{id}", productId)
Expand Down Expand Up @@ -372,8 +380,8 @@ void shouldRetryAndFailAndBreakCloseTheCircuitTest() throws JsonProcessingExcept
.expectBody()
.jsonPath("$.id")
.isEqualTo(product.getId())
.jsonPath("$.code")
.isEqualTo(product.getCode())
.jsonPath("$.productCode")
.isEqualTo(product.getProductCode())
.jsonPath("$.productName")
.isEqualTo(product.getProductName())
.jsonPath("$.description")
Expand Down Expand Up @@ -423,7 +431,7 @@ void shouldFindProductByProductCode() {

webTestClient
.get()
.uri("/api/catalog/productCode/{productCode}", product.getCode())
.uri("/api/catalog/productCode/{productCode}", product.getProductCode())
.exchange()
.expectStatus()
.isOk()
Expand All @@ -432,8 +440,8 @@ void shouldFindProductByProductCode() {
.expectBody()
.jsonPath("$.id")
.isEqualTo(product.getId())
.jsonPath("$.code")
.isEqualTo(product.getCode())
.jsonPath("$.productCode")
.isEqualTo(product.getProductCode())
.jsonPath("$.productName")
.isEqualTo(product.getProductName())
.jsonPath("$.description")
Expand All @@ -449,15 +457,17 @@ void shouldFindProductByProductCodeWithStock() throws JsonProcessingException {
Product product = savedProductList.getFirst();

mockBackendEndpoint(
200, objectMapper.writeValueAsString(new InventoryResponse(product.getCode(), 10)));
200,
objectMapper.writeValueAsString(
new InventoryResponse(product.getProductCode(), 10)));
webTestClient
.get()
.uri(
uriBuilder ->
uriBuilder
.path("/api/catalog/productCode/{productCode}")
.queryParam("fetchInStock", true)
.build(product.getCode()))
.build(product.getProductCode()))
.exchange()
.expectStatus()
.isOk()
Expand All @@ -466,8 +476,8 @@ void shouldFindProductByProductCodeWithStock() throws JsonProcessingException {
.expectBody()
.jsonPath("$.id")
.isEqualTo(product.getId())
.jsonPath("$.code")
.isEqualTo(product.getCode())
.jsonPath("$.productCode")
.isEqualTo(product.getProductCode())
.jsonPath("$.productName")
.isEqualTo(product.getProductName())
.jsonPath("$.description")
Expand All @@ -480,7 +490,8 @@ void shouldFindProductByProductCodeWithStock() throws JsonProcessingException {

@Test
void productsShouldExistsByProductCodes() {
List<String> productCodeList = savedProductList.stream().map(Product::getCode).toList();
List<String> productCodeList =
savedProductList.stream().map(Product::getProductCode).toList();

webTestClient
.get()
Expand Down Expand Up @@ -539,8 +550,8 @@ void shouldCreateNewProduct() {
.expectBody()
.jsonPath("$.id")
.isNotEmpty()
.jsonPath("$.code")
.isEqualTo(productRequest.code())
.jsonPath("$.productCode")
.isEqualTo(productRequest.productCode())
.jsonPath("$.productName")
.isEqualTo(productRequest.productName())
.jsonPath("$.description")
Expand Down Expand Up @@ -622,7 +633,11 @@ void shouldUpdateProduct() {

ProductRequest productRequest =
new ProductRequest(
product.getCode(), product.getProductName(), "Updated Catalog", null, 100D);
product.getProductCode(),
product.getProductName(),
"Updated Catalog",
null,
100D);

webTestClient
.put()
Expand All @@ -637,8 +652,8 @@ void shouldUpdateProduct() {
.expectBody()
.jsonPath("$.id")
.value(is(product.getId().intValue()))
.jsonPath("$.code")
.value(is(product.getCode()))
.jsonPath("$.productCode")
.value(is(product.getProductCode()))
.jsonPath("$.productName")
.value(is(product.getProductName()))
.jsonPath("$.description")
Expand All @@ -662,8 +677,8 @@ void shouldDeleteProduct() {
.expectBody()
.jsonPath("$.id")
.isEqualTo(product.getId())
.jsonPath("$.code")
.isEqualTo(product.getCode())
.jsonPath("$.productCode")
.isEqualTo(product.getProductCode())
.jsonPath("$.productName")
.isEqualTo(product.getProductName())
.jsonPath("$.description")
Expand Down
Loading

0 comments on commit 008d5d7

Please sign in to comment.