Skip to content

Commit

Permalink
feat : adds optional requestParam to fetch StockDetails
Browse files Browse the repository at this point in the history
  • Loading branch information
rajadilipkolli committed Dec 8, 2023
1 parent 6569b8b commit fcf7a66
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 8 deletions.
10 changes: 10 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,16 @@
"projectName": "payment-service",
"args": "",
"envFile": "${workspaceFolder}/.env"
},
{
"type": "java",
"name": "Spring Boot-TestCatalogServiceApplication<catalog-service>",
"request": "launch",
"cwd": "${workspaceFolder}",
"mainClass": "com.example.catalogservice.TestCatalogServiceApplication",
"projectName": "catalog-service",
"args": "",
"envFile": "${workspaceFolder}/.env"
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,27 @@ private Mono<InventoryResponse> getInventoryByProductCode(String code) {
}

@Observed(name = "product.findByCode", contextualName = "findByProductCode")
public Mono<ProductResponse> findProductByProductCode(String productCode) {
return productRepository
.findByCodeAllIgnoreCase(productCode)
.map(productMapper::toProductResponse)
.switchIfEmpty(Mono.error(new ProductNotFoundException(productCode)));
public Mono<ProductResponse> findProductByProductCode(
String productCode, boolean fetchInStock) {
Mono<ProductResponse> productResponseMono =
productRepository
.findByCodeAllIgnoreCase(productCode)
.map(productMapper::toProductResponse)
.switchIfEmpty(Mono.error(new ProductNotFoundException(productCode)));

if (fetchInStock) {
return productResponseMono.flatMap(this::fetchInventoryAndUpdateProductResponse);
}
return productResponseMono;
}

private Mono<ProductResponse> fetchInventoryAndUpdateProductResponse(
ProductResponse productResponse) {
return getInventoryByProductCode(productResponse.code())
.map(
inventoryResponse ->
productResponse.withInStock(
inventoryResponse.availableQuantity() > 0));
}

// saves product to db and sends message that new product is available for inventory
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,11 @@ public Mono<ResponseEntity<ProductResponse>> getProductById(@PathVariable Long i

@GetMapping("/productCode/{productCode}")
public Mono<ResponseEntity<ProductResponse>> getProductByProductCode(
@PathVariable String productCode) {
return productService.findProductByProductCode(productCode).map(ResponseEntity::ok);
@PathVariable String productCode,
@RequestParam(required = false) boolean fetchInStock) {
return productService
.findProductByProductCode(productCode, fetchInStock)
.map(ResponseEntity::ok);
}

@GetMapping("/exists")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,43 @@ void shouldFindProductByProductCode() {
.jsonPath("$.description")
.isEqualTo(product.getDescription())
.jsonPath("$.price")
.isEqualTo(product.getPrice());
.isEqualTo(product.getPrice())
.jsonPath("$.inStock")
.isEqualTo(false);
}

@Test
void shouldFindProductByProductCodeWithStock() throws JsonProcessingException {
Product product = savedProductList.getFirst();

mockBackendEndpoint(
200, objectMapper.writeValueAsString(new InventoryResponse(product.getCode(), 10)));
webTestClient
.get()
.uri(
uriBuilder ->
uriBuilder
.path("/api/catalog/productCode/{productCode}")
.queryParam("fetchInStock", true)
.build(product.getCode()))
.exchange()
.expectStatus()
.isOk()
.expectHeader()
.contentType(MediaType.APPLICATION_JSON)
.expectBody()
.jsonPath("$.id")
.isEqualTo(product.getId())
.jsonPath("$.code")
.isEqualTo(product.getCode())
.jsonPath("$.productName")
.isEqualTo(product.getProductName())
.jsonPath("$.description")
.isEqualTo(product.getDescription())
.jsonPath("$.price")
.isEqualTo(product.getPrice())
.jsonPath("$.inStock")
.isEqualTo(true);
}

@Test
Expand Down
5 changes: 5 additions & 0 deletions test-em-all.sh
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,11 @@ function setupTestData() {
# Update the product_1 available Quantity
recreateComposite $(echo "$RESPONSE" | jq -r .id) "$body" "inventory-service/api/inventory/$(echo "$RESPONSE" | jq -r .id)" "PUT"

# Verify that communication between catalog-service and inventory service is established
assertCurl 200 "curl -k http://$HOST:$PORT/catalog-service/api/catalog/productCode/$PROD_CODE_1?fetchInStock=true"
assertEqual $PROD_CODE_1 $(echo ${RESPONSE} | jq .code)
assertEqual \"true\" $(echo ${RESPONSE} | jq .inStock)

body="{\"name\": \"$CUSTOMER_NAME"
body+=\
'","email": "[email protected]","address": "docker Address","amountAvailable":1000}'
Expand Down

0 comments on commit fcf7a66

Please sign in to comment.