Skip to content

Commit

Permalink
feat: remove transient and use response (#625)
Browse files Browse the repository at this point in the history
* feat: remove transient and use response

* feat: adds negative test case
  • Loading branch information
rajadilipkolli authored Dec 8, 2023
1 parent 0312da4 commit 6569b8b
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ Licensed under MIT License Copyright (c) 2021-2023 Raja Kolli.
import java.io.Serial;
import java.io.Serializable;
import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.Transient;
import org.springframework.data.relational.core.mapping.Table;

@Table(name = "products")
Expand All @@ -27,23 +26,14 @@ public class Product implements Serializable {

private double price;

@Transient private boolean inStock;

public Product() {}

public Product(
Long id,
String code,
String productName,
String description,
double price,
boolean inStock) {
public Product(Long id, String code, String productName, String description, double price) {
this.id = id;
this.code = code;
this.productName = productName;
this.description = description;
this.price = price;
this.inStock = inStock;
}

public Long getId() {
Expand Down Expand Up @@ -85,12 +75,4 @@ public double getPrice() {
public void setPrice(double price) {
this.price = price;
}

public boolean isInStock() {
return inStock;
}

public void setInStock(boolean inStock) {
this.inStock = inStock;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,15 @@ Licensed under MIT License Copyright (c) 2021-2023 Raja Kolli.
@Mapper(componentModel = "spring")
public interface ProductMapper {

@Mapping(target = "inStock", ignore = true)
@Mapping(target = "withInStock", ignore = true)
ProductResponse toProductResponse(Product product);

@Mapping(target = "id", ignore = true)
@Mapping(target = "inStock", ignore = true)
Product toEntity(ProductRequest productRequest);

ProductDto toProductDto(ProductRequest productRequest);

@Mapping(target = "inStock", ignore = true)
@Mapping(target = "id", ignore = true)
void mapProductWithRequest(ProductRequest productRequest, @MappingTarget Product product);
}
Original file line number Diff line number Diff line change
Expand Up @@ -130,16 +130,15 @@ public Mono<ProductResponse> findProductById(Long id) {
return productRepository
.findById(id)
.switchIfEmpty(Mono.error(new ProductNotFoundException(id)))
.map(productMapper::toProductResponse)
.flatMap(
product ->
getInventoryByProductCode(product.getCode())
productResponse ->
getInventoryByProductCode(productResponse.code())
.map(
inventoryDto -> {
product.setInStock(
return productResponse.withInStock(
inventoryDto.availableQuantity() > 0);
return product;
}))
.map(productMapper::toProductResponse);
}));
}

private Mono<InventoryResponse> getInventoryByProductCode(String code) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ void setUp() {

List<Product> productList =
List.of(
new Product(null, "P001", "name 1", "description 1", 9.0, false),
new Product(null, "P002", "name 2", "description 2", 10.0, false),
new Product(null, "P003", "name 3", "description 3", 11.0, false));
new Product(null, "P001", "name 1", "description 1", 9.0),
new Product(null, "P002", "name 2", "description 2", 10.0),
new Product(null, "P003", "name 3", "description 3", 11.0));
savedProductList =
productRepository
.deleteAll()
Expand Down Expand Up @@ -201,7 +201,7 @@ void shouldFindProductById() 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.getCode(), 0)));
webTestClient
.get()
.uri("/api/catalog/id/{id}", productId)
Expand All @@ -220,7 +220,9 @@ void shouldFindProductById() throws JsonProcessingException {
.jsonPath("$.description")
.isEqualTo(product.getDescription())
.jsonPath("$.price")
.isEqualTo(product.getPrice());
.isEqualTo(product.getPrice())
.jsonPath("$.inStock")
.isEqualTo(false);
checkHealthStatus("default", CircuitBreaker.State.CLOSED);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,7 @@ void shouldReturn400WhenCreateNewProductWithoutCode() throws Exception {
@Test
void shouldUpdateProduct() {
Long productId = 1L;
Product product =
new Product(productId, "code 1", "Updated name", "description 1", 9.0, true);
Product product = new Product(productId, "code 1", "Updated name", "description 1", 9.0);
ProductRequest productRequest =
new ProductRequest("code 1", "Updated name", "description 1", 9.0);
ProductResponse productResponse =
Expand Down Expand Up @@ -241,7 +240,7 @@ void shouldReturn404WhenUpdatingNonExistingProduct() {
@Test
void shouldDeleteProduct() {
Long productId = 1L;
Product product = new Product(1L, "code 1", "Updated name", "description 1", 9.0, true);
Product product = new Product(1L, "code 1", "Updated name", "description 1", 9.0);
ProductResponse productResponse =
new ProductResponse(1L, "code 1", "Updated name", "description 1", 9.0, true);
given(productService.findByIdWithMapping(productId)).willReturn(Mono.just(productResponse));
Expand Down

0 comments on commit 6569b8b

Please sign in to comment.