Commit f3212ca7 authored by Sumaiyya Burney's avatar Sumaiyya Burney

Merge branch 'AFP-59' into 'dev'

Afp 59

See merge request !4
parents b7b0fe12 cdec8ee3
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;
......@@ -29,9 +30,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("")
......
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)
......
......@@ -5,8 +5,11 @@ import com.nisum.ascend.promotions.model.Promotion;
import org.springframework.data.mongodb.repository.ReactiveMongoRepository;
import org.springframework.stereotype.Repository;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
@Repository
public interface PromotionRepository extends ReactiveMongoRepository<Promotion, String> {
Flux<Promotion> findByProductSku(String sku);
Mono<Promotion> findByPromotionId(String promoId);
}
......@@ -18,6 +18,11 @@ 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);
}
......
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;
......@@ -29,16 +28,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 +55,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")
......
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