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.integration.FcrepoTestUtils.getFcrepoBaseUrl;
021import static org.fcrepo.camel.integration.FcrepoTestUtils.getTextDocument;
022import static org.fcrepo.camel.integration.FcrepoTestUtils.getTurtleDocument;
023import static org.junit.Assert.assertEquals;
024import static org.junit.Assert.assertTrue;
025
026import java.util.HashMap;
027import java.util.Map;
028
029import org.apache.camel.EndpointInject;
030import org.apache.camel.Exchange;
031import org.apache.camel.Produce;
032import org.apache.camel.ProducerTemplate;
033import org.apache.camel.builder.RouteBuilder;
034import org.apache.camel.builder.xml.Namespaces;
035import org.apache.camel.component.mock.MockEndpoint;
036import org.apache.camel.test.junit4.CamelTestSupport;
037import org.apache.jena.vocabulary.RDF;
038import org.fcrepo.camel.FcrepoComponent;
039import org.fcrepo.camel.FcrepoHeaders;
040import org.junit.Before;
041import org.junit.Test;
042
043/**
044 * Test adding an RDF resource
045 * @author Aaron Coburn
046 * @since Dec 26, 2014
047 */
048public class FcrepoComponentConfigurationIT extends CamelTestSupport {
049
050    private static final String REPOSITORY = "http://fedora.info/definitions/v4/repository#";
051
052    @EndpointInject(uri = "mock:created")
053    protected MockEndpoint createdEndpoint;
054
055    @EndpointInject(uri = "mock:filter")
056    protected MockEndpoint filteredEndpoint;
057
058    @EndpointInject(uri = "mock:container")
059    protected MockEndpoint containerEndpoint;
060
061    @EndpointInject(uri = "mock:verifyGone")
062    protected MockEndpoint goneEndpoint;
063
064    @EndpointInject(uri = "mock:deleted")
065    protected MockEndpoint deletedEndpoint;
066
067    @Produce(uri = "direct:filter")
068    protected ProducerTemplate template;
069
070    @Before
071    public void setUp() throws Exception {
072        super.setUp();
073    }
074
075    @Test
076    public void testGetContainer() throws InterruptedException {
077        // Assertions
078        createdEndpoint.expectedMessageCount(2);
079        createdEndpoint.expectedHeaderReceived(Exchange.HTTP_RESPONSE_CODE, 201);
080
081        containerEndpoint.expectedMessageCount(1);
082        containerEndpoint.expectedHeaderReceived(Exchange.CONTENT_TYPE, "application/rdf+xml");
083        containerEndpoint.expectedHeaderReceived(Exchange.HTTP_RESPONSE_CODE, 200);
084
085        filteredEndpoint.expectedMessageCount(1);
086        filteredEndpoint.expectedHeaderReceived(Exchange.CONTENT_TYPE, "application/rdf+xml");
087        filteredEndpoint.expectedHeaderReceived(Exchange.HTTP_RESPONSE_CODE, 200);
088
089        deletedEndpoint.expectedMessageCount(2);
090        deletedEndpoint.expectedBodiesReceived(null, null);
091        deletedEndpoint.expectedHeaderReceived(Exchange.HTTP_RESPONSE_CODE, 204);
092
093        goneEndpoint.expectedMessageCount(2);
094        goneEndpoint.expectedHeaderReceived(Exchange.HTTP_RESPONSE_CODE, 410);
095
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 fullPath = template.requestBodyAndHeaders(
101                "direct:create", getTurtleDocument(), headers, String.class);
102
103        // Strip off the baseUrl to get the resource path
104        final String identifier = fullPath.replaceAll(getFcrepoBaseUrl(), "");
105        final String binary = "/file";
106
107        headers.clear();
108        headers.put(Exchange.HTTP_METHOD, "PUT");
109        headers.put(Exchange.CONTENT_TYPE, "text/plain");
110        headers.put(FcrepoHeaders.FCREPO_IDENTIFIER, identifier + binary);
111
112        template.sendBodyAndHeaders("direct:create", getTextDocument(), headers);
113
114        template.sendBodyAndHeader("direct:get", null, FcrepoHeaders.FCREPO_IDENTIFIER, identifier);
115        template.sendBodyAndHeader("direct:get", null, FcrepoHeaders.FCREPO_IDENTIFIER, identifier + binary);
116
117        template.sendBodyAndHeader("direct:delete", null, FcrepoHeaders.FCREPO_IDENTIFIER, identifier + binary);
118        template.sendBodyAndHeader("direct:delete", null, FcrepoHeaders.FCREPO_IDENTIFIER, identifier);
119
120        // Confirm that assertions passed
121        createdEndpoint.assertIsSatisfied();
122        filteredEndpoint.assertIsSatisfied();
123        containerEndpoint.assertIsSatisfied();
124        goneEndpoint.assertIsSatisfied();
125        deletedEndpoint.assertIsSatisfied();
126
127        // skip first message, as we've already extracted the body
128        assertEquals(getFcrepoBaseUrl() + identifier + binary,
129                createdEndpoint.getExchanges().get(1).getIn().getBody(String.class));
130
131        // Check deleted container
132        goneEndpoint.getExchanges().forEach(exchange -> {
133            assertTrue(exchange.getIn().getHeader(Exchange.CONTENT_TYPE, String.class).contains("application/rdf+xml"));
134        });
135    }
136
137    @Override
138    protected RouteBuilder createRouteBuilder() {
139
140        final FcrepoComponent fcrepo = (FcrepoComponent)context.getComponent("fcrepo");
141        fcrepo.setBaseUrl(getFcrepoBaseUrl());
142
143        return new RouteBuilder() {
144            @Override
145            public void configure() {
146
147                final Namespaces ns = new Namespaces("rdf", RDF.uri);
148
149                from("direct:create")
150                    .to("fcrepo:foo")
151                    .to("mock:created");
152
153                // use an explicit scheme with the fcrepo: endpoint
154                from("direct:get")
155                    .to("fcrepo:bar")
156                    .filter().xpath(
157                        "/rdf:RDF/rdf:Description/rdf:type" +
158                        "[@rdf:resource='" + REPOSITORY + "Container']", ns)
159                    .to("mock:filter")
160                    .to("fcrepo:baz")
161                    .to("mock:container");
162
163                from("direct:delete")
164                    .setHeader(Exchange.HTTP_METHOD, constant("DELETE"))
165                    .to("fcrepo:foo")
166                    .to("mock:deleted")
167                    .setHeader(Exchange.HTTP_METHOD, constant("GET"))
168                    .to("fcrepo:bar?throwExceptionOnFailure=false")
169                    .to("mock:verifyGone");
170            }
171        };
172    }
173}