Skip to content

Commit

Permalink
Merge branch 'f-lab-edu#198-Subscriber-구현체-작성' of https://github.com/…
Browse files Browse the repository at this point in the history
…JooHyukKim/in-bob-we-trust into f-lab-edu#198-Subscriber-구현체-작성
  • Loading branch information
JooHyukKim committed Mar 18, 2022
2 parents 7e5e822 + 3f6472c commit cd0ff3b
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 145 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
package com.inbobwetrust.controller;

import static com.inbobwetrust.controller.TestParameterGenerator.generate;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.inbobwetrust.domain.Delivery;
import com.inbobwetrust.domain.DeliveryStatus;
import com.inbobwetrust.publisher.DeliveryPublisher;
import com.inbobwetrust.repository.DeliveryRepository;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Random;
import java.util.stream.Stream;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
Expand All @@ -32,35 +42,20 @@
import org.testcontainers.junit.jupiter.Testcontainers;
import reactor.test.StepVerifier;

import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Random;
import java.util.stream.Stream;

import static com.inbobwetrust.controller.TestParameterGenerator.generate;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@AutoConfigureWebTestClient
@Testcontainers
@ContextConfiguration
public class DeliveryControllerIntgrationTest {
private static final String DEFAULT_MONGO_DATABASE = "inbob";

@Autowired
WebTestClient testClient;
@Autowired WebTestClient testClient;

@Autowired
DeliveryRepository deliveryRepository;
@Autowired DeliveryRepository deliveryRepository;

@SpyBean
DeliveryPublisher deliveryPublisher;
@SpyBean DeliveryPublisher deliveryPublisher;

@SpyBean
AmqpTemplate amqpTemplate;
@SpyBean AmqpTemplate amqpTemplate;

ObjectMapper mapper = new ObjectMapper().registerModule(new JavaTimeModule());

Expand All @@ -76,7 +71,7 @@ static Stream<Arguments> possibleAllDelivery() {

@Container
private static RabbitMQContainer container =
new RabbitMQContainer("rabbitmq:3.7.25-management-alpine");
new RabbitMQContainer("rabbitmq:3.7.25-management-alpine");

@BeforeAll
static void beforeAll() {
Expand All @@ -97,10 +92,10 @@ void setUp() {

static String extractSimpleMongoUri(GenericContainer<?> container) {
return String.format(
"mongodb://%s:%d/%s",
container.getHost(),
container.getMappedPort(MongoProperties.DEFAULT_PORT),
DEFAULT_MONGO_DATABASE);
"mongodb://%s:%d/%s",
container.getHost(),
container.getMappedPort(MongoProperties.DEFAULT_PORT),
DEFAULT_MONGO_DATABASE);
}

@DisplayName("[서버-신규주문수신]")
Expand All @@ -110,16 +105,16 @@ void sendAddDeliveryEvent_success(Delivery delivery) throws JsonProcessingExcept
// Arrange
// Act
var actual =
testClient
.post()
.uri("/api/delivery")
.bodyValue(delivery)
.exchange()
.expectStatus()
.isOk()
.expectBody(Delivery.class)
.returnResult()
.getResponseBody();
testClient
.post()
.uri("/api/delivery")
.bodyValue(delivery)
.exchange()
.expectStatus()
.isOk()
.expectBody(Delivery.class)
.returnResult()
.getResponseBody();
delivery.setId(Objects.requireNonNull(actual).getId());
var savedCnt = deliveryRepository.findAll();
// Assert
Expand All @@ -141,28 +136,28 @@ void acceptDelivery(Delivery delivery) throws JsonProcessingException {
// Act
if (!expected.getOrderTime().isBefore(expected.getPickupTime())) {
var responseBody =
testClient
.put()
.uri("/api/delivery/accept")
.bodyValue(expected)
.exchange()
.expectStatus()
.isEqualTo(HttpStatus.BAD_REQUEST)
.expectBody(String.class)
.returnResult();
testClient
.put()
.uri("/api/delivery/accept")
.bodyValue(expected)
.exchange()
.expectStatus()
.isEqualTo(HttpStatus.BAD_REQUEST)
.expectBody(String.class)
.returnResult();
return;
}
var responseBody =
testClient
.put()
.uri("/api/delivery/accept")
.bodyValue(expected)
.exchange()
.expectStatus()
.isOk()
.expectBody(Delivery.class)
.returnResult()
.getResponseBody();
testClient
.put()
.uri("/api/delivery/accept")
.bodyValue(expected)
.exchange()
.expectStatus()
.isOk()
.expectBody(Delivery.class)
.returnResult()
.getResponseBody();
// Assert
Assertions.assertEquals(expected, responseBody);
// WireMock.verify(1, postRequestedFor(urlPathEqualTo(testUrl)));
Expand All @@ -178,31 +173,31 @@ void setDeliveryRider(Delivery delivery) {
// Act
if (expected.getDeliveryStatus().equals(DeliveryStatus.ACCEPTED)) {
var actual =
testClient
testClient
.put()
.uri("/api/delivery/rider")
.bodyValue(expected)
.exchange()
.expectStatus()
.isOk()
.expectBody(Delivery.class)
.returnResult()
.getResponseBody();
// Assert
expected.copyTimeFields(actual);
Assertions.assertEquals(expected, actual);
} else {
// Act & Assert
testClient
.put()
.uri("/api/delivery/rider")
.bodyValue(expected)
.exchange()
.expectStatus()
.isOk()
.expectBody(Delivery.class)
.isBadRequest()
.expectBody(String.class)
.returnResult()
.getResponseBody();
// Assert
expected.copyTimeFields(actual);
Assertions.assertEquals(expected, actual);
} else {
// Act & Assert
testClient
.put()
.uri("/api/delivery/rider")
.bodyValue(expected)
.exchange()
.expectStatus()
.isBadRequest()
.expectBody(String.class)
.returnResult()
.getResponseBody();
}
}

Expand All @@ -218,29 +213,29 @@ void setPickedUp(Delivery delivery) {
if (before.getDeliveryStatus().equals(DeliveryStatus.ACCEPTED)) {
// Act
var actual =
testClient
.put()
.uri("/api/delivery/pickup")
.bodyValue(expected)
.exchange()
.expectStatus()
.isOk()
.expectBody(Delivery.class)
.returnResult()
.getResponseBody();
testClient
.put()
.uri("/api/delivery/pickup")
.bodyValue(expected)
.exchange()
.expectStatus()
.isOk()
.expectBody(Delivery.class)
.returnResult()
.getResponseBody();
// Assert
expected.copyTimeFields(actual);
Assertions.assertEquals(expected, actual);
} else {
// Act
testClient
.put()
.uri("/api/delivery/pickup")
.bodyValue(expected)
.exchange()
.expectStatus()
.isBadRequest()
.expectBody(String.class);
.put()
.uri("/api/delivery/pickup")
.bodyValue(expected)
.exchange()
.expectStatus()
.isBadRequest()
.expectBody(String.class);
}
}

Expand All @@ -255,29 +250,29 @@ void setComplete(Delivery delivery) {
expected.setDeliveryStatus(DeliveryStatus.COMPLETE);
// Act
var actual =
testClient
.put()
.uri("/api/delivery/complete")
.bodyValue(expected)
.exchange()
.expectStatus()
.isOk()
.expectBody(Delivery.class)
.returnResult()
.getResponseBody();
testClient
.put()
.uri("/api/delivery/complete")
.bodyValue(expected)
.exchange()
.expectStatus()
.isOk()
.expectBody(Delivery.class)
.returnResult()
.getResponseBody();
// Assert
expected.copyTimeFields(actual);
Assertions.assertEquals(expected, actual);
} else {
// Act
testClient
.put()
.uri("/api/delivery/complete")
.bodyValue(expected)
.exchange()
.expectStatus()
.isBadRequest()
.expectBody(String.class);
.put()
.uri("/api/delivery/complete")
.bodyValue(expected)
.exchange()
.expectStatus()
.isBadRequest()
.expectBody(String.class);
}
}

Expand All @@ -291,22 +286,22 @@ void getDeliveries(List<Delivery> deliveries) {
int expectedSize = 10;
int page = 3;
var uri =
UriComponentsBuilder.fromUriString("/api/delivery")
.queryParam("page", page)
.queryParam("size", expectedSize)
.buildAndExpand()
.toUri();
UriComponentsBuilder.fromUriString("/api/delivery")
.queryParam("page", page)
.queryParam("size", expectedSize)
.buildAndExpand()
.toUri();
// Act
var actual =
testClient
.get()
.uri(uri)
.exchange()
.expectStatus()
.isOk()
.expectBodyList(Delivery.class)
.returnResult()
.getResponseBody();
testClient
.get()
.uri(uri)
.exchange()
.expectStatus()
.isOk()
.expectBodyList(Delivery.class)
.returnResult()
.getResponseBody();
// Assert
Assertions.assertEquals(expectedSize, actual.size());
}
Expand All @@ -324,22 +319,22 @@ void getDelivery(Delivery delivery) {
// Act
if (isIdWrong) {
testClient
.get()
.uri("/api/delivery/{id}", delivery.getId())
.exchange()
.expectStatus()
.isEqualTo(HttpStatus.BAD_REQUEST);
} else {
var actual =
testClient
.get()
.uri("/api/delivery/{id}", delivery.getId())
.exchange()
.expectStatus()
.isOk()
.expectBody(Delivery.class)
.returnResult()
.getResponseBody();
.isEqualTo(HttpStatus.BAD_REQUEST);
} else {
var actual =
testClient
.get()
.uri("/api/delivery/{id}", delivery.getId())
.exchange()
.expectStatus()
.isOk()
.expectBody(Delivery.class)
.returnResult()
.getResponseBody();
// Assert
expected.copyTimeFields(actual);
Assertions.assertEquals(expected, actual);
Expand Down
Loading

0 comments on commit cd0ff3b

Please sign in to comment.