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