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.fcrepo.camel.FcrepoHeaders.FCREPO_AGENT;
021import static org.fcrepo.camel.FcrepoHeaders.FCREPO_DATE_TIME;
022import static org.fcrepo.camel.FcrepoHeaders.FCREPO_EVENT_ID;
023import static org.fcrepo.camel.FcrepoHeaders.FCREPO_EVENT_TYPE;
024import static org.fcrepo.camel.FcrepoHeaders.FCREPO_RESOURCE_TYPE;
025import static org.fcrepo.camel.FcrepoHeaders.FCREPO_URI;
026import static org.apache.camel.model.dataformat.JsonLibrary.Jackson;
027import static org.apache.camel.util.ObjectHelper.loadResourceAsStream;
028
029import java.io.IOException;
030import java.util.ArrayList;
031import java.util.List;
032
033import org.apache.camel.EndpointInject;
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.EventProcessor;
040import org.junit.Test;
041
042/**
043 * @author acoburn
044 */
045public class EventProcessorTest extends CamelTestSupport {
046
047    @EndpointInject(uri = "mock:result")
048    protected MockEndpoint resultEndpoint;
049
050    @Produce(uri = "direct:start")
051    protected ProducerTemplate template;
052
053    @Test
054    public void testEventProcessor() throws IOException, InterruptedException {
055
056        final List<String> agents = new ArrayList<>();
057        agents.add("fedo raAdmin");
058        agents.add("CLAW client/1.0");
059
060        final List<String> eventTypes = new ArrayList<>();
061        eventTypes.add("http://www.w3.org/ns/prov#Activity");
062        eventTypes.add("http://fedora.info/definitions/v4/event#ResourceCreation");
063
064        final List<String> resourceTypes = new ArrayList<>();
065        resourceTypes.add("http://www.w3.org/ns/prov#Entity");
066        resourceTypes.add("http://fedora.info/definitions/v4/repository#Resource");
067        resourceTypes.add("http://fedora.info/definitions/v4/repository#Container");
068        resourceTypes.add("http://example.org/CustomType");
069
070        template.sendBody("direct:start", loadResourceAsStream("event.json"));
071        template.sendBody("direct:string", loadResourceAsStream("event.json"));
072        template.sendBody("direct:json", loadResourceAsStream("event.json"));
073
074        // Confirm that assertions passed
075        resultEndpoint.expectedMessageCount(3);
076        resultEndpoint.expectedHeaderReceived(FCREPO_URI, "http://localhost:8080/fcrepo/rest/path/to/resource");
077        resultEndpoint.expectedHeaderReceived(FCREPO_RESOURCE_TYPE, resourceTypes);
078        resultEndpoint.expectedHeaderReceived(FCREPO_DATE_TIME, "2016-05-19T17:17:39-04:00Z");
079        resultEndpoint.expectedHeaderReceived(FCREPO_EVENT_ID, "urn:uuid:3c834a8f-5638-4412-aa4b-35ea80416a18");
080        resultEndpoint.expectedHeaderReceived(FCREPO_EVENT_TYPE, eventTypes);
081        resultEndpoint.expectedHeaderReceived(FCREPO_AGENT, agents);
082
083        resultEndpoint.assertIsSatisfied();
084    }
085
086    @Override
087    protected RouteBuilder createRouteBuilder() {
088        return new RouteBuilder() {
089            @Override
090            public void configure() throws IOException {
091                from("direct:json")
092                    .unmarshal().json(Jackson)
093                    .process(new EventProcessor())
094                    .to("mock:result");
095
096                from("direct:string")
097                    .convertBodyTo(String.class)
098                    .process(new EventProcessor())
099                    .to("mock:result");
100
101                from("direct:start")
102                    .process(new EventProcessor())
103                    .to("mock:result");
104            }
105        };
106    }
107}