package com.codingchili.core;

import io.vertx.core.Vertx;
import io.vertx.ext.unit.Async;
import io.vertx.ext.unit.TestContext;
import io.vertx.ext.unit.junit.VertxUnitRunner;
import org.junit.*;
import org.junit.runner.RunWith;

import java.util.ArrayList;
import java.util.List;

import com.codingchili.core.context.Delay;
import com.codingchili.core.context.LaunchContext;
import com.codingchili.core.testing.ContextMock;

/**
 * @author Robin Duda
 *         <p>
 *         Tests for the launcher.
 */
@RunWith(VertxUnitRunner.class)
public class LauncherIT {
    public static Async async;
    private ContextMock context;

    @Before
    public void setUp() {
        context = new ContextMock(Vertx.vertx());
        Delay.initialize(context);
    }

    @After
    public void tearDown(TestContext test) {
        context.vertx().close(test.asyncAssertSuccess());
    }

    @Test
    public void testFailNotVerticle(TestContext test) {
        async = test.async();

        new Launcher(getLaunchContextFor("com.codingchili.core.LauncherTest$1")) {
            @Override
            void exit() {
                async.complete();
            }
        };
    }

    @Test
    public void testDeployAService(TestContext test) {
        async = test.async();
        new Launcher(getLaunchContextFor("com.codingchili.core.IsClusterNode"));
    }

    public LaunchContext getLaunchContextFor(String node) {
        return new LaunchContext(new String[]{node}) {
            @Override
            protected List<String> block(String block) {
                List<String> list = new ArrayList<>();
                list.add(node);
                return list;
            }
        };
    }

    /**
     * Test class that is not a cluster node and will fail to deploy.
     */
    private static class NotClusterNode {
    }
}

@@ -1016,42 +1016,321 @@
 For(
-%22com.codingchili.core.LauncherTest
+NotClusterNode.class.getName())) %7B%0A            @Override%0A            void exit() %7B%0A                async.complete();%0A            %7D%0A        %7D;%0A    %7D%0A%0A    @Test%0A    public void testFailNotFound(TestContext test) %7B%0A        async = test.async();%0A%0A        new Launcher(getLaunchContextFor(%22com.codingchili.core.Missing
 $1%22)
@@ -1736,20 +1736,16 @@
 tring%5B%5D%7B
-node
 %7D) %7B%0A   

Improve Launcher integration tests.