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