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.ldpath;
019
020import static org.apache.camel.Exchange.CONTENT_TYPE;
021import static org.apache.camel.Exchange.HTTP_METHOD;
022import static org.apache.camel.Exchange.HTTP_URI;
023import static org.apache.camel.util.ObjectHelper.loadResourceAsStream;
024import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
025import static org.junit.Assert.assertFalse;
026import static org.junit.Assert.assertTrue;
027
028import java.io.File;
029import java.util.Dictionary;
030import java.util.HashMap;
031import java.util.List;
032import java.util.Map;
033import java.util.Properties;
034
035import com.fasterxml.jackson.databind.ObjectMapper;
036import org.apache.camel.EndpointInject;
037import org.apache.camel.Produce;
038import org.apache.camel.ProducerTemplate;
039import org.apache.camel.builder.AdviceWithRouteBuilder;
040import org.apache.camel.component.mock.MockEndpoint;
041import org.apache.camel.test.blueprint.CamelBlueprintTestSupport;
042import org.apache.camel.util.KeyValueHolder;
043import org.apache.marmotta.ldcache.api.LDCachingBackend;
044import org.apache.marmotta.ldcache.backend.file.LDCachingFileBackend;
045import org.junit.Test;
046import org.openrdf.repository.RepositoryException;
047
048/**
049 * Test the route workflow.
050 *
051 * @author acoburn
052 * @since Aug 6, 2016
053 */
054public class RouteTest extends CamelBlueprintTestSupport {
055
056    private final ObjectMapper MAPPER = new ObjectMapper();
057
058    @EndpointInject(uri = "mock:result")
059    protected MockEndpoint resultEndpoint;
060
061    @Produce(uri = "direct:start")
062    protected ProducerTemplate template;
063
064    @Override
065    public boolean isUseAdviceWith() {
066        return true;
067    }
068
069    @Override
070    public boolean isUseRouteBuilder() {
071        return false;
072    }
073
074    @Override
075    protected String getBlueprintDescriptor() {
076        return "/OSGI-INF/blueprint/blueprint-test.xml";
077    }
078
079    @Override
080    protected void addServicesOnStartup(final Map<String, KeyValueHolder<Object, Dictionary>> services) {
081
082        final String cacheDir = System.getProperty("project.build.directory", "target") +
083                "/ldcache-" + randomAlphabetic(5);
084
085        final LDCachingBackend backend;
086        try {
087            backend = new LDCachingFileBackend(new File(cacheDir));
088            backend.initialize();
089        } catch (final RepositoryException ex) {
090            throw new RuntimeException("Could not initialize LDCache backend at " + cacheDir, ex);
091        }
092        services.put(LDCachingBackend.class.getName(),
093                asService(backend, "osgi.jndi.service.name", "fcrepo/LDCacheBackend"));
094    }
095
096    @Override
097    protected Properties useOverridePropertiesWithPropertiesComponent() {
098        final String restPort = System.getProperty("fcrepo.dynamic.ldpath.port", "9085");
099
100        final Properties props = new Properties();
101        props.put("rest.port", restPort);
102        return props;
103    }
104
105    @Test
106    public void testGetDefault() throws Exception {
107        final String uri = "http://fedora.info/definitions/v4/event#ResourceCreation";
108        resultEndpoint.expectedMessageCount(1);
109        resultEndpoint.expectedHeaderReceived(CONTENT_TYPE, "application/json");
110
111        context.getRouteDefinition("FcrepoLDPathGet").adviceWith(context, new AdviceWithRouteBuilder() {
112            @Override
113            public void configure() throws Exception {
114                weaveAddLast().to("mock:result");
115            }
116        });
117        context.start();
118
119        template.sendBodyAndHeader("direct:get", null, "context", uri);
120
121        assertMockEndpointsSatisfied();
122        final String result = resultEndpoint.getExchanges().get(0).getIn().getBody(String.class);
123
124        @SuppressWarnings("unchecked")
125        final List<Map<String, List<String>>> data = MAPPER.readValue(result, List.class);
126
127        assertFalse(data.isEmpty());
128        assertTrue(data.get(0).containsKey("label"));
129        assertTrue(data.get(0).containsKey("type"));
130        assertTrue(data.get(0).get("label").contains("resource creation"));
131        assertTrue(data.get(0).get("type").contains("http://www.w3.org/2000/01/rdf-schema#Class"));
132        assertTrue(data.get(0).get("id").contains(uri));
133    }
134
135    @Test
136    public void testOptions() throws Exception {
137        getMockEndpoint("mock:language:simple:resource:classpath:org/fcrepo/camel/ldpath/options.ttl")
138            .expectedMessageCount(1);
139
140        context.getRouteDefinition("FcrepoLDPathRest").adviceWith(context, new AdviceWithRouteBuilder() {
141            @Override
142            public void configure() throws Exception {
143                replaceFromWith("direct:start");
144                mockEndpointsAndSkip("*");
145            }
146        });
147        context.start();
148
149        template.sendBodyAndHeader(null, HTTP_METHOD, "OPTIONS");
150        assertMockEndpointsSatisfied();
151    }
152
153    @Test
154    public void testGetParam() throws Exception {
155        final String uri = "http://fedora.info/definitions/v4/repository#Binary";
156        getMockEndpoint("mock:http4:localhost").expectedMessageCount(1);
157        getMockEndpoint("mock:http4:localhost").expectedHeaderReceived(HTTP_URI, "http://example.org/ldpath");
158
159        resultEndpoint.expectedMessageCount(1);
160        resultEndpoint.expectedHeaderReceived(CONTENT_TYPE, "application/json");
161
162        context.getRouteDefinition("FcrepoLDPathGet").adviceWith(context, new AdviceWithRouteBuilder() {
163            @Override
164            public void configure() throws Exception {
165                mockEndpointsAndSkip("http4:*");
166                weaveAddLast().to("mock:result");
167            }
168        });
169        context.start();
170
171        final Map<String, Object> headers = new HashMap<>();
172        headers.put("ldpath", "http://example.org/ldpath");
173        headers.put("context", uri);
174        template.sendBodyAndHeaders("direct:get", loadResourceAsStream("test.ldpath"), headers);
175
176        assertMockEndpointsSatisfied();
177        final String result = resultEndpoint.getExchanges().get(0).getIn().getBody(String.class);
178
179        @SuppressWarnings("unchecked")
180        final List<Map<String, List<String>>> data = MAPPER.readValue(result, List.class);
181
182        assertFalse(data.isEmpty());
183        assertTrue(data.get(0).containsKey("label"));
184        assertTrue(data.get(0).containsKey("type"));
185        assertTrue(data.get(0).get("label").contains("binary"));
186        assertTrue(data.get(0).get("type").contains("Class"));
187        assertTrue(data.get(0).get("id").contains(uri));
188    }
189
190    @Test
191    public void testMimicPost() throws Exception {
192        final String uri = "http://fedora.info/definitions/v4/repository#Container";
193        resultEndpoint.expectedMessageCount(1);
194        resultEndpoint.expectedHeaderReceived(CONTENT_TYPE, "application/json");
195
196        context.getRouteDefinition("FcrepoLDPathPrepare").adviceWith(context, new AdviceWithRouteBuilder() {
197            @Override
198            public void configure() throws Exception {
199                weaveAddLast().to("mock:result");
200            }
201        });
202        context.start();
203
204        template.sendBodyAndHeader("direct:ldpathPrepare", loadResourceAsStream("test.ldpath"), "context", uri);
205
206        assertMockEndpointsSatisfied();
207        final String result = resultEndpoint.getExchanges().get(0).getIn().getBody(String.class);
208
209        @SuppressWarnings("unchecked")
210        final List<Map<String, List<String>>> data = MAPPER.readValue(result, List.class);
211
212        assertFalse(data.isEmpty());
213        assertTrue(data.get(0).containsKey("label"));
214        assertTrue(data.get(0).containsKey("type"));
215        assertTrue(data.get(0).get("label").contains("Fedora Container"));
216        assertTrue(data.get(0).get("type").contains("Class"));
217        assertTrue(data.get(0).get("id").contains(uri));
218
219    }
220}