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.JmsHeaders;
031import org.fcrepo.camel.RdfNamespaces;
032import org.junit.Assert;
033import org.junit.Test;
034import org.junit.runner.RunWith;
035import org.springframework.test.context.ContextConfiguration;
036import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
037
038/**
039 * Test adding an RDF resource
040 * @author Aaron Coburn
041 * @since Dec 26, 2014
042 */
043@RunWith(SpringJUnit4ClassRunner.class)
044@ContextConfiguration({"/spring-test/test-container.xml"})
045public class FcrepoContainerPreferIT extends CamelTestSupport {
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:container")
054    protected MockEndpoint containerEndpoint;
055
056    @EndpointInject(uri = "mock:verifyGone")
057    protected MockEndpoint goneEndpoint;
058
059    @EndpointInject(uri = "mock:deleted")
060    protected MockEndpoint deletedEndpoint;
061
062    @EndpointInject(uri = "mock:verifyNotFound")
063    protected MockEndpoint notFoundEndpoint;
064
065    @Produce(uri = "direct:filter")
066    protected ProducerTemplate template;
067
068    @Test
069    public void testGetContainer() throws InterruptedException {
070        // Assertions
071        createdEndpoint.expectedMessageCount(2);
072        createdEndpoint.expectedHeaderReceived(Exchange.HTTP_RESPONSE_CODE, 201);
073
074        containerEndpoint.expectedMessageCount(1);
075        containerEndpoint.expectedHeaderReceived(Exchange.CONTENT_TYPE, "application/rdf+xml");
076        containerEndpoint.expectedHeaderReceived(Exchange.HTTP_RESPONSE_CODE, 200);
077
078        filteredEndpoint.expectedMessageCount(4);
079        filteredEndpoint.expectedHeaderReceived(Exchange.CONTENT_TYPE, "application/rdf+xml");
080        filteredEndpoint.expectedHeaderReceived(Exchange.HTTP_RESPONSE_CODE, 200);
081
082        deletedEndpoint.expectedMessageCount(4);
083        deletedEndpoint.expectedBodiesReceived(null, null, null, 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 Map<String, Object> headers = new HashMap<>();
093        headers.put(Exchange.HTTP_METHOD, "POST");
094        headers.put(Exchange.CONTENT_TYPE, "text/turtle");
095
096        final String fullPath = template.requestBodyAndHeaders(
097                "direct:create",
098                FcrepoTestUtils.getTurtleDocument(),
099                headers, String.class);
100
101        // Strip off the baseUrl to get the resource path
102        final String identifier = fullPath.replaceAll(FcrepoTestUtils.getFcrepoBaseUrl(), "");
103
104        final String child = "/child";
105
106        headers.clear();
107        headers.put(Exchange.HTTP_METHOD, "PUT");
108        headers.put(Exchange.CONTENT_TYPE, "text/turtle");
109        headers.put(FcrepoHeaders.FCREPO_IDENTIFIER, identifier + child);
110
111        template.sendBodyAndHeaders("direct:create", FcrepoTestUtils.getTurtleDocument(), headers);
112
113        template.sendBodyAndHeader("direct:includeServerManaged", null, FcrepoHeaders.FCREPO_IDENTIFIER,
114                identifier);
115        template.sendBodyAndHeader("direct:includeContainment", null, FcrepoHeaders.FCREPO_IDENTIFIER,
116                identifier);
117
118        template.sendBodyAndHeader("direct:omitServerManaged", null, JmsHeaders.IDENTIFIER,
119                identifier);
120        template.sendBodyAndHeader("direct:omitContainmentShort", null, FcrepoHeaders.FCREPO_IDENTIFIER,
121                identifier);
122        template.sendBodyAndHeader("direct:omitContainmentFull", null, FcrepoHeaders.FCREPO_IDENTIFIER,
123                identifier);
124
125        template.sendBodyAndHeader("direct:includeContainmentOmitManaged", null, FcrepoHeaders.FCREPO_IDENTIFIER,
126                identifier);
127
128        headers.clear();
129        headers.put(FcrepoHeaders.FCREPO_IDENTIFIER, identifier);
130        headers.put(FcrepoHeaders.FCREPO_PREFER, "return=representation; " +
131                    "omit=\"http://fedora.info/definitions/v4/repository#ServerManaged\"; " +
132                    "include=\"http://www.w3.org/ns/ldp#PreferContainment\";");
133        template.sendBodyAndHeaders("direct:preferHeadersCheckContainment", null, headers);
134        template.sendBodyAndHeaders("direct:preferHeadersCheckServerManaged", null, headers);
135
136        headers.put(FcrepoHeaders.FCREPO_PREFER, "return=representation; " +
137                    "omit=\"http://fedora.info/definitions/v4/repository#ServerManaged " +
138                    "http://www.w3.org/ns/ldp#PreferContainment\"");
139        template.sendBodyAndHeaders("direct:preferHeadersCheckContainment", null, headers);
140        template.sendBodyAndHeaders("direct:preferHeadersCheckServerManaged", null, headers);
141
142        template.sendBodyAndHeader("direct:delete", null, FcrepoHeaders.FCREPO_IDENTIFIER, identifier + child);
143        template.sendBodyAndHeader("direct:delete", null, FcrepoHeaders.FCREPO_IDENTIFIER, identifier);
144
145        // Confirm that assertions passed
146        createdEndpoint.assertIsSatisfied();
147        filteredEndpoint.assertIsSatisfied();
148        containerEndpoint.assertIsSatisfied();
149        goneEndpoint.assertIsSatisfied();
150        notFoundEndpoint.assertIsSatisfied();
151        deletedEndpoint.assertIsSatisfied();
152
153        // Check deleted container
154        for (Exchange exchange : goneEndpoint.getExchanges()) {
155            Assert.assertTrue(exchange.getIn().getHeader(Exchange.CONTENT_TYPE, String.class).contains("text/html"));
156            Assert.assertTrue(exchange.getIn().getBody(String.class).contains("Gone"));
157        }
158
159        for (Exchange exchange : notFoundEndpoint.getExchanges()) {
160            Assert.assertTrue(exchange.getIn().getHeader(Exchange.CONTENT_TYPE, String.class).contains("text/html"));
161            Assert.assertTrue(exchange.getIn().getBody(String.class).contains("Not Found"));
162        }
163    }
164
165    @Override
166    protected RouteBuilder createRouteBuilder() {
167        return new RouteBuilder() {
168            @Override
169            public void configure() {
170
171                final String fcrepo_uri = FcrepoTestUtils.getFcrepoEndpointUri();
172
173                final Namespaces ns = new Namespaces("rdf", RdfNamespaces.RDF);
174                ns.add("fedora", RdfNamespaces.REPOSITORY);
175                ns.add("ldp", RdfNamespaces.LDP);
176
177                from("direct:create")
178                    .to(fcrepo_uri)
179                    .to("mock:created");
180
181                from("direct:preferHeadersCheckContainment")
182                    .to(fcrepo_uri)
183                    .filter().xpath(
184                        "/rdf:RDF/rdf:Description/ldp:contains", ns)
185                    .to("mock:filter");
186
187                from("direct:preferHeadersCheckServerManaged")
188                    .to(fcrepo_uri)
189                    .filter().xpath(
190                        "/rdf:RDF/rdf:Description/fedora:created", ns)
191                    .to("mock:filter");
192
193                from("direct:includeServerManaged")
194                    .to(fcrepo_uri + "?preferInclude=ServerManaged")
195                    .filter().xpath(
196                        "/rdf:RDF/rdf:Description/fedora:created", ns)
197                    .to("mock:filter")
198                    .to(fcrepo_uri)
199                    .to("mock:container");
200
201                from("direct:includeContainmentOmitManaged")
202                    .to(fcrepo_uri + "?preferOmit=ServerManaged&preferInclude=PreferContainment")
203                    .filter().xpath(
204                        "/rdf:RDF/rdf:Description/ldp:contains", ns)
205                    .to("mock:filter");
206
207                from("direct:omitServerManaged")
208                    .to(fcrepo_uri + "?preferOmit=ServerManaged")
209                    .filter().xpath(
210                        "/rdf:RDF/rdf:Description/fedora:created", ns)
211                    .to("mock:filter")
212                    .to(fcrepo_uri)
213                    .to("mock:container");
214
215                from("direct:includeContainment")
216                    .to(fcrepo_uri + "?preferInclude=PreferContainment")
217                    .filter().xpath(
218                            "/rdf:RDF/rdf:Description/ldp:contains", ns)
219                    .to("mock:filter");
220
221                from("direct:omitContainmentShort")
222                    .to(fcrepo_uri + "?preferOmit=PreferContainment")
223                    .filter().xpath(
224                            "/rdf:RDF/rdf:Description/ldp:contains", ns)
225                    .to("mock:filter");
226
227                from("direct:omitContainmentFull")
228                    .to(fcrepo_uri + "?preferOmit=http://www.w3.org/ns/ldp#PreferContainment")
229                    .filter().xpath(
230                            "/rdf:RDF/rdf:Description/ldp:contains", ns)
231                    .to("mock:filter");
232
233                from("direct:delete")
234                    .setHeader(Exchange.HTTP_METHOD, constant("DELETE"))
235                    .to(fcrepo_uri)
236                    .to("mock:deleted")
237                    .setHeader(Exchange.HTTP_METHOD, constant("GET"))
238                    .to(fcrepo_uri + "?throwExceptionOnFailure=false")
239                    .to("mock:verifyGone")
240                    .setHeader(Exchange.HTTP_METHOD, constant("DELETE"))
241                    .to(fcrepo_uri + "?tombstone=true")
242                    .to("mock:deleted")
243                    .setHeader(Exchange.HTTP_METHOD, constant("GET"))
244                    .to(fcrepo_uri + "?throwExceptionOnFailure=false")
245                    .to("mock:verifyNotFound");
246            }
247        };
248    }
249}