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.fcrepo.camel.FcrepoHeaders.FCREPO_BASE_URL;
022import static org.fcrepo.camel.FcrepoHeaders.FCREPO_IDENTIFIER;
023import static org.fcrepo.camel.FcrepoHeaders.FCREPO_URI;
024import static org.junit.Assert.assertTrue;
025
026import java.io.IOException;
027
028import org.apache.camel.EndpointInject;
029import org.apache.camel.Exchange;
030import org.apache.camel.NoSuchHeaderException;
031import org.apache.camel.Produce;
032import org.apache.camel.ProducerTemplate;
033import org.apache.camel.builder.RouteBuilder;
034import org.apache.camel.component.mock.MockEndpoint;
035import org.apache.camel.impl.DefaultExchange;
036import org.apache.camel.test.junit4.CamelTestSupport;
037import org.fcrepo.camel.processor.SparqlDeleteProcessor;
038import org.junit.Test;
039
040/**
041 * Test adding a non-RDF resource
042 * @author Aaron Coburn
043 * @since November 7, 2014
044 */
045public class SparqlDeleteProcessorTest extends CamelTestSupport {
046
047    @EndpointInject(uri = "mock:result")
048    protected MockEndpoint resultEndpoint;
049
050    @Produce(uri = "direct:start")
051    protected ProducerTemplate template;
052
053    @Test
054    public void missingHeaders() throws IOException, InterruptedException {
055        final Exchange in = new DefaultExchange(context());
056        in.getIn().setHeader(FCREPO_IDENTIFIER, "/foo");
057        final Exchange out = template.send(in);
058        assertTrue(out.isFailed());
059        assertTrue(out.getException() instanceof NoSuchHeaderException);
060    }
061
062    @Test
063    public void testDelete() throws IOException, InterruptedException {
064        final String base = "http://localhost/rest/";
065        final String uri = "path/book3";
066        final String incomingDoc =
067            "<rdf:RDF" +
068            "    xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\"" +
069            "    xmlns:vcard=\"http://www.w3.org/2001/vcard-rdf/3.0#\"" +
070            "    xmlns:dc=\"http://purl.org/dc/elements/1.1/\"" +
071            "    xmlns=\"" + uri + "\">" +
072            "  <rdf:Description rdf:about=\"" + base + "\">" +
073            "    <dc:title>Silas Marner</dc:title>" +
074            "  </rdf:Description>" +
075            "  <rdf:Description rdf:about=\"" + uri + "\">" +
076            "    <dc:title>Middlemarch</dc:title>" +
077            "    <dc:relation rdf:resource=\"" + uri + "/appendix\"/>" +
078            "    <dc:relation rdf:resource=\"" + uri + "#appendix2\"/>" +
079            "    <dc:relation rdf:resource=\"http://some-other-uri/appendix3\"/>" +
080            "    <dc:relation rdf:resource=\"" + uri + "\"/>" +
081            "    <dc:creator rdf:parseType=\"Resource\">" +
082            "      <vcard:FN>George Elliot</vcard:FN>" +
083            "      <vcard:N rdf:parseType=\"Resource\">" +
084            "        <vcard:Family>Elliot</vcard:Family>" +
085            "        <vcard:Given>George</vcard:Given>" +
086            "      </vcard:N>" +
087            "    </dc:creator>" +
088            "  </rdf:Description>" +
089            "</rdf:RDF>";
090
091        // Assertions
092        resultEndpoint.expectedBodiesReceived("update=" +
093                encode("DELETE WHERE { <" + uri + "> ?p ?o }", "UTF-8"));
094        resultEndpoint.expectedHeaderReceived(Exchange.CONTENT_TYPE,
095                "application/x-www-form-urlencoded; charset=utf-8");
096        resultEndpoint.expectedHeaderReceived(Exchange.HTTP_METHOD, "POST");
097
098        // Test
099        template.sendBodyAndHeader(incomingDoc, FCREPO_URI, uri);
100        template.sendBodyAndHeader(incomingDoc, FCREPO_BASE_URL, uri);
101
102        // Confirm that assertions passed
103        resultEndpoint.expectedMessageCount(2);
104        resultEndpoint.assertIsSatisfied();
105    }
106
107    @Override
108    protected RouteBuilder createRouteBuilder() {
109        return new RouteBuilder() {
110            @Override
111            public void configure() throws IOException {
112                onException(IOException.class)
113                    .handled(true);
114
115                from("direct:start")
116                    .process(new SparqlDeleteProcessor())
117                    .to("mock:result");
118            }
119        };
120    }
121}