package viper.cditests;

import static org.assertj.core.api.Assertions.assertThat;

import java.lang.reflect.Field;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Properties;
import javax.enterprise.inject.Instance;
import javax.inject.Inject;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import viper.ConfigurationResolver;

@RunWith(Arquillian.class)
public class EnumWithPropertiesFileTest {

  private static final String EXPECTED_AGE = "42";
  private static final String EXPECTED_NAME = "Obama";

  @Inject
  @EnumWithPropertiesFileConfiguration(EnumWithPropertiesFile.NAME)
  Instance<String> name;

  @Inject
  @EnumWithPropertiesFileConfiguration(EnumWithPropertiesFile.AGE)
  Instance<String> age;

  @BeforeClass
  public static void setUp() throws Exception{
    /* Generating property file from path encoded in generated configuration resolver.
     * This is ugly but the alternative is to
     *  + edit the enum by manipulating the java source file and changing the property file's path
     *  + re-run the compiler via java
     *  + class-load the compiled sources and generated sources
     *  + generate a deployable jar with shrinkwrap
     *  + make sure there is a valid property file in the path
     *  + finally run the test
     */
    Field field = EnumWithPropertiesFilePropertyFileConfigurationResolver.class.getDeclaredField(
        "PROPERTIES_PATH");
    field.setAccessible(true);
    String propertyFilePath = String.class.cast(field.get(null));

    Properties properties = new Properties();
    properties.setProperty(EnumWithPropertiesFile.NAME.name().toLowerCase(), EXPECTED_NAME);
    properties.setProperty(EnumWithPropertiesFile.AGE.name().toLowerCase(), EXPECTED_AGE);
    properties.store(Files.newOutputStream(Paths.get(propertyFilePath)), null);
  }

  @Test
  public void shouldReadPropertiesFromFile() throws Exception {
    assertThat(name.get()).isEqualTo(EXPECTED_NAME);
    assertThat(age.get()).isEqualTo(EXPECTED_AGE);
  }

  @Deployment
  public static JavaArchive createDeployment() {
    return ShrinkWrap.create(JavaArchive.class)
        .addClass(EnumWithPropertiesFile.class)
        .addClass(EnumWithPropertiesFilePropertyFileConfigurationResolver.class)
        .addClass(ConfigurationResolver.class)
        .addClass(EnumWithPropertiesFileConfiguration.class)
        .addClass(EnumWithPropertiesFileConfigurationBean.class)
        .addAsManifestResource(
            EmptyAsset.INSTANCE, "beans.xml");
  }
}

@@ -133,24 +133,51 @@
 file.Files;%0A
+import java.nio.file.Path;%0A
 import java.
@@ -192,16 +192,16 @@
 .Paths;%0A
-
 import j
@@ -539,16 +539,45 @@
 rchive;%0A
+import org.junit.AfterClass;%0A
 import o
@@ -1074,16 +1074,47 @@
 %3E age;%0A%0A
+  static Path propertiesPath;%0A%0A
   @Befor
@@ -2128,71 +2128,260 @@
 ties
-.store(Files.newOutputStream(Paths.get(propertyFilePath)), null
+Path = Paths.get(propertyFilePath);%0A    Files.deleteIfExists(propertiesPath);%0A    properties.store(Files.newOutputStream(propertiesPath), null);%0A  %7D%0A%0A  @AfterClass%0A  public static void cleanup() throws Exception %7B%0A%09  Files.deleteIfExists(propertiesPath
 );%0A 

Clean properties file before and after tests