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 static org.fcrepo.camel.integration.FcrepoTestUtils.getFcrepoBaseUrl;
019import static org.fcrepo.camel.integration.FcrepoTestUtils.getTextDocument;
020import static org.fcrepo.camel.integration.FcrepoTestUtils.getTurtleDocument;
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.fcrepo.camel.FcrepoComponent;
034import org.fcrepo.camel.FcrepoHeaders;
035import org.fcrepo.camel.JmsHeaders;
036import org.fcrepo.camel.RdfNamespaces;
037import org.junit.Assert;
038import org.junit.Before;
039import org.junit.Test;
040import org.junit.runner.RunWith;
041import org.springframework.test.context.ContextConfiguration;
042import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
043
044/**
045 * Test adding an RDF resource
046 * @author Aaron Coburn
047 * @since Dec 26, 2014
048 */
049@RunWith(SpringJUnit4ClassRunner.class)
050@ContextConfiguration({"/spring-test/test-container.xml"})
051public class FcrepoComponentConfigurationIT extends CamelTestSupport {
052
053    @EndpointInject(uri = "mock:created")
054    protected MockEndpoint createdEndpoint;
055
056    @EndpointInject(uri = "mock:filter")
057    protected MockEndpoint filteredEndpoint;
058
059    @EndpointInject(uri = "mock:container")
060    protected MockEndpoint containerEndpoint;
061
062    @EndpointInject(uri = "mock:verifyGone")
063    protected MockEndpoint goneEndpoint;
064
065    @EndpointInject(uri = "mock:deleted")
066    protected MockEndpoint deletedEndpoint;
067
068    @EndpointInject(uri = "mock:verifyNotFound")
069    protected MockEndpoint notFoundEndpoint;
070
071    @Produce(uri = "direct:filter")
072    protected ProducerTemplate template;
073
074    @Before
075    public void setUp() throws Exception {
076        super.setUp();
077    }
078
079    @Test
080    public void testGetContainer() throws InterruptedException {
081        // Assertions
082        createdEndpoint.expectedMessageCount(2);
083        createdEndpoint.expectedHeaderReceived(Exchange.HTTP_RESPONSE_CODE, 201);
084
085        containerEndpoint.expectedMessageCount(2);
086        containerEndpoint.expectedHeaderReceived(Exchange.CONTENT_TYPE, "application/rdf+xml");
087        containerEndpoint.expectedHeaderReceived(Exchange.HTTP_RESPONSE_CODE, 200);
088
089        filteredEndpoint.expectedMessageCount(2);
090        filteredEndpoint.expectedHeaderReceived(Exchange.CONTENT_TYPE, "application/rdf+xml");
091        filteredEndpoint.expectedHeaderReceived(Exchange.HTTP_RESPONSE_CODE, 200);
092
093        deletedEndpoint.expectedMessageCount(4);
094        deletedEndpoint.expectedBodiesReceived(null, null, null, null);
095        deletedEndpoint.expectedHeaderReceived(Exchange.HTTP_RESPONSE_CODE, 204);
096
097        goneEndpoint.expectedMessageCount(2);
098        goneEndpoint.expectedHeaderReceived(Exchange.HTTP_RESPONSE_CODE, 410);
099
100        notFoundEndpoint.expectedMessageCount(2);
101        notFoundEndpoint.expectedHeaderReceived(Exchange.HTTP_RESPONSE_CODE, 404);
102
103        final Map<String, Object> headers = new HashMap<>();
104        headers.put(Exchange.HTTP_METHOD, "POST");
105        headers.put(Exchange.CONTENT_TYPE, "text/turtle");
106
107        final String fullPath = template.requestBodyAndHeaders(
108                "direct:create", getTurtleDocument(), headers, String.class);
109
110        // Strip off the baseUrl to get the resource path
111        final String identifier = fullPath.replaceAll(getFcrepoBaseUrl(), "");
112        final String binary = "/file";
113
114        headers.clear();
115        headers.put(Exchange.HTTP_METHOD, "PUT");
116        headers.put(Exchange.CONTENT_TYPE, "text/plain");
117        headers.put(FcrepoHeaders.FCREPO_IDENTIFIER, identifier + binary);
118
119        template.sendBodyAndHeaders("direct:create", getTextDocument(), headers);
120
121        template.sendBodyAndHeader("direct:get", null, FcrepoHeaders.FCREPO_IDENTIFIER, identifier);
122        template.sendBodyAndHeader("direct:get", null, FcrepoHeaders.FCREPO_IDENTIFIER, identifier + binary);
123
124        template.sendBodyAndHeader("direct:get", null, JmsHeaders.IDENTIFIER, identifier);
125        template.sendBodyAndHeader("direct:get", null, JmsHeaders.IDENTIFIER, identifier + binary);
126
127        template.sendBodyAndHeader("direct:delete", null, FcrepoHeaders.FCREPO_IDENTIFIER, identifier + binary);
128        template.sendBodyAndHeader("direct:delete", null, FcrepoHeaders.FCREPO_IDENTIFIER, identifier);
129
130        // Confirm that assertions passed
131        createdEndpoint.assertIsSatisfied();
132        filteredEndpoint.assertIsSatisfied();
133        containerEndpoint.assertIsSatisfied();
134        goneEndpoint.assertIsSatisfied();
135        notFoundEndpoint.assertIsSatisfied();
136        deletedEndpoint.assertIsSatisfied();
137
138        // skip first message, as we've already extracted the body
139        Assert.assertEquals(getFcrepoBaseUrl() + identifier + binary,
140                createdEndpoint.getExchanges().get(1).getIn().getBody(String.class));
141
142        // Check deleted container
143        for (Exchange exchange : goneEndpoint.getExchanges()) {
144            Assert.assertTrue(exchange.getIn().getHeader(Exchange.CONTENT_TYPE, String.class).contains("text/html"));
145            Assert.assertTrue(exchange.getIn().getBody(String.class).contains("Gone"));
146        }
147
148        for (Exchange exchange : notFoundEndpoint.getExchanges()) {
149            Assert.assertTrue(exchange.getIn().getHeader(Exchange.CONTENT_TYPE, String.class).contains("text/html"));
150            Assert.assertTrue(exchange.getIn().getBody(String.class).contains("Not Found"));
151        }
152    }
153
154    @Override
155    protected RouteBuilder createRouteBuilder() {
156
157        final FcrepoComponent fcrepo = (FcrepoComponent)context.getComponent("fcrepo");
158        fcrepo.setBaseUrl(getFcrepoBaseUrl());
159
160        return new RouteBuilder() {
161            @Override
162            public void configure() {
163
164                final Namespaces ns = new Namespaces("rdf", RdfNamespaces.RDF);
165
166                from("direct:create")
167                    .to("fcrepo:foo")
168                    .to("mock:created");
169
170                // use an explicit scheme with the fcrepo: endpoint
171                from("direct:get")
172                    .to("fcrepo:bar")
173                    .filter().xpath(
174                        "/rdf:RDF/rdf:Description/rdf:type" +
175                        "[@rdf:resource='" + RdfNamespaces.REPOSITORY + "Container']", ns)
176                    .to("mock:filter")
177                    .to("fcrepo:baz")
178                    .to("mock:container");
179
180                from("direct:delete")
181                    .setHeader(Exchange.HTTP_METHOD, constant("DELETE"))
182                    .to("fcrepo:foo")
183                    .to("mock:deleted")
184                    .setHeader(Exchange.HTTP_METHOD, constant("GET"))
185                    .to("fcrepo:bar?throwExceptionOnFailure=false")
186                    .to("mock:verifyGone")
187                    .setHeader(Exchange.HTTP_METHOD, constant("DELETE"))
188                    .to("fcrepo:baz?tombstone=true")
189                    .to("mock:deleted")
190                    .setHeader(Exchange.HTTP_METHOD, constant("GET"))
191                    .to("fcrepo:foobar?throwExceptionOnFailure=false")
192                    .to("mock:verifyNotFound");
193            }
194        };
195    }
196}