Skip to content

Commit

Permalink
test: add order consumer test on api-gateway
Browse files Browse the repository at this point in the history
  • Loading branch information
jangjunha committed Sep 11, 2023
1 parent a352691 commit 8444c87
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 0 deletions.
16 changes: 16 additions & 0 deletions .github/workflows/publish-pact.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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/
Expand Down
9 changes: 9 additions & 0 deletions ftgo-api-gateway/README.md
Original file line number Diff line number Diff line change
@@ -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/
Original file line number Diff line number Diff line change
@@ -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)
}
}

0 comments on commit 8444c87

Please sign in to comment.