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, 091 "application/x-www-form-urlencoded; charset=utf-8"); 092 resultEndpoint.expectedHeaderReceived(Exchange.HTTP_METHOD, "POST"); 093 094 // Test 095 final Map<String, Object> headers = new HashMap<>(); 096 headers.put(FcrepoHeaders.FCREPO_BASE_URL, base); 097 headers.put(FcrepoHeaders.FCREPO_IDENTIFIER, path); 098 template.sendBodyAndHeaders(incomingDoc, headers); 099 100 headers.clear(); 101 headers.put(JmsHeaders.BASE_URL, base); 102 headers.put(JmsHeaders.IDENTIFIER, path); 103 template.sendBodyAndHeaders(incomingDoc, headers); 104 105 headers.clear(); 106 headers.put(JmsHeaders.BASE_URL, base); 107 headers.put(FcrepoHeaders.FCREPO_IDENTIFIER, path); 108 template.sendBodyAndHeaders(incomingDoc, headers); 109 110 headers.clear(); 111 headers.put(FcrepoHeaders.FCREPO_BASE_URL, base); 112 headers.put(JmsHeaders.IDENTIFIER, path); 113 template.sendBodyAndHeaders(incomingDoc, headers); 114 115 headers.clear(); 116 headers.put(FcrepoHeaders.FCREPO_BASE_URL, base + path); 117 template.sendBodyAndHeaders(incomingDoc, headers); 118 119 120 // Confirm that assertions passed 121 resultEndpoint.expectedMessageCount(5); 122 resultEndpoint.assertIsSatisfied(); 123 } 124 125 @Override 126 protected RouteBuilder createRouteBuilder() { 127 return new RouteBuilder() { 128 @Override 129 public void configure() throws IOException { 130 onException(IOException.class) 131 .handled(true); 132 133 from("direct:start") 134 .process(new SparqlDeleteProcessor()) 135 .to("mock:result"); 136 } 137 }; 138 } 139}