/*
 * Copyright 2016, TeamDev Ltd. 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 org.spine3.testdata;

import com.google.common.util.concurrent.MoreExecutors;
import org.spine3.base.Event;
import org.spine3.base.EventContext;
import org.spine3.server.event.EventBus;
import org.spine3.server.event.EventStore;
import org.spine3.server.storage.StorageFactory;
import org.spine3.test.project.ProjectId;
import org.spine3.test.project.event.ProjectCreated;
import org.spine3.test.project.event.ProjectStarted;
import org.spine3.test.project.event.TaskAdded;

import static org.spine3.protobuf.Messages.toAny;
import static org.spine3.testdata.TestAggregateIdFactory.newProjectId;
import static org.spine3.testdata.TestContextFactory.createEventContext;
import static org.spine3.testdata.TestEventMessageFactory.*;

/**
 * The utility class which is used for creating Events for tests.
 *
 * @author Mikhail Mikhaylov
 */
public class TestEventFactory {

    private static final ProjectId STUB_PROJECT_ID = newProjectId();
    private static final EventContext STUB_EVENT_CONTEXT = createEventContext();


    private TestEventFactory() {}


    /**
     * Creates a new {@link Event} with default properties.
     */
    public static Event projectCreated() {
        return projectCreated(STUB_PROJECT_ID, STUB_EVENT_CONTEXT);
    }

    /**
     * Creates a new {@link Event} with default properties.
     */
    public static Event taskAdded() {
        return taskAdded(STUB_PROJECT_ID, STUB_EVENT_CONTEXT);
    }

    /**
     * Creates a new {@link Event} with default properties.
     */
    public static Event projectStarted() {
        return projectStarted(STUB_PROJECT_ID, STUB_EVENT_CONTEXT);
    }

    /**
     * Creates a new {@link Event} with the given projectId.
     */
    public static Event projectCreated(ProjectId projectId) {
        return projectCreated(projectId, createEventContext(projectId));
    }

    /**
     * Creates a new {@link Event} with the given projectId.
     */
    public static Event taskAdded(ProjectId projectId) {
        return taskAdded(projectId, createEventContext(projectId));
    }

    /**
     * Creates a new {@link Event} with the given projectId.
     */
    public static Event projectStarted(ProjectId projectId) {
        return projectStarted(projectId, createEventContext(projectId));
    }

    /**
     * Creates a new {@link Event} with the given projectId and eventContext.
     */
    public static Event projectCreated(ProjectId projectId, EventContext eventContext) {

        final ProjectCreated event = projectCreatedEvent(projectId);
        final Event.Builder builder = Event.newBuilder().setContext(eventContext).setMessage(toAny(event));
        return builder.build();
    }

    /**
     * Creates a new {@link Event} with the given projectId and eventContext.
     */
    public static Event taskAdded(ProjectId projectId, EventContext eventContext) {

        final TaskAdded event = taskAddedEvent(projectId);
        final Event.Builder builder = Event.newBuilder().setContext(eventContext).setMessage(toAny(event));
        return builder.build();
    }

    /**
     * Creates a new {@link Event} with the given projectId and eventContext.
     */
    public static Event projectStarted(ProjectId projectId, EventContext eventContext) {

        final ProjectStarted event = projectStartedEvent(projectId);
        final Event.Builder builder = Event.newBuilder().setContext(eventContext).setMessage(toAny(event));
        return builder.build();
    }

    /**
     * Creates a new event bus with the given storage factory.
     */
    public static EventBus newEventBus(StorageFactory storageFactory) {
        final EventStore store = EventStore.newBuilder()
                                           .setStreamExecutor(MoreExecutors.directExecutor())
                                           .setStorage(storageFactory.createEventStorage())
                                           .build();
        final EventBus eventBus = EventBus.newInstance(store);
        return eventBus;
    }
}

@@ -1280,16 +1280,127 @@
 storage.
+EventStorage;%0Aimport org.spine3.server.storage.StorageFactory;%0Aimport org.spine3.server.storage.memory.InMemory
 StorageF
@@ -5131,11 +5131,601 @@
 s;%0A    %7D
+%0A%0A    /**%0A     * Creates a new event bus with the %7B@link InMemoryStorageFactory%7D.%0A     */%0A    public static EventBus newEventBus() %7B%0A        final EventStorage storage = InMemoryStorageFactory.getInstance().createEventStorage();%0A        final EventStore store = EventStore.newBuilder()%0A                                           .setStreamExecutor(MoreExecutors.directExecutor())%0A                                           .setStorage(storage)%0A                                           .build();%0A        final EventBus eventBus = EventBus.newInstance(store);%0A        return eventBus;%0A    %7D
 %0A%7D%0A

Add a method.