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.junit.Assert.assertEquals;
026import static org.junit.Assert.assertNotNull;
027import static org.junit.Assert.assertTrue;
028import static org.ops4j.pax.exam.CoreOptions.bundle;
029import static org.ops4j.pax.exam.CoreOptions.maven;
030import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.configureConsole;
031import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.editConfigurationFilePut;
032import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.features;
033import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.karafDistributionConfiguration;
034import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.keepRuntimeFolder;
035import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.logLevel;
036import static org.ops4j.pax.exam.util.PathUtils.getBaseDir;
037import static org.osgi.framework.Bundle.ACTIVE;
038import static org.osgi.framework.Constants.OBJECTCLASS;
039import static org.osgi.framework.FrameworkUtil.createFilter;
040import static org.slf4j.LoggerFactory.getLogger;
041
042import java.io.File;
043import java.io.IOException;
044import java.util.HashMap;
045import java.util.Map;
046import javax.inject.Inject;
047
048import org.apache.camel.CamelContext;
049import org.apache.camel.ProducerTemplate;
050import org.apache.camel.component.mock.MockEndpoint;
051import org.apache.http.HttpResponse;
052import org.apache.http.client.methods.HttpPost;
053import org.apache.http.impl.client.CloseableHttpClient;
054import org.apache.http.util.EntityUtils;
055import org.apache.karaf.features.FeaturesService;
056import org.junit.Test;
057import org.junit.runner.RunWith;
058import org.ops4j.pax.exam.Configuration;
059import org.ops4j.pax.exam.ConfigurationManager;
060import org.ops4j.pax.exam.CoreOptions;
061import org.ops4j.pax.exam.Option;
062import org.ops4j.pax.exam.junit.PaxExam;
063import org.ops4j.pax.exam.karaf.options.LogLevelOption.LogLevel;
064import org.ops4j.pax.exam.spi.reactors.ExamReactorStrategy;
065import org.ops4j.pax.exam.spi.reactors.PerClass;
066import org.osgi.framework.BundleContext;
067import org.osgi.framework.InvalidSyntaxException;
068import org.osgi.util.tracker.ServiceTracker;
069import org.slf4j.Logger;
070
071/**
072 * @author Aaron Coburn
073 * @since July 21, 2016
074 */
075@RunWith(PaxExam.class)
076@ExamReactorStrategy(PerClass.class)
077public class KarafIT {
078
079    private final CloseableHttpClient httpclient = createDefault();
080    private static Logger LOGGER = getLogger(KarafIT.class);
081
082    @Inject
083    protected FeaturesService featuresService;
084
085    @Inject
086    protected BundleContext bundleContext;
087
088    @Configuration
089    public Option[] config() throws Exception {
090        final ConfigurationManager cm = new ConfigurationManager();
091        final String artifactName = cm.getProperty("project.artifactId") + "-" + cm.getProperty("project.version");
092        final String fcrepoServiceBundle = "file:" + getBaseDir() + "/target/" + artifactName + ".jar";
093        final String fcrepoPort = cm.getProperty("fcrepo.dynamic.test.port");
094        final String jmsPort = cm.getProperty("fcrepo.dynamic.jms.port");
095        final String rmiRegistryPort = cm.getProperty("karaf.rmiRegistry.port");
096        final String rmiServerPort = cm.getProperty("karaf.rmiServer.port");
097        final String sshPort = cm.getProperty("karaf.ssh.port");
098        return new Option[] {
099            karafDistributionConfiguration()
100                .frameworkUrl(maven().groupId("org.apache.karaf").artifactId("apache-karaf")
101                        .versionAsInProject().type("zip"))
102                .unpackDirectory(new File("target", "exam"))
103                .useDeployFolder(false),
104            logLevel(LogLevel.WARN),
105            keepRuntimeFolder(),
106            configureConsole().ignoreLocalConsole(),
107            features(maven().groupId("org.apache.karaf.features").artifactId("standard")
108                        .type("xml").classifier("features").versionAsInProject(), "scr"),
109            features(maven().groupId("org.apache.camel.karaf").artifactId("apache-camel")
110                        .type("xml").classifier("features").versionAsInProject(), "camel",
111                        "camel-blueprint"),
112            features(maven().groupId("org.fcrepo.camel").artifactId("fcrepo-camel")
113                        .type("xml").classifier("features").versionAsInProject(), "fcrepo-camel"),
114
115            CoreOptions.systemProperty("fcrepo.port").value(fcrepoPort),
116            CoreOptions.systemProperty("jms.port").value(jmsPort),
117            CoreOptions.systemProperty("fcrepo.service.bundle").value(fcrepoServiceBundle),
118
119            editConfigurationFilePut("etc/org.fcrepo.camel.service.cfg", "fcrepo.baseUrl",
120                    "http://localhost:" + fcrepoPort + "/fcrepo/rest"),
121
122            bundle(fcrepoServiceBundle).start(),
123
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       };
128    }
129
130    @Test
131    public void testInstallation() throws Exception {
132        assertTrue(featuresService.isInstalled(featuresService.getFeature("camel-core")));
133        assertTrue(featuresService.isInstalled(featuresService.getFeature("fcrepo-camel")));
134        assertNotNull(bundleContext);
135        assertEquals(ACTIVE,
136                bundleContext.getBundle(System.getProperty("fcrepo.service.bundle")).getState());
137    }
138
139    @Test
140    public void testService() throws Exception {
141        final String baseUrl = "http://localhost:" + System.getProperty("fcrepo.port") + "/fcrepo/rest";
142        final CamelContext ctx = getOsgiService(CamelContext.class,
143                "(camel.context.name=FcrepoService)", 10000);
144
145        assertNotNull(ctx);
146
147        final MockEndpoint resultEndpoint = (MockEndpoint) ctx.getEndpoint("mock:result");
148
149        final String url1 = post(baseUrl).replace(baseUrl, "");
150        final String url2 = post(baseUrl).replace(baseUrl, "");
151
152        final ProducerTemplate template = ctx.createProducerTemplate();
153        final Map<String, Object> headers = new HashMap<>();
154        headers.put(FCREPO_BASE_URL, baseUrl);
155        headers.put(FCREPO_IDENTIFIER, url1);
156        template.sendBodyAndHeaders(ctx.getEndpoint("direct:start"), null, headers);
157
158        headers.put(FCREPO_IDENTIFIER, url2);
159        template.sendBodyAndHeaders(ctx.getEndpoint("direct:start"), null, headers);
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}