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.apache.camel.util.ObjectHelper.loadResourceAsStream;
022import static org.fcrepo.client.FcrepoClient.client;
023import static org.slf4j.LoggerFactory.getLogger;
024
025import java.io.File;
026
027import java.net.URI;
028import java.util.Properties;
029
030import org.apache.camel.EndpointInject;
031import org.apache.camel.Produce;
032import org.apache.camel.ProducerTemplate;
033import org.apache.camel.builder.AdviceWithRouteBuilder;
034import org.apache.camel.component.mock.MockEndpoint;
035import org.apache.camel.test.blueprint.CamelBlueprintTestSupport;
036import org.apache.commons.io.IOUtils;
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 = client().throwExceptionOnFailure().build();
070        final FcrepoResponse res = client.post(URI.create("http://localhost:" + FCREPO_PORT + "/fcrepo/rest"))
071                .body(loadResourceAsStream("indexable.ttl"), "text/turtle").perform();
072        fullPath = res.getLocation().toString();
073    }
074
075    @Override
076    public boolean isUseAdviceWith() {
077        return true;
078    }
079
080    @Override
081    public boolean isUseRouteBuilder() {
082        return false;
083    }
084
085    @Override
086    protected String getBlueprintDescriptor() {
087        return "/OSGI-INF/blueprint/blueprint-test.xml";
088    }
089
090    @Override
091    protected Properties useOverridePropertiesWithPropertiesComponent() {
092        final String jmsPort = System.getProperty("fcrepo.dynamic.jms.port", "61616");
093        final Properties props = new Properties();
094        props.put("serialization.descriptions", "target/serialization/descriptions");
095        props.put("serialization.binaries", "target/serialization/binaries");
096        props.put("serialization.stream", "direct:foo");
097        props.put("jms.brokerUrl", "tcp://localhost:" + jmsPort);
098        props.put("input.stream", "direct:start");
099        return props;
100    }
101
102    @Test
103    public void testAddedEventRouter() throws Exception {
104        final String path = fullPath.replaceFirst("http://localhost:[0-9]+", "");
105        final String fcrepoEndpoint = "mock:fcrepo:localhost";
106
107        context.getRouteDefinition("FcrepoSerialization").adviceWith(context, new AdviceWithRouteBuilder() {
108            @Override
109            public void configure() throws Exception {
110                mockEndpoints("*");
111            }
112        });
113        context.getRouteDefinition("FcrepoSerializationMetadataUpdater").adviceWith(context,
114                new AdviceWithRouteBuilder() {
115            @Override
116            public void configure() throws Exception {
117                mockEndpoints("*");
118            }
119        });
120        context.getRouteDefinition("FcrepoSerializationBinaryUpdater").adviceWith(context,
121                new AdviceWithRouteBuilder() {
122            @Override
123            public void configure() throws Exception {
124                mockEndpoints("*");
125            }
126        });
127
128        context.start();
129
130        getMockEndpoint("mock://direct:metadata").expectedMessageCount(1);
131        getMockEndpoint("mock://direct:binary").expectedMessageCount(1);
132        // Binary request should not go through, so only 1 message to the fcrepoEndpoint
133        getMockEndpoint(fcrepoEndpoint).expectedMessageCount(1);
134
135        final File f = new File("target/serialization/descriptions/" + path  + ".ttl");
136
137        assertFalse(f.exists());
138        final String event = IOUtils.toString(loadResourceAsStream("event.json"), "UTF-8");
139        event.replaceAll("http://localhost/rest/path/to/resource", fullPath);
140
141        template.sendBody("direct:start", event.replaceAll("http://localhost/rest/path/to/resource", fullPath));
142
143        await().until(() -> f.exists());
144
145        assertMockEndpointsSatisfied();
146    }
147}