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.service; 019 020import static org.apache.camel.component.mock.MockEndpoint.assertIsSatisfied; 021import static org.apache.http.HttpStatus.SC_CREATED; 022import static org.apache.http.impl.client.HttpClients.createDefault; 023import static org.fcrepo.camel.FcrepoHeaders.FCREPO_BASE_URL; 024import static org.fcrepo.camel.FcrepoHeaders.FCREPO_IDENTIFIER; 025import static org.fcrepo.camel.FcrepoHeaders.FCREPO_URI; 026import static org.junit.Assert.assertEquals; 027import static org.junit.Assert.assertNotNull; 028import static org.junit.Assert.assertTrue; 029import static org.ops4j.pax.exam.CoreOptions.bundle; 030import static org.ops4j.pax.exam.CoreOptions.maven; 031import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.configureConsole; 032import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.editConfigurationFilePut; 033import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.features; 034import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.karafDistributionConfiguration; 035import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.keepRuntimeFolder; 036import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.logLevel; 037import static org.ops4j.pax.exam.util.PathUtils.getBaseDir; 038import static org.osgi.framework.Bundle.ACTIVE; 039import static org.osgi.framework.Constants.OBJECTCLASS; 040import static org.osgi.framework.FrameworkUtil.createFilter; 041import static org.slf4j.LoggerFactory.getLogger; 042 043import java.io.File; 044import java.io.IOException; 045import java.util.HashMap; 046import java.util.Map; 047import javax.inject.Inject; 048 049import org.apache.camel.CamelContext; 050import org.apache.camel.ProducerTemplate; 051import org.apache.camel.component.mock.MockEndpoint; 052import org.apache.http.HttpResponse; 053import org.apache.http.client.methods.HttpPost; 054import org.apache.http.impl.client.CloseableHttpClient; 055import org.apache.http.util.EntityUtils; 056import org.apache.karaf.features.FeaturesService; 057import org.junit.Test; 058import org.junit.runner.RunWith; 059import org.ops4j.pax.exam.Configuration; 060import org.ops4j.pax.exam.ConfigurationManager; 061import org.ops4j.pax.exam.CoreOptions; 062import org.ops4j.pax.exam.Option; 063import org.ops4j.pax.exam.junit.PaxExam; 064import org.ops4j.pax.exam.karaf.options.LogLevelOption.LogLevel; 065import org.ops4j.pax.exam.spi.reactors.ExamReactorStrategy; 066import org.ops4j.pax.exam.spi.reactors.PerClass; 067import org.osgi.framework.BundleContext; 068import org.osgi.framework.InvalidSyntaxException; 069import org.osgi.util.tracker.ServiceTracker; 070import org.slf4j.Logger; 071 072/** 073 * @author Aaron Coburn 074 * @since July 21, 2016 075 */ 076@RunWith(PaxExam.class) 077@ExamReactorStrategy(PerClass.class) 078public class KarafIT { 079 080 private final CloseableHttpClient httpclient = createDefault(); 081 private static Logger LOGGER = getLogger(KarafIT.class); 082 083 @Inject 084 protected FeaturesService featuresService; 085 086 @Inject 087 protected BundleContext bundleContext; 088 089 @Configuration 090 public Option[] config() throws Exception { 091 final ConfigurationManager cm = new ConfigurationManager(); 092 final String artifactName = cm.getProperty("project.artifactId") + "-" + cm.getProperty("project.version"); 093 final String fcrepoServiceBundle = "file:" + getBaseDir() + "/target/" + artifactName + ".jar"; 094 final String fcrepoPort = cm.getProperty("fcrepo.dynamic.test.port"); 095 final String jmsPort = cm.getProperty("fcrepo.dynamic.jms.port"); 096 final String rmiRegistryPort = cm.getProperty("karaf.rmiRegistry.port"); 097 final String rmiServerPort = cm.getProperty("karaf.rmiServer.port"); 098 final String sshPort = cm.getProperty("karaf.ssh.port"); 099 return new Option[] { 100 karafDistributionConfiguration() 101 .frameworkUrl(maven().groupId("org.apache.karaf").artifactId("apache-karaf") 102 .versionAsInProject().type("zip")) 103 .unpackDirectory(new File("target", "exam")) 104 .useDeployFolder(false), 105 logLevel(LogLevel.WARN), 106 keepRuntimeFolder(), 107 configureConsole().ignoreLocalConsole(), 108 features(maven().groupId("org.apache.karaf.features").artifactId("standard") 109 .type("xml").classifier("features").versionAsInProject(), "scr"), 110 features(maven().groupId("org.apache.camel.karaf").artifactId("apache-camel") 111 .type("xml").classifier("features").versionAsInProject(), "camel", 112 "camel-blueprint", "camel-jackson"), 113 features(maven().groupId("org.fcrepo.camel").artifactId("fcrepo-camel") 114 .type("xml").classifier("features").versionAsInProject(), "fcrepo-camel"), 115 116 CoreOptions.systemProperty("fcrepo.port").value(fcrepoPort), 117 CoreOptions.systemProperty("jms.port").value(jmsPort), 118 CoreOptions.systemProperty("fcrepo.service.bundle").value(fcrepoServiceBundle), 119 120 editConfigurationFilePut("etc/org.fcrepo.camel.service.cfg", "fcrepo.baseUrl", 121 "http://localhost:" + fcrepoPort + "/fcrepo/rest"), 122 123 bundle(fcrepoServiceBundle).start(), 124 125 editConfigurationFilePut("etc/org.apache.karaf.management.cfg", "rmiRegistryPort", rmiRegistryPort), 126 editConfigurationFilePut("etc/org.apache.karaf.management.cfg", "rmiServerPort", rmiServerPort), 127 editConfigurationFilePut("etc/org.apache.karaf.shell.cfg", "sshPort", sshPort) 128 }; 129 } 130 131 @Test 132 public void testInstallation() throws Exception { 133 assertTrue(featuresService.isInstalled(featuresService.getFeature("camel-core"))); 134 assertTrue(featuresService.isInstalled(featuresService.getFeature("fcrepo-camel"))); 135 assertNotNull(bundleContext); 136 assertEquals(ACTIVE, 137 bundleContext.getBundle(System.getProperty("fcrepo.service.bundle")).getState()); 138 } 139 140 @Test 141 public void testService() throws Exception { 142 final String baseUrl = "http://localhost:" + System.getProperty("fcrepo.port") + "/fcrepo/rest"; 143 final CamelContext ctx = getOsgiService(CamelContext.class, 144 "(camel.context.name=FcrepoService)", 10000); 145 146 assertNotNull(ctx); 147 148 final MockEndpoint resultEndpoint = (MockEndpoint) ctx.getEndpoint("mock:result"); 149 150 final String url1 = post(baseUrl).replace(baseUrl, ""); 151 final String url2 = post(baseUrl).replace(baseUrl, ""); 152 153 final ProducerTemplate template = ctx.createProducerTemplate(); 154 final Map<String, Object> headers = new HashMap<>(); 155 headers.put(FCREPO_BASE_URL, baseUrl); 156 headers.put(FCREPO_IDENTIFIER, url1); 157 template.sendBodyAndHeaders(ctx.getEndpoint("direct:start"), null, headers); 158 159 template.sendBodyAndHeader(ctx.getEndpoint("direct:start"), null, FCREPO_URI, baseUrl + url2); 160 161 resultEndpoint.expectedMinimumMessageCount(2); 162 assertIsSatisfied(resultEndpoint); 163 } 164 165 private String post(final String url) { 166 try { 167 final HttpPost httppost = new HttpPost(url); 168 final HttpResponse response = httpclient.execute(httppost); 169 assertEquals(SC_CREATED, response.getStatusLine().getStatusCode()); 170 return EntityUtils.toString(response.getEntity(), "UTF-8"); 171 } catch (IOException ex) { 172 LOGGER.debug("Unable to extract HttpEntity response into an InputStream: ", ex); 173 return ""; 174 } 175 } 176 177 private <T> T getOsgiService(final Class<T> type, final String filter, final long timeout) { 178 try { 179 final ServiceTracker tracker = new ServiceTracker(bundleContext, 180 createFilter("(&(" + OBJECTCLASS + "=" + type.getName() + ")" + filter + ")"), null); 181 tracker.open(true); 182 final Object svc = type.cast(tracker.waitForService(timeout)); 183 if (svc == null) { 184 throw new RuntimeException("Gave up waiting for service " + filter); 185 } 186 return type.cast(svc); 187 } catch (InvalidSyntaxException e) { 188 throw new IllegalArgumentException("Invalid filter", e); 189 } catch (InterruptedException e) { 190 throw new RuntimeException(e); 191 } 192 } 193}