/*
 * Copyright 2017, 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.server.command;

import org.junit.Test;
import org.spine3.base.Command;
import org.spine3.server.command.error.InvalidCommandException;
import org.spine3.server.storage.CurrentTenant;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.spine3.base.CommandValidationError.INVALID_COMMAND;

/**
 * @author Alexander Yevsyukov
 */
public class SingletenantCommandBusShould extends AbstractCommandBusTestSuite {

    public SingletenantCommandBusShould() {
        super(false);
    }

    @Test
    public void post_command_and_do_not_set_current_tenant_if_not_multitenant() {
        commandBus.register(createProjectHandler);

        commandBus.post(newCommandWithoutTenantId(), responseObserver);

        assertFalse(CurrentTenant.get().isPresent());
    }

    @Test
    public void return_InvalidCommandException_if_command_is_invalid() {
        commandBus.register(createProjectHandler);
        final Command cmd = newCommandWithoutContext();

        commandBus.post(cmd, responseObserver);

        checkCommandError(responseObserver.getThrowable(), INVALID_COMMAND, InvalidCommandException.class, cmd);
        assertTrue(responseObserver.getResponses().isEmpty());
    }
}

@@ -1370,16 +1370,159 @@
 COMMAND;
+%0Aimport static org.spine3.base.CommandValidationError.TENANT_INAPPLICABLE;%0Aimport static org.spine3.server.command.Given.Command.createProject;
 %0A%0A/**%0A *
@@ -1700,32 +1700,46 @@
 (false);%0A    %7D%0A%0A
+    @Override%0A
     @Test%0A    pu
@@ -1752,125 +1752,171 @@
 oid 
-post_command_and_do_not_set_current_tenant_if_not_multitenant() %7B%0A        commandBus.register(createProjectHandler);%0A
+setUp() %7B%0A        super.setUp();%0A        commandBus.register(createProjectHandler);%0A    %7D%0A%0A    @Test%0A    public void post_command_and_do_not_set_current_tenant() %7B
 %0A   
@@ -2078,62 +2078,138 @@
 d re
-turn_InvalidCommandException_if_command_is_invalid() %7B
+ject_invalid_command() %7B%0A        final Command cmd = newCommandWithoutContext();%0A%0A        commandBus.post(cmd, responseObserver);%0A
 %0A   
@@ -2218,49 +2218,305 @@
    c
+heckC
 ommand
-Bus.register(createProjectHandler);
+Error(responseObserver.getThrowable(), INVALID_COMMAND, InvalidCommandException.class, cmd);%0A        assertTrue(responseObserver.getResponses().isEmpty());%0A    %7D%0A%0A    @Test%0A    public void reject_multitenant_command_in_single_tenant_context() %7B%0A        // Create a multi-tenant command.
 %0A   
@@ -2540,39 +2540,28 @@
 d cmd = 
-newCommandWithoutContex
+createProjec
 t();%0A%0A  
@@ -2662,39 +2662,43 @@
 hrowable(), 
-INVALID_COMMAND
+TENANT_INAPPLICABLE
 , InvalidCom

Test rejection of multi-tenant command in single-tenant context