package com.marklogic.hub.core;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.marklogic.hub.DatabaseKind;
import com.marklogic.hub.HubConfig;
import com.marklogic.hub.HubTestBase;
import com.marklogic.hub.error.DataHubConfigurationException;
import org.apache.commons.io.FileUtils;
import org.junit.Before;
import org.junit.Test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

import static org.junit.Assert.*;

//import org.apache.htrace.fasterxml.jackson.databind.ObjectMapper;


public class HubConfigTest extends HubTestBase {


    private static File projectPath = new File(PROJECT_PATH);

    @Before
    public void setup() throws IOException {
        FileUtils.deleteDirectory(projectPath);
        HubConfig config = getHubFlowRunnerConfig();
        config.initHubProject();
    }

    private void deleteProp(String key) {
        try {
            File gradleProperties = new File(projectPath, "gradle.properties");
            Properties props = new Properties();
            FileInputStream fis = new FileInputStream(gradleProperties);
            props.load(fis);
            fis.close();
            props.remove(key);
            FileOutputStream fos = new FileOutputStream(gradleProperties);
            props.store(fos, "");
            fos.close();
        }
        catch(IOException e) {
            throw new RuntimeException(e);
        }
    }
    private void writeProp(String key, String value) {
        try {
            File gradleProperties = new File(projectPath, "gradle.properties");
            Properties props = new Properties();
            FileInputStream fis = new FileInputStream(gradleProperties);
            props.load(fis);
            fis.close();
            props.put(key, value);
            FileOutputStream fos = new FileOutputStream(gradleProperties);
            props.store(fos, "");
            fos.close();
        }
        catch(IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Test
    public void testLoadBalancerProps() {
        deleteProp("mlLoadBalancerHosts");
        assertNull(getHubFlowRunnerConfig().getLoadBalancerHost());

        writeProp("mlIsHostLoadBalancer", "true");
        assertTrue(getHubFlowRunnerConfig().getIsHostLoadBalancer());

        writeProp("mlLoadBalancerHosts", getHubFlowRunnerConfig().getHost());
        assertEquals(getHubFlowRunnerConfig().getHost(), getHubFlowRunnerConfig().getLoadBalancerHost());

        try {
            writeProp("mlLoadBalancerHosts", "host1");
            getHubFlowRunnerConfig();
        }
        catch (DataHubConfigurationException e){
            assertEquals( "mlLoadBalancerHosts must be the same as mlHost", e.getMessage());
        }

        deleteProp("mlIsHostLoadBalancer");
        assertFalse(getHubFlowRunnerConfig().getIsHostLoadBalancer());
    }


    @Test
    public void testHubInfo() {

        HubConfig config = getHubFlowRunnerConfig();
        ObjectMapper objmapper = new ObjectMapper();

        try {

            JsonNode jsonNode = objmapper.readTree(config.getInfo());

            assertTrue(jsonNode.get("stagingDbName").asText().equals(config.getDbName(DatabaseKind.STAGING)));

            assertTrue(jsonNode.get("stagingHttpName").asText().equals(config.getHttpName(DatabaseKind.STAGING)));

            assertTrue(jsonNode.get("finalForestsPerHost").asInt() == config.getForestsPerHost(DatabaseKind.FINAL));

            assertTrue(jsonNode.get("finalPort").asInt() == config.getPort(DatabaseKind.FINAL));

        }
        catch (Exception e)
        {
            throw new DataHubConfigurationException("Your datahub configuration could not serialize");
        }
    }


}

@@ -3233,36 +3233,38 @@
           assert
-True
+Equals
 (jsonNode.get(%22s
@@ -3282,32 +3282,26 @@
 e%22).asText()
-.equals(
+, 
 config.getDb
@@ -3319,33 +3319,32 @@
 seKind.STAGING))
-)
 ;%0A%0A            a
@@ -3340,36 +3340,38 @@
           assert
-True
+Equals
 (jsonNode.get(%22s
@@ -3399,16 +3399,10 @@
 xt()
-.equals(
+, 
 conf
@@ -3438,17 +3438,16 @@
 TAGING))
-)
 ;%0A%0A     
@@ -3451,36 +3451,38 @@
           assert
-True
+Equals
 (jsonNode.get(%22f
@@ -3501,35 +3501,39 @@
 erHost%22).asInt()
- ==
+, (int)
  config.getFores
@@ -3583,20 +3583,22 @@
   assert
-True
+Equals
 (jsonNod
@@ -3627,11 +3627,15 @@
 nt()
- ==
+, (int)
  con

test cleanup