<commit_before>package org.motechproject.admin.it;

import ch.lambdaj.Lambda;
import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.util.EntityUtils;
import org.codehaus.jackson.JsonFactory;
import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.map.ObjectMapper;
import org.joda.time.DateTime;
import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mortbay.jetty.HttpStatus;
import org.motechproject.admin.domain.StatusMessage;
import org.motechproject.admin.messages.Level;
import org.motechproject.admin.service.StatusMessageService;
import org.motechproject.commons.date.util.DateUtil;
import org.motechproject.testing.osgi.BasePaxIT;
import org.motechproject.testing.osgi.container.MotechNativeTestContainerFactory;
import org.motechproject.testing.utils.TestContext;
import org.ops4j.pax.exam.ExamFactory;
import org.ops4j.pax.exam.junit.PaxExam;
import org.ops4j.pax.exam.spi.reactors.ExamReactorStrategy;
import org.ops4j.pax.exam.spi.reactors.PerSuite;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;

import javax.inject.Inject;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.List;

import static ch.lambdaj.Lambda.having;
import static ch.lambdaj.Lambda.on;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail;

@RunWith(PaxExam.class)
@ExamReactorStrategy(PerSuite.class)
@ExamFactory(MotechNativeTestContainerFactory.class)
public class AdminBundleIT extends BasePaxIT {
    private static final String ERROR_MSG = "test-error";
    private static final String DEBUG_MSG = "test-debug";
    private static final String WARNING_MSG = "test-warn";
    private static final String MODULE_NAME = "test-module";
    private static final DateTime TIMEOUT = DateUtil.nowUTC().plusHours(1);
    private static final String HOST = "localhost";
    private static final int PORT = TestContext.getJettyPort();

    @Inject
    private StatusMessageService statusMessageService;

    @Inject
    private BundleContext bundleContext;

    @BeforeClass
    public static void beforeClass() throws Exception {
        createAdminUser();
        login();
    }

    @Before
    public void setUp() {
        setUpSecurityContextForDefaultUser("manageBundles", "manageMessages", "manageLogs", "manageActivemq", "manageSettings");
    }

    @Test
    public void testStatusMessageService() {
        statusMessageService.error(ERROR_MSG, MODULE_NAME, TIMEOUT);
        statusMessageService.warn(WARNING_MSG, MODULE_NAME, TIMEOUT);
        statusMessageService.debug(DEBUG_MSG, MODULE_NAME, TIMEOUT);

        List<StatusMessage> messages = statusMessageService.getActiveMessages();
        messages = Lambda.filter(having(on(StatusMessage.class).getTimeout(), equalTo(TIMEOUT)), messages);

        assertFalse(messages.isEmpty());

        StatusMessage msg = findMsgByText(messages, ERROR_MSG);
        assertEquals(Level.ERROR, msg.getLevel());
        assertEquals(MODULE_NAME, msg.getModuleName());

        msg = findMsgByText(messages, WARNING_MSG);
        assertEquals(Level.WARN, msg.getLevel());
        assertEquals(MODULE_NAME, msg.getModuleName());

        msg = findMsgByText(messages, DEBUG_MSG);
        assertEquals(Level.DEBUG, msg.getLevel());
        assertEquals(MODULE_NAME, msg.getModuleName());
    }

    @Test
    public void testBundleController() throws IOException, InterruptedException {
        final String response = apiGet("bundles/");

        assertTrue(StringUtils.isNotBlank(response));
        JsonNode json = responseToJson(response);
        assertTrue("No bundles listed as active", json.size() > 0);
    }

    @Test
    public void testSettingsController() throws IOException, InterruptedException {
        final String response = apiGet("settings/platform");

        assertTrue(StringUtils.isNotBlank(response));
        JsonNode json = responseToJson(response);
        assertTrue("No settings listed", json.size() > 0);
    }

    @Test
    public void testMessageController() throws IOException, InterruptedException {
        statusMessageService.error(ERROR_MSG, MODULE_NAME, TIMEOUT);

        final String response = apiGet("messages");

        assertTrue(StringUtils.isNotBlank(response));
        JsonNode json = responseToJson(response);
        assertTrue("No messages listed", json.size() > 0);
    }

    @Test
    public void testUploadBundleFromRepository() throws IOException, InterruptedException {
            uploadBundle("Repository", "org.motechproject:cms-lite:LATEST", null,
                "on","org.motechproject.cms-lite");
    }

    @Test
    public void testUploadBundleFromFile() throws IOException, InterruptedException {
        File file = new File("src/test/resources/motech-upload-test-bundle.jar");

        uploadBundle("File", null, file, "on", "motech-upload-test-bundle");
    }

