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    private static String FEDORA_USERNAME = "fedoraAdmin";
059    private static String FEDORA_PASSWORD = "fedoraAdmin";
060
061    @EndpointInject(uri = "mock:result")
062    protected MockEndpoint resultEndpoint;
063
064    @Produce(uri = "direct:start")
065    protected ProducerTemplate template;
066
067    private String fullPath = "";
068    private final String binary = "binary.txt";
069
070    @Override
071    protected void doPreSetup() throws Exception {
072        final FcrepoClient client =  client().throwExceptionOnFailure()
073                .credentials(FEDORA_USERNAME, FEDORA_PASSWORD).build();
074        final FcrepoResponse res = client.post(URI.create("http://localhost:" + FCREPO_PORT + "/fcrepo/rest"))
075                .body(loadResourceAsStream("indexable.ttl"), "text/turtle").perform();
076        fullPath = res.getLocation().toString();
077    }
078
079    @Override
080    public boolean isUseAdviceWith() {
081        return true;
082    }
083
084    @Override
085    public boolean isUseRouteBuilder() {
086        return false;
087    }
088
089    @Override
090    protected String getBlueprintDescriptor() {
091        return "/OSGI-INF/blueprint/blueprint-test.xml";
092    }
093
094    @Override
095    protected Properties useOverridePropertiesWithPropertiesComponent() {
096        final String jmsPort = System.getProperty("fcrepo.dynamic.jms.port", "61616");
097        final Properties props = new Properties();
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]+", "");
109        final String fcrepoEndpoint = "mock:fcrepo:localhost";
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        getMockEndpoint("mock://direct:metadata").expectedMessageCount(1);
135        getMockEndpoint("mock://direct:binary").expectedMessageCount(1);
136        // Binary request should not go through, so only 1 message to the fcrepoEndpoint
137        getMockEndpoint(fcrepoEndpoint).expectedMessageCount(1);
138
139        final File f = new File("target/serialization/descriptions/" + path  + ".ttl");
140
141        assertFalse(f.exists());
142        final String event = IOUtils.toString(loadResourceAsStream("event.json"), "UTF-8");
143        event.replaceAll("http://localhost/rest/path/to/resource", fullPath);
144
145        template.sendBody("direct:start", event.replaceAll("http://localhost/rest/path/to/resource", fullPath));
146
147        await().until(() -> f.exists());
148
149        assertMockEndpointsSatisfied();
150    }
151}