/*
 * Copyright 2018, 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.tenant;

import com.google.common.annotations.VisibleForTesting;
import io.spine.annotation.Internal;
import io.spine.core.TenantId;
import io.spine.server.security.InvocationGuard;

/**
 * Support utility for tenant-aware tests.
 *
 * @author Alexander Yevsyukov
 * @apiNote This class is only for the internal use by the Testutil Server library.
 * Calling its methods from other code would result in run-time exceptions.
 */
@Internal
@VisibleForTesting
public final class TenantAwareTestSupport {

    /** Prevents instantiation of this utility class. */
    private TenantAwareTestSupport() {
    }

    /** Sets the current tenant. */
    public static void inject(TenantId tenantId) throws SecurityException  {
        checkCaller();
        CurrentTenant.set(tenantId);
    }

    /** Clears the current tenant. */
    public static void clear() throws SecurityException {
        checkCaller();
        CurrentTenant.clear();
    }
    private static void checkCaller() {
        InvocationGuard.allowOnly("io.spine.testing.server.tenant.TenantAwareTest");
    }
}

@@ -1532,16 +1532,114 @@
 port %7B%0A%0A
+    private static final String CALLER_CLASS = %22io.spine.testing.server.tenant.TenantAwareTest%22;%0A%0A
     /** 
@@ -1683,24 +1683,24 @@
 y class. */%0A
-
     private 
@@ -1850,36 +1850,62 @@
   %7B%0A        
-checkCaller(
+InvocationGuard.allowOnly(CALLER_CLASS
 );%0A        C
@@ -2047,182 +2047,77 @@
     
-checkCaller();%0A        CurrentTenant.clear();%0A    %7D%0A    private static void checkCaller() %7B%0A        InvocationGuard.allowOnly(%22io.spine.testing.server.tenant.TenantAwareTest%22
+InvocationGuard.allowOnly(CALLER_CLASS);%0A        CurrentTenant.clear(
 );%0A 

Fix calling class check