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