Commit a5f7220d authored by John Lam's avatar John Lam

add testing for find product by sku / invalid sku

parent d90fbf02
...@@ -19,8 +19,8 @@ public class ProductController { ...@@ -19,8 +19,8 @@ public class ProductController {
ProductService productService; ProductService productService;
@GetMapping("{sku}") @GetMapping("{sku}")
public ResponseEntity<Mono<ProductDto>> getProductDto(@PathVariable String sku) { public ResponseEntity<Mono<ProductDto>> getProductBySku(@PathVariable String sku) {
Mono<ProductDto> monoProd = productService.getProduct(sku); Mono<ProductDto> monoProd = productService.getProductBySku(sku);
HttpStatus status = monoProd != null ? HttpStatus.OK : HttpStatus.NOT_FOUND; HttpStatus status = monoProd != null ? HttpStatus.OK : HttpStatus.NOT_FOUND;
return new ResponseEntity<>(monoProd, status); return new ResponseEntity<>(monoProd, status);
} }
......
...@@ -17,7 +17,7 @@ public class ProductService { ...@@ -17,7 +17,7 @@ public class ProductService {
@Autowired @Autowired
ProductRepository productRepository; ProductRepository productRepository;
public Mono<ProductDto> getProduct(String sku){ public Mono<ProductDto> getProductBySku(String sku){
return productRepository.findBySku(sku) return productRepository.findBySku(sku)
.map(existingProduct -> generateDtoFromProduct(existingProduct)) .map(existingProduct -> generateDtoFromProduct(existingProduct))
.switchIfEmpty(Mono.error(new ResourceNotFoundException(HttpStatus.NOT_FOUND, "product not found"))); .switchIfEmpty(Mono.error(new ResourceNotFoundException(HttpStatus.NOT_FOUND, "product not found")));
......
package com.nisum.ascend.inventory.controller; package com.nisum.ascend.inventory.controller;
import com.nisum.ascend.inventory.dto.ProductDto;
import com.nisum.ascend.inventory.model.Product;
import com.nisum.ascend.inventory.repository.ProductRepository;
import com.nisum.ascend.inventory.service.ProductService;
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.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.ResponseEntity;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit.jupiter.SpringExtension;
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 static org.junit.jupiter.api.Assertions.*;
@ExtendWith(SpringExtension.class) //integrates springtestframework into junit 5
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) //provides support for testing springboot app
@DirtiesContext
@AutoConfigureWebTestClient //autoconfigures the webclient
@ActiveProfiles("test")
@Slf4j
class ProductControllerTest { class ProductControllerTest {
@BeforeEach @Autowired
void setUp() { private WebTestClient client;
@Test
void testProductInvalidSkuNotFound() {
client
.get()
.uri("/products/".concat("{sku}"), "invalid")
.exchange()
.expectStatus()
.is5xxServerError();
} }
@Test @Test
void getProductDto() { void testProductBySkuFound() {
String sku = "100000";
client.get()
.uri("/products/".concat("/{sku}"), sku)
.exchange()
.expectStatus()
.isOk()
.expectBody()
.jsonPath("$.sku", sku);
} }
} }
\ No newline at end of file
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