Skip to content

Commit

Permalink
test: add contract to restaurant at kitchen svc
Browse files Browse the repository at this point in the history
  • Loading branch information
jangjunha committed Sep 15, 2023
1 parent 410e591 commit 6584b6f
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,13 @@
import me.jangjunha.ftgo.kitchen_service.domain.RestaurantDetailsVerificationException;
import me.jangjunha.ftgo.kitchen_service.domain.Ticket;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.UUID;

import static io.eventuate.tram.commands.consumer.CommandHandlerReplyBuilder.withFailure;
import static io.eventuate.tram.commands.consumer.CommandHandlerReplyBuilder.withSuccess;
import static io.eventuate.tram.sagas.participant.SagaReplyMessageBuilder.withLock;

@Component
public class KitchenServiceCommandHandler {
private final KitchenService kitchenService;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,11 @@
import me.jangjunha.ftgo.restaurant_service.api.events.RestaurantCreated;
import me.jangjunha.ftgo.restaurant_service.api.events.RestaurantMenuRevised;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

@Component
public class KitchenServiceEventConsumer {
private final KitchenService kitchenService;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import io.eventuate.tram.sagas.participant.SagaCommandDispatcherFactory;
import io.eventuate.tram.sagas.spring.participant.SagaParticipantConfiguration;
import io.eventuate.tram.spring.commands.common.TramCommandsCommonAutoConfiguration;
import io.eventuate.tram.spring.consumer.common.TramNoopDuplicateMessageDetectorConfiguration;
import io.eventuate.tram.spring.events.common.TramEventsCommonAutoConfiguration;
import io.eventuate.tram.spring.events.publisher.TramEventsPublisherConfiguration;
import io.eventuate.tram.spring.events.subscriber.TramEventSubscriberConfiguration;
Expand All @@ -29,7 +28,7 @@
// configure default CommandNameMapping
TramCommandsCommonAutoConfiguration.class,
})
public class KitchenServiceConfiguration {
public class KitchenServiceMessagingConfiguration {
@Bean
public DomainEventDispatcher domainEventDispatcher(
KitchenServiceEventConsumer kitchenServiceEventConsumer,
Expand All @@ -41,12 +40,26 @@ public DomainEventDispatcher domainEventDispatcher(
@Bean
@Autowired
public CommandDispatcher commandDispatcher(
KitchenServiceCommandHandler kitchenServiceCommandHandler,
SagaCommandDispatcherFactory sagaCommandDispatcherFactory
){
KitchenServiceCommandHandler kitchenServiceCommandHandler,
SagaCommandDispatcherFactory sagaCommandDispatcherFactory
) {
return sagaCommandDispatcherFactory.make(
"kitchenServiceCommandDispatcher",
kitchenServiceCommandHandler.commandHandlers()
"kitchenServiceCommandDispatcher",
kitchenServiceCommandHandler.commandHandlers()
);
}

@Bean
public KitchenServiceEventConsumer kitchenServiceEventConsumer(
KitchenService kitchenService
) {
return new KitchenServiceEventConsumer(kitchenService);
}

@Bean
public KitchenServiceCommandHandler kitchenServiceCommandHandler(
KitchenService kitchenService
) {
return new KitchenServiceCommandHandler(kitchenService);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import me.jangjunha.ftgo.kitchen_service.domain.Ticket;
import me.jangjunha.ftgo.kitchen_service.service.KitchenService;
import me.jangjunha.ftgo.kitchen_service.service.KitchenServiceCommandHandler;
import me.jangjunha.ftgo.kitchen_service.service.KitchenServiceConfiguration;
import me.jangjunha.ftgo.kitchen_service.service.KitchenServiceMessagingConfiguration;
import me.jangjunha.ftgo.kitchen_service.service.KitchenServiceEventConsumer;
import me.jangjunha.ftgo.pact.provider.junitsupport.filter.ByInteractionType;
import org.junit.jupiter.api.BeforeEach;
Expand Down Expand Up @@ -148,7 +148,7 @@ void setUp(PactVerificationContext context) {
@EnableAutoConfiguration
@Import({
TramSagaInMemoryConfiguration.class,
KitchenServiceConfiguration.class,
KitchenServiceMessagingConfiguration.class,
})
static class TestConfiguration {
@Bean
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
package me.jangjunha.ftgo.kitchen_service;

import au.com.dius.pact.consumer.MessagePactBuilder;
import au.com.dius.pact.consumer.dsl.PactDslJsonBody;
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.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 io.eventuate.tram.events.subscriber.DomainEventDispatcher;
import io.eventuate.tram.messaging.common.Message;
import io.eventuate.tram.messaging.common.MessageImpl;
import io.eventuate.tram.spring.consumer.common.TramNoopDuplicateMessageDetectorConfiguration;
import io.eventuate.tram.spring.inmemory.TramInMemoryCommonConfiguration;
import me.jangjunha.ftgo.common.Money;
import me.jangjunha.ftgo.kitchen_service.domain.MenuItem;
import me.jangjunha.ftgo.kitchen_service.service.KitchenService;
import me.jangjunha.ftgo.kitchen_service.service.KitchenServiceMessagingConfiguration;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.test.context.junit.jupiter.SpringExtension;

import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;

import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.verify;

@ExtendWith(SpringExtension.class)
@ExtendWith(PactConsumerTestExt.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE)
@PactTestFor(providerName = "ftgo-restaurant-service", providerType = ProviderType.ASYNCH, pactVersion = PactSpecVersion.V4)
public class RestaurantServicePactTest {

@MockBean
private KitchenService kitchenService;

@Autowired
DomainEventDispatcher domainEventDispatcher;

@Pact(consumer = "ftgo-kitchen-service")
V4Pact restaurantCreated(MessagePactBuilder builder) {
return builder
.expectsToReceive("`RestaurantCreated` event")
.withMetadata(
Map.of(
"event-aggregate-type", "me.jangjunha.ftgo.restaurant_service.domain.Restaurant",
"event-type", "me.jangjunha.ftgo.restaurant_service.api.events.RestaurantCreated",
"event-aggregate-id", "97e3c4c2-f336-4435-9314-ad1a633495df",
"ID", ""
)
)
.withContent(new PactDslJsonBody()
.stringType("name", "A Cafe")
.eachLike("menuItems")
.stringType("id", "americano")
.stringType("name", "Americano")
.object("price", new PactDslJsonBody().numberType("amount", 2500))
)
.toPact();
}

@Test
@PactTestFor(pactMethod = "restaurantCreated")
void testConsumeRestaurantCreated(V4Interaction.AsynchronousMessage interaction) {
Message message = new MessageImpl(interaction.contentsAsString(), interaction.getMetadata().entrySet().stream().collect(Collectors.toMap(
Map.Entry::getKey,
e -> e.getValue().toString()
)));
domainEventDispatcher.messageHandler(message);

verify(kitchenService).upsertRestaurant(
eq(UUID.fromString("97e3c4c2-f336-4435-9314-ad1a633495df")),
eq(List.of(
new MenuItem("americano", "Americano", new Money("2500"))
))
);
}

@Pact(consumer = "ftgo-kitchen-service")
V4Pact restaurantMenuRevised(MessagePactBuilder builder) {
return builder
.expectsToReceive("`RestaurantMenuRevised` event")
.withMetadata(
Map.of(
"event-aggregate-type", "me.jangjunha.ftgo.restaurant_service.domain.Restaurant",
"event-type", "me.jangjunha.ftgo.restaurant_service.api.events.RestaurantMenuRevised",
"event-aggregate-id", "97e3c4c2-f336-4435-9314-ad1a633495df",
"ID", ""
)
)
.withContent(new PactDslJsonBody()
.eachLike("menuItems")
.stringType("id", "americano")
.stringType("name", "Americano")
.object("price", new PactDslJsonBody().numberType("amount", 2500))
)
.toPact();
}

@Test
@PactTestFor(pactMethod = "restaurantMenuRevised")
void testConsumeRestaurantMenuRevised(V4Interaction.AsynchronousMessage interaction) {
Message message = new MessageImpl(interaction.contentsAsString(), interaction.getMetadata().entrySet().stream().collect(Collectors.toMap(
Map.Entry::getKey,
e -> e.getValue().toString()
)));
domainEventDispatcher.messageHandler(message);

verify(kitchenService).upsertRestaurant(
eq(UUID.fromString("97e3c4c2-f336-4435-9314-ad1a633495df")),
eq(List.of(
new MenuItem("americano", "Americano", new Money("2500"))
))
);
}

@Configuration
@EnableAutoConfiguration
@Import({
KitchenServiceMessagingConfiguration.class,
TramInMemoryCommonConfiguration.class,
TramNoopDuplicateMessageDetectorConfiguration.class,
})
static class TestConfiguration {
}
}

0 comments on commit 6584b6f

Please sign in to comment.