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 org.apache.commons.lang3.StringUtils.normalizeSpace;
019import static org.fcrepo.camel.integration.FcrepoTestUtils.getFcrepoEndpointUri;
020import static org.fcrepo.camel.integration.FcrepoTestUtils.getN3Document;
021import static org.fcrepo.camel.integration.FcrepoTestUtils.getTurtleDocument;
022import static java.net.URLEncoder.encode;
023
024import java.io.IOException;
025import java.util.HashMap;
026import java.util.Map;
027
028import org.apache.camel.EndpointInject;
029import org.apache.camel.Exchange;
030import org.apache.camel.Processor;
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.test.junit4.CamelTestSupport;
036import org.fcrepo.camel.processor.SparqlInsertProcessor;
037import org.junit.Test;
038
039/**
040 * Test adding a non-RDF resource
041 * @author Aaron Coburn
042 * @since November 7, 2014
043 */
044public class SparqlInsertProcessorTest extends CamelTestSupport {
045
046    @EndpointInject(uri = "mock:result")
047    protected MockEndpoint resultEndpoint;
048
049    @Produce(uri = "direct:start")
050    protected ProducerTemplate template;
051
052    @Test
053    public void testInsert() throws IOException, InterruptedException {
054        final String base = "http://localhost/rest";
055        final String path = "/path/a/b/c";
056        final String document = getN3Document();
057
058        // Assertions
059        resultEndpoint.allMessages().body().startsWith("update=" + encode("INSERT DATA { ", "UTF-8"));
060        resultEndpoint.allMessages().body().endsWith(encode("}", "UTF-8"));
061        for (final String s : document.split("\n")) {
062            resultEndpoint.expectedBodyReceived().body().contains(encode(s, "UTF-8"));
063        }
064        resultEndpoint.expectedBodyReceived().body().contains(
065                encode("<" + base + path + "> dc:title \"some title & other\" .", "UTF-8"));
066        resultEndpoint.expectedHeaderReceived("Content-Type", "application/x-www-form-urlencoded");
067        resultEndpoint.expectedHeaderReceived(Exchange.HTTP_METHOD, "POST");
068
069        // Test
070        final Map<String, Object> headers = new HashMap<>();
071        headers.put(FcrepoHeaders.FCREPO_BASE_URL, base);
072        headers.put(FcrepoHeaders.FCREPO_IDENTIFIER, path);
073        headers.put(Exchange.CONTENT_TYPE, "application/n-triples");
074        template.sendBodyAndHeaders(document, headers);
075
076        headers.clear();
077        headers.put(JmsHeaders.BASE_URL, base);
078        headers.put(JmsHeaders.IDENTIFIER, path);
079        headers.put(Exchange.CONTENT_TYPE, "application/n-triples");
080        template.sendBodyAndHeaders(document, headers);
081
082        headers.clear();
083        headers.put(JmsHeaders.BASE_URL, base);
084        headers.put(FcrepoHeaders.FCREPO_IDENTIFIER, path);
085        headers.put(Exchange.CONTENT_TYPE, "text/turtle");
086        template.sendBodyAndHeaders(getTurtleDocument(), headers);
087
088        headers.clear();
089        headers.put(FcrepoHeaders.FCREPO_BASE_URL, base);
090        headers.put(JmsHeaders.IDENTIFIER, path);
091        headers.put(Exchange.CONTENT_TYPE, "application/n-triples");
092        template.sendBodyAndHeaders(document, headers);
093
094        // Confirm that assertions passed
095        resultEndpoint.expectedMessageCount(4);
096        resultEndpoint.assertIsSatisfied();
097    }
098
099    @Override
100    protected RouteBuilder createRouteBuilder() {
101        return new RouteBuilder() {
102            @Override
103            public void configure() throws IOException {
104
105                final String fcrepo_uri = getFcrepoEndpointUri();
106
107                from("direct:start")
108                    .process(new SparqlInsertProcessor())
109                    // Normalize the whitespace to make it easier to compare
110                    .process(new Processor() {
111                        public void process(final Exchange exchange) throws Exception {
112                           final String payload = exchange.getIn().getBody(String.class);
113                           exchange.getIn().setBody(normalizeSpace(payload));
114                       }
115                    })
116                    .to("mock:result");
117            }
118        };
119    }
120}