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.assertTrue;
021
022import java.util.HashMap;
023import java.util.Map;
024
025import org.apache.camel.EndpointInject;
026import org.apache.camel.Exchange;
027import org.apache.camel.Produce;
028import org.apache.camel.ProducerTemplate;
029import org.apache.camel.builder.RouteBuilder;
030import org.apache.camel.builder.xml.Namespaces;
031import org.apache.camel.component.mock.MockEndpoint;
032import org.apache.camel.test.junit4.CamelTestSupport;
033import org.apache.jena.vocabulary.RDF;
034import org.fcrepo.camel.FcrepoHeaders;
035import org.junit.Test;
036
037/**
038 * Test deleting an RDF resource
039 * @author Aaron Coburn
040 * @since Dec 26, 2014
041 */
042public class FcrepoDeleteIT extends CamelTestSupport {
043
044    private static final String REPOSITORY = "http://fedora.info/definitions/v4/repository#";
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:container")
053    protected MockEndpoint containerEndpoint;
054
055    @EndpointInject(uri = "mock:verifyGone")
056    protected MockEndpoint goneEndpoint;
057
058    @EndpointInject(uri = "mock:deleted")
059    protected MockEndpoint deletedEndpoint;
060
061    @Produce(uri = "direct:filter")
062    protected ProducerTemplate template;
063
064    @Test
065    public void testDeleteContainer() throws InterruptedException {
066        // Assertions
067        createdEndpoint.expectedMessageCount(1);
068        createdEndpoint.expectedHeaderReceived(Exchange.HTTP_RESPONSE_CODE, 201);
069
070        containerEndpoint.expectedMessageCount(1);
071        containerEndpoint.expectedHeaderReceived(Exchange.HTTP_RESPONSE_CODE, 200);
072
073        filteredEndpoint.expectedMessageCount(1);
074        filteredEndpoint.expectedHeaderReceived(Exchange.CONTENT_TYPE, "application/rdf+xml");
075        filteredEndpoint.expectedHeaderReceived(Exchange.HTTP_RESPONSE_CODE, 200);
076
077        deletedEndpoint.expectedMessageCount(1);
078        deletedEndpoint.allMessages().body().equals(null);
079        deletedEndpoint.expectedHeaderReceived(Exchange.HTTP_RESPONSE_CODE, 204);
080
081        goneEndpoint.expectedMessageCount(1);
082        goneEndpoint.expectedHeaderReceived(Exchange.HTTP_RESPONSE_CODE, 410);
083
084        final Map<String, Object> headers = new HashMap<>();
085        headers.put(Exchange.HTTP_METHOD, "POST");
086        headers.put(Exchange.CONTENT_TYPE, "text/turtle");
087
088        final String fullPath = template.requestBodyAndHeaders(
089                "direct:create",
090                FcrepoTestUtils.getTurtleDocument(),
091                headers, String.class);
092
093        // Strip off the baseUrl to get the resource path
094        final String identifier = fullPath.replaceAll(FcrepoTestUtils.getFcrepoBaseUrl(), "");
095
096        template.sendBodyAndHeader("direct:get", null, FcrepoHeaders.FCREPO_IDENTIFIER, identifier);
097
098        template.sendBodyAndHeader("direct:delete", null, FcrepoHeaders.FCREPO_IDENTIFIER, identifier);
099
100        // Confirm that assertions passed
101        createdEndpoint.assertIsSatisfied();
102        filteredEndpoint.assertIsSatisfied();
103        containerEndpoint.assertIsSatisfied();
104        goneEndpoint.assertIsSatisfied();
105        deletedEndpoint.assertIsSatisfied();
106
107        // Check deleted container
108        goneEndpoint.getExchanges().forEach(exchange -> {
109            assertTrue(exchange.getIn().getHeader(Exchange.CONTENT_TYPE, String.class).contains("application/rdf+xml"));
110        });
111     }
112
113    @Test
114    public void testDeleteNestedContainer() throws InterruptedException {
115        // Assertions
116        createdEndpoint.expectedMessageCount(2);
117        createdEndpoint.expectedHeaderReceived(Exchange.HTTP_RESPONSE_CODE, 201);
118
119        containerEndpoint.expectedMessageCount(2);
120        containerEndpoint.expectedHeaderReceived(Exchange.HTTP_RESPONSE_CODE, 200);
121
122        filteredEndpoint.expectedMessageCount(2);
123        filteredEndpoint.expectedHeaderReceived(Exchange.CONTENT_TYPE, "application/rdf+xml");
124        filteredEndpoint.expectedHeaderReceived(Exchange.HTTP_RESPONSE_CODE, 200);
125
126        deletedEndpoint.expectedMessageCount(1);
127        deletedEndpoint.expectedHeaderReceived(Exchange.HTTP_RESPONSE_CODE, 204);
128        deletedEndpoint.allMessages().body().equals(null);
129
130        goneEndpoint.expectedMessageCount(2);
131        goneEndpoint.expectedHeaderReceived(Exchange.HTTP_RESPONSE_CODE, 410);
132
133        final Map<String, Object> headers = new HashMap<>();
134        headers.put(Exchange.HTTP_METHOD, "POST");
135        headers.put(Exchange.CONTENT_TYPE, "text/turtle");
136
137        final String fullPath = template.requestBodyAndHeaders(
138                "direct:create",
139                FcrepoTestUtils.getTurtleDocument(),
140                headers, String.class);
141
142        // Strip off the baseUrl to get the resource path
143        final String identifier = fullPath.replaceAll(FcrepoTestUtils.getFcrepoBaseUrl(), "");
144        final String child = identifier + "/child";
145
146        headers.clear();
147        headers.put(FcrepoHeaders.FCREPO_IDENTIFIER, child);
148        headers.put(Exchange.HTTP_METHOD, "PUT");
149        headers.put(Exchange.CONTENT_TYPE, "text/turtle");
150
151        template.sendBodyAndHeaders("direct:create", FcrepoTestUtils.getTurtleDocument(), headers);
152
153        template.sendBodyAndHeader("direct:get", null, FcrepoHeaders.FCREPO_IDENTIFIER, identifier);
154        template.sendBodyAndHeader("direct:get", null, FcrepoHeaders.FCREPO_IDENTIFIER, child);
155
156        template.sendBodyAndHeader("direct:delete", null, FcrepoHeaders.FCREPO_IDENTIFIER, identifier);
157
158        template.sendBodyAndHeader("direct:getPostDelete", null, FcrepoHeaders.FCREPO_IDENTIFIER, child);
159
160        // Confirm that assertions passed
161        createdEndpoint.assertIsSatisfied();
162        filteredEndpoint.assertIsSatisfied();
163        containerEndpoint.assertIsSatisfied();
164        goneEndpoint.assertIsSatisfied();
165        deletedEndpoint.assertIsSatisfied();
166
167        // Check deleted container
168        goneEndpoint.getExchanges().forEach(exchange -> {
169            assertTrue(exchange.getIn().getHeader(Exchange.CONTENT_TYPE, String.class).contains("application/rdf+xml"));
170        });
171     }
172
173    @Override
174    protected RouteBuilder createRouteBuilder() {
175        return new RouteBuilder() {
176            @Override
177            public void configure() {
178
179                final String fcrepo_uri = FcrepoTestUtils.getFcrepoEndpointUri();
180
181                final Namespaces ns = new Namespaces("rdf", RDF.uri);
182
183                from("direct:create")
184                    .to(fcrepo_uri)
185                    .to("mock:created");
186
187                from("direct:get")
188                    .to(fcrepo_uri)
189                    .filter().xpath(
190                        "/rdf:RDF/rdf:Description/rdf:type" +
191                        "[@rdf:resource='" + REPOSITORY + "Container']", ns)
192                    .to("mock:filter")
193                    .to(fcrepo_uri)
194                    .to("mock:container");
195
196                from("direct:delete")
197                    .setHeader(Exchange.HTTP_METHOD, constant("DELETE"))
198                    .to(fcrepo_uri)
199                    .to("mock:deleted")
200                    .setHeader(Exchange.HTTP_METHOD, constant("GET"))
201                    .to(fcrepo_uri + "?throwExceptionOnFailure=false")
202                    .to("mock:verifyGone");
203
204                from("direct:getPostDelete")
205                    .to(fcrepo_uri + "?throwExceptionOnFailure=false")
206                    .to("mock:verifyGone");
207
208            }
209        };
210    }
211}