/*
 * Copyright (C) 2016 Cognifide Limited
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.knotx.junit.rule;

import io.knotx.junit.util.FileReader;
import io.knotx.launcher.KnotxStarterVerticle;
import io.vertx.core.AsyncResult;
import io.vertx.core.DeploymentOptions;
import io.vertx.core.Future;
import io.vertx.core.Handler;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.unit.Async;
import io.vertx.ext.unit.impl.CompletionImpl;
import io.vertx.ext.unit.impl.Helper;
import io.vertx.ext.unit.junit.RunTestOnContext;
import io.vertx.rxjava.core.Vertx;

import java.io.IOException;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;

public class TestVertxDeployer implements TestRule {

  private RunTestOnContext vertxContext;

  public TestVertxDeployer(RunTestOnContext vertxContext) {
    this.vertxContext = vertxContext;
  }

  @Override
  public Statement apply(final Statement base, Description description) {
    return new Statement() {
      @Override
      public void evaluate() throws Throwable {
        KnotxConfiguration knotxConfig = description.getAnnotation(KnotxConfiguration.class);
        if (knotxConfig == null || knotxConfig.value().isEmpty()) {
          throw new IllegalArgumentException(
              "Missing @KnotxConfiguration annotation with the path to configuration JSON");
        }
        Vertx vertx = Vertx.newInstance(vertxContext.vertx());

        CompletableFuture<String> toComplete = new CompletableFuture<>();

        vertx.deployVerticle(KnotxStarterVerticle.class.getName(),
            new DeploymentOptions().setConfig(readJson(knotxConfig.value())), ar -> {
              if (ar.succeeded()) {
                toComplete.complete("Success");
              } else {
                toComplete.completeExceptionally(ar.cause());
              }
            });

        try {
          toComplete.get();
        } catch (ExecutionException ignore) {
        } catch (InterruptedException e) {
          Thread.currentThread().interrupt();
          Helper.uncheckedThrow(e);
        }

        base.evaluate();
      }
    };
  }

  private JsonObject readJson(String path) throws IOException {
    return new JsonObject(FileReader.readText(path));
  }
}

@@ -718,42 +718,8 @@
 le;%0A
-import io.vertx.core.AsyncResult;%0A
 impo
@@ -779,160 +779,23 @@
 ore.
-Future;%0Aimport io.vertx.core.Handler;%0Aimport io.vertx.core.json.JsonObject;%0Aimport io.vertx.ext.unit.Async;%0Aimport io.vertx.ext.unit.impl.CompletionImpl
+json.JsonObject
 ;%0Aim
@@ -918,325 +918,242 @@
 tx;%0A
-%0A
 import 
-java.io.IOException;%0Aimport java.util.concurrent.CompletableFuture;%0Aimport java.util.concurrent.CountDownLatch;%0Aimport java.util.concurrent.ExecutionException;%0Aimport java.util.concurrent.TimeUnit;%0A%0Aimport org.junit.rules.TestRule;%0Aimport org.junit.runner.Description;%0Aimport org.junit.runners.model.Statement
+org.junit.rules.TestRule;%0Aimport org.junit.runner.Description;%0Aimport org.junit.runners.model.Statement;%0A%0Aimport java.io.IOException;%0Aimport java.util.concurrent.CompletableFuture;%0Aimport java.util.concurrent.ExecutionException
 ;%0A%0Ap

Format cleanup