package org.folio.rest.tools.utils;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

import io.vertx.core.json.JsonObject;

public class EnvsTest {
  @Rule
  public ExpectedException thrown = ExpectedException.none();

  @Before
  public void setUp() {
    Map<String, String> map = new HashMap<>();
    map.put("DB_HOST", "example.com");
    map.put("DB_QUERYTIMEOUT", "8");
    map.put("DB_MAXPOOLSIZE", "5");
    map.put("DB_EXPLAIN_QUERY_THRESHOLD", "100");
    // we dropped support for dot form. check that it is ignored
    map.put("db.username", "superwoman");
    map.put("DB.USERNAME", "superwoman");
    Envs.setEnv(Collections.unmodifiableMap(map));
  }

  @Test
  public void host() {
    assertEquals("example.com", Envs.getEnv(Envs.DB_HOST));
  }

  @Test
  public void user() {
    assertNull(Envs.getEnv(Envs.DB_USERNAME));  // because db.username
  }

  @Test
  public void querytimeout() {
    assertEquals("8", Envs.getEnv(Envs.DB_QUERYTIMEOUT));
  }

  @Test
  public void maxpoolsize() {
    assertEquals("5", Envs.getEnv(Envs.DB_MAXPOOLSIZE));
  }

  @Test
  public void database() {
    assertNull(Envs.getEnv(Envs.DB_DATABASE));
  }

  @Test
  public void threshold() {
    assertEquals("100", Envs.getEnv(Envs.DB_EXPLAIN_QUERY_THRESHOLD));
  }

  @Test
  public void allDBConfs() {
    JsonObject json = Envs.allDBConfs();
    assertEquals(4, json.size());
    assertEquals("example.com", json.getValue("host"));
    assertEquals(Integer.valueOf(8), json.getValue("queryTimeout"));
    assertEquals(Integer.valueOf(5), json.getValue("maxPoolSize"));
    assertEquals(Long.valueOf(100), json.getValue(Envs.DB_EXPLAIN_QUERY_THRESHOLD.name()));
  }

  @Test
  public void numberFormatException() {
    Envs.setEnv(Collections.singletonMap("DB_PORT", "qqq"));

    thrown.expect(NumberFormatException.class);
    thrown.expectMessage("DB_PORT");
    thrown.expectMessage("qqq");

    Envs.allDBConfs();
  }

  @Test
  public void explainThresholdException() {
    Envs.setEnv(Collections.singletonMap("DB_EXPLAIN_QUERY_THRESHOLD", "qqq"));

    thrown.expect(NumberFormatException.class);
    thrown.expectMessage("DB_EXPLAIN_QUERY_THRESHOLD");
    thrown.expectMessage("qqq");

    Envs.allDBConfs();
  }

}

@@ -635,24 +635,77 @@
 IZE%22, %225%22);%0A
+    map.put(%22DB_CONNECTION_RELEASE_DELAY%22, %2212345%22);%0A
     map.put(
@@ -1344,32 +1344,160 @@
 OOLSIZE));%0A  %7D%0A%0A
+  @Test%0A  public void connectionReleaseDelay() %7B%0A    assertEquals(%2212345%22, Envs.getEnv(Envs.DB_CONNECTION_RELEASE_DELAY));%0A  %7D%0A%0A
   @Test%0A  public
@@ -1774,17 +1774,17 @@
 tEquals(
-4
+5
 , json.s

Add DB_CONNECTION_RELEASE_DELAY to Envs unit test.