Search in sources :

Example 6 with Recipe

use of uk.gov.justice.services.example.cakeshop.domain.aggregate.Recipe in project microservice_framework by CJSCommonPlatform.

the class RecipeCommandHandler method removeRecipe.

@Handles("example.command.remove-recipe")
public void removeRecipe(final JsonEnvelope command) throws EventStreamException {
    LOGGER.trace("=============> Inside remove-recipe Command Handler. RecipeId: " + command.payloadAsJsonObject().getString(FIELD_RECIPE_ID));
    final UUID recipeId = getUUID(command.payloadAsJsonObject(), FIELD_RECIPE_ID).get();
    final EventStream eventStream = eventSource.getStreamById(recipeId);
    final Recipe recipe = aggregateService.get(eventStream, Recipe.class);
    eventStream.append(recipe.removeRecipe().map(toEnvelopeWithMetadataFrom(command)));
}
Also used : Recipe(uk.gov.justice.services.example.cakeshop.domain.aggregate.Recipe) EventStream(uk.gov.justice.services.eventsourcing.source.core.EventStream) UUID(java.util.UUID) JsonObjects.getUUID(uk.gov.justice.services.messaging.JsonObjects.getUUID) Handles(uk.gov.justice.services.core.annotation.Handles)

Example 7 with Recipe

use of uk.gov.justice.services.example.cakeshop.domain.aggregate.Recipe in project microservice_framework by CJSCommonPlatform.

the class MakeCakeCommandHandlerTest method shouldHandleMakeCakeCommand.

@SuppressWarnings("unchecked")
@Test
public void shouldHandleMakeCakeCommand() throws Exception {
    final Recipe recipe = new Recipe();
    final String cakeName = "Chocolate cake";
    recipe.apply(new RecipeAdded(RECIPE_ID, cakeName, false, EMPTY_LIST));
    when(eventSource.getStreamById(RECIPE_ID)).thenReturn(eventStream);
    when(aggregateService.get(eventStream, Recipe.class)).thenReturn(recipe);
    final JsonEnvelope command = envelopeFrom(metadataWithRandomUUID(COMMAND_NAME), createObjectBuilder().add("recipeId", RECIPE_ID.toString()).add("cakeId", CAKE_ID.toString()).build());
    makeCakeCommandHandler.makeCake(command);
    assertThat(eventStream, eventStreamAppendedWith(streamContaining(jsonEnvelope(withMetadataEnvelopedFrom(command).withName(EVENT_NAME), payloadIsJson(allOf(withJsonPath("$.cakeId", equalTo(CAKE_ID.toString())), withJsonPath("$.name", equalTo(cakeName))))).thatMatchesSchema())).withToleranceOf(CONSECUTIVE));
}
Also used : RecipeAdded(uk.gov.justice.services.example.cakeshop.domain.event.RecipeAdded) Recipe(uk.gov.justice.services.example.cakeshop.domain.aggregate.Recipe) JsonEnvelope(uk.gov.justice.services.messaging.JsonEnvelope) Test(org.junit.Test)

Example 8 with Recipe

use of uk.gov.justice.services.example.cakeshop.domain.aggregate.Recipe in project microservice_framework by CJSCommonPlatform.

the class RecipeCommandHandlerTest method shouldHandleAddRecipeCommand.

@Test
public void shouldHandleAddRecipeCommand() throws Exception {
    final Recipe recipe = new Recipe();
    final UUID commandId = randomUUID();
    final JsonEnvelope command = envelopeFrom(metadataOf(commandId, ADD_RECIPE_COMMAND_NAME), createObjectBuilder().add("recipeId", RECIPE_ID.toString()).add("name", RECIPE_NAME).add("glutenFree", GULTEN_FREE).add("ingredients", createArrayBuilder().add(createObjectBuilder().add("name", "Flour").add("quantity", 200))).build());
    when(eventSource.getStreamById(RECIPE_ID)).thenReturn(eventStream);
    when(aggregateService.get(eventStream, Recipe.class)).thenReturn(recipe);
    recipeCommandHandler.addRecipe(command);
    assertThat(eventStream, eventStreamAppendedWith(streamContaining(jsonEnvelope(withMetadataEnvelopedFrom(command).withName(ADD_RECIPE_EVENT_NAME), payloadIsJson(allOf(withJsonPath("$.recipeId", equalTo(RECIPE_ID.toString())), withJsonPath("$.name", equalTo(RECIPE_NAME)), withJsonPath("$.glutenFree", equalTo(GULTEN_FREE)), withJsonPath("$.ingredients.length()", equalTo(1)), withJsonPath("$.ingredients[0].name", equalTo("Flour")), withJsonPath("$.ingredients[0].quantity", equalTo(200))))).thatMatchesSchema())));
}
Also used : Recipe(uk.gov.justice.services.example.cakeshop.domain.aggregate.Recipe) JsonEnvelope(uk.gov.justice.services.messaging.JsonEnvelope) UUID(java.util.UUID) UUID.randomUUID(java.util.UUID.randomUUID) Test(org.junit.Test)

Example 9 with Recipe

use of uk.gov.justice.services.example.cakeshop.domain.aggregate.Recipe in project microservice_framework by CJSCommonPlatform.

the class RecipeCommandHandlerTest method existingRecipe.

private Recipe existingRecipe() {
    final Recipe recipe = new Recipe();
    recipe.apply(new RecipeAdded(RECIPE_ID, RECIPE_NAME, GULTEN_FREE, emptyList()));
    return recipe;
}
Also used : RecipeAdded(uk.gov.justice.services.example.cakeshop.domain.event.RecipeAdded) Recipe(uk.gov.justice.services.example.cakeshop.domain.aggregate.Recipe)

Aggregations

Recipe (uk.gov.justice.services.example.cakeshop.domain.aggregate.Recipe)9 UUID (java.util.UUID)6 Handles (uk.gov.justice.services.core.annotation.Handles)5 EventStream (uk.gov.justice.services.eventsourcing.source.core.EventStream)5 JsonObjects.getUUID (uk.gov.justice.services.messaging.JsonObjects.getUUID)5 Test (org.junit.Test)2 RecipeAdded (uk.gov.justice.services.example.cakeshop.domain.event.RecipeAdded)2 JsonEnvelope (uk.gov.justice.services.messaging.JsonEnvelope)2 JsonObjects.getString (uk.gov.justice.services.messaging.JsonObjects.getString)2 UUID.randomUUID (java.util.UUID.randomUUID)1 AggregateSnapshot (uk.gov.justice.domain.snapshot.AggregateSnapshot)1 DefaultObjectInputStreamStrategy (uk.gov.justice.domain.snapshot.DefaultObjectInputStreamStrategy)1 Ingredient (uk.gov.justice.services.example.cakeshop.domain.Ingredient)1 JsonObjects.getBoolean (uk.gov.justice.services.messaging.JsonObjects.getBoolean)1