<commit_before>package io.cloudsoft.tosca.a4c.brooklyn;

import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertTrue;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Path;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import javax.annotation.Nullable;

import org.apache.brooklyn.api.entity.Application;
import org.apache.brooklyn.api.entity.Entity;
import org.apache.brooklyn.api.entity.EntitySpec;
import org.apache.brooklyn.api.location.LocationSpec;
import org.apache.brooklyn.api.policy.PolicySpec;
import org.apache.brooklyn.camp.brooklyn.spi.dsl.BrooklynDslDeferredSupplier;
import org.apache.brooklyn.config.ConfigKey;
import org.apache.brooklyn.core.config.ConfigKeys;
import org.apache.brooklyn.core.entity.Entities;
import org.apache.brooklyn.core.entity.EntityAsserts;
import org.apache.brooklyn.core.sensor.Sensors;
import org.apache.brooklyn.core.test.policy.TestPolicy;
import org.apache.brooklyn.entity.database.mysql.MySqlNode;
import org.apache.brooklyn.entity.software.base.SameServerEntity;
import org.apache.brooklyn.entity.software.base.SoftwareProcess;
import org.apache.brooklyn.entity.software.base.VanillaSoftwareProcess;
import org.apache.brooklyn.entity.stock.BasicApplication;
import org.apache.brooklyn.entity.webapp.tomcat.TomcatServer;
import org.apache.brooklyn.location.ssh.SshMachineLocation;
import org.apache.brooklyn.policy.autoscaling.AutoScalerPolicy;
import org.apache.brooklyn.util.core.ResourceUtils;
import org.apache.brooklyn.util.stream.Streams;
import org.testng.annotations.Test;

import com.google.common.base.Predicate;
import com.google.common.collect.Iterables;
import com.google.common.collect.Iterators;
import com.google.common.io.Files;

import alien4cloud.component.repository.exception.CSARVersionAlreadyExistsException;
import alien4cloud.tosca.parser.ParsingException;
import alien4cloud.utils.FileUtil;
import io.cloudsoft.tosca.a4c.Alien4CloudIntegrationTest;
import io.cloudsoft.tosca.a4c.brooklyn.util.EntitySpecs;

public class ToscaTypePlanTransformerIntegrationTest extends Alien4CloudIntegrationTest {

    private String DATABASE_DEPENDENCY_INJECTION = "$brooklyn:formatString(\"jdbc:" +
            "%s%s?user=%s\\\\&password=%s\",$brooklyn:entity(\"mysql_server\")" +
            ".attributeWhenReady(\"datastore.url\")," +
            "visitors," +
            "brooklyn," +
            "br00k11n)";

    @Test
    public void testSimpleHostedTopologyParser() throws Exception {
        EntitySpec<? extends Application> app = create("classpath://templates/simple-web-server.yaml");
        assertNotNull(app);
        assertEquals(app.getChildren().size(), 1);

        EntitySpec<?> server = app.getChildren().get(0);
        assertEquals(server.getConfig().get(SoftwareProcess.CHILDREN_STARTABLE_MODE),
                SoftwareProcess.ChildStartableMode.BACKGROUND_LATE);

        assertEquals(server.getChildren().size(), 1);

        EntitySpec<?> hostedSoftwareComponent = server.getChildren().get(0);

        assertEquals(server.getFlags().get("tosca.node.type"), "tosca.nodes.Compute");
        assertEquals(server.getType(), SameServerEntity.class);
        assertEquals(server.getLocationSpecs().size(), 1);
        assertEquals(server.getLocationSpecs().get(0).getFlags().get("name"), "localhost");

        assertEquals(hostedSoftwareComponent.getFlags().get("tosca.node.type"),
                "tosca.nodes.SoftwareComponent");
        assertEquals(hostedSoftwareComponent.getType().getName(),
                "org.apache.brooklyn.entity.software.base.VanillaSoftwareProcess");

        assertFlagValueContains(hostedSoftwareComponent, VanillaSoftwareProcess.INSTALL_COMMAND.getName(),
                "# install python if not present");
        assertFlagValueContains(hostedSoftwareComponent, VanillaSoftwareProcess.CUSTOMIZE_COMMAND.getName(),
                "# create the web page to serve");
        assertFlagValueContains(hostedSoftwareComponent, VanillaSoftwareProcess.LAUNCH_COMMAND.getName(),
                "# launch in background (ensuring no streams open), and record PID to file");
        assertFlagValueContains(hostedSoftwareComponent, VanillaSoftwareProcess.STOP_COMMAND.getName(),
                "kill -9 `cat ${PID_FILE:-pid.txt}`");
    }

