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;
019
020import static org.apache.camel.util.ObjectHelper.loadResourceAsStream;
021import static org.fcrepo.camel.FcrepoHeaders.FCREPO_URI;
022
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.component.mock.MockEndpoint;
030import org.apache.camel.test.blueprint.CamelBlueprintTestSupport;
031import org.apache.commons.io.IOUtils;
032
033import org.junit.Test;
034
035/**
036 * Test the route workflow.
037 *
038 * @author Aaron Coburn
039 * @since 2015-06-18
040 */
041public class RouteTest extends CamelBlueprintTestSupport {
042
043    @EndpointInject(uri = "mock:result")
044    protected MockEndpoint resultEndpoint;
045
046    @Produce(uri = "direct:start")
047    protected ProducerTemplate template;
048
049    private static final String baseURL = "http://localhost/rest";
050    private static final String identifier = "/file1";
051
052    @Override
053    public boolean isUseAdviceWith() {
054        return true;
055    }
056
057    @Override
058    public boolean isUseRouteBuilder() {
059        return false;
060    }
061
062    @Override
063    protected String getBlueprintDescriptor() {
064        return "/OSGI-INF/blueprint/blueprint-test.xml";
065    }
066
067    @Override
068    protected Properties useOverridePropertiesWithPropertiesComponent() {
069         final Properties props = new Properties();
070         props.put("fixity.failure", "mock:failure");
071         props.put("fixity.success", "mock:success");
072         props.put("fixity.stream", "seda:foo");
073         return props;
074    }
075
076    @Test
077    public void testBinaryFixitySuccess() throws Exception {
078
079        context.getRouteDefinition("FcrepoFixity").adviceWith(context, new AdviceWithRouteBuilder() {
080            @Override
081            public void configure() throws Exception {
082                replaceFromWith("direct:start");
083                mockEndpointsAndSkip("fcrepo:*");
084            }
085        });
086        context.start();
087
088        getMockEndpoint("mock:failure").expectedMessageCount(0);
089        getMockEndpoint("mock:success").expectedMessageCount(1);
090
091        final String body = IOUtils.toString(loadResourceAsStream("fixity.rdf"), "UTF-8");
092        template.sendBodyAndHeader(body, FCREPO_URI, baseURL + identifier);
093
094        assertMockEndpointsSatisfied();
095    }
096
097    @Test
098    public void testBinaryFixityFailure() throws Exception {
099
100        context.getRouteDefinition("FcrepoFixity").adviceWith(context, new AdviceWithRouteBuilder() {
101            @Override
102            public void configure() throws Exception {
103                replaceFromWith("direct:start");
104                mockEndpointsAndSkip("fcrepo:*");
105            }
106        });
107        context.start();
108
109        getMockEndpoint("mock:failure").expectedMessageCount(1);
110        getMockEndpoint("mock:success").expectedMessageCount(0);
111
112        final String body = IOUtils.toString(loadResourceAsStream("fixityFailure.rdf"), "UTF-8");
113        template.sendBodyAndHeader(body, FCREPO_URI, baseURL + identifier);
114
115        assertMockEndpointsSatisfied();
116    }
117
118    @Test
119    public void testNonBinary() throws Exception {
120
121        context.getRouteDefinition("FcrepoFixity").adviceWith(context, new AdviceWithRouteBuilder() {
122            @Override
123            public void configure() throws Exception {
124                replaceFromWith("direct:start");
125                mockEndpointsAndSkip("fcrepo:*");
126            }
127        });
128        context.start();
129
130        getMockEndpoint("mock:failure").expectedMessageCount(0);
131        getMockEndpoint("mock:success").expectedMessageCount(0);
132
133        final String body = IOUtils.toString(loadResourceAsStream("container.rdf"), "UTF-8");
134        template.sendBodyAndHeader(body, FCREPO_URI, baseURL + identifier);
135
136        assertMockEndpointsSatisfied();
137    }
138}