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