<commit_before>/*
 * Copyright 2012 JBoss Inc
 *
 * 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.overlord.sramp.governance.services;

import static org.overlord.sramp.common.test.resteasy.TestPortProvider.generateURL;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;

import org.apache.commons.io.IOUtils;
import org.jboss.as.cli.CliInitializationException;
import org.jboss.as.cli.CommandContext;
import org.jboss.as.cli.CommandContextFactory;
import org.jboss.as.cli.CommandLineException;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.overlord.sramp.common.test.resteasy.BaseResourceTest;
import org.overlord.sramp.governance.ConfigException;
import org.overlord.sramp.governance.Target;
import org.overlord.sramp.governance.services.rhq.RHQDeployUtil;


/**
 * Tests the Deployment API.
 *
 * @author <EMAIL>
 */
public class DeploymentResourceTest extends BaseResourceTest {

	@BeforeClass
	public static void setUpBrms() throws Exception {
		dispatcher.getRegistry().addPerRequestResource(DeploymentResource.class);
	}

	/**
	 * This is an integration test, and only works if artifact 'e67e1b09-1de7-4945-a47f-45646752437a'
     * exists in the repo; check the following urls to find out:
     *
	 * http://localhost:8080/s-ramp-server/s-ramp?query=/s-ramp[@uuid%3D'e67e1b09-1de7-4945-a47f-45646752437a']
	 * http://localhost:8080/s-ramp-server/s-ramp/user/BpmnDocument/e67e1b09-1de7-4945-a47f-45646752437a
	 *
	 * @throws Exception
	 */
	@Test @Ignore
	public void testDeployCopy() {
	    try {
	        URL url = new URL(generateURL("/deploy/copy/dev/e67e1b09-1de7-4945-a47f-45646752437a")); //$NON-NLS-1$
	        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
	        connection.setRequestMethod("POST"); //$NON-NLS-1$
	        connection.setConnectTimeout(10000);
	        connection.setReadTimeout(10000);
	        connection.connect();
	        int responseCode = connection.getResponseCode();
	        if (responseCode == 200) {
	             InputStream is = (InputStream) connection.getContent();
	             String reply = IOUtils.toString(is);
	             System.out.println("reply=" + reply); //$NON-NLS-1$
	        } else {
	            System.err.println("endpoint could not be reached"); //$NON-NLS-1$
	            Assert.fail();
	        }

	    } catch (Exception e) {
	        e.printStackTrace();
	        Assert.fail();
	    }
	}

	@Test @Ignore
	public void testDeployMaven() {
	}

