Commit 52107bd4 authored by Khai Yuan ​Liew's avatar Khai Yuan ​Liew

Merge branch 'AFP-56-get-product-id-feature-branch' into 'dev'

Afp 56 get product id feature branch

See merge request !6
parents 89bc8fc7 4b8d1bfb
package com.nisum.ascend.inventory.controller; package com.nisum.ascend.inventory.controller;
import com.nisum.ascend.inventory.dto.ProductDto; import com.nisum.ascend.inventory.dto.ProductDto;
import com.nisum.ascend.inventory.model.Product;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import com.nisum.ascend.inventory.service.ProductService; import com.nisum.ascend.inventory.service.ProductService;
import reactor.core.publisher.Mono;
import org.springframework.beans.factory.annotation.Autowired;
import reactor.core.publisher.Flux; import reactor.core.publisher.Flux;
@RestController @RestController
...@@ -15,6 +21,13 @@ public class ProductController { ...@@ -15,6 +21,13 @@ public class ProductController {
@Autowired @Autowired
ProductService productService; ProductService productService;
@GetMapping("/{sku}")
public ResponseEntity<Mono<ProductDto>> getProductBySku(@PathVariable String sku) {
Mono<ProductDto> monoProd = productService.getProductBySku(sku);
HttpStatus status = monoProd != null ? HttpStatus.OK : HttpStatus.NOT_FOUND;
return new ResponseEntity<>(monoProd, status);
}
@GetMapping() @GetMapping()
public ResponseEntity<Flux<ProductDto>> getAllProducts() { public ResponseEntity<Flux<ProductDto>> getAllProducts() {
return ResponseEntity.ok(productService.findAllProducts()); return ResponseEntity.ok(productService.findAllProducts());
......
package com.nisum.ascend.inventory.exception;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;
@ResponseStatus(value = HttpStatus.NOT_FOUND)
public class ResourceNotFoundException extends Exception{
HttpStatus status;
public ResourceNotFoundException(HttpStatus status, String message) {
super(message);
this.status = status;
}
}
package com.nisum.ascend.inventory.repository; package com.nisum.ascend.inventory.repository;
import com.nisum.ascend.inventory.model.Product; import com.nisum.ascend.inventory.model.Product;
import org.springframework.data.mongodb.repository.Query;
import org.springframework.data.mongodb.repository.ReactiveMongoRepository; import org.springframework.data.mongodb.repository.ReactiveMongoRepository;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
import reactor.core.publisher.Mono;
@Repository @Repository
public interface ProductRepository extends ReactiveMongoRepository<Product, String> { public interface ProductRepository extends ReactiveMongoRepository<Product, String> {
Mono<Product> findBySku(String sku);
} }
package com.nisum.ascend.inventory.service; package com.nisum.ascend.inventory.service;
import com.nisum.ascend.inventory.dto.ProductDto; import com.nisum.ascend.inventory.dto.ProductDto;
import com.nisum.ascend.inventory.exception.ResourceNotFoundException;
import com.nisum.ascend.inventory.model.Product; import com.nisum.ascend.inventory.model.Product;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import reactor.core.publisher.Flux; import reactor.core.publisher.Flux;
import com.nisum.ascend.inventory.repository.ProductRepository; import com.nisum.ascend.inventory.repository.ProductRepository;
import reactor.core.publisher.Mono;
import static com.nisum.ascend.inventory.dto.ProductDto.generateDtoFromProduct;
@Service @Service
public class ProductService { public class ProductService {
@Autowired @Autowired
ProductRepository productRepository; ProductRepository productRepository;
public Mono<ProductDto> getProductBySku(String sku){
return productRepository.findBySku(sku)
.map(existingProduct -> generateDtoFromProduct(existingProduct))
.switchIfEmpty(Mono.error(new ResourceNotFoundException(HttpStatus.NOT_FOUND, "product not found")));
}
public Flux<ProductDto> findAllProducts() { public Flux<ProductDto> findAllProducts() {
return productRepository.findAll() return productRepository.findAll()
.map(ProductDto::generateDtoFromProduct); .map(ProductDto::generateDtoFromProduct);
......
spring.config.import=classpath:secret.properties
spring.data.mongodb.uri=mongodb+srv://admin:${db.password}@inventory-promotions.d4nfz.mongodb\
.net/${spring.data.mongodb.database}?retryWrites=true&w=majority
spring.data.mongodb.database=test
...@@ -3,4 +3,4 @@ spring.config.import=classpath:secret.properties ...@@ -3,4 +3,4 @@ spring.config.import=classpath:secret.properties
server.port=8080 server.port=8080
spring.data.mongodb.uri=mongodb+srv://admin:${db.password}@inventory-promotions.d4nfz.mongodb\ spring.data.mongodb.uri=mongodb+srv://admin:${db.password}@inventory-promotions.d4nfz.mongodb\
.net/${spring.data.mongodb.database}?retryWrites=true&w=majority .net/${spring.data.mongodb.database}?retryWrites=true&w=majority
spring.data.mongodb.database=products-promotions-DB spring.data.mongodb.database=test
package com.nisum.ascend.inventory.controller; package com.nisum.ascend.inventory.controller;
import com.nisum.ascend.inventory.controller.ProductController; import com.nisum.ascend.inventory.dto.ProductDto;
import com.nisum.ascend.inventory.model.Product; import com.nisum.ascend.inventory.model.Product;
import com.nisum.ascend.inventory.repository.ProductRepository; import com.nisum.ascend.inventory.repository.ProductRepository;
import com.nisum.ascend.inventory.service.ProductService;
import com.nisum.ascend.inventory.controller.ProductController;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient; import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.ResponseEntity;
import org.mockito.Mock;
import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.HttpHeaders; import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
...@@ -18,6 +22,12 @@ import org.springframework.test.annotation.DirtiesContext; ...@@ -18,6 +22,12 @@ import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit.jupiter.SpringExtension; import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.web.reactive.server.WebTestClient; import org.springframework.test.web.reactive.server.WebTestClient;
import org.springframework.web.bind.annotation.ResponseBody;
import reactor.core.publisher.Mono;
import static org.junit.jupiter.api.Assertions.*;
import org.springframework.web.reactive.function.client.WebClient; import org.springframework.web.reactive.function.client.WebClient;
import java.util.List; import java.util.List;
...@@ -36,10 +46,36 @@ class ProductControllerTest { ...@@ -36,10 +46,36 @@ class ProductControllerTest {
@Autowired @Autowired
private WebTestClient webTestClient; private WebTestClient webTestClient;
@BeforeEach @BeforeEach
void setUp() { public void setup(){
} }
@Test
void testProductInvalidSkuNotFound() {
webTestClient
.get()
.uri("/api/products/".concat("{sku}"), "invalid")
.exchange()
.expectStatus()
.isNotFound();
}
@Test
void testProductBySkuFound() {
String sku = "000001";
webTestClient.get()
.uri("/api/products/".concat("/{sku}"), sku)
.exchange()
.expectStatus()
.isOk()
.expectBody()
.jsonPath("$.sku", sku);
}
@Test @Test
void testFindAllProducts(){ void testFindAllProducts(){
webTestClient.get() webTestClient.get()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment