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.camel.Exchange.CONTENT_TYPE;
021import static org.apache.camel.Exchange.HTTP_METHOD;
022import static org.fcrepo.camel.FcrepoHeaders.FCREPO_URI;
023import static org.apache.camel.model.dataformat.JsonLibrary.Jackson;
024import static org.apache.camel.util.ObjectHelper.loadResourceAsStream;
025
026import java.io.InputStream;
027import java.io.IOException;
028import java.util.HashMap;
029import java.util.Map;
030
031import org.apache.camel.EndpointInject;
032import org.apache.camel.Produce;
033import org.apache.camel.ProducerTemplate;
034import org.apache.camel.builder.RouteBuilder;
035import org.apache.camel.component.mock.MockEndpoint;
036import org.apache.camel.test.junit4.CamelTestSupport;
037import org.fcrepo.camel.processor.LdnProcessor;
038import org.junit.Test;
039
040/**
041 * @author acoburn
042 */
043public class LdnProcessorTest extends CamelTestSupport {
044
045    @EndpointInject(uri = "mock:result")
046    protected MockEndpoint resultEndpoint;
047
048    @EndpointInject(uri = "mock:agent")
049    protected MockEndpoint agentEndpoint;
050
051    @Produce(uri = "direct:start")
052    protected ProducerTemplate template;
053
054    @Test
055    public void testLdnProcessor() throws IOException, InterruptedException {
056        final String uri = "http://localhost:8080/fcrepo/rest/path/to/resource";
057        final InputStream document = loadResourceAsStream("event.json");
058
059        // Test
060        final Map<String, Object> headers = new HashMap<>();
061        headers.put(FCREPO_URI, uri);
062        headers.put(CONTENT_TYPE, "application/ld+json");
063        template.sendBodyAndHeaders(document, headers);
064
065        // Confirm that assertions passed
066        resultEndpoint.expectedMessageCount(1);
067        resultEndpoint.expectedHeaderReceived(HTTP_METHOD, "POST");
068        resultEndpoint.expectedHeaderReceived(CONTENT_TYPE, "application/ld+json");
069        resultEndpoint.assertIsSatisfied();
070
071        agentEndpoint.expectedMessageCount(2);
072        agentEndpoint.assertIsSatisfied();
073    }
074
075    @Override
076    protected RouteBuilder createRouteBuilder() {
077        return new RouteBuilder() {
078            @Override
079            public void configure() throws IOException {
080                from("direct:start")
081                    .process(new LdnProcessor())
082                    .unmarshal().json(Jackson)
083                    .split(simple("${body[@graph]}"))
084                    .choice()
085                    .when().simple("${body[wasAssociatedWith]} and ${body[@id]} == ''")
086                        .to("mock:result")
087                    .when().simple("${body[name]} and ${body[@id]} == '#agent0'")
088                        .to("mock:agent")
089                    .when().simple("${body[name]} and ${body[@id]} == '#agent1'")
090                        .to("mock:agent");
091            }
092        };
093    }
094}