<commit_before>/*******************************************************************************
 * Copyright (c) 2013 GigaSpaces Technologies Ltd. All rights reserved
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/

package org.cloudifysource.cosmo.orchestrator.integration;

import com.google.common.base.Charsets;
import com.google.common.base.Throwables;
import com.google.common.collect.Maps;
import com.google.common.io.Resources;
import com.ning.http.client.AsyncHttpClient;
import com.ning.http.client.Request;
import org.cloudifysource.cosmo.config.TestConfig;
import org.cloudifysource.cosmo.manager.config.JettyFileServerForPluginsConfig;
import org.cloudifysource.cosmo.manager.dsl.DSLImporter;
import org.cloudifysource.cosmo.manager.dsl.config.JettyDSLImporterConfig;
import org.cloudifysource.cosmo.monitor.RiemannEventsLogger;
import org.cloudifysource.cosmo.monitor.config.RiemannEventsLoggerConfig;
import org.cloudifysource.cosmo.monitor.config.StateCacheFeederConfig;
import org.cloudifysource.cosmo.orchestrator.workflow.RuoteRuntime;
import org.cloudifysource.cosmo.orchestrator.workflow.RuoteWorkflow;
import org.cloudifysource.cosmo.orchestrator.workflow.config.DefaultRuoteWorkflowConfig;
import org.cloudifysource.cosmo.orchestrator.workflow.config.RuoteRuntimeConfig;
import org.cloudifysource.cosmo.statecache.config.StateCacheConfig;
import org.cloudifysource.cosmo.tasks.config.CeleryWorkerProcessConfig;
import org.cloudifysource.cosmo.tasks.config.EventHandlerConfig;
import org.cloudifysource.cosmo.tasks.config.JythonProxyConfig;
import org.cloudifysource.cosmo.tasks.config.TaskExecutorConfig;
import org.cloudifysource.cosmo.utils.config.TemporaryDirectoryConfig;
import org.robobninjas.riemann.spring.RiemannTestConfiguration;
import org.robobninjas.riemann.spring.server.RiemannProcess;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.PropertySource;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
import org.testng.annotations.Test;

import javax.inject.Inject;
import java.io.IOException;
import java.net.URL;
import java.util.Map;

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

/**
 * Test requirements:
 *
 * Python packages: celery, paramiko & python-vagrant.
 * Running rabbitmq server.
 *
 * @author <NAME>
 * @since 0.1
 */
@ContextConfiguration(classes = { VagrantAndWebserverServiceIT.Config.class })
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
public class VagrantAndWebserverServiceIT extends AbstractTestNGSpringContextTests {

    /**
     */
    @Configuration
    @Import({
            TemporaryDirectoryConfig.class,
            JettyFileServerForPluginsConfig.class,
            CeleryWorkerProcessConfig.class,
            StateCacheConfig.class,
            StateCacheFeederConfig.class,
            RiemannTestConfiguration.class,
            DefaultRuoteWorkflowConfig.class,
            RuoteRuntimeConfig.class,
            TaskExecutorConfig.class,
            EventHandlerConfig.class,
            JythonProxyConfig.class,
            JettyDSLImporterConfig.class,
            RiemannEventsLoggerConfig.class

    })
    @PropertySource("org/cloudifysource/cosmo/orchestrator/integration/config/test.properties")
    static class Config extends TestConfig {
    }

    @Inject
    private DSLImporter dslImporter;

    @Inject
    private RuoteRuntime ruoteRuntime;

    @Inject
    private RuoteWorkflow ruoteWorkflow;

    @Inject
    private TemporaryDirectoryConfig.TemporaryDirectory temporaryDirectory;

    @Value("${riemann.server.config-resource}")
    private String riemannConfigResourcePath;

    @Inject
    private RiemannProcess riemannProcess;

    @Inject
    private JettyFileServerForPluginsConfig jettyFileServerForPluginsConfig;

    @Inject
    private RiemannEventsLogger riemannEventsLogger;

    @Test(timeOut = 60000 * 5, groups = "vagrant", enabled = false)
    public void testWithVagrantHostProvisionerAndSimpleWebServerInstaller() throws IOException {
        test("org/cloudifysource/cosmo/dsl/integration_phase1/integration-phase1.yaml");
    }

