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

import com.google.common.collect.ImmutableList;
import io.spine.base.CommandMessage;
import io.spine.server.BoundedContextBuilder;
import io.spine.test.client.ClientTestContext;
import io.spine.test.client.command.LogInUser;
import io.spine.test.client.event.UserAccountCreated;
import io.spine.test.client.event.UserLoggedIn;
import io.spine.testing.core.given.GivenUserId;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import static com.google.common.truth.Truth.assertThat;

@DisplayName("`CommandRequest` should")
class CommandRequestTest extends AbstractClientTest {

    /** Registers which event consumers were called. */
    private final ConsumerCallCounter counter = new ConsumerCallCounter();
    private CommandRequest commandRequest;

    @BeforeEach
    void createCommandRequest() {
        counter.clear();
        CommandMessage cmd = LogInUser
                .newBuilder()
                .setUser(GivenUserId.generated())
                .build();
        commandRequest = client().asGuest()
                                 .command(cmd);
    }

    @AfterEach
    void clearCounter() {
        counter.clear();
    }

    @Override
    protected ImmutableList<BoundedContextBuilder> contexts() {
        return ImmutableList.of(ClientTestContext.builder());
    }

    @Test
    @DisplayName("deliver an event to a consumer")
    void eventConsumer() {
        commandRequest.observe(UserLoggedIn.class, counter::add)
                      .observe(UserAccountCreated.class, counter::add)
                      .post();
        assertThat(counter.containsAll(UserLoggedIn.class, UserAccountCreated.class))
                .isTrue();
    }

    @Test
    @DisplayName("deliver an event and its context fo a consumer")
    void eventAndContextConsumer() {
        commandRequest.observe(UserLoggedIn.class, (e, c) -> counter.add(e))
                      .observe(UserAccountCreated.class, (e, c) -> counter.add(e))
                      .post();
        assertThat(counter.containsAll(UserLoggedIn.class, UserAccountCreated.class))
                .isTrue();
    }

}

@@ -1359,16 +1359,86 @@
 ggedIn;%0A
+import io.spine.test.client.rejection.Rejections.UserAlreadyLoggedIn;%0A
 import i
@@ -2938,17 +2938,17 @@
 context 
-f
+t
 o a cons
@@ -3302,10 +3302,494 @@
 %0A    %7D%0A%0A
+    @Test%0A    @DisplayName(%22deliver a rejection to its consumers%22)%0A    void rejections() %7B%0A        // Post the command so that the user is logged in. We are not interested in events here.%0A        commandRequest.post();%0A        // Now post the command again, expecting the rejection.%0A        commandRequest.observe(UserAlreadyLoggedIn.class, counter::add)%0A                      .post();%0A%0A        assertThat(counter.contains(UserAlreadyLoggedIn.class))%0A                .isTrue();%0A    %7D%0A
 %7D%0A

Add tests for consuming a rejection