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.reindexing.integration;
019
020import static org.apache.camel.Exchange.HTTP_URI;
021import static org.fcrepo.camel.FcrepoHeaders.FCREPO_URI;
022import static org.fcrepo.camel.reindexing.ReindexingHeaders.REINDEXING_RECIPIENTS;
023import static org.fcrepo.camel.reindexing.ReindexingHeaders.REINDEXING_PREFIX;
024import static org.slf4j.LoggerFactory.getLogger;
025
026import java.io.IOException;
027import java.util.Dictionary;
028import java.util.Map;
029import java.util.Properties;
030
031import org.apache.activemq.camel.component.ActiveMQComponent;
032import org.apache.camel.EndpointInject;
033import org.apache.camel.Exchange;
034import org.apache.camel.Processor;
035import org.apache.camel.Produce;
036import org.apache.camel.ProducerTemplate;
037import org.apache.camel.component.mock.MockEndpoint;
038import org.apache.camel.test.blueprint.CamelBlueprintTestSupport;
039import org.apache.camel.util.KeyValueHolder;
040import org.apache.http.HttpResponse;
041import org.apache.http.client.methods.HttpPost;
042import org.apache.http.impl.client.CloseableHttpClient;
043import org.apache.http.impl.client.HttpClients;
044import org.apache.http.util.EntityUtils;
045import org.fcrepo.camel.FcrepoComponent;
046
047import org.junit.Test;
048import org.slf4j.Logger;
049
050/**
051 * Test the route workflow.
052 *
053 * @author Aaron Coburn
054 * @since 2015-04-10
055 */
056public class RouteIT extends CamelBlueprintTestSupport {
057
058    private static final Logger LOGGER = getLogger(RouteIT.class);
059
060    @EndpointInject(uri = "mock:result")
061    protected MockEndpoint resultEndpoint;
062
063    @Produce(uri = "direct:reindex")
064    protected ProducerTemplate template;
065
066    private String fullPath = "";
067
068    @Override
069    protected String getBlueprintDescriptor() {
070        return "/OSGI-INF/blueprint/blueprint-test.xml";
071    }
072
073    @Override
074    public void doPreSetup() throws Exception {
075        super.doPreSetup();
076        final String webPort = System.getProperty("fcrepo.dynamic.test.port", "8080");
077
078        final String basePath = "http://localhost:" + webPort + "/fcrepo/rest";
079        final String subPath = post(basePath);
080        final String jmsPort = System.getProperty("fcrepo.dynamic.jms.port", "61616");
081
082        for (int i = 0; i < 10; ++i) {
083            post(basePath);
084            post(subPath);
085        }
086    }
087    @Override
088    protected void addServicesOnStartup(final Map<String, KeyValueHolder<Object, Dictionary>> services) {
089        final String jmsPort = System.getProperty("fcrepo.dynamic.jms.port", "61616");
090        final String webPort = System.getProperty("fcrepo.dynamic.test.port", "8080");
091        final ActiveMQComponent amq = new ActiveMQComponent();
092
093        amq.setBrokerURL("tcp://localhost:" + jmsPort);
094        amq.setExposeAllQueues(true);
095
096        final FcrepoComponent fcrepo = new FcrepoComponent();
097        fcrepo.setBaseUrl("http://localhost:" + webPort + "/fcrepo/rest");
098
099        services.put("broker", asService(amq, "osgi.jndi.service.name", "fcrepo/Broker"));
100        services.put("fcrepo", asService(fcrepo, "osgi.jndi.service.name", "fcrepo/Camel"));
101    }
102
103    @Override
104    public boolean isUseRouteBuilder() {
105        return false;
106    }
107
108    @Override
109    protected Properties useOverridePropertiesWithPropertiesComponent() {
110        final String restPort = System.getProperty("fcrepo.dynamic.reindexing.port", "9080");
111
112        final Properties props = new Properties();
113        props.put("reindexing.stream", "broker:queue:reindexing");
114        props.put("rest.prefix", "/reindexing");
115        props.put("rest.port", restPort);
116
117        return props;
118    }
119
120    @Test
121    public void testReindexingRouter() throws Exception {
122        final String webPort = System.getProperty("fcrepo.dynamic.test.port", "8080");
123
124        getMockEndpoint("mock:result").expectedMinimumMessageCount(21);
125
126        template.send("direct:reindex", new Processor() {
127            public void process(final Exchange exchange) throws Exception {
128                exchange.getIn().setHeader(REINDEXING_RECIPIENTS, "mock:result");
129                exchange.getIn().setHeader(FCREPO_URI, "http://localhost:" + webPort + "/fcrepo/rest/");
130                exchange.getIn().setHeader(HTTP_URI, "http://localhost:9080/reindexing/");
131                exchange.getIn().setHeader(REINDEXING_PREFIX, "/reindexing");
132            }
133        });
134
135        assertMockEndpointsSatisfied();
136    }
137
138    private String post(final String url) {
139        final CloseableHttpClient httpclient = HttpClients.createDefault();
140        try {
141            final HttpPost httppost = new HttpPost(url);
142            final HttpResponse response = httpclient.execute(httppost);
143            return EntityUtils.toString(response.getEntity(), "UTF-8");
144        } catch (IOException ex) {
145            LOGGER.debug("Unable to extract HttpEntity response into an InputStream: ", ex);
146            return "";
147        }
148    }
149}