    @Test(groups = "vagrant", enabled = true)
    public void testWithVagrantHostProvisionerAndRemoteCeleryWorker() throws IOException {
        test("org/cloudifysource/cosmo/dsl/integration_phase3/integration-phase3.yaml");
    }

    @Test(groups = "vagrant", enabled = true)
    public void testPhase4() throws Exception {
        test("org/cloudifysource/cosmo/dsl/integration_phase4/integration-phase4.yaml");
        assertPhase4FlaskApp();
    }

    private void test(String dslPath) throws IOException {

        String dslLocation = dslImporter.importDSL(dslPath);

        final Map<String, Object> workitemFields = Maps.newHashMap();
        workitemFields.put("dsl", dslLocation);
        workitemFields.put("riemann_pid", String.valueOf(riemannProcess.getPid()));
        workitemFields.put("riemann_config_path", Resources.getResource(riemannConfigResourcePath).getPath());
        workitemFields.put("riemann_config_template", readRiemannConfigTemplate());

        final Object wfid = ruoteWorkflow.asyncExecute(workitemFields);
        ruoteRuntime.waitForWorkflow(wfid);
    }

    private static String readRiemannConfigTemplate() {
        URL resource = Resources.getResource("riemann/riemann.config.template");
        try {
            return Resources.toString(resource, Charsets.UTF_8);
        } catch (IOException e) {
            throw Throwables.propagate(e);
        }
    }

    private void assertPhase4FlaskApp() throws Exception {
        AsyncHttpClient client = new AsyncHttpClient();
        try {
            String service = "http://10.0.0.5:8888/db";
            String key = "mykey";
            String value = "myvalue";
            Request request = client.preparePut(service + "/" + key + "/" + value).build();
            client.executeRequest(request).get();
            request = client.prepareGet(service + "/" + key).build();
            String result = client.executeRequest(request).get().getResponseBody();
            assertThat(result).isEqualTo(value);
            assertThat(true).isFalse();
        } finally {
            client.close();
        }
    }

}<commit_msg>CLOUDIFY-1944 To be or not to be<commit_after>/*******************************************************************************
 * Copyright (c) 2013 GigaSpaces Technologies Ltd. All rights reserved
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/

package org.cloudifysource.cosmo.orchestrator.integration;

import com.google.common.base.Charsets;
import com.google.common.base.Throwables;
import com.google.common.collect.Maps;
import com.google.common.io.Resources;
import com.ning.http.client.AsyncHttpClient;
import com.ning.http.client.Request;
import org.cloudifysource.cosmo.config.TestConfig;
import org.cloudifysource.cosmo.manager.config.JettyFileServerForPluginsConfig;
import org.cloudifysource.cosmo.manager.dsl.DSLImporter;
import org.cloudifysource.cosmo.manager.dsl.config.JettyDSLImporterConfig;
import org.cloudifysource.cosmo.monitor.RiemannEventsLogger;
import org.cloudifysource.cosmo.monitor.config.RiemannEventsLoggerConfig;
import org.cloudifysource.cosmo.monitor.config.StateCacheFeederConfig;
import org.cloudifysource.cosmo.orchestrator.workflow.RuoteRuntime;
import org.cloudifysource.cosmo.orchestrator.workflow.RuoteWorkflow;
import org.cloudifysource.cosmo.orchestrator.workflow.config.DefaultRuoteWorkflowConfig;
import org.cloudifysource.cosmo.orchestrator.workflow.config.RuoteRuntimeConfig;
import org.cloudifysource.cosmo.statecache.config.StateCacheConfig;
import org.cloudifysource.cosmo.tasks.config.CeleryWorkerProcessConfig;
import org.cloudifysource.cosmo.tasks.config.EventHandlerConfig;
import org.cloudifysource.cosmo.tasks.config.JythonProxyConfig;
import org.cloudifysource.cosmo.tasks.config.TaskExecutorConfig;
import org.cloudifysource.cosmo.utils.config.TemporaryDirectoryConfig;
import org.robobninjas.riemann.spring.RiemannTestConfiguration;
import org.robobninjas.riemann.spring.server.RiemannProcess;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.PropertySource;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
import org.testng.annotations.Test;

import javax.inject.Inject;
import java.io.IOException;
import java.net.URL;
import java.util.Map;

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

/**
 * Test requirements:
 *
 * Python packages: celery, paramiko & python-vagrant.
 * Running rabbitmq server.
 *
 * @author <NAME>
 * @since 0.1
 */
