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.integration;
019
020import static java.util.UUID.randomUUID;
021import static org.apache.activemq.camel.component.ActiveMQComponent.activeMQComponent;
022import static org.apache.camel.Exchange.CONTENT_TYPE;
023import static org.apache.camel.Exchange.HTTP_METHOD;
024import static org.fcrepo.camel.FcrepoHeaders.FCREPO_AGENT;
025import static org.fcrepo.camel.FcrepoHeaders.FCREPO_DATE_TIME;
026import static org.fcrepo.camel.FcrepoHeaders.FCREPO_EVENT_ID;
027import static org.fcrepo.camel.FcrepoHeaders.FCREPO_EVENT_TYPE;
028import static org.fcrepo.camel.FcrepoHeaders.FCREPO_RESOURCE_TYPE;
029import static org.fcrepo.camel.FcrepoHeaders.FCREPO_URI;
030import static org.fcrepo.camel.integration.FcrepoTestUtils.getTurtleDocument;
031
032import java.io.InputStream;
033
034import org.apache.camel.EndpointInject;
035import org.apache.camel.Produce;
036import org.apache.camel.ProducerTemplate;
037import org.apache.camel.builder.RouteBuilder;
038import org.apache.camel.component.mock.MockEndpoint;
039import org.apache.camel.test.junit4.CamelTestSupport;
040import org.fcrepo.camel.processor.EventProcessor;
041import org.junit.Test;
042
043/**
044 * Test handling a Fedora Event
045 * @author Aaron Coburn
046 * @since September 14, 2016
047 */
048public class FcrepoEventStreamIT extends CamelTestSupport {
049
050    @EndpointInject(uri = "mock:results")
051    protected MockEndpoint resultsEndpoint;
052
053    @Produce(uri = "direct:create")
054    protected ProducerTemplate template;
055
056    private final String container = randomUUID().toString();
057
058    @Test
059    public void testGetMessage() throws Exception {
060        final String webPort = System.getProperty("fcrepo.dynamic.test.port", "8080");
061        final String baseContainer = "http://localhost:" + webPort + "/fcrepo/rest/" + container;
062        final String fcrepoResource = "http://fedora.info/definitions/v4/repository#Resource";
063
064        resetMocks();
065
066        resultsEndpoint.expectedMessageCount(10);
067        resultsEndpoint.allMessages().header(FCREPO_URI).startsWith(baseContainer);
068        resultsEndpoint.allMessages().header(FCREPO_RESOURCE_TYPE).contains(fcrepoResource);
069        resultsEndpoint.allMessages().header(FCREPO_EVENT_TYPE).isNotNull();
070        resultsEndpoint.allMessages().header(FCREPO_AGENT).contains("bypassAdmin");
071        resultsEndpoint.allMessages().header(FCREPO_EVENT_ID).startsWith("urn:uuid:");
072        resultsEndpoint.allMessages().header(FCREPO_DATE_TIME).isNotNull();
073
074        template.sendBody("direct:setup", null);
075        template.sendBodyAndHeader("direct:create", getTurtleDocument(), CONTENT_TYPE, "text/turtle");
076        template.sendBodyAndHeader("direct:create", getTurtleDocument(), CONTENT_TYPE, "text/turtle");
077
078        resultsEndpoint.assertIsSatisfied();
079    }
080
081    @Override
082    protected RouteBuilder createRouteBuilder() {
083        final String webPort = System.getProperty("fcrepo.dynamic.test.port", "8080");
084        final String jmsPort = System.getProperty("fcrepo.dynamic.jms.port", "61616");
085
086        context().addComponent("activemq", activeMQComponent("tcp://localhost:" + jmsPort));
087
088        return new RouteBuilder() {
089            @Override
090            public void configure() {
091                from("activemq:queue:fedora")
092                    .convertBodyTo(String.class)
093                    .multicast().to("direct:process", "direct:stream");
094
095                from("direct:stream")
096                    .convertBodyTo(InputStream.class)
097                    .to("direct:process");
098
099                from("direct:process")
100                    .process(new EventProcessor())
101                    .filter()
102                    .simple("${header.CamelFcrepoUri} starts with 'http://localhost:" +
103                            webPort + "/fcrepo/rest/" + container + "'")
104                    .to("mock:results");
105
106                from("direct:setup")
107                    .setHeader(HTTP_METHOD).constant("PUT")
108                    .to("http4:localhost:" + webPort + "/fcrepo/rest/" + container);
109
110                from("direct:create")
111                    .setHeader(HTTP_METHOD).constant("POST")
112                    .to("http4:localhost:" + webPort + "/fcrepo/rest/" + container);
113            }
114        };
115    }
116}