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