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