package okapi;

import com.jayway.restassured.RestAssured;
import com.jayway.restassured.response.Response;
import guru.nidi.ramltester.RamlDefinition;
import guru.nidi.ramltester.RamlLoaders;
import guru.nidi.ramltester.restassured.RestAssuredClient;
import io.vertx.core.Vertx;
import io.vertx.core.logging.Logger;
import io.vertx.core.logging.LoggerFactory;
import io.vertx.ext.unit.Async;
import io.vertx.ext.unit.TestContext;
import io.vertx.ext.unit.junit.VertxUnitRunner;
import org.folio.okapi.MainCluster;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

@RunWith(VertxUnitRunner.class)
public class MainClusterTest {

  private final Logger logger = LoggerFactory.getLogger("okapi");
  private final int port = Integer.parseInt(System.getProperty("port", "9130"));
  Async async;
  Vertx vertx;
  RamlDefinition api;


  public MainClusterTest() {
  }

  @Before
  public void setUp(TestContext context) {
    async = context.async();
    api = RamlLoaders.fromFile("src/main/raml").load("okapi.raml");
    RestAssured.port = port;
    async.complete();
  }

  @After
  public void tearDown(TestContext context) {
    async = context.async();
    if (vertx == null) {
      async.complete();
    } else {
      vertx.close(x -> {
        vertx = null;
        async.complete();
      });
    }
  }

  @Test
  public void testBadMode(TestContext context) {
    async = context.async();

    String[] args = {"bad"};
    MainCluster.main1(args, res -> {
      vertx = res.succeeded() ? res.result() : null;
      Assert.assertFalse(res.succeeded());
      async.complete();
    });
  }

  @Test
  public void testNoArgs(TestContext context) {
    async = context.async();

    String[] args = {};
    MainCluster.main1(args, res -> {
      Assert.assertFalse(res.succeeded());
      vertx = res.succeeded() ? res.result() : null;
      async.complete();
    });
  }

  @Test
  public void testDevMode(TestContext context) {
    async = context.async();

    String[] args = {"dev"};

    MainCluster.main1(args, res -> {
      vertx = res.succeeded() ? res.result() : null;
      Assert.assertTrue("main1 " + res.cause(), res.succeeded());

      RestAssuredClient c;
      Response r;

      c = api.createRestAssured();
      r = c.given().get("/_/version").then().statusCode(200).log().ifError().extract().response();

      Assert.assertTrue("raml: " + c.getLastReport().toString(),
        c.getLastReport().isEmpty());
      async.complete();
    });
  }

  @Test
  public void testDeploymentMode(TestContext context) {
    async = context.async();

    String[] args = {"deployment"};

    MainCluster.main1(args, res -> {
      vertx = res.succeeded() ? res.result() : null;
      Assert.assertTrue("main1" + res.cause(), res.succeeded());

      RestAssuredClient c;
      Response r;

      c = api.createRestAssured();
      r = c.given().get("/_/deployment/modules").then().statusCode(200).log().ifError().extract().response();

      Assert.assertTrue("raml: " + c.getLastReport().toString(),
        c.getLastReport().isEmpty());
      async.complete();
    });
  }

  @Test
  public void testProxyMode(TestContext context) {
    async = context.async();

    String[] args = {"proxy"};

    MainCluster.main1(args, res -> {
      vertx = res.succeeded() ? res.result() : null;
      Assert.assertTrue("main1 " + res.cause(), res.succeeded());

      RestAssuredClient c;
      Response r;

      c = api.createRestAssured();
      r = c.given().get("/_/proxy/modules").then().statusCode(200).log().ifError().extract().response();

      Assert.assertTrue("raml: " + c.getLastReport().toString(),
        c.getLastReport().isEmpty());
      async.complete();
    });
  }

}

@@ -1380,24 +1380,603 @@
 %0A    %7D%0A  %7D%0A%0A
+  @Test%0A  public void testHelp(TestContext context) %7B%0A    async = context.async();%0A%0A    String%5B%5D args = %7B%22help%22%7D;%0A    MainCluster.main1(args, res -%3E %7B%0A      vertx = res.succeeded() ? res.result() : null;%0A      Assert.assertTrue(res.succeeded());%0A      async.complete();%0A    %7D);%0A  %7D%0A%0A  @Test%0A  public void testBadOption(TestContext context) %7B%0A    async = context.async();%0A%0A    String%5B%5D args = %7B%22-bad-option%22%7D;%0A    MainCluster.main1(args, res -%3E %7B%0A      vertx = res.succeeded() ? res.result() : null;%0A      Assert.assertFalse(res.succeeded());%0A      async.complete();%0A    %7D);%0A  %7D%0A%0A
   @Test%0A  pu

Test help mode and bad option handdling OKAPI-432