001/* 002 * Copyright 2016 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 java.net.URI.create; 019import static org.apache.camel.component.mock.MockEndpoint.assertIsSatisfied; 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 java.net.URI; 042import javax.inject.Inject; 043 044import org.apache.camel.CamelContext; 045import org.apache.camel.component.mock.MockEndpoint; 046import org.apache.http.client.methods.CloseableHttpResponse; 047import org.apache.http.client.methods.HttpGet; 048import org.apache.http.client.methods.HttpPost; 049import org.apache.http.entity.StringEntity; 050import org.apache.http.impl.client.CloseableHttpClient; 051import org.apache.karaf.features.FeaturesService; 052import org.fcrepo.client.FcrepoClient; 053import org.junit.Test; 054import org.junit.runner.RunWith; 055import org.ops4j.pax.exam.Configuration; 056import org.ops4j.pax.exam.ConfigurationManager; 057import org.ops4j.pax.exam.CoreOptions; 058import org.ops4j.pax.exam.Option; 059import org.ops4j.pax.exam.junit.PaxExam; 060import org.ops4j.pax.exam.karaf.options.LogLevelOption.LogLevel; 061import org.ops4j.pax.exam.spi.reactors.ExamReactorStrategy; 062import org.ops4j.pax.exam.spi.reactors.PerClass; 063import org.osgi.framework.BundleContext; 064import org.osgi.framework.InvalidSyntaxException; 065import org.osgi.util.tracker.ServiceTracker; 066import org.slf4j.Logger; 067 068/** 069 * @author Aaron Coburn 070 * @since February 8, 2016 071 */ 072@RunWith(PaxExam.class) 073@ExamReactorStrategy(PerClass.class) 074public class KarafIT { 075 076 private static Logger LOGGER = getLogger(KarafIT.class); 077 078 @Inject 079 protected FeaturesService featuresService; 080 081 @Inject 082 protected BundleContext bundleContext; 083 084 @Configuration 085 public Option[] config() { 086 final ConfigurationManager cm = new ConfigurationManager(); 087 final String fcrepoPort = cm.getProperty("fcrepo.dynamic.test.port"); 088 final String jmsPort = cm.getProperty("fcrepo.dynamic.jms.port"); 089 final String reindexingPort = cm.getProperty("fcrepo.dynamic.reindexing.port"); 090 final String rmiRegistryPort = cm.getProperty("karaf.rmiRegistry.port"); 091 final String rmiServerPort = cm.getProperty("karaf.rmiServer.port"); 092 final String sshPort = cm.getProperty("karaf.ssh.port"); 093 final String fcrepoBaseUrl = "localhost:" + fcrepoPort + "/fcrepo/rest"; 094 095 final String version = cm.getProperty("project.version"); 096 final String fcrepoAudit = getBundleUri("fcrepo-audit-triplestore", version); 097 final String fcrepoFixity = getBundleUri("fcrepo-fixity", version); 098 final String fcrepoReindexing = getBundleUri("fcrepo-reindexing", version); 099 final String fcrepoSerialization = getBundleUri("fcrepo-serialization", version); 100 final String fcrepoIndexingSolr = getBundleUri("fcrepo-indexing-solr", version); 101 final String fcrepoIndexingTriplestore = getBundleUri("fcrepo-indexing-triplestore", version); 102 final String fcrepoServiceAmq = getBundleUri("fcrepo-service-activemq", version); 103 final String fcrepoService = getBundleUri("fcrepo-service-camel", version); 104 105 return new Option[] { 106 karafDistributionConfiguration() 107 .frameworkUrl(maven().groupId("org.apache.karaf").artifactId("apache-karaf") 108 .versionAsInProject().type("zip")) 109 .unpackDirectory(new File("target", "exam")) 110 .useDeployFolder(false), 111 logLevel(LogLevel.WARN), 112 keepRuntimeFolder(), 113 configureConsole().ignoreLocalConsole(), 114 features(maven().groupId("org.apache.karaf.features").artifactId("standard") 115 .versionAsInProject().classifier("features").type("xml"), "scr"), 116 features(maven().groupId("org.apache.camel.karaf").artifactId("apache-camel") 117 .type("xml").classifier("features").versionAsInProject(), "camel-mustache", 118 "camel-blueprint", "camel-http4", "camel-spring", "camel-exec", "camel-jetty9", 119 "camel-jacksonxml"), 120 features(maven().groupId("org.apache.activemq").artifactId("activemq-karaf") 121 .type("xml").classifier("features").versionAsInProject(), "activemq-camel"), 122 features(maven().groupId("org.fcrepo.camel").artifactId("fcrepo-camel") 123 .type("xml").classifier("features").versionAsInProject(), "fcrepo-camel"), 124 mavenBundle().groupId("org.codehaus.woodstox").artifactId("woodstox-core-asl").versionAsInProject(), 125 126 CoreOptions.systemProperty("o.f.c.serialization-bundle").value(fcrepoSerialization), 127 CoreOptions.systemProperty("o.f.c.fixity-bundle").value(fcrepoFixity), 128 CoreOptions.systemProperty("o.f.c.reindexing-bundle").value(fcrepoReindexing), 129 CoreOptions.systemProperty("o.f.c.a.triplestore-bundle").value(fcrepoAudit), 130 CoreOptions.systemProperty("o.f.c.i.triplestore-bundle").value(fcrepoIndexingTriplestore), 131 CoreOptions.systemProperty("o.f.c.i.solr-bundle").value(fcrepoIndexingSolr), 132 CoreOptions.systemProperty("o.f.c.s.activemq-bundle").value(fcrepoServiceAmq), 133 CoreOptions.systemProperty("o.f.c.s.camel-bundle").value(fcrepoService), 134 135 bundle(fcrepoAudit).start(), 136 bundle(fcrepoIndexingSolr).start(), 137 bundle(fcrepoIndexingTriplestore).start(), 138 bundle(fcrepoFixity).start(), 139 bundle(fcrepoSerialization).start(), 140 bundle(fcrepoReindexing).start(), 141 bundle(fcrepoServiceAmq).start(), 142 bundle(fcrepoService).start(), 143 144 CoreOptions.systemProperty("fcrepo.port").value(fcrepoPort), 145 CoreOptions.systemProperty("karaf.reindexing.port").value(reindexingPort), 146 editConfigurationFilePut("etc/org.apache.karaf.management.cfg", "rmiRegistryPort", rmiRegistryPort), 147 editConfigurationFilePut("etc/org.apache.karaf.management.cfg", "rmiServerPort", rmiServerPort), 148 editConfigurationFilePut("etc/org.apache.karaf.shell.cfg", "sshPort", sshPort), 149 editConfigurationFilePut("etc/org.fcrepo.camel.serialization.cfg", "serialization.descriptions", 150 "data/tmp/descriptions"), 151 editConfigurationFilePut("etc/org.fcrepo.camel.reindexing.cfg", "rest.port", reindexingPort), 152 editConfigurationFilePut("etc/org.fcrepo.camel.service.cfg", "fcrepo.baseUrl", fcrepoBaseUrl), 153 editConfigurationFilePut("etc/org.fcrepo.camel.service.activemq.cfg", "jms.brokerUrl", 154 "tcp://localhost:" + jmsPort) 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 // We aren't running solr or a triplestore, so stop these to prevent 185 // unnecessary errors. 186 bundleContext.getBundle(System.getProperty("o.f.c.i.solr-bundle")).stop(); 187 bundleContext.getBundle(System.getProperty("o.f.c.i.triplestore-bundle")).stop(); 188 bundleContext.getBundle(System.getProperty("o.f.c.a.triplestore-bundle")).stop(); 189 190 final FcrepoClient fcrepoClient = new FcrepoClient(null, null, null, true); 191 final URI baseUrl = create("http://localhost:" + System.getProperty("fcrepo.port") + "/fcrepo/rest"); 192 final URI url1 = fcrepoClient.post(baseUrl, null, null).getLocation(); 193 final URI url2 = fcrepoClient.post(baseUrl, null, null).getLocation(); 194 fcrepoClient.post(url1, null, null); 195 fcrepoClient.post(url2, null, null); 196 197 final MockEndpoint resultEndpoint = (MockEndpoint) ctx.getEndpoint("mock:results"); 198 resultEndpoint.expectedMessageCount(5); 199 200 final CloseableHttpClient client = createDefault(); 201 final String reindexingUrl = "http://localhost:" + System.getProperty("karaf.reindexing.port") + "/reindexing/"; 202 try (final CloseableHttpResponse response = client.execute(new HttpGet(reindexingUrl))) { 203 assertEquals(SC_OK, response.getStatusLine().getStatusCode()); 204 } 205 206 final HttpPost post = new HttpPost(reindexingUrl); 207 post.addHeader("Content-Type", "application/json"); 208 post.setEntity(new StringEntity("[\"mock:results\"]")); 209 try (final CloseableHttpResponse response = client.execute(post)) { 210 assertEquals(SC_OK, response.getStatusLine().getStatusCode()); 211 } 212 213 assertIsSatisfied(resultEndpoint); 214 } 215 216 private <T> T getOsgiService(final Class<T> type, final String filter, final long timeout) { 217 try { 218 final ServiceTracker tracker = new ServiceTracker(bundleContext, 219 createFilter("(&(" + OBJECTCLASS + "=" + type.getName() + ")" + filter + ")"), null); 220 tracker.open(true); 221 final Object svc = type.cast(tracker.waitForService(timeout)); 222 if (svc == null) { 223 throw new RuntimeException("Gave up waiting for service " + filter); 224 } 225 return type.cast(svc); 226 } catch (InvalidSyntaxException e) { 227 throw new IllegalArgumentException("Invalid filter", e); 228 } catch (InterruptedException e) { 229 throw new RuntimeException(e); 230 } 231 } 232 233 private static String getBundleUri(final String artifactId, final String version) { 234 final File artifact = new File(getBaseDir() + "/../" + artifactId + "/target/" + 235 artifactId + "-" + version + ".jar"); 236 if (artifact.exists()) { 237 return artifact.toURI().toString(); 238 } 239 return "mvn:org.fcrepo.camel/" + artifactId + "/" + version; 240 } 241 242}