001/*
002 * Licensed to DuraSpace under one or more contributor license agreements.
003 * See the NOTICE file distributed with this work for additional information
004 * regarding copyright ownership.
005 *
006 * DuraSpace licenses this file to you under the Apache License,
007 * Version 2.0 (the "License"); you may not use this file except in
008 * compliance with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.fcrepo.camel.karaf;
019
020import static org.apache.http.HttpStatus.SC_OK;
021import static org.apache.http.impl.client.HttpClients.createDefault;
022import static org.junit.Assert.assertEquals;
023import static org.junit.Assert.assertNotNull;
024import static org.junit.Assert.assertTrue;
025import static org.ops4j.pax.exam.CoreOptions.bundle;
026import static org.ops4j.pax.exam.CoreOptions.maven;
027import static org.ops4j.pax.exam.CoreOptions.mavenBundle;
028import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.configureConsole;
029import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.editConfigurationFilePut;
030import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.features;
031import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.karafDistributionConfiguration;
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;
036import static org.osgi.framework.Constants.OBJECTCLASS;
037import static org.osgi.framework.FrameworkUtil.createFilter;
038import static org.slf4j.LoggerFactory.getLogger;
039
040import java.io.File;
041import javax.inject.Inject;
042
043import org.apache.camel.CamelContext;
044import org.apache.http.client.methods.CloseableHttpResponse;
045import org.apache.http.client.methods.HttpGet;
046import org.apache.http.impl.client.CloseableHttpClient;
047import org.apache.karaf.features.FeaturesService;
048import org.junit.Test;
049import org.junit.runner.RunWith;
050import org.ops4j.pax.exam.Configuration;
051import org.ops4j.pax.exam.ConfigurationManager;
052import org.ops4j.pax.exam.CoreOptions;
053import org.ops4j.pax.exam.Option;
054import org.ops4j.pax.exam.junit.PaxExam;
055import org.ops4j.pax.exam.karaf.options.LogLevelOption.LogLevel;
056import org.ops4j.pax.exam.spi.reactors.ExamReactorStrategy;
057import org.ops4j.pax.exam.spi.reactors.PerClass;
058import org.osgi.framework.BundleContext;
059import org.osgi.framework.InvalidSyntaxException;
060import org.osgi.util.tracker.ServiceTracker;
061import org.slf4j.Logger;
062
063/**
064 * @author Aaron Coburn
065 * @since February 8, 2016
066 */
067@RunWith(PaxExam.class)
068@ExamReactorStrategy(PerClass.class)
069public class KarafIT {
070
071    private static Logger LOGGER = getLogger(KarafIT.class);
072
073    @Inject
074    protected FeaturesService featuresService;
075
076    @Inject
077    protected BundleContext bundleContext;
078
079    @Configuration
080    public Option[] config() {
081        final ConfigurationManager cm = new ConfigurationManager();
082        final String fcrepoPort = cm.getProperty("fcrepo.dynamic.test.port");
083        final String jmsPort = cm.getProperty("fcrepo.dynamic.jms.port");
084        final String reindexingPort = cm.getProperty("fcrepo.dynamic.reindexing.port");
085        final String rmiRegistryPort = cm.getProperty("karaf.rmiRegistry.port");
086        final String rmiServerPort = cm.getProperty("karaf.rmiServer.port");
087        final String sshPort = cm.getProperty("karaf.ssh.port");
088        final String fcrepoBaseUrl = "localhost:" + fcrepoPort + "/fcrepo/rest";
089
090        final String version = cm.getProperty("project.version");
091        final String fcrepoAudit = getBundleUri("fcrepo-audit-triplestore", version);
092        final String fcrepoFixity = getBundleUri("fcrepo-fixity", version);
093        final String fcrepoReindexing = getBundleUri("fcrepo-reindexing", version);
094        final String fcrepoSerialization = getBundleUri("fcrepo-serialization", version);
095        final String fcrepoIndexingSolr = getBundleUri("fcrepo-indexing-solr", version);
096        final String fcrepoIndexingTriplestore = getBundleUri("fcrepo-indexing-triplestore", version);
097        final String fcrepoServiceAmq = getBundleUri("fcrepo-service-activemq", version);
098        final String fcrepoService = getBundleUri("fcrepo-service-camel", version);
099
100        final String fcrepoReindexingBlueprint = getBlueprintUri("fcrepo-reindexing-blueprint", version);
101
102        return new Option[] {
103            karafDistributionConfiguration()
104                .frameworkUrl(maven().groupId("org.apache.karaf").artifactId("apache-karaf")
105                        .versionAsInProject().type("zip"))
106                .unpackDirectory(new File("target", "exam"))
107                .useDeployFolder(false),
108            logLevel(LogLevel.WARN),
109            keepRuntimeFolder(),
110            configureConsole().ignoreLocalConsole(),
111            features(maven().groupId("org.apache.karaf.features").artifactId("standard")
112                        .versionAsInProject().classifier("features").type("xml"), "scr"),
113            features(maven().groupId("org.apache.camel.karaf").artifactId("apache-camel")
114                        .type("xml").classifier("features").versionAsInProject(), "camel-mustache",
115                        "camel-blueprint", "camel-http4", "camel-spring", "camel-exec", "camel-jetty9",
116                        "camel-jacksonxml"),
117            features(maven().groupId("org.apache.activemq").artifactId("activemq-karaf")
118                        .type("xml").classifier("features").versionAsInProject(), "activemq-camel"),
119            features(maven().groupId("org.fcrepo.camel").artifactId("fcrepo-camel")
120                        .type("xml").classifier("features").versionAsInProject(), "fcrepo-camel"),
121            mavenBundle().groupId("org.codehaus.woodstox").artifactId("woodstox-core-asl").versionAsInProject(),
122
123            CoreOptions.systemProperty("o.f.c.serialization-bundle").value(fcrepoSerialization),
124            CoreOptions.systemProperty("o.f.c.fixity-bundle").value(fcrepoFixity),
125            CoreOptions.systemProperty("o.f.c.reindexing-bundle").value(fcrepoReindexing),
126            CoreOptions.systemProperty("o.f.c.a.triplestore-bundle").value(fcrepoAudit),
127            CoreOptions.systemProperty("o.f.c.i.triplestore-bundle").value(fcrepoIndexingTriplestore),
128            CoreOptions.systemProperty("o.f.c.i.solr-bundle").value(fcrepoIndexingSolr),
129            CoreOptions.systemProperty("o.f.c.s.activemq-bundle").value(fcrepoServiceAmq),
130            CoreOptions.systemProperty("o.f.c.s.camel-bundle").value(fcrepoService),
131
132            bundle(fcrepoAudit).start(),
133            bundle(fcrepoIndexingSolr).start(),
134            bundle(fcrepoIndexingTriplestore).start(),
135            bundle(fcrepoFixity).start(),
136            bundle(fcrepoSerialization).start(),
137            bundle(fcrepoReindexing).start(),
138            bundle(fcrepoServiceAmq).start(),
139            bundle(fcrepoService).start(),
140            bundle(fcrepoReindexingBlueprint).start(),
141
142            CoreOptions.systemProperty("fcrepo.port").value(fcrepoPort),
143            CoreOptions.systemProperty("karaf.reindexing.port").value(reindexingPort),
144            editConfigurationFilePut("etc/org.apache.karaf.management.cfg", "rmiRegistryPort", rmiRegistryPort),
145            editConfigurationFilePut("etc/org.apache.karaf.management.cfg", "rmiServerPort", rmiServerPort),
146            editConfigurationFilePut("etc/org.apache.karaf.shell.cfg", "sshPort", sshPort),
147            editConfigurationFilePut("etc/org.fcrepo.camel.serialization.cfg", "serialization.descriptions",
148                    "data/tmp/descriptions"),
149            editConfigurationFilePut("etc/org.fcrepo.camel.reindexing.cfg", "rest.port", reindexingPort),
150            editConfigurationFilePut("etc/org.fcrepo.camel.service.cfg", "fcrepo.baseUrl", fcrepoBaseUrl),
151            editConfigurationFilePut("etc/org.fcrepo.camel.service.activemq.cfg", "jms.brokerUrl",
152                    "tcp://localhost:" + jmsPort),
153            editConfigurationFilePut("etc/org.ops4j.pax.logging.cfg",
154                    "log4j.logger.org.apache.camel.impl.converter", "ERROR, stdout")
155       };
156    }
157
158
159    @Test
160    public void testInstallation() throws Exception {
161
162        assertTrue(featuresService.isInstalled(featuresService.getFeature("camel-core")));
163        assertTrue(featuresService.isInstalled(featuresService.getFeature("fcrepo-camel")));
164        assertTrue(featuresService.isInstalled(featuresService.getFeature("activemq-camel")));
165        assertTrue(featuresService.isInstalled(featuresService.getFeature("camel-blueprint")));
166        assertTrue(featuresService.isInstalled(featuresService.getFeature("camel-http4")));
167        assertTrue(featuresService.isInstalled(featuresService.getFeature("camel-jetty9")));
168        assertNotNull(bundleContext);
169
170        assertEquals(ACTIVE, bundleContext.getBundle(System.getProperty("o.f.c.serialization-bundle")).getState());
171        assertEquals(ACTIVE, bundleContext.getBundle(System.getProperty("o.f.c.fixity-bundle")).getState());
172        assertEquals(ACTIVE, bundleContext.getBundle(System.getProperty("o.f.c.reindexing-bundle")).getState());
173        assertEquals(ACTIVE, bundleContext.getBundle(System.getProperty("o.f.c.i.solr-bundle")).getState());
174        assertEquals(ACTIVE, bundleContext.getBundle(System.getProperty("o.f.c.i.triplestore-bundle")).getState());
175        assertEquals(ACTIVE, bundleContext.getBundle(System.getProperty("o.f.c.s.activemq-bundle")).getState());
176        assertEquals(ACTIVE, bundleContext.getBundle(System.getProperty("o.f.c.s.camel-bundle")).getState());
177    }
178
179    @Test
180    public void testReindexingService() throws Exception {
181        final CamelContext ctx = getOsgiService(CamelContext.class, "(camel.context.name=FcrepoIndexer)", 10000);
182        assertNotNull(ctx);
183
184        final CloseableHttpClient client = createDefault();
185        final String reindexingUrl = "http://localhost:" + System.getProperty("karaf.reindexing.port") + "/reindexing/";
186        try (final CloseableHttpResponse response = client.execute(new HttpGet(reindexingUrl))) {
187            assertEquals(SC_OK, response.getStatusLine().getStatusCode());
188        }
189    }
190
191    private <T> T getOsgiService(final Class<T> type, final String filter, final long timeout) {
192        try {
193            final ServiceTracker tracker = new ServiceTracker(bundleContext,
194                    createFilter("(&(" + OBJECTCLASS + "=" + type.getName() + ")" + filter + ")"), null);
195            tracker.open(true);
196            final Object svc = type.cast(tracker.waitForService(timeout));
197            if (svc == null) {
198                throw new RuntimeException("Gave up waiting for service " + filter);
199            }
200            return type.cast(svc);
201        } catch (InvalidSyntaxException e) {
202            throw new IllegalArgumentException("Invalid filter", e);
203        } catch (InterruptedException e) {
204            throw new RuntimeException(e);
205        }
206    }
207
208    private static String getBlueprintUri(final String artifactId, final String version) {
209        final File artifact = new File(getBaseDir() + "/../blueprint/" + artifactId + "/target/" +
210                artifactId + "-" + version + ".jar");
211        if (artifact.exists()) {
212            return artifact.toURI().toString();
213        }
214        return "mvn:org.fcrepo.camel/" + artifactId + "/" + version;
215    }
216
217    private static String getBundleUri(final String artifactId, final String version) {
218        final File artifact = new File(getBaseDir() + "/../" + artifactId + "/target/" +
219                artifactId + "-" + version + ".jar");
220        if (artifact.exists()) {
221            return artifact.toURI().toString();
222        }
223        return "mvn:org.fcrepo.camel/" + artifactId + "/" + version;
224    }
225
226}