001/*
002 * The contents of this file are subject to the license and copyright
003 * detailed in the LICENSE and NOTICE files at the root of the source
004 * tree.
005 */
006package org.fcrepo.camel.reindexing.integration;
007
008import org.apache.camel.EndpointInject;
009import org.apache.camel.Exchange;
010import org.apache.camel.Processor;
011import org.apache.camel.Produce;
012import org.apache.camel.ProducerTemplate;
013import org.apache.camel.builder.RouteBuilder;
014import org.apache.camel.component.activemq.ActiveMQComponent;
015import org.apache.camel.component.mock.MockEndpoint;
016import org.apache.camel.spring.javaconfig.CamelConfiguration;
017import org.apache.http.HttpResponse;
018import org.apache.http.auth.AuthScope;
019import org.apache.http.auth.UsernamePasswordCredentials;
020import org.apache.http.client.methods.HttpPost;
021import org.apache.http.impl.client.BasicCredentialsProvider;
022import org.apache.http.impl.client.CloseableHttpClient;
023import org.apache.http.impl.client.HttpClients;
024import org.apache.http.util.EntityUtils;
025import org.fcrepo.camel.reindexing.ReindexingRouter;
026import org.junit.Before;
027import org.junit.BeforeClass;
028import org.junit.Test;
029import org.junit.runner.RunWith;
030import org.slf4j.Logger;
031import org.springframework.beans.factory.annotation.Autowired;
032import org.springframework.context.annotation.Bean;
033import org.springframework.context.annotation.ComponentScan;
034import org.springframework.context.annotation.Configuration;
035import org.springframework.test.annotation.DirtiesContext;
036import org.springframework.test.context.ContextConfiguration;
037import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
038import org.springframework.test.context.support.AnnotationConfigContextLoader;
039
040import java.io.IOException;
041
042import static org.apache.camel.Exchange.HTTP_URI;
043import static org.fcrepo.camel.FcrepoHeaders.FCREPO_URI;
044import static org.fcrepo.camel.reindexing.ReindexingHeaders.REINDEXING_PREFIX;
045import static org.fcrepo.camel.reindexing.ReindexingHeaders.REINDEXING_RECIPIENTS;
046import static org.slf4j.LoggerFactory.getLogger;
047
048/**
049 * Test the route workflow.
050 *
051 * @author Aaron Coburn
052 * @since 2015-04-10
053 */
054
055@RunWith(SpringJUnit4ClassRunner.class)
056@ContextConfiguration(classes = {RouteIT.ContextConfig.class}, loader = AnnotationConfigContextLoader.class)
057public class RouteIT {
058
059    private static final Logger LOGGER = getLogger(RouteIT.class);
060
061    private static final String FEDORA_AUTH_USERNAME = "fedoraAdmin";
062    private static final String FEDORA_AUTH_PASSWORD = "fedoraAdmin";
063
064    @EndpointInject("mock:result")
065    protected MockEndpoint resultEndpoint;
066
067    @Produce("direct:reindex")
068    protected ProducerTemplate template;
069
070    @Autowired
071    private ActiveMQComponent activeMQComponent;
072
073    private final String fullPath = "";
074
075    private static final BasicCredentialsProvider provider = new BasicCredentialsProvider();
076
077    public RouteIT() {
078        provider.setCredentials(AuthScope.ANY,
079                new UsernamePasswordCredentials(FEDORA_AUTH_USERNAME, FEDORA_AUTH_PASSWORD));
080    }
081
082    @BeforeClass
083    public static void beforeClass() {
084
085        final String jmsPort = System.getProperty("fcrepo.dynamic.jms.port", "61616");
086        final String webPort = System.getProperty("fcrepo.dynamic.test.port", "8080");
087
088        System.setProperty("fcrepo.baseUrl", "http://localhost:" + webPort + "/fcrepo/rest");
089        System.setProperty("fcrepo.authUsername", FEDORA_AUTH_USERNAME);
090        System.setProperty("fcrepo.authPassword", FEDORA_AUTH_PASSWORD);
091        System.setProperty("jms.brokerUrl", "tcp://localhost:" + jmsPort);
092    }
093
094    @Before
095    public void setup() {
096        final String webPort = System.getProperty("fcrepo.dynamic.test.port", "8080");
097        final String basePath = "http://localhost:" + webPort + "/fcrepo/rest";
098        final String subPath = post(basePath);
099
100        for (int i = 0; i < 10; ++i) {
101            post(basePath);
102            post(subPath);
103        }
104    }
105
106    @DirtiesContext
107    @Test
108    public void testReindexingRouter() throws Exception {
109        final String webPort = System.getProperty("fcrepo.dynamic.test.port", "8080");
110
111        resultEndpoint.expectedMinimumMessageCount(21);
112
113        template.send("direct:reindex", new Processor() {
114            @Override
115            public void process(final Exchange exchange) throws Exception {
116                exchange.getIn().setHeader(REINDEXING_RECIPIENTS, "mock:result");
117                exchange.getIn().setHeader(FCREPO_URI, "http://localhost:" + webPort + "/fcrepo/rest/");
118                exchange.getIn().setHeader(HTTP_URI, "http://localhost:" +  webPort + "/reindexing/");
119                exchange.getIn().setHeader(REINDEXING_PREFIX, "/reindexing");
120            }
121        });
122
123        MockEndpoint.assertIsSatisfied(resultEndpoint);
124    }
125
126    private String post(final String url) {
127        final CloseableHttpClient httpclient = HttpClients.custom().setDefaultCredentialsProvider(provider).build();
128
129        try {
130            final HttpPost httppost = new HttpPost(url);
131
132            final HttpResponse response = httpclient.execute(httppost);
133            return EntityUtils.toString(response.getEntity(), "UTF-8");
134        } catch (final IOException ex) {
135            LOGGER.debug("Unable to extract HttpEntity response into an InputStream: ", ex);
136            return "";
137        }
138    }
139
140    @Configuration
141    @ComponentScan(basePackages = {"org.fcrepo.camel"})
142    static class ContextConfig extends CamelConfiguration {
143
144        @Bean
145        public RouteBuilder route() {
146            return new ReindexingRouter();
147        }
148    }
149}