	/**
	 * Only works with a running RHQ Server.
	 *
	 * @throws IOException
	 * @throws ConfigException
	 */
    @Test @Ignore
	public void testDeployRHQ() throws IOException, ConfigException {
		Target target = new Target("stage", "http://www.jboss.org/overlord/deployment-status.owl#InDev", //$NON-NLS-1$ //$NON-NLS-2$
		        "rhqadmin", "rhqadmin", "http://localhost:7080"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$

		RHQDeployUtil rhqDeployUtil = new RHQDeployUtil(target.getUser(), target.getPassword(),
				target.getRhqBaseUrl(), target.getPort());
		String groupName = target.getName();

		String artifactName = "test-simple2.war"; //$NON-NLS-1$
		//String artifactName = "s-ramp-server.war";
		//String artifactName = "dtgov-workflows2.jar";

		Integer groupId = rhqDeployUtil.getGroupIdForGroup(groupName);

		rhqDeployUtil.wipeArchiveIfNecessary(artifactName, groupId);
		List<Integer> resourceIds = rhqDeployUtil.getServerIdsForGroup(groupId);
		InputStream is = getClass().getClassLoader().getResourceAsStream(artifactName);
		byte[] fileContent = IOUtils.toByteArray(is);

		IOUtils.closeQuietly(is);
		for (Integer resourceId : resourceIds) {
		    System.out.println(String.format("Deploying %1$s to RHQ Server %2$s", artifactName, resourceId)); //$NON-NLS-1$
			rhqDeployUtil.deploy(resourceId, fileContent, artifactName);
		}<commit_msg>https://bugzilla.redhat.com/show_bug.cgi?id=1053683 rhqPluginName no longer hardcoded<commit_after>/*
 * Copyright 2012 JBoss Inc
 *
 * 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.overlord.sramp.governance.services;

import static org.overlord.sramp.common.test.resteasy.TestPortProvider.generateURL;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;

import org.apache.commons.io.IOUtils;
import org.jboss.as.cli.CliInitializationException;
import org.jboss.as.cli.CommandContext;
import org.jboss.as.cli.CommandContextFactory;
import org.jboss.as.cli.CommandLineException;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.overlord.sramp.common.test.resteasy.BaseResourceTest;
import org.overlord.sramp.governance.ConfigException;
import org.overlord.sramp.governance.Target;
import org.overlord.sramp.governance.services.rhq.RHQDeployUtil;


/**
 * Tests the Deployment API.
 *
 * @author <EMAIL>
 */
public class DeploymentResourceTest extends BaseResourceTest {

	@BeforeClass
	public static void setUpBrms() throws Exception {
		dispatcher.getRegistry().addPerRequestResource(DeploymentResource.class);
	}

	/**
	 * This is an integration test, and only works if artifact 'e67e1b09-1de7-4945-a47f-45646752437a'
     * exists in the repo; check the following urls to find out:
     *
	 * http://localhost:8080/s-ramp-server/s-ramp?query=/s-ramp[@uuid%3D'e67e1b09-1de7-4945-a47f-45646752437a']
	 * http://localhost:8080/s-ramp-server/s-ramp/user/BpmnDocument/e67e1b09-1de7-4945-a47f-45646752437a
	 *
	 * @throws Exception
	 */
	@Test @Ignore
	public void testDeployCopy() {
	    try {
	        URL url = new URL(generateURL("/deploy/copy/dev/e67e1b09-1de7-4945-a47f-45646752437a")); //$NON-NLS-1$
	        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
	        connection.setRequestMethod("POST"); //$NON-NLS-1$
	        connection.setConnectTimeout(10000);
	        connection.setReadTimeout(10000);
	        connection.connect();
	        int responseCode = connection.getResponseCode();
	        if (responseCode == 200) {
	             InputStream is = (InputStream) connection.getContent();
	             String reply = IOUtils.toString(is);
	             System.out.println("reply=" + reply); //$NON-NLS-1$
	        } else {
	            System.err.println("endpoint could not be reached"); //$NON-NLS-1$
	            Assert.fail();
	        }

	    } catch (Exception e) {
	        e.printStackTrace();
	        Assert.fail();
	    }
	}

	@Test @Ignore
	public void testDeployMaven() {
	}

	/**
	 * Only works with a running RHQ Server.
	 *
	 * @throws IOException
	 * @throws ConfigException
	 */
    @Test @Ignore
	public void testDeployRHQ() throws IOException, ConfigException {
		Target target = new Target("stage", "http://www.jboss.org/overlord/deployment-status.owl#InDev", //$NON-NLS-1$ //$NON-NLS-2$
		        "rhqadmin", "rhqadmin", "http://localhost:7080", "JBossAS7"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$

		RHQDeployUtil rhqDeployUtil = new RHQDeployUtil(target.getUser(), target.getPassword(),
				target.getRhqBaseUrl(), target.getPort(), "JBossAS7");
		String groupName = target.getName();

		String artifactName = "test-simple2.war"; //$NON-NLS-1$
		//String artifactName = "s-ramp-server.war";
		//String artifactName = "dtgov-workflows2.jar";

		Integer groupId = rhqDeployUtil.getGroupIdForGroup(groupName);

		rhqDeployUtil.wipeArchiveIfNecessary(artifactName, groupId);
		List<Integer> resourceIds = rhqDeployUtil.getServerIdsForGroup(groupId);
		InputStream is = getClass().getClassLoader().getResourceAsStream(artifactName);
		byte[] fileContent = IOUtils.toByteArray(is);

		IOUtils.closeQuietly(is);
		for (Integer resourceId : resourceIds) {
		    System.out.println(String.format("Deploying %1$s to RHQ Server %2$s", artifactName, resourceId)); //$NON-NLS-1$
			rhqDeployUtil.deploy(resourceId, fileContent, artifactName);
		}
{'fraction_non_alphanumeric': 0.1253866548828988, 'fraction_numerical': 0.033031374281926644, 'mean_word_length': 3.8128654970760234, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 1, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, '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': '2409634', 'n_tokens_mistral': 3198, 'n_tokens_neox': 2875, 'n_words': 618}