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