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 static org.junit.Assert.assertTrue;
021
022import java.util.HashMap;
023import java.util.Map;
024
025import org.apache.camel.EndpointInject;
026import org.apache.camel.Exchange;
027import org.apache.camel.Produce;
028import org.apache.camel.ProducerTemplate;
029import org.apache.camel.builder.RouteBuilder;
030import org.apache.camel.builder.xml.Namespaces;
031import org.apache.camel.component.mock.MockEndpoint;
032import org.apache.camel.test.junit4.CamelTestSupport;
033import org.apache.jena.vocabulary.RDF;
034import org.fcrepo.camel.FcrepoHeaders;
035import org.junit.Test;
036
037/**
038 * Test adding an RDF resource
039 * @author Aaron Coburn
040 * @since Dec 26, 2014
041 */
042public class FcrepoContainerPatchIT extends CamelTestSupport {
043
044    private static final String REPOSITORY = "http://fedora.info/definitions/v4/repository#";
045
046    @EndpointInject(uri = "mock:created")
047    protected MockEndpoint createdEndpoint;
048
049    @EndpointInject(uri = "mock:filter")
050    protected MockEndpoint filteredEndpoint;
051
052    @EndpointInject(uri = "mock:title")
053    protected MockEndpoint titleEndpoint;
054
055    @EndpointInject(uri = "mock:verifyGone")
056    protected MockEndpoint goneEndpoint;
057
058    @EndpointInject(uri = "mock:operation")
059    protected MockEndpoint operationEndpoint;
060
061    @Produce(uri = "direct:filter")
062    protected ProducerTemplate template;
063
064    @Test
065    public void testGetContainer() throws InterruptedException {
066        // Assertions
067        createdEndpoint.expectedMessageCount(1);
068        createdEndpoint.expectedHeaderReceived(Exchange.HTTP_RESPONSE_CODE, 201);
069
070        operationEndpoint.expectedMessageCount(2);
071        operationEndpoint.expectedBodiesReceived(null, null);
072        operationEndpoint.expectedHeaderReceived(Exchange.HTTP_RESPONSE_CODE, 204);
073
074        titleEndpoint.expectedMessageCount(3);
075        titleEndpoint.expectedBodiesReceivedInAnyOrder(
076                "some title & other", "some title & other", "some other title");
077        titleEndpoint.expectedHeaderReceived(Exchange.CONTENT_TYPE, "application/rdf+xml");
078        titleEndpoint.expectedHeaderReceived(Exchange.HTTP_RESPONSE_CODE, 200);
079
080        filteredEndpoint.expectedMessageCount(2);
081        filteredEndpoint.expectedHeaderReceived(Exchange.CONTENT_TYPE, "application/rdf+xml");
082        filteredEndpoint.expectedHeaderReceived(Exchange.HTTP_RESPONSE_CODE, 200);
083
084        goneEndpoint.expectedMessageCount(1);
085        goneEndpoint.expectedHeaderReceived(Exchange.HTTP_RESPONSE_CODE, 410);
086
087        final Map<String, Object> headers = new HashMap<>();
088        headers.put(Exchange.HTTP_METHOD, "POST");
089        headers.put(Exchange.CONTENT_TYPE, "text/turtle");
090
091        final String fullPath = template.requestBodyAndHeaders(
092                "direct:create",
093                FcrepoTestUtils.getTurtleDocument(),
094                headers, String.class);
095
096        // Strip off the baseUrl to get the resource path
097        final String identifier = fullPath.replaceAll(FcrepoTestUtils.getFcrepoBaseUrl(), "");
098
099        template.sendBodyAndHeader("direct:title", null, FcrepoHeaders.FCREPO_IDENTIFIER, identifier);
100
101        final String patchDoc = "PREFIX dc: <http://purl.org/dc/elements/1.1/>\n\n" +
102                        "INSERT { <> dc:title \"some other title\" } WHERE {}";
103
104        template.sendBodyAndHeader("direct:patch", patchDoc, FcrepoHeaders.FCREPO_IDENTIFIER, identifier);
105
106        template.sendBodyAndHeader("direct:title", null, FcrepoHeaders.FCREPO_IDENTIFIER, identifier);
107
108
109        template.sendBodyAndHeader("direct:delete", null, FcrepoHeaders.FCREPO_IDENTIFIER, identifier);
110
111        // Confirm that assertions passed
112        createdEndpoint.assertIsSatisfied();
113        filteredEndpoint.assertIsSatisfied();
114        titleEndpoint.assertIsSatisfied();
115        goneEndpoint.assertIsSatisfied();
116        operationEndpoint.assertIsSatisfied();
117
118        // Check deleted container
119        goneEndpoint.getExchanges().forEach(exchange -> {
120            assertTrue(exchange.getIn().getHeader(Exchange.CONTENT_TYPE, String.class).contains("application/rdf+xml"));
121        });
122    }
123
124    @Override
125    protected RouteBuilder createRouteBuilder() {
126        return new RouteBuilder() {
127            @Override
128            public void configure() {
129
130                final String fcrepo_uri = FcrepoTestUtils.getFcrepoEndpointUri();
131
132                final Namespaces ns = new Namespaces("rdf", RDF.uri);
133                ns.add("dc", "http://purl.org/dc/elements/1.1/");
134
135                from("direct:create")
136                    .to(fcrepo_uri)
137                    .to("mock:created");
138
139                from("direct:patch")
140                    .setHeader(Exchange.HTTP_METHOD, constant("PATCH"))
141                    .to(fcrepo_uri)
142                    .to("mock:operation");
143
144                from("direct:title")
145                    .to(fcrepo_uri)
146                    .convertBodyTo(org.w3c.dom.Document.class)
147                    .filter().xpath(
148                        "/rdf:RDF/rdf:Description/rdf:type" +
149                        "[@rdf:resource='" + REPOSITORY + "Container']", ns)
150                    .to("mock:filter")
151                    .split().xpath(
152                        "/rdf:RDF/rdf:Description/dc:title/text()", ns)
153                    .to("mock:title");
154
155                from("direct:delete")
156                    .setHeader(Exchange.HTTP_METHOD, constant("DELETE"))
157                    .to(fcrepo_uri)
158                    .to("mock:operation")
159                    .setHeader(Exchange.HTTP_METHOD, constant("GET"))
160                    .to(fcrepo_uri + "?throwExceptionOnFailure=false")
161                    .to("mock:verifyGone");
162            }
163        };
164    }
165}