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 org.apache.camel.EndpointInject;
024import org.apache.camel.Exchange;
025import org.apache.camel.Produce;
026import org.apache.camel.ProducerTemplate;
027import org.apache.camel.builder.RouteBuilder;
028import org.apache.camel.builder.xml.Namespaces;
029import org.apache.camel.builder.xml.XPathBuilder;
030import org.apache.camel.component.mock.MockEndpoint;
031import org.apache.camel.test.junit4.CamelTestSupport;
032import org.apache.jena.vocabulary.RDF;
033import org.fcrepo.camel.FcrepoHeaders;
034import org.junit.Test;
035
036/**
037 * Test adding a new resource with POST
038 * @author Aaron Coburn
039 * @since November 7, 2014
040 */
041public class FcrepoPostIT extends CamelTestSupport {
042
043    private static final String REPOSITORY = "http://fedora.info/definitions/v4/repository#";
044
045    @EndpointInject(uri = "mock:created")
046    protected MockEndpoint createdEndpoint;
047
048    @EndpointInject(uri = "mock:result")
049    protected MockEndpoint resultEndpoint;
050
051    @EndpointInject(uri = "mock:deleted")
052    protected MockEndpoint deletedEndpoint;
053
054    @Produce(uri = "direct:start")
055    protected ProducerTemplate template;
056
057    @Test
058    public void testPost() throws InterruptedException {
059        // Assertions
060        resultEndpoint.expectedMessageCount(1);
061        resultEndpoint.expectedBodiesReceived("some title & other");
062
063        createdEndpoint.expectedMessageCount(1);
064        createdEndpoint.expectedHeaderReceived(Exchange.HTTP_RESPONSE_CODE, 201);
065
066        deletedEndpoint.expectedMessageCount(1);
067        deletedEndpoint.expectedHeaderReceived(Exchange.HTTP_RESPONSE_CODE, 204);
068
069        // Setup
070        final Map<String, Object> headers = new HashMap<>();
071        headers.put(Exchange.HTTP_METHOD, "POST");
072        headers.put(Exchange.CONTENT_TYPE, "text/turtle");
073
074        final String fullPath = template.requestBodyAndHeaders(
075                "direct:setup", FcrepoTestUtils.getTurtleDocument(), headers, String.class);
076
077        final String identifier = fullPath.replaceAll(FcrepoTestUtils.getFcrepoBaseUrl(), "");
078
079        // Test
080        template.sendBodyAndHeader(null, FcrepoHeaders.FCREPO_IDENTIFIER, identifier);
081
082        // Teardown
083        final Map<String, Object> teardownHeaders = new HashMap<>();
084        teardownHeaders.put(Exchange.HTTP_METHOD, "DELETE");
085        teardownHeaders.put(FcrepoHeaders.FCREPO_IDENTIFIER, identifier);
086        template.sendBodyAndHeaders("direct:teardown", null, teardownHeaders);
087
088        // Confirm that the assertions passed
089        resultEndpoint.assertIsSatisfied();
090        deletedEndpoint.assertIsSatisfied();
091        createdEndpoint.assertIsSatisfied();
092    }
093
094    @Override
095    protected RouteBuilder createRouteBuilder() {
096        return new RouteBuilder() {
097            @Override
098            public void configure() {
099                final String fcrepo_uri = FcrepoTestUtils.getFcrepoEndpointUri();
100
101                final Namespaces ns = new Namespaces("rdf", RDF.uri);
102
103                final XPathBuilder titleXpath = new XPathBuilder("/rdf:RDF/rdf:Description/dc:title/text()");
104                titleXpath.namespaces(ns);
105                titleXpath.namespace("dc", "http://purl.org/dc/elements/1.1/");
106
107                from("direct:setup")
108                    .to(fcrepo_uri)
109                    .to("mock:created");
110
111                from("direct:start")
112                    .to(fcrepo_uri)
113                    .convertBodyTo(org.w3c.dom.Document.class)
114                    .filter().xpath(
115                        "/rdf:RDF/rdf:Description/rdf:type" +
116                        "[@rdf:resource='" + REPOSITORY + "Resource']", ns)
117                    .split(titleXpath)
118                    .to("mock:result");
119
120                from("direct:teardown")
121                    .to(fcrepo_uri)
122                    .to("mock:deleted");
123            }
124        };
125    }
126}