    private void uploadBundle(String moduleSource, String moduleId, File bundleFile,
                                 String startBundle, String bundleSymbolicName) throws IOException, InterruptedException {
        String uri = String.format("http://%s:%d/admin/api/bundles/upload/", HOST, PORT);

        HttpPost httpPost = new HttpPost(uri);
        MultipartEntityBuilder entity = MultipartEntityBuilder.create();
        Charset chars = Charset.forName("UTF-8");
        entity.setCharset(chars);
        entity.addTextBody("moduleSource", moduleSource, ContentType.MULTIPART_FORM_DATA);
        if (moduleSource.equals("Repository")) {
            entity.addTextBody("moduleId", moduleId, ContentType.MULTIPART_FORM_DATA);
        } else if (moduleSource.equals("File")) {
            assertNotNull(bundleFile);
            entity.addBinaryBody("bundleFile", bundleFile);
        } else {<commit_msg>FIX Remove dependency int AdminBundleIT test from cms-lite module<commit_after>package org.motechproject.admin.it;

import ch.lambdaj.Lambda;
import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.util.EntityUtils;
import org.codehaus.jackson.JsonFactory;
import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.map.ObjectMapper;
import org.joda.time.DateTime;
import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mortbay.jetty.HttpStatus;
import org.motechproject.admin.domain.StatusMessage;
import org.motechproject.admin.messages.Level;
import org.motechproject.admin.service.StatusMessageService;
import org.motechproject.commons.date.util.DateUtil;
import org.motechproject.testing.osgi.BasePaxIT;
import org.motechproject.testing.osgi.container.MotechNativeTestContainerFactory;
import org.motechproject.testing.utils.TestContext;
import org.ops4j.pax.exam.ExamFactory;
import org.ops4j.pax.exam.junit.PaxExam;
import org.ops4j.pax.exam.spi.reactors.ExamReactorStrategy;
import org.ops4j.pax.exam.spi.reactors.PerSuite;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;

import javax.inject.Inject;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.List;

import static ch.lambdaj.Lambda.having;
import static ch.lambdaj.Lambda.on;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail;

@RunWith(PaxExam.class)
@ExamReactorStrategy(PerSuite.class)
@ExamFactory(MotechNativeTestContainerFactory.class)
public class AdminBundleIT extends BasePaxIT {
    private static final String ERROR_MSG = "test-error";
    private static final String DEBUG_MSG = "test-debug";
    private static final String WARNING_MSG = "test-warn";
    private static final String MODULE_NAME = "test-module";
    private static final DateTime TIMEOUT = DateUtil.nowUTC().plusHours(1);
    private static final String HOST = "localhost";
    private static final int PORT = TestContext.getJettyPort();

    @Inject
    private StatusMessageService statusMessageService;

    @Inject
    private BundleContext bundleContext;

    @BeforeClass
    public static void beforeClass() throws Exception {
        createAdminUser();
        login();
    }

    @Before
    public void setUp() {
        setUpSecurityContextForDefaultUser("manageBundles", "manageMessages", "manageLogs", "manageActivemq", "manageSettings");
    }

    @Test
    public void testStatusMessageService() {
        statusMessageService.error(ERROR_MSG, MODULE_NAME, TIMEOUT);
        statusMessageService.warn(WARNING_MSG, MODULE_NAME, TIMEOUT);
        statusMessageService.debug(DEBUG_MSG, MODULE_NAME, TIMEOUT);

        List<StatusMessage> messages = statusMessageService.getActiveMessages();
        messages = Lambda.filter(having(on(StatusMessage.class).getTimeout(), equalTo(TIMEOUT)), messages);

        assertFalse(messages.isEmpty());

        StatusMessage msg = findMsgByText(messages, ERROR_MSG);
        assertEquals(Level.ERROR, msg.getLevel());
        assertEquals(MODULE_NAME, msg.getModuleName());

        msg = findMsgByText(messages, WARNING_MSG);
        assertEquals(Level.WARN, msg.getLevel());
        assertEquals(MODULE_NAME, msg.getModuleName());

        msg = findMsgByText(messages, DEBUG_MSG);
        assertEquals(Level.DEBUG, msg.getLevel());
        assertEquals(MODULE_NAME, msg.getModuleName());
    }

    @Test
    public void testBundleController() throws IOException, InterruptedException {
        final String response = apiGet("bundles/");

        assertTrue(StringUtils.isNotBlank(response));
        JsonNode json = responseToJson(response);
        assertTrue("No bundles listed as active", json.size() > 0);
    }

    @Test
    public void testSettingsController() throws IOException, InterruptedException {
        final String response = apiGet("settings/platform");

        assertTrue(StringUtils.isNotBlank(response));
        JsonNode json = responseToJson(response);
        assertTrue("No settings listed", json.size() > 0);
    }

    @Test
    public void testMessageController() throws IOException, InterruptedException {
        statusMessageService.error(ERROR_MSG, MODULE_NAME, TIMEOUT);

        final String response = apiGet("messages");

        assertTrue(StringUtils.isNotBlank(response));
        JsonNode json = responseToJson(response);
        assertTrue("No messages listed", json.size() > 0);
    }

    @Test
    public void testUploadBundleFromRepository() throws IOException, InterruptedException {
        uploadBundle("Repository", "org.motechproject:motech-tasks:LATEST", null,
                "on","org.motechproject.motech-tasks");
    }

    @Test
    public void testUploadBundleFromFile() throws IOException, InterruptedException {
        File file = new File("src/test/resources/motech-upload-test-bundle.jar");

        uploadBundle("File", null, file, "on", "motech-upload-test-bundle");
    }

    private void uploadBundle(String moduleSource, String moduleId, File bundleFile,
                                 String startBundle, String bundleSymbolicName) throws IOException, InterruptedException {
        String uri = String.format("http://%s:%d/admin/api/bundles/upload/", HOST, PORT);

        HttpPost httpPost = new HttpPost(uri);
        MultipartEntityBuilder entity = MultipartEntityBuilder.create();
        Charset chars = Charset.forName("UTF-8");
        entity.setCharset(chars);
        entity.addTextBody("moduleSource", moduleSource, ContentType.MULTIPART_FORM_DATA);
        if (moduleSource.equals("Repository")) {
            entity.addTextBody("moduleId", moduleId, ContentType.MULTIPART_FORM_DATA);
        } else if (moduleSource.equals("File")) {
            assertNotNull(bundleFile);
            entity.addBinaryBody("bundleFile", bundleFile);
        } else {
{'fraction_non_alphanumeric': 0.10494018952928383, 'fraction_numerical': 0.0013981668479105174, 'mean_word_length': 4.170682730923695, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5760346', 'n_tokens_mistral': 3678, 'n_tokens_neox': 3585, 'n_words': 726}