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