/*
 * Copyright 2019, TeamDev. All rights reserved.
 *
 * Redistribution and use in source and/or binary forms, with or without
 * modification, must retain the above copyright notice and the following
 * disclaimer.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package io.spine.testing.server.aggregate;

import com.google.protobuf.Message;
import io.spine.server.type.CommandEnvelope;
import io.spine.server.type.EventEnvelope;
import io.spine.testing.client.TestActorRequestFactory;
import io.spine.testing.server.TestEventFactory;
import io.spine.testing.server.aggregate.given.TuMessageLog;
import io.spine.testing.server.log.FloatLogged;
import io.spine.testing.server.log.LogInteger;
import io.spine.testing.server.log.ValueLogged;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

@DisplayName("AggregateMessageDispatcher should")
class AggregateMessageDispatcherTest {

    private TuMessageLog aggregate;

    @BeforeEach
    void setUp() {
        aggregate = new TuMessageLog(1L);
    }

    @Test
    @DisplayName("dispatch command")
    void dispatchCommand() {
        TestActorRequestFactory factory = new TestActorRequestFactory(getClass());
        int messageValue = 2017_07_28;
        LogInteger message = LogInteger.newBuilder()
                                       .setValue(messageValue)
                                       .build();
        CommandEnvelope commandEnvelope = CommandEnvelope.of(factory.createCommand(message));
        List<? extends Message> eventMessages =
                AggregateMessageDispatcher.dispatchCommand(aggregate, commandEnvelope);
        assertTrue(aggregate.state()
                            .getValue()
                            .contains(String.valueOf(messageValue)));
        assertEquals(1, eventMessages.size());
        assertTrue(eventMessages.get(0) instanceof ValueLogged);
    }

    @Test
    @DisplayName("dispatch event")
    void dispatchEvent() {
        TestEventFactory factory = TestEventFactory.newInstance(getClass());
        float messageValue = 2017.0729f;
        FloatLogged message = FloatLogged.newBuilder()
                                         .setValue(messageValue)
                                         .build();
        EventEnvelope eventEnvelope = EventEnvelope.of(factory.createEvent(message));
        List<? extends Message> eventMessages =
                AggregateMessageDispatcher.dispatchEvent(aggregate, eventEnvelope);
        assertTrue(aggregate.state()
                            .getValue()
                            .contains(String.valueOf(messageValue)));
        assertEquals(1, eventMessages.size());
        assertTrue(eventMessages.get(0) instanceof ValueLogged);
    }
}

@@ -1061,34 +1061,94 @@
 ort 
-com.google.protobuf.Messag
+io.spine.server.entity.ProducedEvents;%0Aimport io.spine.server.entity.PropagationOutcom
 e;%0Ai
@@ -1673,24 +1673,182 @@
 ort 
-java.util.List;%0A
+static io.spine.testing.server.aggregate.AggregateMessageDispatcher.dispatchCommand;%0Aimport static io.spine.testing.server.aggregate.AggregateMessageDispatcher.dispatchEvent;
 %0Aimp
@@ -2241,21 +2241,17 @@
 ispatchC
-omman
+m
 d() %7B%0A  
@@ -2641,91 +2641,37 @@
     
-List%3C? extends Message%3E eventMessages =%0A                AggregateMessageDispatcher.
+PropagationOutcome outcome = 
 disp
@@ -2850,32 +2850,188 @@
 essageValue)));%0A
+        assertTrue(outcome.hasSuccess());%0A        ProducedEvents events = outcome.getSuccess()%0A                                       .getProducedEvents();%0A
         assertEq
@@ -3039,37 +3039,39 @@
 als(1, event
-Messages.size
+s.getEventCount
 ());%0A       
@@ -3083,38 +3083,54 @@
 rtTrue(event
-Messages.get(0
+s.getEvent(0).enclosedMessage(
 ) instanceof
@@ -3215,18 +3215,16 @@
 spatchEv
-en
 t() %7B%0A  
@@ -3608,91 +3608,37 @@
     
-List%3C? extends Message%3E eventMessages =%0A                AggregateMessageDispatcher.
+PropagationOutcome outcome = 
 disp
@@ -3813,32 +3813,188 @@
 essageValue)));%0A
+        assertTrue(outcome.hasSuccess());%0A        ProducedEvents events = outcome.getSuccess()%0A                                       .getProducedEvents();%0A
         assertEq
@@ -4010,21 +4010,23 @@
 vent
-Messages.size
+s.getEventCount
 ());
@@ -4054,22 +4054,38 @@
 vent
-Messages.get(0
+s.getEvent(0).enclosedMessage(
 ) in

Update test util tests