001/*
002 * Copyright 2016 DuraSpace, Inc.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.fcrepo.camel.serialization.integration;
017
018import static com.jayway.awaitility.Awaitility.await;
019import static org.fcrepo.camel.RdfNamespaces.REPOSITORY;
020import static org.slf4j.LoggerFactory.getLogger;
021
022import java.io.File;
023
024import java.net.URI;
025import java.util.HashMap;
026import java.util.Map;
027import java.util.Properties;
028
029import org.apache.camel.EndpointInject;
030import org.apache.camel.Produce;
031import org.apache.camel.ProducerTemplate;
032import org.apache.camel.builder.AdviceWithRouteBuilder;
033import org.apache.camel.component.mock.MockEndpoint;
034import org.apache.camel.test.blueprint.CamelBlueprintTestSupport;
035import org.apache.camel.util.ObjectHelper;
036import org.fcrepo.camel.JmsHeaders;
037import org.fcrepo.client.FcrepoClient;
038import org.fcrepo.client.FcrepoResponse;
039
040import org.junit.Test;
041import org.slf4j.Logger;
042
043/**
044 * Test the route workflow.
045 *
046 * @author bseeger
047 * @since 2015-11-05
048 */
049public class RouteIT extends CamelBlueprintTestSupport {
050
051    final private Logger logger = getLogger(RouteIT.class);
052
053    private static final String FCREPO_PORT = System.getProperty(
054            "fcrepo.dynamic.test.port", "8080");
055
056    private static final String auditContainer = "/audit";
057
058    @EndpointInject(uri = "mock:result")
059    protected MockEndpoint resultEndpoint;
060
061    @Produce(uri = "direct:start")
062    protected ProducerTemplate template;
063
064    private String fullPath = "";
065    private final String binary = "binary.txt";
066
067    @Override
068    protected void doPreSetup() throws Exception {
069        final FcrepoClient client = new FcrepoClient(null, null, null, true);
070        final FcrepoResponse res = client.post(
071                URI.create("http://localhost:" + FCREPO_PORT + "/fcrepo/rest"),
072                ObjectHelper.loadResourceAsStream("indexable.ttl"), "text/turtle");
073        fullPath = res.getLocation().toString();
074    }
075
076    @Override
077    public boolean isUseAdviceWith() {
078        return true;
079    }
080
081    @Override
082    public boolean isUseRouteBuilder() {
083        return false;
084    }
085
086    @Override
087    protected String getBlueprintDescriptor() {
088        return "/OSGI-INF/blueprint/blueprint-test.xml";
089    }
090
091    @Override
092    protected Properties useOverridePropertiesWithPropertiesComponent() {
093        final String jmsPort = System.getProperty("fcrepo.dynamic.jms.port", "61616");
094        final Properties props = new Properties();
095        props.put("fcrepo.baseUrl", "http://localhost:" + FCREPO_PORT + "/fcrepo/rest");
096        props.put("serialization.descriptions", "target/serialization/descriptions");
097        props.put("serialization.binaries", "target/serialization/binaries");
098        props.put("serialization.stream", "direct:foo");
099        props.put("jms.brokerUrl", "tcp://localhost:" + jmsPort);
100        props.put("input.stream", "direct:start");
101        return props;
102    }
103
104    @Test
105    public void testAddedEventRouter() throws Exception {
106        final String path = fullPath.replaceFirst("http://localhost:[0-9]+/fcrepo/rest", "");
107        final String fcrepoEndpoint = "mock:fcrepo:http://localhost:" + FCREPO_PORT + "/fcrepo/rest";
108
109        context.getRouteDefinition("FcrepoSerialization").adviceWith(context, new AdviceWithRouteBuilder() {
110            @Override
111            public void configure() throws Exception {
112                mockEndpoints("*");
113            }
114        });
115        context.getRouteDefinition("FcrepoSerializationMetadataUpdater").adviceWith(context,
116                new AdviceWithRouteBuilder() {
117            @Override
118            public void configure() throws Exception {
119                mockEndpoints("*");
120            }
121        });
122        context.getRouteDefinition("FcrepoSerializationBinaryUpdater").adviceWith(context,
123                new AdviceWithRouteBuilder() {
124            @Override
125            public void configure() throws Exception {
126                mockEndpoints("*");
127            }
128        });
129
130        context.start();
131
132        final Map<String, Object> headers = new HashMap<>();
133        headers.put(JmsHeaders.IDENTIFIER, path);
134        headers.put(JmsHeaders.BASE_URL, "http://localhost:" + FCREPO_PORT + "/fcrepo/rest");
135        headers.put(JmsHeaders.EVENT_TYPE, REPOSITORY + "NODE_ADDED");
136        headers.put(JmsHeaders.TIMESTAMP, 1428360320168L);
137        headers.put(JmsHeaders.PROPERTIES, "");
138
139        getMockEndpoint("mock://direct:metadata").expectedMessageCount(1);
140        getMockEndpoint("mock://direct:binary").expectedMessageCount(1);
141        // Binary request should not go through, so only 1 message to the fcrepoEndpoint
142        getMockEndpoint(fcrepoEndpoint).expectedMessageCount(1);
143
144        final File f = new File("target/serialization/descriptions/" + path  + ".ttl");
145
146        assertFalse(f.exists());
147
148        template.sendBodyAndHeaders("direct:start", "", headers);
149
150        await().until(() -> f.exists());
151
152        assertMockEndpointsSatisfied();
153    }
154}