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_URI; 022import static org.apache.camel.util.ObjectHelper.loadResourceAsStream; 023import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic; 024import static org.fcrepo.camel.ldpath.LDPathRouter.FEDORA_URI; 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, FEDORA_URI, 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 testGetParam() throws Exception { 137 final String uri = "http://fedora.info/definitions/v4/repository#Binary"; 138 getMockEndpoint("mock:http4:localhost").expectedMessageCount(1); 139 getMockEndpoint("mock:http4:localhost").expectedHeaderReceived(HTTP_URI, "http://example.org/ldpath"); 140 141 resultEndpoint.expectedMessageCount(1); 142 resultEndpoint.expectedHeaderReceived(CONTENT_TYPE, "application/json"); 143 144 context.getRouteDefinition("FcrepoLDPathGet").adviceWith(context, new AdviceWithRouteBuilder() { 145 @Override 146 public void configure() throws Exception { 147 mockEndpointsAndSkip("http4:*"); 148 weaveAddLast().to("mock:result"); 149 } 150 }); 151 context.start(); 152 153 final Map<String, Object> headers = new HashMap<>(); 154 headers.put("ldpath", "http://example.org/ldpath"); 155 headers.put(FEDORA_URI, uri); 156 template.sendBodyAndHeaders("direct:get", loadResourceAsStream("test.ldpath"), headers); 157 158 assertMockEndpointsSatisfied(); 159 final String result = resultEndpoint.getExchanges().get(0).getIn().getBody(String.class); 160 161 @SuppressWarnings("unchecked") 162 final List<Map<String, List<String>>> data = MAPPER.readValue(result, List.class); 163 164 assertFalse(data.isEmpty()); 165 assertTrue(data.get(0).containsKey("label")); 166 assertTrue(data.get(0).containsKey("type")); 167 assertTrue(data.get(0).get("label").contains("binary")); 168 assertTrue(data.get(0).get("type").contains("Class")); 169 assertTrue(data.get(0).get("id").contains(uri)); 170 } 171 172 @Test 173 public void testMimicPost() throws Exception { 174 final String uri = "http://fedora.info/definitions/v4/repository#Container"; 175 resultEndpoint.expectedMessageCount(1); 176 resultEndpoint.expectedHeaderReceived(CONTENT_TYPE, "application/json"); 177 178 context.getRouteDefinition("FcrepoLDPathPrepare").adviceWith(context, new AdviceWithRouteBuilder() { 179 @Override 180 public void configure() throws Exception { 181 weaveAddLast().to("mock:result"); 182 } 183 }); 184 context.start(); 185 186 template.sendBodyAndHeader("direct:ldpathPrepare", loadResourceAsStream("test.ldpath"), FEDORA_URI, uri); 187 188 assertMockEndpointsSatisfied(); 189 final String result = resultEndpoint.getExchanges().get(0).getIn().getBody(String.class); 190 191 @SuppressWarnings("unchecked") 192 final List<Map<String, List<String>>> data = MAPPER.readValue(result, List.class); 193 194 assertFalse(data.isEmpty()); 195 assertTrue(data.get(0).containsKey("label")); 196 assertTrue(data.get(0).containsKey("type")); 197 assertTrue(data.get(0).get("label").contains("Fedora Container")); 198 assertTrue(data.get(0).get("type").contains("Class")); 199 assertTrue(data.get(0).get("id").contains(uri)); 200 201 } 202}