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.serialization;
019
020import static org.apache.camel.util.ObjectHelper.loadResourceAsStream;
021import static org.fcrepo.camel.FcrepoHeaders.FCREPO_URI;
022import static org.fcrepo.camel.serialization.SerializationRouter.SERIALIZATION_PATH;
023
024import org.junit.Test;
025
026import org.apache.camel.builder.AdviceWithRouteBuilder;
027
028/**
029 * Test the route workflow (property 'includeBinaries' is false).
030 *
031 * @author Bethany Seeger
032 * @since 2015-09-28
033 */
034
035public class BinaryDisabledRouteTest extends AbstractRouteTest {
036
037    @Test
038    public void testMetatdataSerialization() throws Exception {
039        context.getRouteDefinition("FcrepoSerialization").adviceWith(context, new AdviceWithRouteBuilder() {
040            @Override
041            public void configure() throws Exception {
042                replaceFromWith("direct:start");
043                mockEndpointsAndSkip("direct:metadata");
044                mockEndpointsAndSkip("direct:binary");
045                mockEndpointsAndSkip("direct:delete");
046                mockEndpointsAndSkip("fcrepo:*");
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        template.sendBody(loadResourceAsStream("event.json"));
057
058        assertMockEndpointsSatisfied();
059    }
060
061    @Test
062       public void testMetadataSerializationRemoveNode() throws Exception {
063        context.getRouteDefinition("FcrepoSerialization").adviceWith(context, new AdviceWithRouteBuilder() {
064            @Override
065            public void configure() throws Exception {
066                replaceFromWith("direct:start");
067                mockEndpointsAndSkip("direct:metadata");
068                mockEndpointsAndSkip("direct:binary");
069                mockEndpointsAndSkip("direct:delete");
070                mockEndpointsAndSkip("fcrepo:*");
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        template.sendBody(loadResourceAsStream("event_delete_resource.json"));
080
081        assertMockEndpointsSatisfied();
082    }
083
084    @Test
085    public void testMetadataReSerialization() throws Exception {
086        context.getRouteDefinition("FcrepoReSerialization").adviceWith(context, new AdviceWithRouteBuilder() {
087            @Override
088            public void configure() throws Exception {
089                replaceFromWith("direct:start");
090                mockEndpointsAndSkip("direct:metadata");
091                mockEndpointsAndSkip("direct:binary");
092                mockEndpointsAndSkip("direct:delete");
093                mockEndpointsAndSkip("fcrepo:*");
094            }
095        });
096        context.start();
097
098        getMockEndpoint("mock:direct:metadata").expectedMessageCount(1);
099        getMockEndpoint("mock:direct:binary").expectedMessageCount(1);
100
101        template.sendBodyAndHeader(loadResourceAsStream("binary.rdf"), FCREPO_URI, baseURL);
102
103        assertMockEndpointsSatisfied();
104    }
105
106    @Test
107    public void testMetadataUpdaterIndexable() throws Exception {
108        context.getRouteDefinition("FcrepoSerializationMetadataUpdater").adviceWith(context,
109              new AdviceWithRouteBuilder() {
110                  @Override
111                  public void configure() throws Exception {
112                      replaceFromWith("direct:start");
113                      mockEndpointsAndSkip("fcrepo:*");
114                      mockEndpointsAndSkip("file:*");
115                      weaveAddLast().to("mock:result");
116                  }
117              });
118        context.start();
119
120        resultEndpoint.expectedMessageCount(1);
121
122        // send a file!
123        template.sendBody(loadResourceAsStream("indexable.rdf"));
124
125        assertMockEndpointsSatisfied();
126    }
127
128    @Test
129    public void testMetadataUpdaterBinary() throws Exception {
130        context.getRouteDefinition("FcrepoSerializationBinaryUpdater").adviceWith(context,
131          new AdviceWithRouteBuilder() {
132              @Override
133              public void configure() throws Exception {
134                  replaceFromWith("direct:start");
135                        mockEndpointsAndSkip("fcrepo:*");
136                        mockEndpointsAndSkip("file:*");
137              }
138        });
139        context.start();
140        // this should be zero because writing binaries is disabled by default.
141        getMockEndpoint("mock:file:binary_file").expectedMessageCount(0);
142
143        template.sendBodyAndHeader(loadResourceAsStream("binary.rdf"), SERIALIZATION_PATH, "/foo");
144
145        assertMockEndpointsSatisfied();
146    }
147}