package org.folio.rest.impl;

import java.util.Collections;

import org.folio.rest.RestVerticle;
import org.folio.rest.tools.utils.OutStream;
import org.folio.rest.tools.utils.VertxUtils;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;

import io.vertx.core.Vertx;
import io.vertx.ext.unit.Async;
import io.vertx.ext.unit.TestContext;
import io.vertx.ext.unit.junit.VertxUnitRunner;

@RunWith(VertxUnitRunner.class)
public class CustomHealthCheckTest {
  private static Vertx vertx;
  private static String deployId;

  @BeforeClass
  public static void setUp(TestContext context) {
    Async async = context.async();
    vertx = VertxUtils.getVertxWithExceptionHandler();
    vertx.deployVerticle(RestVerticle.class.getName(), done -> {
      deployId = done.result();
      async.complete();
    });
  }

  @AfterClass
  public static void tearDown(TestContext context) {
    vertx.undeploy(deployId, context.asyncAssertSuccess());
  }

  @Test
  public void getAdminHealth(TestContext context) throws Exception {
    CustomHealthCheck customHealthCheck = new CustomHealthCheck();
    customHealthCheck.getAdminHealth(Collections.emptyMap(), result -> {
      context.assertTrue(result.succeeded());
      OutStream out = (OutStream) result.result().getEntity();
      context.assertEquals(out.getData(), "OK");
    }, null);
  }

  @Test
  public void getAdminModuleStats(TestContext context) throws Exception {
    CustomHealthCheck customHealthCheck = new CustomHealthCheck();
    customHealthCheck.getAdminModuleStats(Collections.emptyMap(), result -> {
      context.assertTrue(result.succeeded());
      String json = (String) result.result().getEntity();
      context.assertTrue(json.startsWith("{"));
      context.assertTrue(json.endsWith("}"));
    }, null);
  }
}

@@ -90,16 +90,64 @@
 rticle;%0A
+import org.folio.rest.tools.utils.NetworkUtils;%0A
 import o
@@ -366,21 +366,99 @@
 tx.core.
-Vertx
+DeploymentOptions;%0Aimport io.vertx.core.Vertx;%0Aimport io.vertx.core.json.JsonObject
 ;%0Aimport
@@ -702,16 +702,43 @@
 eployId;
+%0A  private static int port;
 %0A%0A  @Bef
@@ -827,24 +827,180 @@
 xt.async();%0A
+    port = NetworkUtils.nextFreePort();%0A    DeploymentOptions options = new DeploymentOptions().setConfig(%0A       new JsonObject().put(%22http.port%22, port));%0A
     vertx = 
@@ -1096,16 +1096,25 @@
 tName(),
+ options,
  done -%3E

use next free port for custom health check test