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