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 deleting an RDF resource
039 * @author Aaron Coburn
040 * @since Dec 26, 2014
041 */
042@RunWith(SpringJUnit4ClassRunner.class)
043@ContextConfiguration({"/spring-test/test-container.xml"})
044public class FcrepoDeleteIT 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: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    @EndpointInject(uri = "mock:verifyNotFound")
062    protected MockEndpoint notFoundEndpoint;
063
064    @Produce(uri = "direct:filter")
065    protected ProducerTemplate template;
066
067    @Test
068    public void testDeleteContainer() throws InterruptedException {
069        // Assertions
070        createdEndpoint.expectedMessageCount(1);
071        createdEndpoint.expectedHeaderReceived(Exchange.HTTP_RESPONSE_CODE, 201);
072
073        containerEndpoint.expectedMessageCount(1);
074        containerEndpoint.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.expectedBodiesReceived(null, null);
082        deletedEndpoint.expectedHeaderReceived(Exchange.HTTP_RESPONSE_CODE, 204);
083
084        goneEndpoint.expectedMessageCount(1);
085        goneEndpoint.expectedHeaderReceived(Exchange.HTTP_RESPONSE_CODE, 410);
086
087        notFoundEndpoint.expectedMessageCount(1);
088        notFoundEndpoint.expectedHeaderReceived(Exchange.HTTP_RESPONSE_CODE, 404);
089
090        final Map<String, Object> headers = new HashMap<>();
091        headers.put(Exchange.HTTP_METHOD, "POST");
092        headers.put(Exchange.CONTENT_TYPE, "text/turtle");
093
094        final String fullPath = template.requestBodyAndHeaders(
095                "direct:create",
096                FcrepoTestUtils.getTurtleDocument(),
097                headers, String.class);
098
099        // Strip off the baseUrl to get the resource path
100        final String identifier = fullPath.replaceAll(FcrepoTestUtils.getFcrepoBaseUrl(), "");
101
102        template.sendBodyAndHeader("direct:get", null, FcrepoHeaders.FCREPO_IDENTIFIER, identifier);
103
104        template.sendBodyAndHeader("direct:delete", null, FcrepoHeaders.FCREPO_IDENTIFIER, identifier);
105
106        // Confirm that assertions passed
107        createdEndpoint.assertIsSatisfied();
108        filteredEndpoint.assertIsSatisfied();
109        containerEndpoint.assertIsSatisfied();
110        goneEndpoint.assertIsSatisfied();
111        notFoundEndpoint.assertIsSatisfied();
112        deletedEndpoint.assertIsSatisfied();
113
114        // Check deleted container
115        for (Exchange exchange : goneEndpoint.getExchanges()) {
116            Assert.assertTrue(exchange.getIn().getHeader(Exchange.CONTENT_TYPE, String.class).contains("text/html"));
117            Assert.assertTrue(exchange.getIn().getBody(String.class).contains("Gone"));
118        }
119
120        for (Exchange exchange : notFoundEndpoint.getExchanges()) {
121            Assert.assertTrue(exchange.getIn().getHeader(Exchange.CONTENT_TYPE, String.class).contains("text/html"));
122            Assert.assertTrue(exchange.getIn().getBody(String.class).contains("Not Found"));
123        }
124     }
125
126    @Test
127    public void testDeleteNestedContainer() throws InterruptedException {
128        // Assertions
129        createdEndpoint.expectedMessageCount(2);
130        createdEndpoint.expectedHeaderReceived(Exchange.HTTP_RESPONSE_CODE, 201);
131
132        containerEndpoint.expectedMessageCount(2);
133        containerEndpoint.expectedHeaderReceived(Exchange.HTTP_RESPONSE_CODE, 200);
134
135        filteredEndpoint.expectedMessageCount(2);
136        filteredEndpoint.expectedHeaderReceived(Exchange.CONTENT_TYPE, "application/rdf+xml");
137        filteredEndpoint.expectedHeaderReceived(Exchange.HTTP_RESPONSE_CODE, 200);
138
139        deletedEndpoint.expectedMessageCount(2);
140        deletedEndpoint.expectedBodiesReceived(null, null);
141        deletedEndpoint.expectedHeaderReceived(Exchange.HTTP_RESPONSE_CODE, 204);
142
143        goneEndpoint.expectedMessageCount(1);
144        goneEndpoint.expectedHeaderReceived(Exchange.HTTP_RESPONSE_CODE, 410);
145
146        notFoundEndpoint.expectedMessageCount(2);
147        notFoundEndpoint.expectedHeaderReceived(Exchange.HTTP_RESPONSE_CODE, 404);
148
149        final Map<String, Object> headers = new HashMap<>();
150        headers.put(Exchange.HTTP_METHOD, "POST");
151        headers.put(Exchange.CONTENT_TYPE, "text/turtle");
152
153        final String fullPath = template.requestBodyAndHeaders(
154                "direct:create",
155                FcrepoTestUtils.getTurtleDocument(),
156                headers, String.class);
157
158        // Strip off the baseUrl to get the resource path
159        final String identifier = fullPath.replaceAll(FcrepoTestUtils.getFcrepoBaseUrl(), "");
160        final String child = identifier + "/child";
161
162        headers.clear();
163        headers.put(FcrepoHeaders.FCREPO_IDENTIFIER, child);
164        headers.put(Exchange.HTTP_METHOD, "PUT");
165        headers.put(Exchange.CONTENT_TYPE, "text/turtle");
166
167        template.sendBodyAndHeaders("direct:create", FcrepoTestUtils.getTurtleDocument(), headers);
168
169        template.sendBodyAndHeader("direct:get", null, FcrepoHeaders.FCREPO_IDENTIFIER, identifier);
170        template.sendBodyAndHeader("direct:get", null, FcrepoHeaders.FCREPO_IDENTIFIER, child);
171
172        template.sendBodyAndHeader("direct:delete", null, FcrepoHeaders.FCREPO_IDENTIFIER, identifier);
173
174        template.sendBodyAndHeader("direct:getPostDelete", null, FcrepoHeaders.FCREPO_IDENTIFIER, child);
175
176        // Confirm that assertions passed
177        createdEndpoint.assertIsSatisfied();
178        filteredEndpoint.assertIsSatisfied();
179        containerEndpoint.assertIsSatisfied();
180        goneEndpoint.assertIsSatisfied();
181        notFoundEndpoint.assertIsSatisfied();
182        deletedEndpoint.assertIsSatisfied();
183
184        // Check deleted container
185        for (Exchange exchange : goneEndpoint.getExchanges()) {
186            Assert.assertTrue(exchange.getIn().getHeader(Exchange.CONTENT_TYPE, String.class).contains("text/html"));
187            Assert.assertTrue(exchange.getIn().getBody(String.class).contains("Gone"));
188        }
189
190        for (Exchange exchange : notFoundEndpoint.getExchanges()) {
191            Assert.assertTrue(exchange.getIn().getHeader(Exchange.CONTENT_TYPE, String.class).contains("text/html"));
192            Assert.assertTrue(exchange.getIn().getBody(String.class).contains("Not Found"));
193        }
194     }
195
196    @Override
197    protected RouteBuilder createRouteBuilder() {
198        return new RouteBuilder() {
199            @Override
200            public void configure() {
201
202                final String fcrepo_uri = FcrepoTestUtils.getFcrepoEndpointUri();
203
204                final Namespaces ns = new Namespaces("rdf", RdfNamespaces.RDF);
205
206                from("direct:create")
207                    .to(fcrepo_uri)
208                    .to("mock:created");
209
210                from("direct:get")
211                    .to(fcrepo_uri)
212                    .filter().xpath(
213                        "/rdf:RDF/rdf:Description/rdf:type" +
214                        "[@rdf:resource='" + RdfNamespaces.REPOSITORY + "Container']", ns)
215                    .to("mock:filter")
216                    .to(fcrepo_uri)
217                    .to("mock:container");
218
219                from("direct:delete")
220                    .setHeader(Exchange.HTTP_METHOD, constant("DELETE"))
221                    .to(fcrepo_uri)
222                    .to("mock:deleted")
223                    .setHeader(Exchange.HTTP_METHOD, constant("GET"))
224                    .to(fcrepo_uri + "?throwExceptionOnFailure=false")
225                    .to("mock:verifyGone")
226                    .setHeader(Exchange.HTTP_METHOD, constant("DELETE"))
227                    .to(fcrepo_uri + "?tombstone=true")
228                    .to("mock:deleted")
229                    .setHeader(Exchange.HTTP_METHOD, constant("GET"))
230                    .to(fcrepo_uri + "?throwExceptionOnFailure=false")
231                    .to("mock:verifyNotFound");
232
233                from("direct:getPostDelete")
234                    .to(fcrepo_uri + "?throwExceptionOnFailure=false")
235                    .to("mock:verifyNotFound");
236
237            }
238        };
239    }
240}