Search in sources :

Example 1 with RecipeAdded

use of uk.gov.justice.services.example.cakeshop.domain.event.RecipeAdded in project microservice_framework by CJSCommonPlatform.

the class OtherRecipeEventListener method recipeAdded.

@Handles("other.recipe-added")
public void recipeAdded(final JsonEnvelope event) {
    final RecipeAdded recipeAdded = jsonObjectConverter.convert(event.payloadAsJsonObject(), RecipeAdded.class);
    recipeRepository.save(otherRecipeAddedToRecipeConverter.convert(recipeAdded));
    for (final Ingredient ingredient : recipeAddedToIngredientsConverter.convert(recipeAdded)) {
        if (ingredientRepository.findByNameIgnoreCase(ingredient.getName()).isEmpty()) {
            ingredientRepository.save(ingredient);
        }
    }
}
Also used : RecipeAdded(uk.gov.justice.services.example.cakeshop.domain.event.RecipeAdded) Ingredient(uk.gov.justice.services.example.cakeshop.persistence.entity.Ingredient) Handles(uk.gov.justice.services.core.annotation.Handles)

Example 2 with RecipeAdded

use of uk.gov.justice.services.example.cakeshop.domain.event.RecipeAdded in project microservice_framework by CJSCommonPlatform.

the class RecipeTest method shouldReturnEventWhenApplied.

@Test
public void shouldReturnEventWhenApplied() {
    RecipeAdded event = mock(RecipeAdded.class);
    assertThat(recipe.apply(event), equalTo(event));
}
Also used : RecipeAdded(uk.gov.justice.services.example.cakeshop.domain.event.RecipeAdded) Test(org.junit.Test)

Example 3 with RecipeAdded

use of uk.gov.justice.services.example.cakeshop.domain.event.RecipeAdded in project microservice_framework by CJSCommonPlatform.

the class RecipeTest method shouldReturnRecipeAddedEvent.

@Test
public void shouldReturnRecipeAddedEvent() {
    final Stream<Object> events = recipe.addRecipe(RECIPE_ID, NAME, true, INGREDIENTS);
    final List<Object> eventList = events.collect(toList());
    assertThat(eventList, hasSize(1));
    final Object event = eventList.get(0);
    assertThat(event, instanceOf(RecipeAdded.class));
    final RecipeAdded recipeAdded = (RecipeAdded) event;
    assertThat(recipeAdded.getRecipeId(), equalTo(RECIPE_ID));
    assertThat(recipeAdded.getName(), equalTo(NAME));
    assertThat(recipeAdded.getIngredients(), equalTo(INGREDIENTS));
    assertThat(recipeAdded.isGlutenFree(), is(true));
}
Also used : RecipeAdded(uk.gov.justice.services.example.cakeshop.domain.event.RecipeAdded) Test(org.junit.Test)

Example 4 with RecipeAdded

use of uk.gov.justice.services.example.cakeshop.domain.event.RecipeAdded in project microservice_framework by CJSCommonPlatform.

the class RecipeEventListener method recipeAdded.

@Handles("example.recipe-added")
public void recipeAdded(final JsonEnvelope event) {
    final String recipeId = event.payloadAsJsonObject().getString(FIELD_RECIPE_ID);
    LOGGER.trace("=============> Inside add-recipe Event Listener. RecipeId: " + recipeId);
    final RecipeAdded recipeAdded = jsonObjectConverter.convert(event.payloadAsJsonObject(), RecipeAdded.class);
    recipeRepository.save(recipeAddedToRecipeConverter.convert(recipeAdded));
    LOGGER.trace("=====================================================> Recipe saved, RecipeId: " + recipeId);
    for (final Ingredient ingredient : recipeAddedToIngredientsConverter.convert(recipeAdded)) {
        if (ingredientRepository.findByNameIgnoreCase(ingredient.getName()).isEmpty()) {
            LOGGER.trace("=============> Inside add-recipe Event Listener about to save Ingredient Id: " + ingredient.getId());
            ingredientRepository.save(ingredient);
            LOGGER.trace("=====================================================> Ingredient saved, Ingredient Id: " + ingredient.getId());
        } else {
            LOGGER.trace("=====================================================> Skipped adding ingredient as it already exists, Ingredient Name: " + ingredient.getName());
        }
    }
}
Also used : RecipeAdded(uk.gov.justice.services.example.cakeshop.domain.event.RecipeAdded) Ingredient(uk.gov.justice.services.example.cakeshop.persistence.entity.Ingredient) Handles(uk.gov.justice.services.core.annotation.Handles)

Example 5 with RecipeAdded

use of uk.gov.justice.services.example.cakeshop.domain.event.RecipeAdded 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)

Aggregations

RecipeAdded (uk.gov.justice.services.example.cakeshop.domain.event.RecipeAdded)7 Test (org.junit.Test)4 Handles (uk.gov.justice.services.core.annotation.Handles)2 Recipe (uk.gov.justice.services.example.cakeshop.domain.aggregate.Recipe)2 Ingredient (uk.gov.justice.services.example.cakeshop.persistence.entity.Ingredient)2 UUID (java.util.UUID)1 Ingredient (uk.gov.justice.services.example.cakeshop.domain.Ingredient)1 Recipe (uk.gov.justice.services.example.cakeshop.persistence.entity.Recipe)1 JsonEnvelope (uk.gov.justice.services.messaging.JsonEnvelope)1