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.fcrepo.camel.FcrepoHeaders.FCREPO_URI;
021import static org.junit.Assert.assertEquals;
022import static org.junit.Assert.assertTrue;
023
024import java.util.HashMap;
025import java.util.Map;
026
027import org.apache.camel.EndpointInject;
028import org.apache.camel.Exchange;
029import org.apache.camel.Produce;
030import org.apache.camel.ProducerTemplate;
031import org.apache.camel.builder.RouteBuilder;
032import org.apache.camel.builder.xml.Namespaces;
033import org.apache.camel.component.mock.MockEndpoint;
034import org.apache.camel.test.junit4.CamelTestSupport;
035import org.apache.jena.vocabulary.RDF;
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 FcrepoBinaryGetIT extends CamelTestSupport {
044
045    private static final String REPOSITORY = "http://fedora.info/definitions/v4/repository#";
046
047    private static final String PREMIS = "http://www.loc.gov/premis/rdf/v1#";
048
049    @EndpointInject(uri = "mock:created")
050    protected MockEndpoint createdEndpoint;
051
052    @EndpointInject(uri = "mock:filter")
053    protected MockEndpoint filteredEndpoint;
054
055    @EndpointInject(uri = "mock:binary")
056    protected MockEndpoint binaryEndpoint;
057
058    @EndpointInject(uri = "mock:verifyGone")
059    protected MockEndpoint goneEndpoint;
060
061    @EndpointInject(uri = "mock:deleted")
062    protected MockEndpoint deletedEndpoint;
063
064    @EndpointInject(uri = "mock:fixity")
065    protected MockEndpoint fixityEndpoint;
066
067    @Produce(uri = "direct:filter")
068    protected ProducerTemplate template;
069
070    @Test
071    public void testGetBinary() throws InterruptedException {
072        // Assertions
073        createdEndpoint.expectedMessageCount(2);
074        createdEndpoint.expectedHeaderReceived(Exchange.HTTP_RESPONSE_CODE, 201);
075
076        binaryEndpoint.expectedBodiesReceived(FcrepoTestUtils.getTextDocument());
077        binaryEndpoint.expectedMessageCount(1);
078        binaryEndpoint.expectedHeaderReceived(Exchange.CONTENT_TYPE, "text/plain");
079        binaryEndpoint.expectedHeaderReceived(Exchange.HTTP_RESPONSE_CODE, 200);
080
081        filteredEndpoint.expectedMessageCount(1);
082        filteredEndpoint.expectedHeaderReceived(Exchange.CONTENT_TYPE, "application/rdf+xml");
083        filteredEndpoint.expectedHeaderReceived(Exchange.HTTP_RESPONSE_CODE, 200);
084
085        deletedEndpoint.expectedMessageCount(2);
086        deletedEndpoint.expectedBodiesReceived(null, null);
087        deletedEndpoint.expectedHeaderReceived(Exchange.HTTP_RESPONSE_CODE, 204);
088
089        goneEndpoint.expectedMessageCount(2);
090        goneEndpoint.expectedHeaderReceived(Exchange.HTTP_RESPONSE_CODE, 410);
091
092        fixityEndpoint.expectedMessageCount(3);
093        fixityEndpoint.expectedHeaderReceived(Exchange.HTTP_RESPONSE_CODE, 200);
094
095        final String binary = "/file";
096        final Map<String, Object> headers = new HashMap<>();
097        headers.put(Exchange.HTTP_METHOD, "POST");
098        headers.put(Exchange.CONTENT_TYPE, "text/turtle");
099
100        final String uri = template.requestBodyAndHeaders(
101                "direct:create",
102                FcrepoTestUtils.getTurtleDocument(),
103                headers, String.class);
104
105        headers.clear();
106        headers.put(Exchange.HTTP_METHOD, "PUT");
107        headers.put(Exchange.CONTENT_TYPE, "text/plain");
108        headers.put(FCREPO_URI, uri + binary);
109
110        template.sendBodyAndHeaders("direct:create", FcrepoTestUtils.getTextDocument(), headers);
111
112        template.sendBodyAndHeader("direct:get", null, FCREPO_URI, uri);
113        template.sendBodyAndHeader("direct:get", null, FCREPO_URI, uri + binary);
114
115        template.sendBodyAndHeader("direct:getFixity", null, FCREPO_URI, uri + binary);
116
117        template.sendBodyAndHeader("direct:delete", null, FCREPO_URI, uri + binary);
118        template.sendBodyAndHeader("direct:delete", null, FCREPO_URI, uri);
119
120
121        // Confirm that assertions passed
122        createdEndpoint.assertIsSatisfied();
123        filteredEndpoint.assertIsSatisfied();
124        binaryEndpoint.assertIsSatisfied();
125        goneEndpoint.assertIsSatisfied();
126        deletedEndpoint.assertIsSatisfied();
127        fixityEndpoint.assertIsSatisfied();
128
129        // Additional assertions
130        // skip first message, as we've already extracted the body
131        assertEquals(uri + binary,
132                createdEndpoint.getExchanges().get(1).getIn().getBody(String.class));
133
134        // Check deleted container
135        goneEndpoint.getExchanges().forEach(exchange -> {
136            assertTrue(exchange.getIn().getHeader(Exchange.CONTENT_TYPE, String.class).contains("application/rdf+xml"));
137        });
138    }
139
140    @Override
141    protected RouteBuilder createRouteBuilder() {
142        return new RouteBuilder() {
143            @Override
144            public void configure() {
145
146                final String fcrepo_uri = FcrepoTestUtils.getFcrepoEndpointUri();
147
148                final Namespaces ns = new Namespaces("rdf", RDF.uri);
149                ns.add("premis", PREMIS);
150                ns.add("fedora", REPOSITORY);
151
152                from("direct:create")
153                    .to(fcrepo_uri)
154                    .to("mock:created");
155
156                from("direct:get")
157                    .to(fcrepo_uri)
158                    .filter().xpath(
159                        "/rdf:RDF/rdf:Description/rdf:type" +
160                        "[@rdf:resource='" + REPOSITORY + "Binary']", ns)
161                    .to("mock:filter")
162                    .to(fcrepo_uri + "?metadata=false")
163                    .to("mock:binary");
164
165                from("direct:getFixity")
166                    .to(fcrepo_uri + "?fixity=true")
167                    .filter().xpath(
168                        "/rdf:RDF/rdf:Description/rdf:type" +
169                        "[@rdf:resource='" + PREMIS + "Fixity']", ns)
170                    .to("mock:fixity")
171                    .filter().xpath(
172                        "/rdf:RDF/rdf:Description/premis:hasMessageDigest" +
173                        "[@rdf:resource='urn:sha1:12f68888e3beff267deae42ea86058c9c0565e36']", ns)
174                    .to("mock:fixity")
175                    .filter().xpath(
176                        "/rdf:RDF/rdf:Description/premis:hasEventOutcome" +
177                        "[text()='SUCCESS']", ns)
178                    .to("mock:fixity");
179
180                from("direct:delete")
181                    .setHeader(Exchange.HTTP_METHOD, constant("DELETE"))
182                    .to(fcrepo_uri)
183                    .to("mock:deleted")
184                    .setHeader(Exchange.HTTP_METHOD, constant("GET"))
185                    .to(fcrepo_uri + "?throwExceptionOnFailure=false")
186                    .to("mock:verifyGone");
187            }
188        };
189    }
190}