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.serialization.integration;
019
020import static com.jayway.awaitility.Awaitility.await;
021import static org.fcrepo.camel.RdfNamespaces.REPOSITORY;
022import static org.slf4j.LoggerFactory.getLogger;
023
024import java.io.File;
025
026import java.net.URI;
027import java.util.HashMap;
028import java.util.Map;
029import java.util.Properties;
030
031import org.apache.camel.EndpointInject;
032import org.apache.camel.Produce;
033import org.apache.camel.ProducerTemplate;
034import org.apache.camel.builder.AdviceWithRouteBuilder;
035import org.apache.camel.component.mock.MockEndpoint;
036import org.apache.camel.test.blueprint.CamelBlueprintTestSupport;
037import org.apache.camel.util.ObjectHelper;
038import org.fcrepo.camel.JmsHeaders;
039import org.fcrepo.client.FcrepoClient;
040import org.fcrepo.client.FcrepoResponse;
041
042import org.junit.Test;
043import org.slf4j.Logger;
044
045/**
046 * Test the route workflow.
047 *
048 * @author bseeger
049 * @since 2015-11-05
050 */
051public class RouteIT extends CamelBlueprintTestSupport {
052
053    final private Logger logger = getLogger(RouteIT.class);
054
055    private static final String FCREPO_PORT = System.getProperty(
056            "fcrepo.dynamic.test.port", "8080");
057
058    private static final String auditContainer = "/audit";
059
060    @EndpointInject(uri = "mock:result")
061    protected MockEndpoint resultEndpoint;
062
063    @Produce(uri = "direct:start")
064    protected ProducerTemplate template;
065
066    private String fullPath = "";
067    private final String binary = "binary.txt";
068
069    @Override
070    protected void doPreSetup() throws Exception {
071        final FcrepoClient client = new FcrepoClient(null, null, null, true);
072        final FcrepoResponse res = client.post(
073                URI.create("http://localhost:" + FCREPO_PORT + "/fcrepo/rest"),
074                ObjectHelper.loadResourceAsStream("indexable.ttl"), "text/turtle");
075        fullPath = res.getLocation().toString();
076    }
077
078    @Override
079    public boolean isUseAdviceWith() {
080        return true;
081    }
082
083    @Override
084    public boolean isUseRouteBuilder() {
085        return false;
086    }
087
088    @Override
089    protected String getBlueprintDescriptor() {
090        return "/OSGI-INF/blueprint/blueprint-test.xml";
091    }
092
093    @Override
094    protected Properties useOverridePropertiesWithPropertiesComponent() {
095        final String jmsPort = System.getProperty("fcrepo.dynamic.jms.port", "61616");
096        final Properties props = new Properties();
097        props.put("fcrepo.baseUrl", "http://localhost:" + FCREPO_PORT + "/fcrepo/rest");
098        props.put("serialization.descriptions", "target/serialization/descriptions");
099        props.put("serialization.binaries", "target/serialization/binaries");
100        props.put("serialization.stream", "direct:foo");
101        props.put("jms.brokerUrl", "tcp://localhost:" + jmsPort);
102        props.put("input.stream", "direct:start");
103        return props;
104    }
105
106    @Test
107    public void testAddedEventRouter() throws Exception {
108        final String path = fullPath.replaceFirst("http://localhost:[0-9]+/fcrepo/rest", "");
109        final String fcrepoEndpoint = "mock:fcrepo:http://localhost:" + FCREPO_PORT + "/fcrepo/rest";
110
111        context.getRouteDefinition("FcrepoSerialization").adviceWith(context, new AdviceWithRouteBuilder() {
112            @Override
113            public void configure() throws Exception {
114                mockEndpoints("*");
115            }
116        });
117        context.getRouteDefinition("FcrepoSerializationMetadataUpdater").adviceWith(context,
118                new AdviceWithRouteBuilder() {
119            @Override
120            public void configure() throws Exception {
121                mockEndpoints("*");
122            }
123        });
124        context.getRouteDefinition("FcrepoSerializationBinaryUpdater").adviceWith(context,
125                new AdviceWithRouteBuilder() {
126            @Override
127            public void configure() throws Exception {
128                mockEndpoints("*");
129            }
130        });
131
132        context.start();
133
134        final Map<String, Object> headers = new HashMap<>();
135        headers.put(JmsHeaders.IDENTIFIER, path);
136        headers.put(JmsHeaders.BASE_URL, "http://localhost:" + FCREPO_PORT + "/fcrepo/rest");
137        headers.put(JmsHeaders.EVENT_TYPE, REPOSITORY + "NODE_ADDED");
138        headers.put(JmsHeaders.TIMESTAMP, 1428360320168L);
139        headers.put(JmsHeaders.PROPERTIES, "");
140
141        getMockEndpoint("mock://direct:metadata").expectedMessageCount(1);
142        getMockEndpoint("mock://direct:binary").expectedMessageCount(1);
143        // Binary request should not go through, so only 1 message to the fcrepoEndpoint
144        getMockEndpoint(fcrepoEndpoint).expectedMessageCount(1);
145
146        final File f = new File("target/serialization/descriptions/" + path  + ".ttl");
147
148        assertFalse(f.exists());
149
150        template.sendBodyAndHeaders("direct:start", "", headers);
151
152        await().until(() -> f.exists());
153
154        assertMockEndpointsSatisfied();
155    }
156}