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.apache.camel.model.dataformat.JsonLibrary.Jackson;
025import static org.fcrepo.camel.FcrepoHeaders.FCREPO_AGENT;
026import static org.fcrepo.camel.FcrepoHeaders.FCREPO_DATE_TIME;
027import static org.fcrepo.camel.FcrepoHeaders.FCREPO_EVENT_ID;
028import static org.fcrepo.camel.FcrepoHeaders.FCREPO_EVENT_TYPE;
029import static org.fcrepo.camel.FcrepoHeaders.FCREPO_RESOURCE_TYPE;
030import static org.fcrepo.camel.FcrepoHeaders.FCREPO_URI;
031import static org.fcrepo.camel.integration.FcrepoTestUtils.getTurtleDocument;
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 * Test handling a Fedora Event
044 * @author Aaron Coburn
045 * @since September 14, 2016
046 */
047public class FcrepoEventIT extends CamelTestSupport {
048
049    @EndpointInject(uri = "mock:type")
050    protected MockEndpoint typeEndpoint;
051
052    @EndpointInject(uri = "mock:id")
053    protected MockEndpoint idEndpoint;
054
055    @EndpointInject(uri = "mock:isPartOf")
056    protected MockEndpoint isPartOfEndpoint;
057
058    @EndpointInject(uri = "mock:wasAttributedTo")
059    protected MockEndpoint wasAttributedToEndpoint;
060
061    @EndpointInject(uri = "mock:wasGeneratedBy")
062    protected MockEndpoint wasGeneratedByEndpoint;
063
064    @Produce(uri = "direct:create")
065    protected ProducerTemplate template;
066
067    private final String container = randomUUID().toString();
068
069    @Test
070    public void testGetMessage() throws Exception {
071        final String webPort = System.getProperty("fcrepo.dynamic.test.port", "8080");
072        final String baseContainer = "http://localhost:" + webPort + "/fcrepo/rest/" + container;
073        final String fcrepoResource = "http://fedora.info/definitions/v4/repository#Resource";
074
075        resetMocks();
076
077        isPartOfEndpoint.expectedMessageCount(5);
078        isPartOfEndpoint.allMessages().header(FCREPO_URI).startsWith(baseContainer);
079        isPartOfEndpoint.allMessages().header(FCREPO_RESOURCE_TYPE).contains(fcrepoResource);
080        isPartOfEndpoint.allMessages().header(FCREPO_EVENT_TYPE).isNotNull();
081        isPartOfEndpoint.allMessages().header(FCREPO_AGENT).contains("bypassAdmin");
082        isPartOfEndpoint.allMessages().header(FCREPO_EVENT_ID).startsWith("urn:uuid:");
083        isPartOfEndpoint.allMessages().header(FCREPO_DATE_TIME).isNotNull();
084
085        idEndpoint.expectedMessageCount(5);
086        idEndpoint.allMessages().header(FCREPO_URI).startsWith(baseContainer);
087        idEndpoint.allMessages().header(FCREPO_RESOURCE_TYPE).contains(fcrepoResource);
088        idEndpoint.allMessages().header(FCREPO_EVENT_TYPE).isNotNull();
089        idEndpoint.allMessages().header(FCREPO_AGENT).contains("bypassAdmin");
090        idEndpoint.allMessages().header(FCREPO_EVENT_ID).startsWith("urn:uuid:");
091        idEndpoint.allMessages().header(FCREPO_DATE_TIME).isNotNull();
092
093        typeEndpoint.expectedMessageCount(5);
094        typeEndpoint.allMessages().header(FCREPO_URI).startsWith(baseContainer);
095        typeEndpoint.allMessages().header(FCREPO_RESOURCE_TYPE).contains(fcrepoResource);
096        typeEndpoint.allMessages().header(FCREPO_EVENT_TYPE).isNotNull();
097        typeEndpoint.allMessages().header(FCREPO_AGENT).contains("bypassAdmin");
098        typeEndpoint.allMessages().header(FCREPO_EVENT_ID).startsWith("urn:uuid:");
099        typeEndpoint.allMessages().header(FCREPO_DATE_TIME).isNotNull();
100
101        wasGeneratedByEndpoint.expectedMessageCount(3);
102        wasGeneratedByEndpoint.allMessages().header(FCREPO_URI).startsWith(baseContainer);
103        wasGeneratedByEndpoint.allMessages().header(FCREPO_RESOURCE_TYPE).contains(fcrepoResource);
104        wasGeneratedByEndpoint.allMessages().header(FCREPO_EVENT_TYPE).isNotNull();
105        wasGeneratedByEndpoint.allMessages().header(FCREPO_AGENT).contains("bypassAdmin");
106        wasGeneratedByEndpoint.allMessages().header(FCREPO_EVENT_ID).startsWith("urn:uuid:");
107        wasGeneratedByEndpoint.allMessages().header(FCREPO_DATE_TIME).isNotNull();
108
109        wasAttributedToEndpoint.expectedMessageCount(5);
110        wasAttributedToEndpoint.allMessages().header(FCREPO_URI).startsWith(baseContainer);
111        wasAttributedToEndpoint.allMessages().header(FCREPO_RESOURCE_TYPE).contains(fcrepoResource);
112        wasAttributedToEndpoint.allMessages().header(FCREPO_EVENT_TYPE).isNotNull();
113        wasAttributedToEndpoint.allMessages().header(FCREPO_AGENT).contains("bypassAdmin");
114        wasAttributedToEndpoint.allMessages().header(FCREPO_EVENT_ID).startsWith("urn:uuid:");
115        wasAttributedToEndpoint.allMessages().header(FCREPO_DATE_TIME).isNotNull();
116
117        template.sendBody("direct:setup", null);
118        template.sendBodyAndHeader("direct:create", getTurtleDocument(), CONTENT_TYPE, "text/turtle");
119        template.sendBodyAndHeader("direct:create", getTurtleDocument(), CONTENT_TYPE, "text/turtle");
120
121        idEndpoint.assertIsSatisfied();
122        typeEndpoint.assertIsSatisfied();
123        wasGeneratedByEndpoint.assertIsSatisfied();
124        wasAttributedToEndpoint.assertIsSatisfied();
125        isPartOfEndpoint.assertIsSatisfied();
126    }
127
128    @Override
129    protected RouteBuilder createRouteBuilder() {
130        final String webPort = System.getProperty("fcrepo.dynamic.test.port", "8080");
131        final String jmsPort = System.getProperty("fcrepo.dynamic.jms.port", "61616");
132
133        context().addComponent("activemq", activeMQComponent("tcp://localhost:" + jmsPort));
134
135        return new RouteBuilder() {
136            @Override
137            public void configure() {
138                from("activemq:queue:fedora")
139                    .unmarshal().json(Jackson)
140                    .process(new EventProcessor())
141                    .filter()
142                    .simple("${body[id]} starts with 'http://localhost:" + webPort + "/fcrepo/rest/" + container + "'")
143                    .multicast()
144                    .to("direct:type", "direct:id", "direct:isPartOf", "direct:wasGeneratedBy",
145                            "direct:wasAttributedTo");
146
147                from("direct:type")
148                    .filter()
149                    .simple("'http://fedora.info/definitions/v4/repository#Resource' in ${body[type]}")
150                    .filter(header(FCREPO_RESOURCE_TYPE)
151                            .contains("http://fedora.info/definitions/v4/repository#Resource"))
152                    .to("mock:type");
153
154                from("direct:id")
155                    .filter()
156                    .simple("${body[id]} starts with 'http://localhost:" + webPort + "/fcrepo/rest'")
157                    .filter(header(FCREPO_URI).startsWith("http://localhost:" + webPort + "/fcrepo/rest"))
158                    .filter(header(FCREPO_EVENT_ID).startsWith("urn:uuid:"))
159                    .to("mock:id");
160
161                from("direct:isPartOf")
162                    .filter()
163                    .simple("'http://localhost:" + webPort + "/fcrepo/rest' == ${body[isPartOf]}")
164                    .to("mock:isPartOf");
165
166                from("direct:wasAttributedTo")
167                    .split(simple("${body[wasAttributedTo]}"))
168                    .filter()
169                    .simple("'bypassAdmin' == ${body[name]}")
170                    .filter(header(FCREPO_AGENT).contains("bypassAdmin"))
171                    .to("mock:wasAttributedTo");
172
173                from("direct:wasGeneratedBy")
174                    .filter()
175                    .simple(
176                        "'http://fedora.info/definitions/v4/event#ResourceCreation' in ${body[wasGeneratedBy][type]}")
177                    .filter(header(FCREPO_EVENT_TYPE)
178                            .contains("http://fedora.info/definitions/v4/event#ResourceCreation"))
179                    .to("mock:wasGeneratedBy");
180
181                from("direct:setup")
182                    .setHeader(HTTP_METHOD).constant("PUT")
183                    .to("http4:localhost:" + webPort + "/fcrepo/rest/" + container);
184
185                from("direct:create")
186                    .setHeader(HTTP_METHOD).constant("POST")
187                    .to("http4:localhost:" + webPort + "/fcrepo/rest/" + container);
188            }
189        };
190    }
191}