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

import com.google.common.collect.ImmutableSet;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import io.spine.server.BoundedContext;
import io.spine.server.BoundedContextBuilder;
import io.spine.server.DefaultRepository;
import io.spine.server.bc.given.ProjectAggregate;
import io.spine.server.commandbus.CommandDispatcher;
import io.spine.server.event.EventDispatcher;
import io.spine.server.type.CommandClass;
import io.spine.server.type.CommandEnvelope;
import io.spine.server.type.EventClass;
import io.spine.server.type.EventEnvelope;

/**
 * Test environment class for testing {@code BoundedContext} configuration from
 * outside the framework.
 *
 * @see io.spine.server.BoundedContextTest.RestrictRegistrationCalls
 */
public final class ForeignContextConfig {

    private ForeignContextConfig() {}

    public static void repositoryRegistration() {
        context().register(DefaultRepository.of(ProjectAggregate.class));
    }

    public static void commandDispatcherRegistration() {
        context().registerCommandDispatcher(newCommandDispatcher());
    }

    public static void eventDispatcherRegistration() {
        context().registerEventDispatcher(newEventDispatcher());
    }

    private static BoundedContext context() {
        return BoundedContextBuilder.assumingTests().build();
    }

    private static CommandDispatcher newCommandDispatcher() {
        return new CommandDispatcher() {
            @Override
            public ImmutableSet<CommandClass> messageClasses() {
                return ImmutableSet.of();
            }

            @CanIgnoreReturnValue
            @Override
            public void dispatch(CommandEnvelope envelope) {
                // Do nothing.
            }
        };
    }

    @SuppressWarnings("OverlyComplexAnonymousInnerClass")
    private static EventDispatcher newEventDispatcher() {
        return new EventDispatcher() {
            @Override
            public ImmutableSet<EventClass> externalEventClasses() {
                return ImmutableSet.of();
            }

            @Override
            public ImmutableSet<EventClass> domesticEventClasses() {
                return eventClasses();
            }

            @Override
            public ImmutableSet<EventClass> messageClasses() {
                return ImmutableSet.of();
            }

            @CanIgnoreReturnValue
            @Override
            public void dispatch(EventEnvelope envelope) {
                // Do nothing.
            }
        };
    }
}

@@ -2214,19 +2214,21 @@
 patcher(
-new
+Empty
 EventDis
@@ -2226,32 +2226,44 @@
 yEventDispatcher
+.newInstance
 ());%0A    %7D%0A%0A    
@@ -2808,62 +2808,84 @@
     
-@SuppressWarnings(%22OverlyComplexAnonymousInnerClass%22)%0A
+private static class EmptyEventDispatcher implements EventDispatcher %7B%0A%0A    
     
@@ -2918,36 +2918,33 @@
 cher new
-EventDispatcher
+Instance
 () %7B%0A
+    
         
@@ -2954,16 +2954,21 @@
 urn new 
+Empty
 EventDis
@@ -2968,39 +2968,45 @@
 ventDispatcher()
- %7B%0A    
+;%0A        %7D%0A%0A
         @Overrid
@@ -3007,36 +3007,32 @@
 verride%0A        
-  
-  
 public Immutable
@@ -3076,36 +3076,32 @@
 ) %7B%0A            
-    
 return Immutable
@@ -3110,36 +3110,32 @@
 t.of();%0A        
-    
 %7D%0A%0A            @
@@ -3121,36 +3121,32 @@
      %7D%0A%0A        
-  
-  
 @Override%0A      
@@ -3131,36 +3131,32 @@
       @Override%0A
-    
         public I
@@ -3212,28 +3212,24 @@
             
-    
 return event
@@ -3243,35 +3243,27 @@
 ();%0A        
-  
-  
 %7D%0A%0A
-    
         @Ove
@@ -3260,36 +3260,32 @@
       @Override%0A
-    
         public I
@@ -3331,36 +3331,32 @@
 ) %7B%0A            
-   
- 
 return Immutable
@@ -3365,36 +3365,32 @@
 t.of();%0A        
-    
 %7D%0A%0A            @
@@ -3376,36 +3376,32 @@
      %7D%0A%0A        
- 
-   
 @CanIgnoreReturn
@@ -3406,36 +3406,32 @@
 rnValue%0A        
-   
- 
 @Override%0A      
@@ -3416,36 +3416,32 @@
       @Override%0A
-    
         public v
@@ -3483,36 +3483,32 @@
 ) %7B%0A            
-    
 // Do nothing.%0A 
@@ -3514,32 +3514,17 @@
         
-    %7D%0A        %7D;
+%7D
 %0A    %7D%0A%7D

Conversion anonymous test env. class to inner static