001/**
002 * Copyright 2015 DuraSpace, Inc.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.fcrepo.camel.integration;
017
018import java.util.HashMap;
019import java.util.Map;
020
021import org.apache.camel.EndpointInject;
022import org.apache.camel.Exchange;
023import org.apache.camel.Produce;
024import org.apache.camel.ProducerTemplate;
025import org.apache.camel.builder.RouteBuilder;
026import org.apache.camel.builder.xml.Namespaces;
027import org.apache.camel.component.mock.MockEndpoint;
028import org.apache.camel.test.junit4.CamelTestSupport;
029import org.fcrepo.camel.FcrepoHeaders;
030import org.fcrepo.camel.RdfNamespaces;
031import org.junit.Assert;
032import org.junit.Test;
033import org.junit.runner.RunWith;
034import org.springframework.test.context.ContextConfiguration;
035import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
036
037/**
038 * Test adding a non-RDF resource
039 * @author Aaron Coburn
040 * @since November 7, 2014
041 */
042@RunWith(SpringJUnit4ClassRunner.class)
043@ContextConfiguration({"/spring-test/test-container.xml"})
044public class FcrepoBinaryHeadIT extends CamelTestSupport {
045
046    @EndpointInject(uri = "mock:created")
047    protected MockEndpoint createdEndpoint;
048
049    @EndpointInject(uri = "mock:filter")
050    protected MockEndpoint filteredEndpoint;
051
052    @EndpointInject(uri = "mock:binary")
053    protected MockEndpoint binaryEndpoint;
054
055    @EndpointInject(uri = "mock:verify")
056    protected MockEndpoint goneEndpoint;
057
058    @EndpointInject(uri = "mock:deleted")
059    protected MockEndpoint deletedEndpoint;
060
061    @EndpointInject(uri = "mock:verify2")
062    protected MockEndpoint notFoundEndpoint;
063
064    @Produce(uri = "direct:filter")
065    protected ProducerTemplate template;
066
067    @Test
068    public void testHeadBinary() throws InterruptedException {
069        // Assertions
070        createdEndpoint.expectedMessageCount(2);
071        createdEndpoint.expectedHeaderReceived(Exchange.HTTP_RESPONSE_CODE, 201);
072
073        binaryEndpoint.allMessages().body().equals(null);
074        binaryEndpoint.expectedMessageCount(1);
075        binaryEndpoint.expectedHeaderReceived(Exchange.CONTENT_TYPE, "text/plain");
076        binaryEndpoint.expectedHeaderReceived(Exchange.HTTP_RESPONSE_CODE, 200);
077
078        filteredEndpoint.expectedMessageCount(1);
079        filteredEndpoint.expectedHeaderReceived(Exchange.CONTENT_TYPE, "application/rdf+xml");
080        filteredEndpoint.expectedHeaderReceived(Exchange.HTTP_RESPONSE_CODE, 200);
081
082        deletedEndpoint.expectedMessageCount(4);
083        deletedEndpoint.allMessages().body().equals(null);
084        deletedEndpoint.expectedHeaderReceived(Exchange.HTTP_RESPONSE_CODE, 204);
085
086        goneEndpoint.expectedMessageCount(2);
087        goneEndpoint.expectedHeaderReceived(Exchange.HTTP_RESPONSE_CODE, 410);
088
089        notFoundEndpoint.expectedMessageCount(2);
090        notFoundEndpoint.expectedHeaderReceived(Exchange.HTTP_RESPONSE_CODE, 404);
091
092        final String binary = "/file";
093        final Map<String, Object> headers = new HashMap<>();
094        headers.put(Exchange.HTTP_METHOD, "POST");
095        headers.put(Exchange.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(FcrepoTestUtils.getFcrepoBaseUrl(), "");
104
105        headers.clear();
106        headers.put(Exchange.HTTP_METHOD, "PUT");
107        headers.put(Exchange.CONTENT_TYPE, "text/plain");
108        headers.put(FcrepoHeaders.FCREPO_IDENTIFIER, identifier + binary);
109
110        template.sendBodyAndHeaders("direct:create", FcrepoTestUtils.getTextDocument(), headers);
111
112
113        template.sendBodyAndHeader("direct:head", null, FcrepoHeaders.FCREPO_IDENTIFIER, identifier);
114        template.sendBodyAndHeader("direct:head", null, FcrepoHeaders.FCREPO_IDENTIFIER, identifier + binary);
115
116        template.sendBodyAndHeader("direct:delete", null, FcrepoHeaders.FCREPO_IDENTIFIER, identifier + binary);
117        template.sendBodyAndHeader("direct:delete", null, FcrepoHeaders.FCREPO_IDENTIFIER, identifier);
118
119        // Confirm that assertions passed
120        createdEndpoint.assertIsSatisfied();
121        filteredEndpoint.assertIsSatisfied();
122        binaryEndpoint.assertIsSatisfied();
123        goneEndpoint.assertIsSatisfied();
124        notFoundEndpoint.assertIsSatisfied();
125        deletedEndpoint.assertIsSatisfied();
126
127        // skip first message, as we've already extracted the body
128        Assert.assertEquals(FcrepoTestUtils.getFcrepoBaseUrl() + identifier + binary,
129                createdEndpoint.getExchanges().get(1).getIn().getBody(String.class));
130
131        // Additional assertions
132        // Check deleted container
133        for (Exchange exchange : goneEndpoint.getExchanges()) {
134            Assert.assertTrue(exchange.getIn().getHeader(Exchange.CONTENT_TYPE, String.class).contains("text/html"));
135            Assert.assertTrue(exchange.getIn().getBody(String.class).contains("Gone"));
136        }
137
138        for (Exchange exchange : notFoundEndpoint.getExchanges()) {
139            Assert.assertTrue(exchange.getIn().getHeader(Exchange.CONTENT_TYPE, String.class).contains("text/html"));
140            Assert.assertTrue(exchange.getIn().getBody(String.class).contains("Not Found"));
141        }
142     }
143
144    @Override
145    protected RouteBuilder createRouteBuilder() {
146        return new RouteBuilder() {
147            @Override
148            public void configure() {
149
150                final String fcrepo_uri = FcrepoTestUtils.getFcrepoEndpointUri();
151
152                final Namespaces ns = new Namespaces("rdf", RdfNamespaces.RDF);
153
154                from("direct:create")
155                    .to(fcrepo_uri)
156                    .to("mock:created");
157
158                from("direct:head")
159                    .to(fcrepo_uri)
160                    .filter().xpath(
161                        "/rdf:RDF/rdf:Description/rdf:type" +
162                        "[@rdf:resource='" + RdfNamespaces.REPOSITORY + "Binary']", ns)
163                    .to("mock:filter")
164                    .setHeader(Exchange.HTTP_METHOD, constant("HEAD"))
165                    .to(fcrepo_uri + "?metadata=false")
166                    .to("mock:binary");
167
168                 from("direct:delete")
169                    .setHeader(Exchange.HTTP_METHOD, constant("DELETE"))
170                    .to(fcrepo_uri)
171                    .to("mock:deleted")
172                    .setHeader(Exchange.HTTP_METHOD, constant("GET"))
173                    .to(fcrepo_uri + "?throwExceptionOnFailure=false")
174                    .to("mock:verify")
175                    .setHeader(Exchange.HTTP_METHOD, constant("DELETE"))
176                    .to(fcrepo_uri + "?tombstone=true")
177                    .to("mock:deleted")
178                    .setHeader(Exchange.HTTP_METHOD, constant("GET"))
179                    .to(fcrepo_uri + "?throwExceptionOnFailure=false")
180                    .to("mock:verify2");
181            }
182        };
183    }
184}