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 org.apache.commons.lang3.StringUtils.normalizeSpace; 021import static org.apache.camel.Exchange.CONTENT_TYPE; 022import static org.fcrepo.camel.FcrepoHeaders.FCREPO_URI; 023import static org.fcrepo.camel.integration.FcrepoTestUtils.getFcrepoEndpointUri; 024import static org.fcrepo.camel.integration.FcrepoTestUtils.getN3Document; 025import static java.net.URLEncoder.encode; 026 027import java.io.IOException; 028import java.util.HashMap; 029import java.util.Map; 030 031import org.apache.camel.EndpointInject; 032import org.apache.camel.Exchange; 033import org.apache.camel.Processor; 034import org.apache.camel.Produce; 035import org.apache.camel.ProducerTemplate; 036import org.apache.camel.builder.RouteBuilder; 037import org.apache.camel.component.mock.MockEndpoint; 038import org.apache.camel.test.junit4.CamelTestSupport; 039import org.fcrepo.camel.processor.SparqlInsertProcessor; 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 SparqlInsertProcessorTest 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 testInsert() throws IOException, InterruptedException { 057 final String uri = "http://localhost/rest/path/a/b/c"; 058 final String document = getN3Document(); 059 060 // Assertions 061 resultEndpoint.allMessages().body().startsWith("update=" + encode("INSERT DATA { ", "UTF-8")); 062 resultEndpoint.allMessages().body().endsWith(encode("}", "UTF-8")); 063 for (final String s : document.split("\n")) { 064 resultEndpoint.expectedBodyReceived().body().contains(encode(s, "UTF-8")); 065 } 066 resultEndpoint.expectedBodyReceived().body().contains( 067 encode("<" + uri + "> dc:title \"some title & other\" .", "UTF-8")); 068 resultEndpoint.expectedHeaderReceived("Content-Type", "application/x-www-form-urlencoded; charset=utf-8"); 069 resultEndpoint.expectedHeaderReceived(Exchange.HTTP_METHOD, "POST"); 070 071 // Test 072 final Map<String, Object> headers = new HashMap<>(); 073 headers.put(FCREPO_URI, uri); 074 headers.put(CONTENT_TYPE, "application/n-triples"); 075 template.sendBodyAndHeaders(document, headers); 076 077 // Confirm that assertions passed 078 resultEndpoint.expectedMessageCount(1); 079 resultEndpoint.assertIsSatisfied(); 080 } 081 082 @Override 083 protected RouteBuilder createRouteBuilder() { 084 return new RouteBuilder() { 085 @Override 086 public void configure() throws IOException { 087 088 final String fcrepo_uri = getFcrepoEndpointUri(); 089 090 from("direct:start") 091 .process(new SparqlInsertProcessor()) 092 // Normalize the whitespace to make it easier to compare 093 .process(new Processor() { 094 public void process(final Exchange exchange) throws Exception { 095 final String payload = exchange.getIn().getBody(String.class); 096 exchange.getIn().setBody(normalizeSpace(payload)); 097 } 098 }) 099 .to("mock:result"); 100 } 101 }; 102 } 103}