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