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