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