package io.cloudsoft.tosca.a4c.brooklyn.spec;

import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.when;
import static org.testng.Assert.assertEquals;

import java.nio.file.Paths;
import java.util.Map;

import org.apache.brooklyn.api.entity.EntitySpec;
import org.apache.brooklyn.camp.brooklyn.spi.dsl.methods.BrooklynDslCommon;
import org.apache.brooklyn.core.config.ConfigKeys;
import org.apache.brooklyn.core.test.entity.TestEntity;
import org.apache.brooklyn.entity.software.base.SoftwareProcess;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import alien4cloud.component.repository.exception.CSARVersionNotFoundException;
import alien4cloud.model.components.DeploymentArtifact;
import alien4cloud.model.topology.NodeTemplate;
import io.cloudsoft.tosca.a4c.Alien4CloudToscaTest;
import io.cloudsoft.tosca.a4c.brooklyn.ToscaApplication;
import io.cloudsoft.tosca.a4c.brooklyn.ToscaFacade;

import com.google.common.base.Optional;
import com.google.common.collect.ImmutableMap;

public class RuntimeEnvironmentModifierSpecTest extends Alien4CloudToscaTest {

    @Mock
    private ToscaFacade alien4CloudFacade;
    @Mock
    private NodeTemplate nodeTemplate;
    @Mock
    private ToscaApplication toscaApplication;

    @BeforeMethod(alwaysRun = true)
    @Override
    public void setUp() throws Exception {
        super.setUp();
    }

    @BeforeClass
    public void initMocks(){
        MockitoAnnotations.initMocks(this);
    }

    @Test
    public void testArtifactLocationsAreConfiguredAsShellVariables() throws CSARVersionNotFoundException {
        final String artifactName = "artifactName";
        final String artifactKey  = "artifactKey";

        DeploymentArtifact artifact = new DeploymentArtifact();
        artifact.setArchiveName("archiveName");
        artifact.setArchiveVersion("archiveVersion");
        artifact.setArtifactName(artifactName);
        artifact.setArtifactType("tosca.artifacts.File");
        Map<String, DeploymentArtifact> artifacts = ImmutableMap.of(artifactKey, artifact);

        when(alien4CloudFacade.getArtifacts(anyString(), any(ToscaApplication.class))).thenReturn(artifacts.keySet());
        when(alien4CloudFacade.getArtifactPath(anyString(), any(ToscaApplication.class), anyString())).thenReturn(Optional.of(Paths.get("/tmp")));

        EntitySpec<TestEntity> spec = EntitySpec.create(TestEntity.class);
        RuntimeEnvironmentModifier modifier = new RuntimeEnvironmentModifier(mgmt, alien4CloudFacade);
        modifier.apply(spec, "", toscaApplication);
        assertEquals(spec.getConfig().get(SoftwareProcess.SHELL_ENVIRONMENT.subKey(artifactKey)), BrooklynDslCommon.formatString("%s/%s", BrooklynDslCommon.attributeWhenReady("run.dir"), artifactKey));

    }

}

@@ -2739,21 +2739,26 @@
     
-assertEquals(
+String%5B%5D actual = 
 spec
@@ -2828,17 +2828,77 @@
 actKey))
-,
+.toString().split(%22install.dir%22);%0A        String%5B%5D expected =
  Brookly
@@ -2927,16 +2927,19 @@
 g(%22%25s/%25s
+/%25s
 %22, Brook
@@ -2975,30 +2975,336 @@
 dy(%22
-run.dir%22), artifactKey
+install.dir%22),%22RANDOM%22, artifactKey).toString().split(%22install.dir%22);%0A        assertEquals(actual.length, expected.length);%0A        assertEquals(actual%5B0%5D, expected%5B0%5D);%0A        // remove the random string for comparison, since we can't seed the Random object%0A        assertEquals(actual%5B1%5D.substring(9), expected%5B1%5D.substring(9
 ));%0A

fix failing test