001/**
002 * Copyright 2015 DuraSpace, Inc.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.fcrepo.camel.karaf;
017
018import static org.apache.http.HttpStatus.SC_OK;
019import static org.apache.http.impl.client.HttpClientBuilder.create;
020import static org.slf4j.LoggerFactory.getLogger;
021import static org.junit.Assert.assertEquals;
022import static org.junit.Assert.assertNotNull;
023import static org.junit.Assert.assertTrue;
024import static org.ops4j.pax.exam.CoreOptions.bundle;
025import static org.ops4j.pax.exam.CoreOptions.maven;
026import static org.ops4j.pax.exam.CoreOptions.mavenBundle;
027import static org.ops4j.pax.exam.CoreOptions.systemProperty;
028import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.configureConsole;
029import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.features;
030import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.karafDistributionConfiguration;
031import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.editConfigurationFilePut;
032import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.keepRuntimeFolder;
033import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.logLevel;
034import static org.ops4j.pax.exam.util.PathUtils.getBaseDir;
035import static org.osgi.framework.Bundle.ACTIVE;
036
037import java.io.File;
038
039import javax.inject.Inject;
040
041import org.apache.http.client.methods.CloseableHttpResponse;
042import org.apache.http.client.methods.HttpGet;
043import org.apache.http.client.methods.HttpPost;
044import org.apache.http.entity.StringEntity;
045import org.apache.http.impl.client.CloseableHttpClient;
046import org.apache.karaf.features.FeaturesService;
047import org.junit.Test;
048import org.junit.runner.RunWith;
049import org.ops4j.pax.exam.Configuration;
050import org.ops4j.pax.exam.ConfigurationManager;
051import org.ops4j.pax.exam.Option;
052import org.ops4j.pax.exam.junit.PaxExam;
053import org.ops4j.pax.exam.karaf.options.LogLevelOption.LogLevel;
054import org.osgi.framework.BundleContext;
055import org.slf4j.Logger;
056
057/**
058 * @author Aaron Coburn
059 * @since February 8, 2016
060 */
061@RunWith(PaxExam.class)
062public class KarafIT {
063
064    private static Logger LOG = getLogger(KarafIT.class);
065
066    @Inject
067    protected FeaturesService featuresService;
068
069    @Inject
070    protected BundleContext bundleContext;
071
072    @Configuration
073    public Option[] config() {
074        final ConfigurationManager cm = new ConfigurationManager();
075        final String fcrepoPort = cm.getProperty("fcrepo.dynamic.test.port");
076        final String jmsPort = cm.getProperty("fcrepo.dynamic.jms.port");
077        final String reindexingPort = cm.getProperty("fcrepo.dynamic.reindexing.port");
078        final String rmiRegistryPort = cm.getProperty("karaf.rmiRegistry.port");
079        final String rmiServerPort = cm.getProperty("karaf.rmiServer.port");
080        final String sshPort = cm.getProperty("karaf.ssh.port");
081        final String fcrepoFixity = "file:" + getBaseDir() + "/../fcrepo-fixity/target/fcrepo-fixity-" +
082                        cm.getProperty("project.version") + ".jar";
083        final String fcrepoSerialization = "file:" + getBaseDir() + "/../fcrepo-serialization/target/fcrepo-serialization-" +
084                        cm.getProperty("project.version") + ".jar";
085        final String fcrepoReindexing = "file:" + getBaseDir() + "/../fcrepo-reindexing/target/fcrepo-reindexing-" +
086                        cm.getProperty("project.version") + ".jar";
087        final String fcrepoIndexingSolr = "file:" + getBaseDir() + "/../fcrepo-indexing-solr/target/fcrepo-indexing-solr-" +
088                        cm.getProperty("project.version") + ".jar";
089        final String fcrepoIndexingTriplestore = "file:" + getBaseDir() + "/../fcrepo-indexing-triplestore/target/" +
090                        "fcrepo-indexing-triplestore-" + cm.getProperty("project.version") + ".jar";
091        return new Option[] {
092            karafDistributionConfiguration()
093                .frameworkUrl(maven().groupId("org.apache.karaf").artifactId("apache-karaf")
094                        .versionAsInProject().type("zip"))
095                .unpackDirectory(new File("target", "exam"))
096                .useDeployFolder(false),
097            logLevel(LogLevel.WARN),
098            keepRuntimeFolder(),
099            configureConsole().ignoreLocalConsole(),
100            features(maven().groupId("org.apache.karaf.features").artifactId("standard")
101                        .versionAsInProject().classifier("features").type("xml"), "scr"),
102            features(maven().groupId("org.apache.camel.karaf").artifactId("apache-camel")
103                        .type("xml").classifier("features").versionAsInProject(), "camel-mustache",
104                        "camel-blueprint", "camel-http4", "camel-spring", "camel-exec", "camel-jetty9", "camel-jacksonxml"),
105            features(maven().groupId("org.apache.activemq").artifactId("activemq-karaf")
106                        .type("xml").classifier("features").versionAsInProject(), "activemq-camel"),
107            features(maven().groupId("org.fcrepo.camel").artifactId("fcrepo-camel")
108                        .type("xml").classifier("features").versionAsInProject(), "fcrepo-camel"),
109            mavenBundle().groupId("org.codehaus.woodstox").artifactId("woodstox-core-asl").versionAsInProject(),
110
111            systemProperty("o.f.c.serialization-bundle").value(fcrepoSerialization),
112            systemProperty("o.f.c.fixity-bundle").value(fcrepoFixity),
113            systemProperty("o.f.c.reindexing-bundle").value(fcrepoReindexing),
114            systemProperty("o.f.c.i.triplestore-bundle").value(fcrepoIndexingTriplestore),
115            systemProperty("o.f.c.i.solr-bundle").value(fcrepoIndexingSolr),
116
117            bundle(fcrepoIndexingSolr).start(),
118            bundle(fcrepoIndexingTriplestore).start(),
119            bundle(fcrepoFixity).start(),
120            bundle(fcrepoSerialization).start(),
121            bundle(fcrepoReindexing).start(),
122
123            systemProperty("karaf.reindexing.port").value(reindexingPort),
124            editConfigurationFilePut("etc/org.apache.karaf.management.cfg", "rmiRegistryPort", rmiRegistryPort),
125            editConfigurationFilePut("etc/org.apache.karaf.management.cfg", "rmiServerPort", rmiServerPort),
126            editConfigurationFilePut("etc/org.apache.karaf.shell.cfg", "sshPort", sshPort),
127            editConfigurationFilePut("etc/org.fcrepo.camel.indexing.triplestore.cfg", "fcrepo.baseUrl", "localhost:" + fcrepoPort + "/fcrepo/rest"),
128            editConfigurationFilePut("etc/org.fcrepo.camel.indexing.triplestore.cfg", "jms.brokerUrl", "tcp://localhost:" + jmsPort),
129            editConfigurationFilePut("etc/org.fcrepo.camel.indexing.solr.cfg", "fcrepo.baseUrl", "localhost:" + fcrepoPort + "/fcrepo/rest"),
130            editConfigurationFilePut("etc/org.fcrepo.camel.indexing.solr.cfg", "jms.brokerUrl", "tcp://localhost:" + jmsPort),
131            editConfigurationFilePut("etc/org.fcrepo.camel.reindexing.cfg", "fcrepo.baseUrl", "localhost:" + fcrepoPort + "/fcrepo/rest"),
132            editConfigurationFilePut("etc/org.fcrepo.camel.reindexing.cfg", "jms.brokerUrl", "tcp://localhost:" + jmsPort),
133            editConfigurationFilePut("etc/org.fcrepo.camel.reindexing.cfg", "rest.port", reindexingPort),
134            editConfigurationFilePut("etc/org.fcrepo.camel.serialization.cfg", "jms.brokerUrl", "tcp://localhost:" + jmsPort),
135            editConfigurationFilePut("etc/org.fcrepo.camel.audit.triplestore.cfg", "jms.brokerUrl", "tcp://localhost:" + jmsPort),
136            editConfigurationFilePut("etc/org.fcrepo.camel.fixity.cfg", "jms.brokerUrl", "tcp://localhost:" + jmsPort),
137       };
138    }
139
140    @Test
141    public void testInstallation() throws Exception {
142
143        assertTrue(featuresService.isInstalled(featuresService.getFeature("camel-core")));
144        assertTrue(featuresService.isInstalled(featuresService.getFeature("fcrepo-camel")));
145        assertNotNull(bundleContext);
146
147        assertEquals(ACTIVE, bundleContext.getBundle(System.getProperty("o.f.c.serialization-bundle")).getState());
148        assertEquals(ACTIVE, bundleContext.getBundle(System.getProperty("o.f.c.fixity-bundle")).getState());
149        assertEquals(ACTIVE, bundleContext.getBundle(System.getProperty("o.f.c.reindexing-bundle")).getState());
150        assertEquals(ACTIVE, bundleContext.getBundle(System.getProperty("o.f.c.i.solr-bundle")).getState());
151        assertEquals(ACTIVE, bundleContext.getBundle(System.getProperty("o.f.c.i.triplestore-bundle")).getState());
152    }
153
154    @Test
155    public void testReindexingService() throws Exception {
156        final CloseableHttpClient client = create().build();
157        final String reindexingUrl = "http://localhost:" + System.getProperty("karaf.reindexing.port") + "/reindexing/";
158        try (final CloseableHttpResponse response = client.execute(new HttpGet(reindexingUrl))) {
159            assertEquals(SC_OK, response.getStatusLine().getStatusCode());
160        }
161
162        final HttpPost post = new HttpPost(reindexingUrl);
163        post.addHeader("Content-Type", "application/json");
164        post.setEntity(new StringEntity("[\"log:fcrepo\"]"));
165        try (final CloseableHttpResponse response = client.execute(post)) {
166            assertEquals(SC_OK, response.getStatusLine().getStatusCode());
167        }
168    }
169}