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.integration;
019
020import java.util.HashMap;
021import java.util.Map;
022
023import javax.xml.transform.stream.StreamSource;
024
025import org.apache.jena.vocabulary.RDF;
026import org.apache.camel.EndpointInject;
027import org.apache.camel.Exchange;
028import org.apache.camel.Produce;
029import org.apache.camel.ProducerTemplate;
030import org.apache.camel.builder.RouteBuilder;
031import org.apache.camel.builder.xml.Namespaces;
032import org.apache.camel.builder.xml.XPathBuilder;
033import org.apache.camel.component.mock.MockEndpoint;
034import org.apache.camel.test.junit4.CamelTestSupport;
035import org.fcrepo.camel.FcrepoHeaders;
036import org.junit.Assert;
037import org.junit.Test;
038
039/**
040 * Test adding an RDF resource
041 * @author Aaron Coburn
042 * @since Dec 26, 2014
043 */
044public class FcrepoStreamIT extends CamelTestSupport {
045
046    final private int children = 200;
047
048    @EndpointInject(uri = "mock:created")
049    protected MockEndpoint createdEndpoint;
050
051    @EndpointInject(uri = "mock:child")
052    protected MockEndpoint childEndpoint;
053
054    @EndpointInject(uri = "mock:verifyGone")
055    protected MockEndpoint goneEndpoint;
056
057    @EndpointInject(uri = "mock:deleted")
058    protected MockEndpoint deletedEndpoint;
059
060    @Produce(uri = "direct:filter")
061    protected ProducerTemplate template;
062
063    @Test
064    public void testGetStreamedContainer() throws InterruptedException {
065        // Assertions
066        createdEndpoint.expectedMessageCount(children + 1);
067        createdEndpoint.expectedHeaderReceived(Exchange.HTTP_RESPONSE_CODE, 201);
068
069        childEndpoint.expectedMessageCount(children);
070
071        deletedEndpoint.expectedMessageCount(1);
072        deletedEndpoint.allMessages().body().equals(null);
073        deletedEndpoint.expectedHeaderReceived(Exchange.HTTP_RESPONSE_CODE, 204);
074
075        goneEndpoint.expectedMessageCount(1);
076        goneEndpoint.expectedHeaderReceived(Exchange.HTTP_RESPONSE_CODE, 410);
077
078        final Map<String, Object> headers = new HashMap<>();
079        headers.put(Exchange.HTTP_METHOD, "PUT");
080        headers.put(FcrepoHeaders.FCREPO_IDENTIFIER, "/stream");
081        template.sendBodyAndHeaders("direct:create", null, headers);
082
083        headers.clear();
084        headers.put(Exchange.HTTP_METHOD, "POST");
085        headers.put(Exchange.CONTENT_TYPE, "text/turtle");
086        headers.put(FcrepoHeaders.FCREPO_IDENTIFIER, "/stream");
087
088        for (int i = 0; i < children; ++i) {
089            template.sendBodyAndHeaders("direct:create", FcrepoTestUtils.getTurtleDocument(), headers);
090        }
091
092        template.sendBodyAndHeader("direct:get", null, FcrepoHeaders.FCREPO_IDENTIFIER, "/stream");
093
094        template.sendBodyAndHeader("direct:delete", null, FcrepoHeaders.FCREPO_IDENTIFIER, "/stream");
095
096        // Confirm that assertions passed
097        createdEndpoint.assertIsSatisfied();
098        childEndpoint.assertIsSatisfied();
099        goneEndpoint.assertIsSatisfied();
100        deletedEndpoint.assertIsSatisfied();
101
102        for (Exchange exchange : childEndpoint.getExchanges()) {
103            Assert.assertTrue(exchange.getIn().getBody(String.class).contains("/stream/"));
104        }
105    }
106
107    @Override
108    protected RouteBuilder createRouteBuilder() {
109        return new RouteBuilder() {
110            @Override
111            public void configure() {
112
113                final String fcrepo_uri = FcrepoTestUtils.getFcrepoEndpointUriWithScheme();
114
115                final Namespaces ns = new Namespaces("rdf", RDF.uri);
116                ns.add("ldp", "http://www.w3.org/ns/ldp#");
117
118                final XPathBuilder ldpChildren = new XPathBuilder("/rdf:RDF/rdf:Description/ldp:contains");
119                ldpChildren.namespaces(ns);
120
121                getContext().getStreamCachingStrategy().setSpoolThreshold(1024);
122                getContext().getStreamCachingStrategy().setBufferSize(128);
123                getContext().setStreamCaching(true);
124
125                from("direct:create")
126                    .to(fcrepo_uri)
127                    .to("mock:created");
128
129                from("direct:get")
130                    .streamCaching()
131                    .to(fcrepo_uri)
132                    .convertBodyTo(StreamSource.class)
133                    .split(ldpChildren).streaming()
134                        .transform()
135                        .xpath("/ldp:contains/@rdf:resource", String.class, ns)
136                        .to("mock:child");
137
138                from("direct:delete")
139                    .setHeader(Exchange.HTTP_METHOD, constant("DELETE"))
140                    .to(fcrepo_uri)
141                    .to("mock:deleted")
142                    .setHeader(Exchange.HTTP_METHOD, constant("GET"))
143                    .to(fcrepo_uri + "?throwExceptionOnFailure=false")
144                    .to("mock:verifyGone");
145            }
146        };
147    }
148}