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.fixity.integration;
017
018import static org.junit.Assert.assertTrue;
019
020import java.net.URI;
021import java.util.Dictionary;
022import java.util.HashMap;
023import java.util.Map;
024import java.util.Properties;
025
026import org.apache.activemq.camel.component.ActiveMQComponent;
027import org.apache.camel.EndpointInject;
028import org.apache.camel.Produce;
029import org.apache.camel.ProducerTemplate;
030import org.apache.camel.builder.AdviceWithRouteBuilder;
031import org.apache.camel.builder.xml.Namespaces;
032import org.apache.camel.component.mock.MockEndpoint;
033import org.apache.camel.test.blueprint.CamelBlueprintTestSupport;
034import org.apache.camel.util.KeyValueHolder;
035import org.apache.camel.util.ObjectHelper;
036import org.apache.commons.codec.digest.DigestUtils;
037import org.fcrepo.camel.FcrepoComponent;
038import org.fcrepo.camel.FcrepoHeaders;
039import org.fcrepo.camel.RdfNamespaces;
040import org.fcrepo.client.FcrepoClient;
041import org.fcrepo.client.FcrepoResponse;
042import org.junit.Test;
043
044/**
045 * Test the route workflow.
046 *
047 * @author Aaron Coburn
048 * @since 2015-06-18
049 */
050public class RouteIT extends CamelBlueprintTestSupport {
051
052    @EndpointInject(uri = "mock:result")
053    protected MockEndpoint resultEndpoint;
054
055    @Produce(uri = "direct:start")
056    protected ProducerTemplate template;
057
058    private String fullPath = "";
059    private String digest = "";
060    private final String binary = "binary.txt";
061
062    @Override
063    protected void doPreSetup() throws Exception {
064        final String webPort = System.getProperty("fcrepo.dynamic.test.port", "8080");
065        final FcrepoClient client = new FcrepoClient(null, null, null, true);
066        final FcrepoResponse res = client.post(
067                URI.create("http://localhost:" + webPort + "/fcrepo/rest"),
068                ObjectHelper.loadResourceAsStream(binary), "text/plain");
069        fullPath = res.getLocation().toString();
070
071        digest = DigestUtils.sha1Hex(ObjectHelper.loadResourceAsStream(binary));
072    }
073
074    @Override
075    public boolean isUseAdviceWith() {
076        return true;
077    }
078
079    @Override
080    public boolean isUseRouteBuilder() {
081        return false;
082    }
083
084    @Override
085    protected String getBlueprintDescriptor() {
086        return "/OSGI-INF/blueprint/blueprint-test.xml";
087    }
088
089    @Override
090    protected Properties useOverridePropertiesWithPropertiesComponent() {
091        final Properties props = new Properties();
092        props.put("fixity.stream", "direct:start");
093        props.put("fixity.failure", "mock:failure");
094        props.put("fixity.success", "mock:success");
095        return props;
096    }
097
098    @Override
099    protected void addServicesOnStartup(final Map<String, KeyValueHolder<Object, Dictionary>> services) {
100        final String jmsPort = System.getProperty("fcrepo.dynamic.jms.port", "61616");
101        final String webPort = System.getProperty("fcrepo.dynamic.test.port", "8080");
102        final ActiveMQComponent component = new ActiveMQComponent();
103
104        component.setBrokerURL("tcp://localhost:" + jmsPort);
105        component.setExposeAllQueues(true);
106
107        final FcrepoComponent fcrepo = new FcrepoComponent();
108        fcrepo.setBaseUrl("http://localhost:" + webPort + "/fcrepo/rest");
109
110        services.put("broker", asService(component, "osgi.jndi.service.name", "fcrepo/Broker"));
111        services.put("fcrepo", asService(fcrepo, "osgi.jndi.service.name", "fcrepo/Camel"));
112    }
113
114    @Test
115    public void testFixityOnBinary() throws Exception {
116        final String webPort = System.getProperty("fcrepo.dynamic.test.port", "8080");
117        final String jmsPort = System.getProperty("fcrepo.dynamic.jms.port", "61616");
118        final String path = fullPath.replaceFirst("http://localhost:[0-9]+/fcrepo/rest", "");
119        final String fcrepoEndpoint = "mock:fcrepo:http://localhost:8080/fcrepo/rest";
120        final Namespaces ns = new Namespaces("rdf", RdfNamespaces.RDF);
121        ns.add("fedora", RdfNamespaces.REPOSITORY);
122        ns.add("premis", RdfNamespaces.PREMIS);
123
124        context.getRouteDefinition("FcrepoFixity").adviceWith(context, new AdviceWithRouteBuilder() {
125            @Override
126            public void configure() throws Exception {
127                mockEndpoints("*");
128            }
129        });
130        context.start();
131
132        final Map<String, Object> headers = new HashMap<>();
133        headers.put(FcrepoHeaders.FCREPO_IDENTIFIER, path);
134        headers.put(FcrepoHeaders.FCREPO_BASE_URL, "http://localhost:" + webPort + "/fcrepo/rest");
135
136        getMockEndpoint(fcrepoEndpoint).expectedMessageCount(2);
137        getMockEndpoint("mock:success").expectedMessageCount(1);
138
139        template.sendBodyAndHeaders("direct:start", "", headers);
140
141        assertMockEndpointsSatisfied();
142
143        final String body = getMockEndpoint("mock:success").assertExchangeReceived(0).getIn().getBody(String.class);
144        assertTrue(body.contains(
145                "<premis:hasSize rdf:datatype=\"http://www.w3.org/2001/XMLSchema#long\">74</premis:hasSize>"));
146        assertTrue(body.contains(
147                "<premis:hasMessageDigest rdf:resource=\"urn:sha1:" + digest + "\"/>"));
148    }
149}