From 46c181432b0ab0649672ee5d01d6c0d5bb18fa6a Mon Sep 17 00:00:00 2001 From: "Kim, Joo Hyuk" <61615301+JooHyukKim@users.noreply.github.com> Date: Fri, 18 Mar 2022 09:42:02 +0900 Subject: [PATCH 1/2] =?UTF-8?q?Refactor=20:=20=EB=AF=B8=EC=82=AC=EC=9A=A9?= =?UTF-8?q?=20DeliveryPublisher=20=EC=94=BD=ED=81=AC=20=EB=B2=84=EC=A0=84?= =?UTF-8?q?=20=EC=82=AD=EC=A0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../publisher/DeliveryPublisherImpl.java | 69 ------------------- .../publisher/PublisherConfig.java | 14 ---- 2 files changed, 83 deletions(-) delete mode 100644 delivery-info-service/src/main/java/com/inbobwetrust/publisher/DeliveryPublisherImpl.java delete mode 100644 delivery-info-service/src/main/java/com/inbobwetrust/publisher/PublisherConfig.java diff --git a/delivery-info-service/src/main/java/com/inbobwetrust/publisher/DeliveryPublisherImpl.java b/delivery-info-service/src/main/java/com/inbobwetrust/publisher/DeliveryPublisherImpl.java deleted file mode 100644 index c9dc6f2b..00000000 --- a/delivery-info-service/src/main/java/com/inbobwetrust/publisher/DeliveryPublisherImpl.java +++ /dev/null @@ -1,69 +0,0 @@ -package com.inbobwetrust.publisher; - -import com.inbobwetrust.domain.Delivery; -import com.inbobwetrust.exception.RelayClientException; -import com.inbobwetrust.exception.RelayServerException; -import lombok.RequiredArgsConstructor; -import lombok.extern.slf4j.Slf4j; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.http.HttpStatus; -import org.springframework.web.reactive.function.client.ClientResponse; -import org.springframework.web.reactive.function.client.WebClient; -import reactor.core.publisher.Mono; - -@RequiredArgsConstructor -@Slf4j -public class DeliveryPublisherImpl implements DeliveryPublisher { - private final WebClient webClient; - - @Value("${restClient.proxy.shopUrl}") - private String proxyShopUrl; - - @Value("${restClient.proxy.agencyUrl}") - private String proxyAgencyUrl; - - public Mono sendAddDeliveryEvent(Delivery delivery) { - return webClient - .post() - .uri(proxyShopUrl + "/" + delivery.getShopId()) - .body(Mono.just(delivery), Delivery.class) - .retrieve() - .onStatus( - HttpStatus::is4xxClientError, - clientResponse -> this.handleOn4xxStatus(delivery, clientResponse)) - .onStatus( - HttpStatus::is5xxServerError, - serverResponse -> this.handleOn5xxStatus(delivery, serverResponse)) - .bodyToMono(Delivery.class); - } - - public Mono sendSetRiderEvent(Delivery delivery) { - return webClient - .post() - .uri(proxyAgencyUrl + "/" + delivery.getAgencyId()) - .body(Mono.just(delivery), Delivery.class) - .retrieve() - .onStatus( - HttpStatus::is4xxClientError, - clientResponse -> this.handleOn4xxStatus(delivery, clientResponse)) - .onStatus( - HttpStatus::is5xxServerError, - serverResponse -> this.handleOn5xxStatus(delivery, serverResponse)) - .bodyToMono(Delivery.class); - } - - private Mono handleOn5xxStatus( - Delivery delivery, ClientResponse serverResponse) { - log.info("Status code 5XX is : {}", serverResponse.statusCode().value()); - log.info("Error Status 5XX for delivery : {}", delivery); - return Mono.error( - new RelayServerException("Shop operation failed for delivery : " + delivery)); - } - - private Mono handleOn4xxStatus( - Delivery delivery, ClientResponse clientResponse) { - log.info("Status code 4xx is : {}", clientResponse.statusCode().value()); - log.info("Error Status 4XX for delivery : {}", delivery); - return Mono.error(new RelayClientException("Push Event failed for delivery : " + delivery)); - } -} diff --git a/delivery-info-service/src/main/java/com/inbobwetrust/publisher/PublisherConfig.java b/delivery-info-service/src/main/java/com/inbobwetrust/publisher/PublisherConfig.java deleted file mode 100644 index faa3521b..00000000 --- a/delivery-info-service/src/main/java/com/inbobwetrust/publisher/PublisherConfig.java +++ /dev/null @@ -1,14 +0,0 @@ -package com.inbobwetrust.publisher; - -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.web.reactive.function.client.WebClient; - -@Configuration -public class PublisherConfig { - - @Bean - public WebClient webClient(WebClient.Builder builder) { - return builder.build(); - } -} From fb3d8250b4fa7796814561cb34f8d7d0e5363705 Mon Sep 17 00:00:00 2001 From: "Kim, Joo Hyuk" <61615301+JooHyukKim@users.noreply.github.com> Date: Fri, 18 Mar 2022 09:42:37 +0900 Subject: [PATCH 2/2] =?UTF-8?q?Refactor=20:=20=EC=94=BD=ED=81=AC=EB=B2=84?= =?UTF-8?q?=EC=A0=84=20DeliveryPublisher=20UnitTest=20=EC=BC=80=EC=9D=B4?= =?UTF-8?q?=EC=8A=A4=20=EC=82=AD=EC=A0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../publisher/DeliveryPublisherImplTest.java | 177 ------------------ 1 file changed, 177 deletions(-) delete mode 100644 delivery-info-service/src/test/java/com/inbobwetrust/publisher/DeliveryPublisherImplTest.java diff --git a/delivery-info-service/src/test/java/com/inbobwetrust/publisher/DeliveryPublisherImplTest.java b/delivery-info-service/src/test/java/com/inbobwetrust/publisher/DeliveryPublisherImplTest.java deleted file mode 100644 index 0d59e22b..00000000 --- a/delivery-info-service/src/test/java/com/inbobwetrust/publisher/DeliveryPublisherImplTest.java +++ /dev/null @@ -1,177 +0,0 @@ -package com.inbobwetrust.publisher; - -import static com.github.tomakehurst.wiremock.client.WireMock.*; -import static org.junit.jupiter.api.Assertions.assertTrue; - -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; -import com.github.tomakehurst.wiremock.client.WireMock; -import com.inbobwetrust.domain.Delivery; -import com.inbobwetrust.domain.DeliveryStatus; -import com.inbobwetrust.exception.RelayClientException; -import com.inbobwetrust.exception.RelayServerException; -import java.time.LocalDateTime; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.TestInstance; -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.cloud.contract.wiremock.AutoConfigureWireMock; -import org.springframework.http.HttpHeaders; -import org.springframework.http.HttpStatus; -import org.springframework.http.MediaType; -import org.springframework.test.context.ActiveProfiles; -import reactor.test.StepVerifier; - -@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) -@ActiveProfiles("integration") -@AutoConfigureWebTestClient -@AutoConfigureWireMock(port = 0) -@TestInstance(TestInstance.Lifecycle.PER_CLASS) -class DeliveryPublisherImplTest { - @Autowired DeliveryPublisherImpl deliveryPublisher; - - ObjectMapper mapper = new ObjectMapper().registerModule(new JavaTimeModule()); - - @Test - void sendAddDeliveryEvent() throws JsonProcessingException { - // given - var delivery = makeValidDelivery(); - stubFor( - post(urlPathEqualTo("/relay/v1/shop/" + delivery.getShopId())) - .willReturn( - aResponse() - .withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE) - .withBody(mapper.writeValueAsString(delivery)))); - // when - var response = deliveryPublisher.sendAddDeliveryEvent(delivery); - // then - StepVerifier.create(response) - .consumeNextWith(next -> Assertions.assertEquals(next, delivery)) - .verifyComplete(); - WireMock.verify(1, postRequestedFor(urlPathEqualTo("/relay/v1/shop/" + delivery.getShopId()))); - } - - @Test - void sendAddDeliveryEvent_4xx_isClientError() { - // given - var delivery = makeValidDelivery(); - stubFor( - post(urlPathEqualTo("/relay/v1/shop/" + delivery.getShopId())) - .willReturn(aResponse().withStatus(404))); - // when - var response = deliveryPublisher.sendAddDeliveryEvent(delivery); - // then - StepVerifier.create(response) - .expectErrorMatches( - err -> { - assertTrue(err instanceof RelayClientException); - assertTrue(err.getMessage().contains("Push Event failed for delivery")); - return true; - }) - .verify(); - WireMock.verify(1, postRequestedFor(urlPathEqualTo("/relay/v1/shop/" + delivery.getShopId()))); - } - - @Test - void sendAddDeliveryEvent_5xx_isServerError() throws JsonProcessingException { - // given - var delivery = makeValidDelivery(); - stubFor( - post(urlPathEqualTo("/relay/v1/shop/" + delivery.getShopId())) - .willReturn(aResponse().withStatus(HttpStatus.INTERNAL_SERVER_ERROR.value()))); - // when - var response = deliveryPublisher.sendAddDeliveryEvent(delivery); - // then - StepVerifier.create(response) - .expectErrorMatches( - err -> { - assertTrue(err instanceof RelayServerException); - assertTrue(err.getMessage().contains("Shop operation failed for delivery : ")); - return true; - }) - .verify(); - WireMock.verify(1, postRequestedFor(urlPathEqualTo("/relay/v1/shop/" + delivery.getShopId()))); - } - - @Test - void sendSetRiderEvent_success() throws JsonProcessingException { - // given - var delivery = makeValidDelivery(); - stubFor( - post(urlPathEqualTo("/relay/v1/agency/" + delivery.getAgencyId())) - .willReturn( - aResponse() - .withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE) - .withBody(mapper.writeValueAsString(delivery)))); - // when - var response = deliveryPublisher.sendSetRiderEvent(delivery); - // then - StepVerifier.create(response).expectNext(delivery).verifyComplete(); - WireMock.verify( - 1, postRequestedFor(urlPathEqualTo("/relay/v1/agency/" + delivery.getAgencyId()))); - } - - @Test - void sendSetRiderEvent_4xx_clientError() throws JsonProcessingException { - // given - var delivery = makeValidDelivery(); - stubFor( - post(urlPathEqualTo("/relay/v1/agency/" + delivery.getAgencyId())) - .willReturn(aResponse().withStatus(404))); - // when - var response = deliveryPublisher.sendSetRiderEvent(delivery); - // then - StepVerifier.create(response) - .expectErrorMatches( - err -> { - assertTrue(err instanceof RelayClientException); - assertTrue(err.getMessage().contains("Push Event failed for delivery")); - return true; - }) - .verify(); - WireMock.verify( - 1, postRequestedFor(urlPathEqualTo("/relay/v1/agency/" + delivery.getAgencyId()))); - } - - @Test - void sendSetRiderEvent_5xx_serverError() throws JsonProcessingException { - // given - var delivery = makeValidDelivery(); - stubFor( - post(urlPathEqualTo("/relay/v1/agency/" + delivery.getAgencyId())) - .willReturn(aResponse().withStatus(HttpStatus.INTERNAL_SERVER_ERROR.value()))); - // when - var response = deliveryPublisher.sendSetRiderEvent(delivery); - // then - StepVerifier.create(response) - .expectErrorMatches( - err -> { - assertTrue(err instanceof RelayServerException); - assertTrue(err.getMessage().contains("Shop operation failed for delivery : ")); - return true; - }) - .verify(); - WireMock.verify( - 1, postRequestedFor(urlPathEqualTo("/relay/v1/agency/" + delivery.getAgencyId()))); - } - - private Delivery makeValidDelivery() { - return Delivery.builder() - .id("id-1234") - .orderId("order1") - .shopId("shop-1234") - .riderId("rider-1234") - .agencyId("agency-1234") - .customerId("customer-1234") - .address("서울시 강남구...") - .phoneNumber("01031583977") - .deliveryStatus(DeliveryStatus.ACCEPTED) - .orderTime(LocalDateTime.now().minusMinutes(1)) - .pickupTime(LocalDateTime.now().plusMinutes(30)) - .finishTime(LocalDateTime.now().plusMinutes(60)) - .build(); - } -}