@ContextConfiguration(classes = { VagrantAndWebserverServiceIT.Config.class })
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
public class VagrantAndWebserverServiceIT extends AbstractTestNGSpringContextTests {

    /**
     */
    @Configuration
    @Import({
            TemporaryDirectoryConfig.class,
            JettyFileServerForPluginsConfig.class,
            CeleryWorkerProcessConfig.class,
            StateCacheConfig.class,
            StateCacheFeederConfig.class,
            RiemannTestConfiguration.class,
            DefaultRuoteWorkflowConfig.class,
            RuoteRuntimeConfig.class,
            TaskExecutorConfig.class,
            EventHandlerConfig.class,
            JythonProxyConfig.class,
            JettyDSLImporterConfig.class,
            RiemannEventsLoggerConfig.class

    })
    @PropertySource("org/cloudifysource/cosmo/orchestrator/integration/config/test.properties")
    static class Config extends TestConfig {
    }

    @Inject
    private DSLImporter dslImporter;

    @Inject
    private RuoteRuntime ruoteRuntime;

    @Inject
    private RuoteWorkflow ruoteWorkflow;

    @Inject
    private TemporaryDirectoryConfig.TemporaryDirectory temporaryDirectory;

    @Value("${riemann.server.config-resource}")
    private String riemannConfigResourcePath;

    @Inject
    private RiemannProcess riemannProcess;

    @Inject
    private JettyFileServerForPluginsConfig jettyFileServerForPluginsConfig;

    @Inject
    private RiemannEventsLogger riemannEventsLogger;

    @Test(timeOut = 60000 * 5, groups = "vagrant", enabled = false)
    public void testWithVagrantHostProvisionerAndSimpleWebServerInstaller() throws IOException {
        test("org/cloudifysource/cosmo/dsl/integration_phase1/integration-phase1.yaml");
    }

    @Test(groups = "vagrant", enabled = true)
    public void testWithVagrantHostProvisionerAndRemoteCeleryWorker() throws IOException {
        test("org/cloudifysource/cosmo/dsl/integration_phase3/integration-phase3.yaml");
    }

    @Test(groups = "vagrant", enabled = true)
    public void testPhase4() throws Exception {
        test("org/cloudifysource/cosmo/dsl/integration_phase4/integration-phase4.yaml");
        assertPhase4FlaskApp();
    }

    private void test(String dslPath) throws IOException {

        String dslLocation = dslImporter.importDSL(dslPath);

        final Map<String, Object> workitemFields = Maps.newHashMap();
        workitemFields.put("dsl", dslLocation);
        workitemFields.put("riemann_pid", String.valueOf(riemannProcess.getPid()));
        workitemFields.put("riemann_config_path", Resources.getResource(riemannConfigResourcePath).getPath());
        workitemFields.put("riemann_config_template", readRiemannConfigTemplate());

        final Object wfid = ruoteWorkflow.asyncExecute(workitemFields);
        ruoteRuntime.waitForWorkflow(wfid);
    }

    private static String readRiemannConfigTemplate() {
        URL resource = Resources.getResource("riemann/riemann.config.template");
        try {
            return Resources.toString(resource, Charsets.UTF_8);
        } catch (IOException e) {
            throw Throwables.propagate(e);
        }
    }

    private void assertPhase4FlaskApp() throws Exception {
        AsyncHttpClient client = new AsyncHttpClient();
        try {
            String service = "http://10.0.0.5:8888/db";
            String key = "mykey";
            String value = "myvalue";
            Request request = client.preparePut(service + "/" + key + "/" + value).build();
            client.executeRequest(request).get();
            request = client.prepareGet(service + "/" + key).build();
            String result = client.executeRequest(request).get().getResponseBody();
            assertThat(result).isEqualTo(value);
        } finally {
            client.close();
        }
    }

}
{'fraction_non_alphanumeric': 0.11189056471413539, 'fraction_numerical': 0.005191160996141705, 'mean_word_length': 4.146570397111914, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 2, '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': '5634281', 'n_tokens_mistral': 4067, 'n_tokens_neox': 3918, 'n_words': 715}