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;
019
020import static java.net.URLEncoder.encode;
021import static org.apache.camel.Exchange.HTTP_METHOD;
022import static org.apache.camel.Exchange.CONTENT_TYPE;
023import static org.fcrepo.camel.FcrepoHeaders.FCREPO_BASE_URL;
024import static org.fcrepo.camel.FcrepoHeaders.FCREPO_IDENTIFIER;
025import static org.fcrepo.camel.FcrepoHeaders.FCREPO_URI;
026import static org.junit.Assert.assertTrue;
027
028import java.io.IOException;
029
030import org.apache.camel.EndpointInject;
031import org.apache.camel.Exchange;
032import org.apache.camel.NoSuchHeaderException;
033import org.apache.camel.Produce;
034import org.apache.camel.ProducerTemplate;
035import org.apache.camel.builder.RouteBuilder;
036import org.apache.camel.component.mock.MockEndpoint;
037import org.apache.camel.impl.DefaultExchange;
038import org.apache.camel.test.junit4.CamelTestSupport;
039import org.fcrepo.camel.processor.SparqlDescribeProcessor;
040import org.junit.Test;
041
042/**
043 * Test adding a non-RDF resource
044 * @author Aaron Coburn
045 * @since November 7, 2014
046 */
047public class SparqlDescribeProcessorTest extends CamelTestSupport {
048
049    @EndpointInject(uri = "mock:result")
050    protected MockEndpoint resultEndpoint;
051
052    @Produce(uri = "direct:start")
053    protected ProducerTemplate template;
054
055    @Test
056    public void missingHeaders() throws IOException, InterruptedException {
057        final Exchange in = new DefaultExchange(context());
058        in.getIn().setHeader(FCREPO_IDENTIFIER, "/foo");
059        final Exchange out = template.send(in);
060        assertTrue(out.isFailed());
061        assertTrue(out.getException() instanceof NoSuchHeaderException);
062    }
063
064    @Test
065    public void testDescribe() throws IOException, InterruptedException {
066        final String uri = "http://localhost/rest/path/a/b/c/d";
067
068        // Assertions
069        resultEndpoint.expectedBodiesReceived("query=" + encode("DESCRIBE <" + uri + ">", "UTF-8"));
070        resultEndpoint.expectedHeaderReceived(CONTENT_TYPE, "application/x-www-form-urlencoded; charset=utf-8");
071        resultEndpoint.expectedHeaderReceived(HTTP_METHOD, "POST");
072
073        // Test
074        template.sendBodyAndHeader(null, FCREPO_URI, uri);
075        template.sendBodyAndHeader(null, FCREPO_BASE_URL, uri);
076
077        // Confirm that assertions passed
078        resultEndpoint.expectedMessageCount(2);
079        resultEndpoint.assertIsSatisfied();
080    }
081
082    @Override
083    protected RouteBuilder createRouteBuilder() {
084        return new RouteBuilder() {
085            @Override
086            public void configure() throws IOException {
087                onException(IOException.class)
088                    .handled(true);
089
090                from("direct:start")
091                    .process(new SparqlDescribeProcessor())
092                    .to("mock:result");
093            }
094        };
095    }
096}