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.component.mock.MockEndpoint;
027import org.apache.camel.test.junit4.CamelTestSupport;
028import org.fcrepo.camel.FcrepoHeaders;
029import org.junit.Test;
030import org.junit.runner.RunWith;
031import org.springframework.test.context.ContextConfiguration;
032import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
033
034/**
035 * Test the fcr:transform endpoint
036 * @author Aaron Coburn
037 * @since November 7, 2014
038 */
039@RunWith(SpringJUnit4ClassRunner.class)
040@ContextConfiguration({"/spring-test/test-container.xml"})
041public class FcrepoTransformIT extends CamelTestSupport {
042
043    @EndpointInject(uri = "mock:result")
044    protected MockEndpoint resultEndpoint;
045
046    @Produce(uri = "direct:start")
047    protected ProducerTemplate template;
048
049    @Test
050    public void testTransform() throws InterruptedException {
051
052        // Setup
053        final Map<String, Object> headers = new HashMap<>();
054        headers.put(Exchange.HTTP_METHOD, "POST");
055        headers.put(Exchange.CONTENT_TYPE, "text/turtle");
056
057        final String fullPath = template.requestBodyAndHeaders(
058                "direct:setup", FcrepoTestUtils.getTurtleDocument(), headers, String.class);
059
060        final String identifier = fullPath.replaceAll(FcrepoTestUtils.getFcrepoBaseUrl(), "");
061
062        // Test
063        template.sendBodyAndHeader(null, FcrepoHeaders.FCREPO_IDENTIFIER,
064                identifier);
065
066        final String ldpath = "id = . :: xsd:string ;\n" +
067            "title = dc:title :: xsd:string;";
068
069        headers.clear();
070        headers.put(FcrepoHeaders.FCREPO_IDENTIFIER, identifier);
071        headers.put(Exchange.CONTENT_TYPE, "application/rdf+ldpath");
072        headers.put(Exchange.HTTP_METHOD, "POST");
073        headers.put(Exchange.ACCEPT_CONTENT_TYPE, "application/json");
074        template.sendBodyAndHeaders("direct:post", ldpath, headers);
075
076        headers.clear();
077        headers.put(FcrepoHeaders.FCREPO_IDENTIFIER, identifier);
078        headers.put(Exchange.HTTP_METHOD, "GET");
079        headers.put(Exchange.ACCEPT_CONTENT_TYPE, "application/json");
080        template.sendBodyAndHeaders("direct:get", null, headers);
081
082        headers.clear();
083        headers.put(FcrepoHeaders.FCREPO_IDENTIFIER, identifier);
084        headers.put(FcrepoHeaders.FCREPO_TRANSFORM, "default");
085        headers.put(Exchange.HTTP_METHOD, "GET");
086        template.sendBodyAndHeaders("direct:get2", null, headers);
087
088        headers.clear();
089        headers.put(FcrepoHeaders.FCREPO_IDENTIFIER, identifier);
090        headers.put(FcrepoHeaders.FCREPO_TRANSFORM, "default");
091        template.sendBodyAndHeaders("direct:get2", null, headers);
092
093        headers.clear();
094        headers.put(FcrepoHeaders.FCREPO_IDENTIFIER, identifier);
095        headers.put(FcrepoHeaders.FCREPO_TRANSFORM, "");
096        template.sendBodyAndHeaders("direct:get", null, headers);
097
098
099        // Teardown
100        final Map<String, Object> teardownHeaders = new HashMap<>();
101        teardownHeaders.put(Exchange.HTTP_METHOD, "DELETE");
102        teardownHeaders.put(FcrepoHeaders.FCREPO_IDENTIFIER, identifier);
103        template.sendBodyAndHeaders("direct:teardown", null, teardownHeaders);
104
105        // Assertions
106        resultEndpoint.expectedMessageCount(6);
107        resultEndpoint.expectedHeaderReceived("Content-Type", "application/json");
108        resultEndpoint.assertIsSatisfied();
109    }
110
111    @Override
112    protected RouteBuilder createRouteBuilder() {
113        return new RouteBuilder() {
114            @Override
115            public void configure() {
116                final String fcrepo_uri = FcrepoTestUtils.getFcrepoEndpointUri();
117
118                from("direct:setup")
119                    .to(fcrepo_uri);
120
121                from("direct:start")
122                    .to(fcrepo_uri + "?accept=application/json&transform=default")
123                    .to("mock:result");
124
125                from("direct:get")
126                    .to(fcrepo_uri + "?transform=default")
127                    .to("mock:result");
128
129                from("direct:get2")
130                    .to(fcrepo_uri + "?transform=foo")
131                    .to("mock:result");
132
133
134                from("direct:post")
135                    .to(fcrepo_uri + "?transform=true")
136                    .to("mock:result");
137
138                from("direct:teardown")
139                    .to(fcrepo_uri)
140                    .to(fcrepo_uri + "?tombstone=true");
141            }
142        };
143    }
144}