/*
 * 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.server.route;

import io.spine.core.UserId;
import io.spine.server.DefaultRepository;
import io.spine.server.route.given.user.SessionProjection;
import io.spine.server.route.given.user.SessionRepository;
import io.spine.server.route.given.user.UserAggregate;
import io.spine.server.route.given.user.event.RUserSignedIn;
import io.spine.test.event.RSession;
import io.spine.test.event.RSessionId;
import io.spine.testing.core.given.GivenUserId;
import io.spine.testing.server.blackbox.BlackBoxBoundedContext;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

@DisplayName("Event routing should")
class EventRoutingIntegrationTest {

    //

    /**
     * A test that verifies that the {@linkplain io.spine.core.Event event} routing occurs at the
     * right moment in time.
     *
     * <p>If the routing of `RUserConsentRequested` event is done before its origin
     * (`RUserSignedIn`) is dispatched, the repository won't be able to route the event properly,
     * making the corresponding field `false`.
     */
    @Test
    @DisplayName("only occur after the event origin has already been dispatched")
    void occurAfterOriginDispatched() {
        UserId userId = GivenUserId.generated();
        RSessionId sessionId = RSessionId.generate();
        RUserSignedIn event = RUserSignedIn
                .newBuilder()
                .setUserId(userId)
                .setSessionId(sessionId)
                .build();
        RSession session = RSession
                .newBuilder()
                .setId(sessionId)
                .setUserId(userId)
                .setUserConsentRequested(true)
                .build();

        BlackBoxBoundedContext.singleTenant()
                              .with(DefaultRepository.of(UserAggregate.class))
                              .with(new SessionRepository())
                              .receivesEvent(event)
                              .assertEntity(SessionProjection.class, sessionId)
                              .hasStateThat()
                              .comparingExpectedFieldsOnly()
                              .isEqualTo(session);
    }
}

@@ -1687,24 +1687,16 @@
  %7B%0A%0A
-    //%0A%0A
     /**%0A
     
@@ -1691,16 +1691,16 @@
     /**%0A
+
      * A
@@ -1853,17 +1853,23 @@
 ting of 
-%60
+%7B@code 
 RUserCon
@@ -1881,17 +1881,17 @@
 equested
-%60
+%7D
  event i
@@ -1923,17 +1923,23 @@
      * (
-%60
+%7B@code 
 RUserSig
@@ -1943,17 +1943,17 @@
 SignedIn
-%60
+%7D
 ) is dis
@@ -2008,16 +2008,23 @@
 he event
+%0A     *
  properl
@@ -2025,23 +2025,16 @@
 roperly,
-%0A     *
  making 
@@ -2061,15 +2061,21 @@
 eld 
-%60
+%7B@code 
 false
-%60
+%7D
 .%0A  

Fix doc