    @Test
    public void testDslInChatApplication() throws Exception {
        EntitySpec<? extends Application> app = create("classpath://templates/helloworld-sql.tosca.yaml");
        assertNotNull(app);
        assertEquals(app.getChildren().size(), 2);

        EntitySpec<?> tomcatServer = EntitySpecs
                .findChildEntitySpecByPlanId(app, "tomcat_server");
        assertEquals(tomcatServer.getConfig().get(TomcatServer.ROOT_WAR),
                "http://search.maven.org/remotecontent?filepath=io/brooklyn/example/" +
                        "brooklyn-example-hello-world-sql-webapp/0.6.0/" +
                        "brooklyn-example-hello-world-sql-webapp-0.6.0.war");
        assertNotNull(tomcatServer.getConfig().get(TomcatServer.JAVA_SYSPROPS));

        Map javaSysProps = (Map) tomcatServer.getConfig().get(TomcatServer.JAVA_SYSPROPS);
        assertEquals(javaSysProps.size(), 1);
        assertTrue(javaSysProps.get("brooklyn.example.db.url") instanceof BrooklynDslDeferredSupplier);
        assertEquals(javaSysProps.get("brooklyn.example.db.url").toString(), DATABASE_DEPENDENCY_INJECTION);

        assertEquals(tomcatServer.getLocationSpecs().size(), 1, "Expected one LocationSpec");
        assertTrue(tomcatServer.getLocationSpecs().get(0) instanceof LocationSpec);
    }

    @Test
    public void testFullJcloudsLocationDescription() throws Exception {
        EntitySpec<? extends Application> app = create("classpath://templates/full-location.jclouds.tosca.yaml");
        assertNotNull(app);
        assertEquals(app.getChildren().size(), 1);

        EntitySpec<?> vanillaEntity = Iterables.getOnlyElement(app.getChildren());
        assertEquals(vanillaEntity.getLocationSpecs().size(), 1);
        LocationSpec<?> locationSpec = Iterables.getOnlyElement(vanillaEntity.getLocationSpecs());
        assertEquals(locationSpec.getFlags().get("provider"), "aws-ec2");
        assertEquals(locationSpec.getFlags().get("region"), "us-west-2");
        assertEquals(locationSpec.getFlags().get("identity"), "user-key-id");
        assertEquals(locationSpec.getFlags().get("credential"), "user-key");
    }

    @Test
    public void testFullByonLocationDescription() throws Exception {
        EntitySpec<? extends Application> app = create("classpath://templates/full-location.byon.tosca.yaml");
        assertNotNull(app);
        assertEquals(app.getChildren().size(), 1);

        EntitySpec<?> vanillaEntity = Iterables.getOnlyElement(app.getChildren());
        assertEquals(vanillaEntity.getLocationSpecs().size(), 1);

        LocationSpec<?> locationSpec = Iterables.getOnlyElement(vanillaEntity.getLocationSpecs());
        Map<String, ?> configByon = locationSpec.getFlags();
        assertEquals(configByon.get("user"), "brooklyn");
        assertEquals(configByon.get("provider"), "byon");

        List<?> machineSpecs = (List) configByon.get("byon.machineSpecs");
        assertNotNull(machineSpecs);
        assertEquals(machineSpecs.size(), 1, "machineSpecs=" + Iterables.toString(machineSpecs));

        LocationSpec<?> spec = (LocationSpec<?>) machineSpecs.get(0);
        assertEquals(spec.getFlags().get("address"), "192.168.0.18");
    }

