Commit d7bab1e6 authored by Sumaiyya Burney's avatar Sumaiyya Burney

Merge branch 'dev' into 'AFP-62'

# Conflicts:
#   src/main/java/com/nisum/ascend/promotions/repository/PromotionRepository.java
#   src/test/java/com/nisum/ascend/promotions/controller/PromotionsControllerTest.java
parents 3132ce08 f80b22ac
File added
FROM maven:3.6.0-jdk-11-slim AS build
COPY src /home/app/src
COPY pom.xml /home/app
RUN mvn -f /home/app/pom.xml clean package -DskipTests
FROM openjdk:11-jre-slim
COPY --from=build /home/app/target/promotions-0.0.1-SNAPSHOT.jar /usr/local/lib/promotions.jar
EXPOSE 8081
ENTRYPOINT ["java","-jar","/usr/local/lib/promotions.jar"]
File added
package com.nisum.ascend.promotions.controller;
import com.nisum.ascend.promotions.dto.PromotionDto;
import com.nisum.ascend.promotions.exception.PromotionNotFoundException;
import com.nisum.ascend.promotions.exception.PromotionAlreadyExistsException;
import com.nisum.ascend.promotions.model.Promotion;
import com.nisum.ascend.promotions.repository.PromotionRepository;
......@@ -12,6 +13,7 @@ import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import java.util.ArrayList;
import java.util.List;
@RestController
@RequestMapping("/api/promos")
......@@ -29,9 +31,11 @@ public class PromotionsController {
}
@GetMapping("/{id}")
public Mono<ResponseEntity<PromotionDto>> getPromotionById(@PathVariable String id){
//TODO: get promo by ID
return null;
public ResponseEntity<Mono<PromotionDto>> getPromotionById(@PathVariable String id){
Mono<PromotionDto> responseData = promotionService
.findPromoById(id)
.switchIfEmpty(Mono.error(new PromotionNotFoundException(id)));
return ResponseEntity.ok().body(responseData);
}
@PostMapping("")
......@@ -40,9 +44,8 @@ public class PromotionsController {
}
@PostMapping("/bulkSearch")
public Flux<ResponseEntity<PromotionDto>> bulkSearchPromotionsByItemSku(){
//TODO: bulk search by list of item SKUs
return null;
public ResponseEntity<Flux<PromotionDto>> bulkSearchPromotionsByItemSku(@RequestBody List<String> skus){
return ResponseEntity.ok(promotionService.bulkSearchSku(skus));
}
@PutMapping("/{promoId}")
......
package com.nisum.ascend.promotions.exception;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;
@ResponseStatus(value = HttpStatus.NOT_FOUND)
public class PromotionNotFoundException extends RuntimeException {
public PromotionNotFoundException(String id) {
super("The promotion with the ID of " + id + " was not found.");
}
}
package com.nisum.ascend.promotions.exception;
import org.springframework.core.annotation.Order;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.WebExceptionHandler;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import springfox.documentation.spring.web.json.Json;
import java.nio.charset.StandardCharsets;
@Component
@Order(-2)
class RestWebExceptionHandler implements WebExceptionHandler {
@Override
public Mono<Void> handle(ServerWebExchange exchange, Throwable ex) {
if (ex instanceof PromotionNotFoundException) {
exchange.getResponse().setStatusCode(HttpStatus.NOT_FOUND);
DataBuffer buffer = exchange.getResponse().bufferFactory().wrap(ex.getMessage().getBytes(StandardCharsets.UTF_8));
// marks the response as complete and forbids writing to it
return exchange.getResponse().writeWith(Flux.just(buffer));
}
return Mono.error(ex);
}
}
@Component
@Order(-2)
......
......@@ -6,6 +6,7 @@ import com.nisum.ascend.promotions.repository.PromotionRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Flux;
import java.util.List;
import reactor.core.publisher.Mono;
@Service
......@@ -18,10 +19,19 @@ public class PromotionService {
return promotionRepository.findAll().map(PromotionDto::generateDtoFromPromotion);
}
public Mono<PromotionDto> findPromoById(String promoId) {
return promotionRepository.findByPromotionId(promoId)
.map(PromotionDto::generateDtoFromPromotion);
}
public Flux<PromotionDto> findPromotionsByProductSku(String sku){
return promotionRepository.findByProductSku(sku).map(PromotionDto::generateDtoFromPromotion);
}
public Flux<PromotionDto> bulkSearchSku(List<String> skus){
return Flux.fromIterable(skus).flatMap(sku -> findPromotionsByProductSku(sku));
}
public Mono<PromotionDto> createPromotion(Promotion promotion){
return promotionRepository.save(promotion).map(PromotionDto::generateDtoFromPromotion);
}
......
package com.nisum.ascend.promotions.controller;
import com.nisum.ascend.promotions.dto.PromotionDto;
import com.nisum.ascend.promotions.model.Promotion;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
......@@ -16,6 +15,7 @@ import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.web.reactive.server.WebTestClient;
import reactor.core.publisher.Mono;
import java.util.Arrays;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
......@@ -29,16 +29,21 @@ import static org.junit.jupiter.api.Assertions.*;
class PromotionsControllerTest {
@Autowired
private WebTestClient webTestClient;
@BeforeEach
void setUp() {
}
WebTestClient.ResponseSpec getPromotionWebClient(String promoId) {
return webTestClient.get().uri("/api/promos/" + promoId).exchange();
}
@Test
void getAllPromotions() {
webTestClient.get().uri("/api/promos").exchange()
.expectStatus().isOk()
.expectHeader().contentType(MediaType.APPLICATION_JSON_VALUE)
.expectBodyList(PromotionDto.class)
// .hasSize(2)
.consumeWith(promo ->{
List<PromotionDto> promos = promo.getResponseBody();
assert promos != null;
......@@ -51,6 +56,13 @@ class PromotionsControllerTest {
}
@Test
void getPromotionById() {
getPromotionWebClient("0003")
.expectStatus().isOk()
.expectHeader().contentType(MediaType.APPLICATION_JSON_VALUE)
.expectBody()
.jsonPath("promotionId", "0003");
void createPromotion(){
Promotion promotion = new Promotion("50OFF","SH1234", (float) 0.5,5);
webTestClient.post().uri("/api/promos")
......@@ -113,4 +125,32 @@ class PromotionsControllerTest {
.jsonPath("$.minimumQuantity").isEqualTo(1);
}
@Test
public void bulkSearchTest(){
List<String> skus = Arrays.asList("AFP-1", "AFP-2");
webTestClient.post().uri("/api/promos/bulkSearch")
.contentType(MediaType.valueOf(MediaType.APPLICATION_JSON_VALUE))
.body(Mono.just(skus), List.class)
.exchange()
.expectStatus().isOk()
.expectBodyList(PromotionDto.class)
.consumeWith(promo ->{
List<PromotionDto> promos = promo.getResponseBody();
assert promos != null;
promos.forEach(p -> {
String sku = p.getProductSku();
String promotionId = p.getPromotionId();
assertTrue(skus.contains(sku));
System.out.println("Product SKU: " + sku);
System.out.println("Promotion ID: " + promotionId);
} );
});
}
}
\ 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