Commit cdec8ee3 authored by Sumaiyya Burney's avatar Sumaiyya Burney

Merge branch 'dev' into 'AFP-59'

# Conflicts:
#   src/main/java/com/nisum/ascend/promotions/controller/PromotionsController.java
#   src/main/java/com/nisum/ascend/promotions/exception/RestWebExceptionHandler.java
#   src/test/java/com/nisum/ascend/promotions/controller/PromotionsControllerTest.java
parents 969bcf97 b7b0fe12
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"]
......@@ -2,6 +2,7 @@ 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;
import com.nisum.ascend.promotions.service.PromotionService;
......@@ -36,10 +37,9 @@ public class PromotionsController {
return ResponseEntity.ok().body(responseData);
}
@PostMapping()
public Mono<ResponseEntity<PromotionDto>> createPromotion(@RequestBody Promotion newPromotion){
//TODO: create promotion
return null;
@PostMapping("")
public Mono<PromotionDto> createPromotion(@RequestBody Promotion newPromotion){
return promotionService.createPromotion(newPromotion).onErrorMap(throwable -> new PromotionAlreadyExistsException());
}
@PostMapping("/bulkSearch")
......
package com.nisum.ascend.promotions.exception;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;
@ResponseStatus(value = HttpStatus.NOT_ACCEPTABLE)
public class PromotionAlreadyExistsException extends RuntimeException{
public PromotionAlreadyExistsException(){
super("promotion already existed");
}
}
......@@ -2,6 +2,7 @@ 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;
......@@ -26,4 +27,20 @@ class RestWebExceptionHandler implements WebExceptionHandler {
}
return Mono.error(ex);
}
}
\ No newline at end of file
}
@Component
@Order(-2)
class RestWebExceptionHandler implements WebExceptionHandler {
@Override
public Mono<Void> handle(ServerWebExchange exchange, Throwable ex) {
if (ex instanceof PromotionAlreadyExistsException) {
exchange.getResponse().setStatusCode(HttpStatus.NOT_ACCEPTABLE);
return exchange.getResponse().setComplete();
}
return Mono.error(ex);
}
}
package com.nisum.ascend.promotions.service;
import com.nisum.ascend.promotions.dto.PromotionDto;
import com.nisum.ascend.promotions.model.Promotion;
import com.nisum.ascend.promotions.repository.PromotionRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
......@@ -25,4 +26,8 @@ public class PromotionService {
public Flux<PromotionDto> findPromotionsByProductSku(String sku){
return promotionRepository.findByProductSku(sku).map(PromotionDto::generateDtoFromPromotion);
}
public Mono<PromotionDto> createPromotion(Promotion promotion){
return promotionRepository.save(promotion).map(PromotionDto::generateDtoFromPromotion);
}
}
......@@ -13,6 +13,7 @@ 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 reactor.core.publisher.Mono;
import java.util.List;
......@@ -38,11 +39,10 @@ class PromotionsControllerTest {
@Test
void getAllPromotions() {
webTestClient.get().uri("http://localhost:8081/api/promos").exchange()
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;
......@@ -61,5 +61,53 @@ class PromotionsControllerTest {
.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")
.contentType(MediaType.valueOf(MediaType.APPLICATION_JSON_VALUE))
.body(Mono.just(promotion),Promotion.class)
.exchange()
.expectStatus().isOk()
.expectBody()
.jsonPath("$.promotionId","50OFF");
}
@Test
void getPromotionsByProductSku(){
String skuWithOnePromo = "AFP-1";
String skuWithMultiplePromos = "AFP-2";
webTestClient.get()
.uri(uriBuilder -> uriBuilder
.path("/api/promos")
.queryParam("sku", skuWithOnePromo)
.build())
.exchange()
.expectStatus().isOk()
.expectHeader().contentType(MediaType.APPLICATION_JSON_VALUE)
.expectBodyList(PromotionDto.class)
.hasSize(1)
.consumeWith(promo ->{
List<PromotionDto> promos = promo.getResponseBody();
assert promos != null;
promos.forEach(p -> assertEquals(skuWithOnePromo, p.getProductSku()));
});
webTestClient.get()
.uri(uriBuilder -> uriBuilder
.path("/api/promos")
.queryParam("sku", skuWithMultiplePromos)
.build())
.exchange()
.expectStatus().isOk()
.expectHeader().contentType(MediaType.APPLICATION_JSON_VALUE)
.expectBodyList(PromotionDto.class)
.hasSize(2)
.consumeWith(promo ->{
List<PromotionDto> promos = promo.getResponseBody();
assert promos != null;
promos.forEach(p -> assertEquals(skuWithMultiplePromos, p.getProductSku()));
});
}
}
\ 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