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    private static String FEDORA_USERNAME = "fedoraAdmin";
057    private static String FEDORA_PASSWORD = "fedoraAdmin";
058
059    @EndpointInject(uri = "mock:result")
060    protected MockEndpoint resultEndpoint;
061
062    @Produce(uri = "direct:start")
063    protected ProducerTemplate template;
064
065    private String fullPath = "";
066    private String digest = "";
067    private final String binary = "binary.txt";
068
069    @Override
070    protected void doPreSetup() throws Exception {
071        final String webPort = System.getProperty("fcrepo.dynamic.test.port", "8080");
072        final FcrepoClient client = client().throwExceptionOnFailure()
073                                            .credentials(FEDORA_USERNAME, FEDORA_PASSWORD).build();
074        final FcrepoResponse res = client.post(URI.create("http://localhost:" + webPort + "/fcrepo/rest"))
075                                .body(loadResourceAsStream(binary), "text/plain").perform();
076        fullPath = res.getLocation().toString();
077
078        digest = DigestUtils.sha1Hex(loadResourceAsStream(binary));
079    }
080
081    @Override
082    public boolean isUseAdviceWith() {
083        return true;
084    }
085
086    @Override
087    public boolean isUseRouteBuilder() {
088        return false;
089    }
090
091    @Override
092    protected String getBlueprintDescriptor() {
093        return "/OSGI-INF/blueprint/blueprint-test.xml";
094    }
095
096    @Override
097    protected Properties useOverridePropertiesWithPropertiesComponent() {
098        final Properties props = new Properties();
099        props.put("fixity.stream", "direct:start");
100        props.put("fixity.failure", "mock:failure");
101        props.put("fixity.success", "mock:success");
102        return props;
103    }
104
105    @Override
106    protected void addServicesOnStartup(final Map<String, KeyValueHolder<Object, Dictionary>> services) {
107        final String jmsPort = System.getProperty("fcrepo.dynamic.jms.port", "61616");
108        final String webPort = System.getProperty("fcrepo.dynamic.test.port", "8080");
109        final ActiveMQComponent component = new ActiveMQComponent();
110
111        component.setBrokerURL("tcp://localhost:" + jmsPort);
112        component.setExposeAllQueues(true);
113
114        final FcrepoComponent fcrepo = new FcrepoComponent();
115        fcrepo.setBaseUrl("http://localhost:" + webPort + "/fcrepo/rest");
116        fcrepo.setAuthUsername(FEDORA_USERNAME);
117        fcrepo.setAuthPassword(FEDORA_PASSWORD);
118        services.put("broker", asService(component, "osgi.jndi.service.name", "fcrepo/Broker"));
119        services.put("fcrepo", asService(fcrepo, "osgi.jndi.service.name", "fcrepo/Camel"));
120    }
121
122    @Test
123    public void testFixityOnBinary() throws Exception {
124        final String webPort = System.getProperty("fcrepo.dynamic.test.port", "8080");
125        final String jmsPort = System.getProperty("fcrepo.dynamic.jms.port", "61616");
126        final String path = fullPath.replaceFirst("http://localhost:[0-9]+/fcrepo/rest", "");
127        final String fcrepoEndpoint = "mock:fcrepo:http://localhost:8080/fcrepo/rest";
128        final Namespaces ns = new Namespaces("rdf", RDF.uri);
129        ns.add("fedora", REPOSITORY);
130        ns.add("premis", "http://www.loc.gov/premis/rdf/v1#");
131
132        context.getRouteDefinition("FcrepoFixity").adviceWith(context, new AdviceWithRouteBuilder() {
133            @Override
134            public void configure() throws Exception {
135                mockEndpoints("*");
136            }
137        });
138        context.start();
139
140        getMockEndpoint(fcrepoEndpoint).expectedMessageCount(2);
141        getMockEndpoint("mock:success").expectedMessageCount(1);
142        template.sendBodyAndHeader("direct:start", "", FCREPO_URI,
143                "http://localhost:" + webPort + "/fcrepo/rest" + path);
144
145        assertMockEndpointsSatisfied();
146
147        final String body = getMockEndpoint("mock:success").assertExchangeReceived(0).getIn().getBody(String.class);
148        assertTrue(body.contains(
149                "<premis:hasSize rdf:datatype=\"http://www.w3.org/2001/XMLSchema#long\">74</premis:hasSize>"));
150        assertTrue(body.contains(
151                "<premis:hasMessageDigest rdf:resource=\"urn:sha1:" + digest + "\"/>"));
152    }
153}