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