001/**
002 * Copyright 2015 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;
028import java.util.concurrent.Callable;
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.camel.util.ObjectHelper;
037import org.fcrepo.camel.JmsHeaders;
038import org.fcrepo.client.FcrepoClient;
039import org.fcrepo.client.FcrepoResponse;
040
041import org.junit.Test;
042import org.slf4j.Logger;
043
044/**
045 * Test the route workflow.
046 *
047 * @author bseeger
048 * @since 2015-11-05
049 */
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.xml";
091    }
092
093    @Override
094    protected Properties useOverridePropertiesWithPropertiesComponent() {
095
096        final Properties props = new Properties();
097        props.put("fcrepo.baseUrl", "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("input.stream", "direct:start");
102        return props;
103    }
104
105    @Test
106    public void testAddedEventRouter() throws Exception {
107        final String path = fullPath.replaceFirst("http://localhost:[0-9]+/fcrepo/rest", "");
108        final String fcrepoEndpoint = "mock:fcrepo:localhost:" + FCREPO_PORT + "/fcrepo/rest";
109
110        context.getRouteDefinition("FcrepoSerialization").adviceWith(context, new AdviceWithRouteBuilder() {
111            @Override
112            public void configure() throws Exception {
113                mockEndpoints("*");
114            }
115        });
116        context.getRouteDefinition("FcrepoSerializationMetadataUpdater").adviceWith(context,
117                new AdviceWithRouteBuilder() {
118            @Override
119            public void configure() throws Exception {
120                mockEndpoints("*");
121            }
122        });
123        context.getRouteDefinition("FcrepoSerializationBinaryUpdater").adviceWith(context,
124                new AdviceWithRouteBuilder() {
125            @Override
126            public void configure() throws Exception {
127                mockEndpoints("*");
128            }
129        });
130
131        context.start();
132
133        final Map<String, Object> headers = new HashMap<>();
134        headers.put(JmsHeaders.IDENTIFIER, path);
135        headers.put(JmsHeaders.BASE_URL, "http://localhost:" + FCREPO_PORT + "/fcrepo/rest");
136        headers.put(JmsHeaders.EVENT_TYPE, REPOSITORY + "NODE_ADDED");
137        headers.put(JmsHeaders.TIMESTAMP, 1428360320168L);
138        headers.put(JmsHeaders.PROPERTIES, "");
139
140        getMockEndpoint("mock://direct:metadata").expectedMessageCount(1);
141        getMockEndpoint("mock://direct:binary").expectedMessageCount(1);
142        // Binary request should not go through, so only 1 message to the fcrepoEndpoint
143        getMockEndpoint(fcrepoEndpoint).expectedMessageCount(1);
144
145        final File f = new File("target/serialization/descriptions/" + path  + ".ttl");
146
147        assertFalse(f.exists());
148
149        template.sendBodyAndHeaders("direct:start", "", headers);
150
151        await().until(new Callable<Boolean>() {
152            @Override
153            public Boolean call() throws Exception {
154                return f.exists();
155            }
156        });
157
158        assertMockEndpointsSatisfied();
159    }
160}