From 8444c879aa54d217dcfc5ff980071996fdf25bc3 Mon Sep 17 00:00:00 2001 From: jangjunha Date: Mon, 11 Sep 2023 19:58:47 +0900 Subject: [PATCH] test: add order consumer test on api-gateway --- .github/workflows/publish-pact.yml | 16 ++++ ftgo-api-gateway/README.md | 9 +++ .../proxies/OrderServicePactTest.kt | 80 +++++++++++++++++++ 3 files changed, 105 insertions(+) create mode 100644 ftgo-api-gateway/README.md create mode 100644 ftgo-api-gateway/src/test/kotlin/me/jangjunha/ftgo/apigateway/proxies/OrderServicePactTest.kt diff --git a/.github/workflows/publish-pact.yml b/.github/workflows/publish-pact.yml index 7a79962..e6b8983 100644 --- a/.github/workflows/publish-pact.yml +++ b/.github/workflows/publish-pact.yml @@ -8,16 +8,32 @@ jobs: ftgo-api-gateway: runs-on: ubuntu-latest environment: pact + defaults: + run: + working-directory: ftgo/ steps: + - name: Checkout ftgo-proto repository + uses: actions/checkout@v4 + with: + repository: jangjunha/ftgo-proto + path: ftgo-proto/ - uses: actions/checkout@v4 + with: + path: ftgo/ + - uses: actions/setup-java@v3 with: distribution: liberica java-version: 17 - name: Setup Gradle uses: gradle/gradle-build-action@v2 + - uses: jangjunha/pact-broker-actions/install-plugin@main + with: + source: protobuf + - name: Execute Gradle test run: ./gradlew -p ftgo-api-gateway test --tests '*PactTest' + - uses: jangjunha/pact-broker-actions/publish@main with: target: ftgo-api-gateway/build/pacts/ diff --git a/ftgo-api-gateway/README.md b/ftgo-api-gateway/README.md new file mode 100644 index 0000000..69a38d1 --- /dev/null +++ b/ftgo-api-gateway/README.md @@ -0,0 +1,9 @@ +# FTGO API Gateway + +Spring Cloud Gateway를 사용해 구현한 FTGO의 API Gateway + +## Prerequisites + +* Pact 테스트 실행을 위해 `ftgo`(root project)와 같은 디렉토리에 [`ftgo-proto`][ftgo-proto] 프로젝트가 존재해야합니다. + +[ftgo-proto]: https://github.com/jangjunha/ftgo-proto/ diff --git a/ftgo-api-gateway/src/test/kotlin/me/jangjunha/ftgo/apigateway/proxies/OrderServicePactTest.kt b/ftgo-api-gateway/src/test/kotlin/me/jangjunha/ftgo/apigateway/proxies/OrderServicePactTest.kt new file mode 100644 index 0000000..d9818b8 --- /dev/null +++ b/ftgo-api-gateway/src/test/kotlin/me/jangjunha/ftgo/apigateway/proxies/OrderServicePactTest.kt @@ -0,0 +1,80 @@ +package me.jangjunha.ftgo.apigateway.proxies + +import au.com.dius.pact.consumer.MockServer +import au.com.dius.pact.consumer.dsl.BuilderUtils.filePath +import au.com.dius.pact.consumer.dsl.PactBuilder +import au.com.dius.pact.consumer.junit.MockServerConfig +import au.com.dius.pact.consumer.junit5.PactConsumerTestExt +import au.com.dius.pact.consumer.junit5.PactTestFor +import au.com.dius.pact.consumer.junit5.ProviderType +import au.com.dius.pact.consumer.model.MockServerImplementation +import au.com.dius.pact.core.model.PactSpecVersion +import au.com.dius.pact.core.model.V4Interaction +import au.com.dius.pact.core.model.V4Pact +import au.com.dius.pact.core.model.annotations.Pact +import kotlinx.coroutines.runBlocking +import me.jangjunha.ftgo.apigateway.Destinations +import me.jangjunha.ftgo.order_service.api.Order +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.extension.ExtendWith +import java.util.* + +@ExtendWith(PactConsumerTestExt::class) +@PactTestFor(providerName = "ftgo-order-service", providerType = ProviderType.SYNCH_MESSAGE, pactVersion = PactSpecVersion.V4) +@MockServerConfig(implementation = MockServerImplementation.Plugin, registryEntry = "protobuf/transport/grpc") +class OrderServicePactTest { + + @Pact(consumer = "ftgo-api-gateway") + fun findOrderById(builder: PactBuilder): V4Pact = builder + .usingPlugin("protobuf") + .expectsToReceive("order details", "core/interaction/synchronous-message") + .with(mapOf( + Pair("pact:proto", filePath("../../ftgo-proto/protos/orders.proto")), + Pair("pact:content-type", "application/grpc"), + Pair("pact:proto-service", "OrderService/getOrder"), + Pair("request", mapOf( + Pair("id", "6f2d06a3-5dd2-4096-8644-6084d64eae35"), + )), + Pair("response", mapOf( + Pair("id", "matching(equalTo, '6f2d06a3-5dd2-4096-8644-6084d64eae35')"), + Pair("state", "matching(type, 'APPROVAL_PENDING')"), + Pair("consumerId", "matching(equalTo, '627a9a8a-41af-4daf-a968-00ffc80b53ad')"), + Pair("restaurantId", "matching(equalTo, '97e3c4c2-f336-4435-9314-ad1a633495df')"), + Pair("lineItems", listOf( + mapOf( + Pair("quantity", "matching(number, 2)"), + Pair("menuItemId", "matching(equalTo, 'latte')"), + Pair("name", "matching(equalTo, 'Cafe Latte')"), + Pair("price", mapOf( + Pair("amount", "matching(equalTo, '3500')") + )), + ), + )), + Pair("deliveryInformation", mapOf( + Pair("deliveryTime", mapOf( + Pair("seconds", "matching(number, 0)"), + Pair("nanos", "matching(number, 0)"), + )), + Pair("deliveryAddress", "matching(equalTo, '서울시 강남구 테헤란로 1')") + )), + Pair("orderMinimum", mapOf( + Pair("amount", "matching(equalTo, '2147483647')") + )), + )), + )) + .toPact() + + @Test + @PactTestFor(pactMethod = "findOrderById") + fun testOrderById(mockServer: MockServer, interaction: V4Interaction.SynchronousMessages) = runBlocking { + val service = OrderService(Destinations( + orderServiceUrl = "127.0.0.1:${mockServer.getPort()}", + orderHistoryServiceUrl = "", + kitchenServiceUrl = "", + restaurantServiceUrl = "", + )) + val expected = Order.parseFrom(interaction.response[0].contents.value) + val order = service.findOrderById(UUID.fromString("6f2d06a3-5dd2-4096-8644-6084d64eae35")) + assert(expected == order) + } +}