    @Test<commit_msg>Give more detail in assertion messages<commit_after>package io.cloudsoft.tosca.a4c.brooklyn;

import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertTrue;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Path;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import javax.annotation.Nullable;

import org.apache.brooklyn.api.entity.Application;
import org.apache.brooklyn.api.entity.Entity;
import org.apache.brooklyn.api.entity.EntitySpec;
import org.apache.brooklyn.api.location.LocationSpec;
import org.apache.brooklyn.api.policy.PolicySpec;
import org.apache.brooklyn.camp.brooklyn.spi.dsl.BrooklynDslDeferredSupplier;
import org.apache.brooklyn.config.ConfigKey;
import org.apache.brooklyn.core.config.ConfigKeys;
import org.apache.brooklyn.core.entity.Entities;
import org.apache.brooklyn.core.entity.EntityAsserts;
import org.apache.brooklyn.core.sensor.Sensors;
import org.apache.brooklyn.core.test.policy.TestPolicy;
import org.apache.brooklyn.entity.database.mysql.MySqlNode;
import org.apache.brooklyn.entity.software.base.SameServerEntity;
import org.apache.brooklyn.entity.software.base.SoftwareProcess;
import org.apache.brooklyn.entity.software.base.VanillaSoftwareProcess;
import org.apache.brooklyn.entity.stock.BasicApplication;
import org.apache.brooklyn.entity.webapp.tomcat.TomcatServer;
import org.apache.brooklyn.location.ssh.SshMachineLocation;
import org.apache.brooklyn.policy.autoscaling.AutoScalerPolicy;
import org.apache.brooklyn.util.core.ResourceUtils;
import org.apache.brooklyn.util.stream.Streams;
import org.testng.annotations.Test;

import com.google.common.base.Joiner;
import com.google.common.base.Predicate;
import com.google.common.collect.Iterables;
import com.google.common.collect.Iterators;
import com.google.common.io.Files;

import alien4cloud.component.repository.exception.CSARVersionAlreadyExistsException;
import alien4cloud.tosca.parser.ParsingException;
import alien4cloud.utils.FileUtil;
import io.cloudsoft.tosca.a4c.Alien4CloudIntegrationTest;
import io.cloudsoft.tosca.a4c.brooklyn.util.EntitySpecs;

public class ToscaTypePlanTransformerIntegrationTest extends Alien4CloudIntegrationTest {

    private String DATABASE_DEPENDENCY_INJECTION = "$brooklyn:formatString(\"jdbc:" +
            "%s%s?user=%s\\\\&password=%s\",$brooklyn:entity(\"mysql_server\")" +
            ".attributeWhenReady(\"datastore.url\")," +
            "visitors," +
            "brooklyn," +
            "br00k11n)";

    @Test
    public void testSimpleHostedTopologyParser() throws Exception {
        EntitySpec<? extends Application> app = create("classpath://templates/simple-web-server.yaml");
        assertNotNull(app);
        assertEquals(app.getChildren().size(), 1);

        EntitySpec<?> server = app.getChildren().get(0);
        assertEquals(server.getConfig().get(SoftwareProcess.CHILDREN_STARTABLE_MODE),
                SoftwareProcess.ChildStartableMode.BACKGROUND_LATE);

        assertEquals(server.getChildren().size(), 1);

        EntitySpec<?> hostedSoftwareComponent = server.getChildren().get(0);

        assertEquals(server.getFlags().get("tosca.node.type"), "tosca.nodes.Compute");
        assertEquals(server.getType(), SameServerEntity.class);
        assertEquals(server.getLocationSpecs().size(), 1);
        assertEquals(server.getLocationSpecs().get(0).getFlags().get("name"), "localhost");

        assertEquals(hostedSoftwareComponent.getFlags().get("tosca.node.type"),
                "tosca.nodes.SoftwareComponent");
        assertEquals(hostedSoftwareComponent.getType().getName(),
                "org.apache.brooklyn.entity.software.base.VanillaSoftwareProcess");

        assertFlagValueContains(hostedSoftwareComponent, VanillaSoftwareProcess.INSTALL_COMMAND.getName(),
                "# install python if not present");
        assertFlagValueContains(hostedSoftwareComponent, VanillaSoftwareProcess.CUSTOMIZE_COMMAND.getName(),
                "# create the web page to serve");
        assertFlagValueContains(hostedSoftwareComponent, VanillaSoftwareProcess.LAUNCH_COMMAND.getName(),
                "# launch in background (ensuring no streams open), and record PID to file");
        assertFlagValueContains(hostedSoftwareComponent, VanillaSoftwareProcess.STOP_COMMAND.getName(),
                "kill -9 `cat ${PID_FILE:-pid.txt}`");
    }

