001/*
002 * Copyright 2016 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.serialization;
017
018import static org.fcrepo.camel.JmsHeaders.BASE_URL;
019import static org.fcrepo.camel.JmsHeaders.EVENT_TYPE;
020import static org.fcrepo.camel.JmsHeaders.IDENTIFIER;
021import static org.fcrepo.camel.RdfNamespaces.REPOSITORY;
022
023import org.junit.Test;
024import java.util.Map;
025
026import com.google.common.collect.ImmutableMap;
027import org.apache.camel.builder.AdviceWithRouteBuilder;
028import org.apache.camel.util.ObjectHelper;
029import org.apache.commons.io.IOUtils;
030
031/**
032 * Test the route workflow (property 'includeBinaries' is false).
033 *
034 * @author Bethany Seeger
035 * @since 2015-09-28
036 */
037
038public class BinaryDisabledRouteTest extends AbstractRouteTest {
039
040    @Test
041    public void testMetatdataSerialization() throws Exception {
042        context.getRouteDefinition("FcrepoSerialization").adviceWith(context, new AdviceWithRouteBuilder() {
043            @Override
044            public void configure() throws Exception {
045                replaceFromWith("direct:start");
046                mockEndpointsAndSkip("*");
047            }
048        });
049        context.start();
050
051        getMockEndpoint("mock:direct:metadata").expectedMessageCount(1);
052        getMockEndpoint("mock:direct:binary").expectedMessageCount(1);
053        getMockEndpoint("mock:direct:delete").expectedMessageCount(0);
054
055        // send a file!
056        final String body = IOUtils.toString(ObjectHelper.loadResourceAsStream("binary.rdf"), "UTF-8");
057        final Map<String, Object> headers = ImmutableMap.of(BASE_URL, baseURL);
058
059        template.sendBodyAndHeaders(body, headers);
060
061        assertMockEndpointsSatisfied();
062    }
063
064    @Test
065       public void testMetadataSerializationRemoveNode() throws Exception {
066        context.getRouteDefinition("FcrepoSerialization").adviceWith(context, new AdviceWithRouteBuilder() {
067            @Override
068            public void configure() throws Exception {
069                replaceFromWith("direct:start");
070                mockEndpointsAndSkip("*");
071            }
072        });
073        context.start();
074
075        getMockEndpoint("mock:direct:metadata").expectedMessageCount(0);
076        getMockEndpoint("mock:direct:binary").expectedMessageCount(0);
077        getMockEndpoint("mock:direct:delete").expectedMessageCount(1);
078
079        // send a file!
080        final String body = IOUtils.toString(ObjectHelper.loadResourceAsStream("binary.rdf"), "UTF-8");
081        final Map<String, Object> headers = ImmutableMap.of(
082            BASE_URL, baseURL,
083            IDENTIFIER, identifier,
084            EVENT_TYPE, REPOSITORY + "NODE_REMOVED");
085
086        template.sendBodyAndHeaders(body, headers);
087
088        assertMockEndpointsSatisfied();
089    }
090
091    @Test
092    public void testMetadataReSerialization() throws Exception {
093        context.getRouteDefinition("FcrepoReSerialization").adviceWith(context, new AdviceWithRouteBuilder() {
094            @Override
095            public void configure() throws Exception {
096                replaceFromWith("direct:start");
097                mockEndpointsAndSkip("*");
098            }
099        });
100        context.start();
101
102        getMockEndpoint("mock:direct:metadata").expectedMessageCount(1);
103        getMockEndpoint("mock:direct:binary").expectedMessageCount(1);
104
105        // send a file!
106        final String body = IOUtils.toString(ObjectHelper.loadResourceAsStream("binary.rdf"), "UTF-8");
107        final Map<String, Object> headers = ImmutableMap.of(BASE_URL, baseURL);
108
109        template.sendBodyAndHeaders(body, headers);
110
111        assertMockEndpointsSatisfied();
112    }
113
114    @Test
115    public void testMetadataUpdaterIndexable() throws Exception {
116        context.getRouteDefinition("FcrepoSerializationMetadataUpdater").adviceWith(context,
117              new AdviceWithRouteBuilder() {
118                  @Override
119                  public void configure() throws Exception {
120                      replaceFromWith("direct:start");
121                      mockEndpointsAndSkip("*");
122                      weaveAddLast().to("mock:result");
123                  }
124              });
125        context.start();
126
127        resultEndpoint.expectedMessageCount(1);
128
129        // send a file!
130        final String body = IOUtils.toString(ObjectHelper.loadResourceAsStream("indexable.rdf"), "UTF-8");
131        final Map<String, Object> headers = ImmutableMap.of(BASE_URL, baseURL);
132
133        template.sendBodyAndHeaders(body, headers);
134
135        assertMockEndpointsSatisfied();
136    }
137
138    @Test
139    public void testMetadataUpdaterBinary() throws Exception {
140        context.getRouteDefinition("FcrepoSerializationBinaryUpdater").adviceWith(context,
141          new AdviceWithRouteBuilder() {
142              @Override
143              public void configure() throws Exception {
144                  replaceFromWith("direct:start");
145                  mockEndpointsAndSkip("*");
146              }
147        });
148        context.start();
149        // this should be zero because writing binaries is disabled by default.
150        getMockEndpoint("mock:file:binary_file").expectedMessageCount(0);
151
152        // send a file!
153        final String body = IOUtils.toString(ObjectHelper.loadResourceAsStream("binary.rdf"), "UTF-8");
154        final Map<String, Object> headers = ImmutableMap.of(
155            BASE_URL, baseURL,
156            IDENTIFIER, "foo");
157
158        template.sendBodyAndHeaders(body, headers);
159
160        assertMockEndpointsSatisfied();
161    }
162}