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 a non-RDF resource
040 * @author Aaron Coburn
041 * @since November 7, 2014
042 */
043public class FcrepoBinaryHeadIT extends CamelTestSupport {
044
045    private static final String REPOSITORY = "http://fedora.info/definitions/v4/repository#";
046
047    @EndpointInject(uri = "mock:created")
048    protected MockEndpoint createdEndpoint;
049
050    @EndpointInject(uri = "mock:filter")
051    protected MockEndpoint filteredEndpoint;
052
053    @EndpointInject(uri = "mock:binary")
054    protected MockEndpoint binaryEndpoint;
055
056    @EndpointInject(uri = "mock:verify")
057    protected MockEndpoint goneEndpoint;
058
059    @EndpointInject(uri = "mock:deleted")
060    protected MockEndpoint deletedEndpoint;
061
062    @Produce(uri = "direct:filter")
063    protected ProducerTemplate template;
064
065    @Test
066    public void testHeadBinary() throws InterruptedException {
067        // Assertions
068        createdEndpoint.expectedMessageCount(2);
069        createdEndpoint.expectedHeaderReceived(Exchange.HTTP_RESPONSE_CODE, 201);
070
071        binaryEndpoint.allMessages().body().equals(null);
072        binaryEndpoint.expectedMessageCount(1);
073        binaryEndpoint.expectedHeaderReceived(Exchange.CONTENT_TYPE, "text/plain");
074        binaryEndpoint.expectedHeaderReceived(Exchange.HTTP_RESPONSE_CODE, 200);
075
076        filteredEndpoint.expectedMessageCount(1);
077        filteredEndpoint.expectedHeaderReceived(Exchange.CONTENT_TYPE, "application/rdf+xml");
078        filteredEndpoint.expectedHeaderReceived(Exchange.HTTP_RESPONSE_CODE, 200);
079
080        deletedEndpoint.expectedMessageCount(2);
081        deletedEndpoint.allMessages().body().equals(null);
082        deletedEndpoint.expectedHeaderReceived(Exchange.HTTP_RESPONSE_CODE, 204);
083
084        goneEndpoint.expectedMessageCount(2);
085        goneEndpoint.expectedHeaderReceived(Exchange.HTTP_RESPONSE_CODE, 410);
086
087        final String binary = "/file";
088        final Map<String, Object> headers = new HashMap<>();
089        headers.put(Exchange.HTTP_METHOD, "POST");
090        headers.put(Exchange.CONTENT_TYPE, "text/turtle");
091
092        final String fullPath = template.requestBodyAndHeaders(
093                "direct:create",
094                FcrepoTestUtils.getTurtleDocument(),
095                headers, String.class);
096
097        // Strip off the baseUrl to get the resource path
098        final String identifier = fullPath.replaceAll(FcrepoTestUtils.getFcrepoBaseUrl(), "");
099
100        headers.clear();
101        headers.put(Exchange.HTTP_METHOD, "PUT");
102        headers.put(Exchange.CONTENT_TYPE, "text/plain");
103        headers.put(FcrepoHeaders.FCREPO_IDENTIFIER, identifier + binary);
104
105        template.sendBodyAndHeaders("direct:create", FcrepoTestUtils.getTextDocument(), headers);
106
107
108        template.sendBodyAndHeader("direct:head", null, FcrepoHeaders.FCREPO_IDENTIFIER, identifier);
109        template.sendBodyAndHeader("direct:head", null, FcrepoHeaders.FCREPO_IDENTIFIER, identifier + binary);
110
111        template.sendBodyAndHeader("direct:delete", null, FcrepoHeaders.FCREPO_IDENTIFIER, identifier + binary);
112        template.sendBodyAndHeader("direct:delete", null, FcrepoHeaders.FCREPO_IDENTIFIER, identifier);
113
114        // Confirm that assertions passed
115        createdEndpoint.assertIsSatisfied();
116        filteredEndpoint.assertIsSatisfied();
117        binaryEndpoint.assertIsSatisfied();
118        goneEndpoint.assertIsSatisfied();
119        deletedEndpoint.assertIsSatisfied();
120
121        // skip first message, as we've already extracted the body
122        assertEquals(FcrepoTestUtils.getFcrepoBaseUrl() + identifier + binary,
123                createdEndpoint.getExchanges().get(1).getIn().getBody(String.class));
124
125        // Additional assertions
126        // Check deleted container
127        for (Exchange exchange : goneEndpoint.getExchanges()) {
128            assertTrue(exchange.getIn().getHeader(Exchange.CONTENT_TYPE, String.class).contains("application/rdf+xml"));
129        }
130     }
131
132    @Override
133    protected RouteBuilder createRouteBuilder() {
134        return new RouteBuilder() {
135            @Override
136            public void configure() {
137
138                final String fcrepo_uri = FcrepoTestUtils.getFcrepoEndpointUri();
139
140                final Namespaces ns = new Namespaces("rdf", RDF.uri);
141
142                from("direct:create")
143                    .to(fcrepo_uri)
144                    .to("mock:created");
145
146                from("direct:head")
147                    .to(fcrepo_uri)
148                    .filter().xpath(
149                        "/rdf:RDF/rdf:Description/rdf:type" +
150                        "[@rdf:resource='" + REPOSITORY + "Binary']", ns)
151                    .to("mock:filter")
152                    .setHeader(Exchange.HTTP_METHOD, constant("HEAD"))
153                    .to(fcrepo_uri + "?metadata=false")
154                    .to("mock:binary");
155
156                 from("direct:delete")
157                    .setHeader(Exchange.HTTP_METHOD, constant("DELETE"))
158                    .to(fcrepo_uri)
159                    .to("mock:deleted")
160                    .setHeader(Exchange.HTTP_METHOD, constant("GET"))
161                    .to(fcrepo_uri + "?throwExceptionOnFailure=false")
162                    .to("mock:verify");
163            }
164        };
165    }
166}