    @Test
    public void testDslInChatApplication() throws Exception {
        EntitySpec<? extends Application> app = create("classpath://templates/helloworld-sql.tosca.yaml");
        assertNotNull(app);
        assertEquals(app.getChildren().size(), 2);

        EntitySpec<?> tomcatServer = EntitySpecs
                .findChildEntitySpecByPlanId(app, "tomcat_server");
        assertEquals(tomcatServer.getConfig().get(TomcatServer.ROOT_WAR),
                "http://search.maven.org/remotecontent?filepath=io/brooklyn/example/" +
                        "brooklyn-example-hello-world-sql-webapp/0.6.0/" +
                        "brooklyn-example-hello-world-sql-webapp-0.6.0.war");
        assertNotNull(tomcatServer.getConfig().get(TomcatServer.JAVA_SYSPROPS));

        Map javaSysProps = (Map) tomcatServer.getConfig().get(TomcatServer.JAVA_SYSPROPS);
        assertEquals(javaSysProps.size(), 1);
        assertTrue(javaSysProps.get("brooklyn.example.db.url") instanceof BrooklynDslDeferredSupplier);
        assertEquals(javaSysProps.get("brooklyn.example.db.url").toString(), DATABASE_DEPENDENCY_INJECTION);

        assertEquals(tomcatServer.getLocationSpecs().size(), 1, "Expected one LocationSpec");
        assertTrue(tomcatServer.getLocationSpecs().get(0) instanceof LocationSpec);
    }

    @Test
    public void testFullJcloudsLocationDescription() throws Exception {
        EntitySpec<? extends Application> app = create("classpath://templates/full-location.jclouds.tosca.yaml");
        assertNotNull(app);
        assertEquals(app.getChildren().size(), 1);

        EntitySpec<?> vanillaEntity = Iterables.getOnlyElement(app.getChildren());
        assertEquals(vanillaEntity.getLocationSpecs().size(), 1);
        LocationSpec<?> locationSpec = Iterables.getOnlyElement(vanillaEntity.getLocationSpecs());
        assertEquals(locationSpec.getFlags().get("provider"), "aws-ec2");
        assertEquals(locationSpec.getFlags().get("region"), "us-west-2");
        assertEquals(locationSpec.getFlags().get("identity"), "user-key-id");
        assertEquals(locationSpec.getFlags().get("credential"), "user-key");
    }

    @Test
    public void testFullByonLocationDescription() throws Exception {
        EntitySpec<? extends Application> app = create("classpath://templates/full-location.byon.tosca.yaml");
        assertNotNull(app);
        assertEquals(app.getChildren().size(), 1);

        EntitySpec<?> vanillaEntity = Iterables.getOnlyElement(app.getChildren());
        assertEquals(vanillaEntity.getLocationSpecs().size(), 1);

        LocationSpec<?> locationSpec = Iterables.getOnlyElement(vanillaEntity.getLocationSpecs());
        Map<String, ?> configByon = locationSpec.getFlags();
        final String configStr = Joiner.on(", ").withKeyValueSeparator("=").join(configByon);
        assertEquals(configByon.get("user"), "brooklyn", "expected user=brooklyn in " + configStr);
        assertEquals(configByon.get("provider"), "byon", "expected provider=byon in " + configStr);

        List<?> machineSpecs = (List) configByon.get("byon.machineSpecs");
        assertNotNull(machineSpecs, "expected byon.machineSpecs != null in " + configStr);
        assertEquals(machineSpecs.size(), 1, "machineSpecs=" + Iterables.toString(machineSpecs));

        LocationSpec<?> spec = (LocationSpec<?>) machineSpecs.get(0);
        assertEquals(spec.getFlags().get("address"), "192.168.0.18", "expected address=192.168.0.18 in location flags " +
                Joiner.on(", ").withKeyValueSeparator("=").join(spec.getFlags()));
    }

    @Test
{'fraction_non_alphanumeric': 0.12896070975918886, 'fraction_numerical': 0.0064005069708491765, 'mean_word_length': 4.23937583001328, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 3, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3442922', 'n_tokens_mistral': 4646, 'n_tokens_neox': 4460, 'n_words': 625}