Commit 814be431 authored by John Lam's avatar John Lam

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

# Conflicts:
#   src/main/java/com/nisum/ascend/inventory/controller/ProductController.java
#   src/main/java/com/nisum/ascend/inventory/service/ProductService.java
#   src/test/java/com/nisum/ascend/inventory/controller/ProductControllerTest.java
parents de4cf430 89bc8fc7
File added
File added
......@@ -11,6 +11,9 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.nisum.ascend.inventory.service.ProductService;
import reactor.core.publisher.Mono;
import org.springframework.beans.factory.annotation.Autowired;
import reactor.core.publisher.Flux;
@RestController
@RequestMapping("/api/products")
......@@ -23,5 +26,9 @@ public class ProductController {
Mono<ProductDto> monoProd = productService.getProductBySku(sku);
HttpStatus status = monoProd != null ? HttpStatus.OK : HttpStatus.NOT_FOUND;
return new ResponseEntity<>(monoProd, status);
@GetMapping()
public ResponseEntity<Flux<ProductDto>> getAllProducts() {
return ResponseEntity.ok(productService.findAllProducts());
}
}
......@@ -4,6 +4,9 @@ import lombok.AllArgsConstructor;
import lombok.Getter;
import com.nisum.ascend.inventory.model.Product;
import java.util.ArrayList;
import java.util.List;
@Getter
@AllArgsConstructor
public class ProductDto {
......@@ -16,14 +19,14 @@ public class ProductDto {
private String brand;
private String category;
private int stock;
private PromotionDto promotion;
private List<PromotionDto> promotions;
public static ProductDto generateDtoFromProduct(Product product) {
// TODO: Fetch the product's promotion from the Promotions com.nisum.ascend.inventory.service
PromotionDto promotion = new PromotionDto();
List<PromotionDto> promotions = new ArrayList<>();
return new ProductDto(product.getSku(), product.getUpc(), product.getProductName(), product.getProductDescription(), product.getPrice(), product.getProductImageUrl(), product.getBrand(), product.getCategory(), product.getAvailableStock(), promotion);
return new ProductDto(product.getSku(), product.getUpc(), product.getProductName(), product.getProductDescription(), product.getPrice(), product.getProductImageUrl(), product.getBrand(), product.getCategory(), product.getAvailableStock(), promotions);
}
}
......@@ -22,4 +22,11 @@ public class ProductService {
.map(existingProduct -> generateDtoFromProduct(existingProduct))
.switchIfEmpty(Mono.error(new ResourceNotFoundException(HttpStatus.NOT_FOUND, "product not found")));
}
public Flux<ProductDto> findAllProducts() {
return productRepository.findAll()
.map(ProductDto::generateDtoFromProduct);
}
}
......@@ -4,6 +4,8 @@ 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 com.nisum.ascend.inventory.controller.ProductController;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
......@@ -12,6 +14,10 @@ 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.mockito.Mock;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit.jupiter.SpringExtension;
......@@ -21,10 +27,18 @@ import reactor.core.publisher.Mono;
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
import org.springframework.web.reactive.function.client.WebClient;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
@ExtendWith(SpringExtension.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@DirtiesContext
@AutoConfigureWebTestClient //autoconfigures the webclient
@AutoConfigureWebTestClient
@ActiveProfiles("test")
@Slf4j
class ProductControllerTest {
......@@ -60,4 +74,26 @@ class ProductControllerTest {
.expectBody()
.jsonPath("$.sku", sku);
}
@Test
void testFindAllProducts(){
webTestClient.get()
.uri("/api/products")
.exchange()
.expectStatus().isOk()
.expectHeader().contentType(MediaType.APPLICATION_JSON_VALUE)
.expectBodyList(Product.class)
.consumeWith(product ->{
List<Product> products = product.getResponseBody();
products.forEach( p ->{
assertNotNull(p.getSku());
assertNotNull(p.getUpc());
System.out.println("Product SKU: " + p.getSku());
System.out.println("Product UPC: " + p.getUpc());
});
});
}
}
\ 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