/*
 * Copyright (c) 2010-2012. Axon Framework
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.axonframework.integrationtests.commandhandling;

import org.axonframework.commandhandling.AnnotationCommandHandlerAdapter;
import org.axonframework.commandhandling.CommandBus;
import org.axonframework.commandhandling.SimpleCommandBus;
import org.axonframework.eventhandling.EventMessage;
import org.axonframework.eventsourcing.DomainEventMessage;
import org.axonframework.eventsourcing.EventSourcingRepository;
import org.axonframework.eventsourcing.GenericDomainEventMessage;
import org.axonframework.eventsourcing.eventstore.DomainEventStream;
import org.axonframework.eventsourcing.eventstore.EmbeddedEventStore;
import org.axonframework.eventsourcing.eventstore.EventStore;
import org.axonframework.eventsourcing.eventstore.inmemory.InMemoryEventStorageEngine;
import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;
import org.junit.Before;
import org.junit.Test;
import org.mockito.InOrder;

import java.util.UUID;

import static org.axonframework.commandhandling.GenericCommandMessage.asCommandMessage;
import static org.mockito.Mockito.*;

/**
 * @author Allard Buijze
 */
public class EventPublicationOrderTest {

    private CommandBus commandBus;
    private EventStore eventStore;

    @Before
    public void setUp() {
        this.commandBus = new SimpleCommandBus();
        eventStore = spy(new EmbeddedEventStore(new InMemoryEventStorageEngine()));
        EventSourcingRepository<StubAggregate> repository =
                new EventSourcingRepository<>(StubAggregate.class, eventStore);
        StubAggregateCommandHandler target = new StubAggregateCommandHandler();
        target.setRepository(repository);
        target.setEventBus(eventStore);
        new AnnotationCommandHandlerAdapter(target).subscribe(commandBus);
    }

    @Test
    public void testPublicationOrderIsMaintained_AggregateAdded() {
        String aggregateId = UUID.randomUUID().toString();
        when(eventStore.readEvents(aggregateId)).thenReturn(DomainEventStream.of(new GenericDomainEventMessage<>("test",
                                                                                                                 aggregateId,
                                                                                                                 0,
                                                                                                                 new StubAggregateCreatedEvent(
                                                                                                                         aggregateId))));
        doAnswer(invocation -> {
            System.out.println("Published event: " + invocation.getArguments()[0].toString());
            return Void.class;
        }).when(eventStore).publish(isA(EventMessage.class));
        commandBus.dispatch(asCommandMessage(new UpdateStubAggregateWithExtraEventCommand(aggregateId)));
        InOrder inOrder = inOrder(eventStore, eventStore, eventStore);
        inOrder.verify(eventStore).publish(isA(DomainEventMessage.class));
        inOrder.verify(eventStore).publish(argThat(new NotADomainEventMatcher()));
        inOrder.verify(eventStore).publish(isA(DomainEventMessage.class));
    }

    private static class NotADomainEventMatcher extends BaseMatcher<EventMessage> {

        @Override
        public boolean matches(Object o) {
            return !(o instanceof DomainEventMessage);
        }

        @Override
        public void describeTo(Description description) {
            description.appendText("Not a DomainEventMessage");
        }
    }
}

@@ -21,17 +21,17 @@
 2010-201
-2
+8
 . Axon F
@@ -1383,74 +1383,8 @@
 ne;%0A
-import org.hamcrest.BaseMatcher;%0Aimport org.hamcrest.Description;%0A
 impo
@@ -1454,16 +1454,52 @@
 InOrder;
+%0Aimport org.mockito.ArgumentMatcher;
 %0A%0Aimport
@@ -1671,16 +1671,40 @@
  Buijze%0A
+ * @author Nakul Mishra%0A
  */%0Apubl
@@ -3840,20 +3840,27 @@
 her 
-extends Base
+implements Argument
 Matc
@@ -3933,60 +3933,8 @@
 hes(
-Object o) %7B%0A            return !(o instanceof Domain
 Even
@@ -3945,139 +3945,47 @@
 sage
-);%0A 
+ o) %7B%0A
       
- %7D%0A%0A
       
-  @Override%0A        public void describeTo(Description description) %7B%0A            description.appendText(%22Not a
+return !(o instanceof
  Dom
@@ -3999,17 +3999,16 @@
 tMessage
-%22
 );%0A     

Fix Mockito 2 support