package actors;

import com.google.common.io.Files;
import metadata.etl.models.EtlJobName;
import org.junit.Test;
import wherehows.common.Constant;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.Properties;

import static org.fest.assertions.Assertions.assertThat;

public class ConfigUtilTest {

  @Test
  public void shouldGenerateEtlJobDefaultCommand() {
    // when:
    String command = ConfigUtil.generateCommand(0L, "", new Properties());

    // then:
    assertThat(command.startsWith("java -cp ")).isTrue();
    assertThat(command.endsWith(" -Dconfig=/var/tmp/wherehows/exec/0.properties metadata.etl.Launcher")).isTrue();
  }

  @Test
  public void shouldGenerateEtlJobCommandWithConfiguredDirectory() {
    final String applicationDirectory = "./temporary-directory";

    // given:
    Properties etlJobProperties = new Properties();
    etlJobProperties.put(Constant.WH_APP_FOLDER_KEY, applicationDirectory);

    // when:
    String command = ConfigUtil.generateCommand(1L, "", etlJobProperties);

    // then:
    assertThat(command.startsWith("java -cp ")).isTrue();
    assertThat(command.endsWith(" -Dconfig=" + applicationDirectory + "/exec/1.properties metadata.etl.Launcher")).isTrue();
  }

  @Test
  public void shouldGeneratePropertiesWithValues() throws IOException {
    final long whEtlExecId = System.currentTimeMillis();
    final Properties etlJobProperties = new Properties();
    etlJobProperties.put("p1", "v1");
    etlJobProperties.put("p2", "v2");
    etlJobProperties.put("p3", "v3");

    final File propertiesFile = createTemporaryPropertiesFile(whEtlExecId, etlJobProperties);

    // when:
    final EtlJobName etlJobName = EtlJobName.valueOf("HIVE_DATASET_METADATA_ETL");
    ConfigUtil.generateProperties(etlJobName, 2, whEtlExecId, etlJobProperties);

    // then:
    final String content = Files.toString(propertiesFile, Charset.defaultCharset());
    assertThat(content).contains("p1=v1");
    assertThat(content).contains("p2=v2");
    assertThat(content).contains("p3=v3");
  }

  @Test
  public void shouldGeneratePropertiesFileAndDeleteIt() throws IOException {
    final long whEtlExecId = System.currentTimeMillis();
    final Properties etlJobProperties = new Properties();

    final File propertiesFile = createTemporaryPropertiesFile(whEtlExecId, etlJobProperties);

    // expect:
    assertThat(propertiesFile.exists()).isFalse();

    // when:
    final EtlJobName etlJobName = EtlJobName.valueOf("AZKABAN_EXECUTION_METADATA_ETL");
    ConfigUtil.generateProperties(etlJobName, 2, whEtlExecId, etlJobProperties);

    // then:
    assertThat(propertiesFile.exists()).isTrue();

    // when:
    ConfigUtil.deletePropertiesFile(etlJobProperties, whEtlExecId);

    // then:
    assertThat(propertiesFile.exists()).isFalse();
  }

  private File createTemporaryPropertiesFile(long whEtlExecId, Properties etlJobProperties) {
    final File tempDir = Files.createTempDir();
    tempDir.deleteOnExit();
    final String tempDirPath = tempDir.getAbsolutePath();

    etlJobProperties.put(Constant.WH_APP_FOLDER_KEY, tempDirPath);

    return new File(tempDirPath + "/exec", whEtlExecId + ".properties");
  }
}

@@ -520,32 +520,46 @@
 sertThat(command
+)%0A            
 .startsWith(%22jav
@@ -570,51 +570,30 @@
 p %22)
-).isTrue();%0A    assertThat(command.endsWith
+%0A            .contains
 (%22 -
@@ -628,32 +628,59 @@
 xec/0.properties
+ %22)%0A            .endsWith(%22
  metadata.etl.La
@@ -683,34 +683,24 @@
 l.Launcher%22)
-).isTrue()
 ;%0A  %7D%0A%0A  @Te
@@ -1104,16 +1104,30 @@
 (command
+)%0A            
 .startsW
@@ -1146,51 +1146,30 @@
 p %22)
-).isTrue();%0A    assertThat(command.endsWith
+%0A            .contains
 (%22 -
@@ -1222,16 +1222,43 @@
 operties
+ %22)%0A            .endsWith(%22
  metadat
@@ -1273,26 +1273,16 @@
 uncher%22)
-).isTrue()
 ;%0A  %7D%0A%0A 
@@ -1976,32 +1976,45 @@
 ertThat(content)
+%0A            
 .contains(%22p1=v1
@@ -2019,33 +2019,21 @@
 v1%22)
-;
 %0A    
-assertThat(content)
+        
 .con
@@ -2050,33 +2050,21 @@
 v2%22)
-;
 %0A    
-assertThat(content)
+        
 .con
@@ -2424,34 +2424,30 @@
 tiesFile
-.exists()).isFalse
+).doesNotExist
 ();%0A%0A   
@@ -2660,32 +2660,33 @@
 t(propertiesFile
+)
 .exists()).isTru
@@ -2682,18 +2682,8 @@
 ts()
-).isTrue()
 ;%0A%0A 
@@ -2809,26 +2809,22 @@
 File
-.exists()).isFalse
+).doesNotExist
 ();%0A

test assertions more readable