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;
017
018import static org.apache.camel.Exchange.HTTP_METHOD;
019import static org.apache.camel.Exchange.CONTENT_TYPE;
020import static org.apache.camel.Exchange.ACCEPT_CONTENT_TYPE;
021import static java.net.URLEncoder.encode;
022
023import java.io.IOException;
024import java.util.HashMap;
025import java.util.Map;
026
027import org.apache.camel.EndpointInject;
028import org.apache.camel.Produce;
029import org.apache.camel.ProducerTemplate;
030import org.apache.camel.builder.RouteBuilder;
031import org.apache.camel.component.mock.MockEndpoint;
032import org.apache.camel.test.junit4.CamelTestSupport;
033import org.fcrepo.camel.processor.SparqlDescribeProcessor;
034import org.junit.Test;
035
036/**
037 * Test adding a non-RDF resource
038 * @author Aaron Coburn
039 * @since November 7, 2014
040 */
041public class SparqlDescribeProcessorTest 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 missingHeaders() throws IOException, InterruptedException {
051        final Map<String, Object> headers = new HashMap<>();
052        headers.put(FcrepoHeaders.FCREPO_IDENTIFIER, "/foo");
053        template.sendBodyAndHeaders(null, headers);
054        resultEndpoint.expectedMessageCount(0);
055        resultEndpoint.assertIsSatisfied();
056    }
057
058    @Test
059    public void testDescribe() throws IOException, InterruptedException {
060        final String base = "http://localhost/rest";
061        final String path = "/path/a/b/c/d";
062
063        // Assertions
064        resultEndpoint.expectedBodiesReceived("query=" + encode("DESCRIBE <" + base + path + ">", "UTF-8"));
065        resultEndpoint.expectedHeaderReceived(CONTENT_TYPE, "application/x-www-form-urlencoded; charset=utf-8");
066        resultEndpoint.expectedHeaderReceived(HTTP_METHOD, "POST");
067        resultEndpoint.expectedHeaderReceived(ACCEPT_CONTENT_TYPE, "application/rdf+xml");
068
069        // Test
070        final Map<String, Object> headers = new HashMap<>();
071        headers.put(FcrepoHeaders.FCREPO_BASE_URL, base);
072        headers.put(FcrepoHeaders.FCREPO_IDENTIFIER, path);
073        template.sendBodyAndHeaders(null, headers);
074
075        headers.clear();
076        headers.put(JmsHeaders.BASE_URL, base);
077        headers.put(JmsHeaders.IDENTIFIER, path);
078        template.sendBodyAndHeaders(null, headers);
079
080        headers.clear();
081        headers.put(JmsHeaders.BASE_URL, base);
082        headers.put(FcrepoHeaders.FCREPO_IDENTIFIER, path);
083        template.sendBodyAndHeaders(null, headers);
084
085        headers.clear();
086        headers.put(FcrepoHeaders.FCREPO_BASE_URL, base);
087        headers.put(JmsHeaders.IDENTIFIER, path);
088        template.sendBodyAndHeaders(null, headers);
089
090        headers.clear();
091        headers.put(FcrepoHeaders.FCREPO_BASE_URL, base + path);
092        template.sendBodyAndHeaders(null, headers);
093
094        // Confirm that assertions passed
095        resultEndpoint.expectedMessageCount(5);
096        resultEndpoint.assertIsSatisfied();
097    }
098
099    @Override
100    protected RouteBuilder createRouteBuilder() {
101        return new RouteBuilder() {
102            @Override
103            public void configure() throws IOException {
104                onException(IOException.class)
105                    .handled(true);
106
107                from("direct:start")
108                    .process(new SparqlDescribeProcessor())
109                    .to("mock:result");
110            }
111        };
112    }
113}