001/* 002 * The contents of this file are subject to the license and copyright 003 * detailed in the LICENSE and NOTICE files at the root of the source 004 * tree. 005 */ 006package org.fcrepo.camel.httpforwarding; 007 008import org.apache.camel.CamelContext; 009import org.apache.camel.Produce; 010import org.apache.camel.ProducerTemplate; 011import org.apache.camel.builder.AdviceWith; 012import org.apache.camel.builder.RouteBuilder; 013import org.apache.camel.component.mock.MockEndpoint; 014import org.apache.camel.model.ModelCamelContext; 015import org.apache.camel.spring.javaconfig.CamelConfiguration; 016import org.junit.BeforeClass; 017import org.junit.Test; 018import org.junit.runner.RunWith; 019import org.springframework.beans.factory.annotation.Autowired; 020import org.springframework.context.annotation.Bean; 021import org.springframework.context.annotation.ComponentScan; 022import org.springframework.context.annotation.Configuration; 023import org.springframework.test.annotation.DirtiesContext; 024import org.springframework.test.context.ContextConfiguration; 025import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 026import org.springframework.test.context.support.AnnotationConfigContextLoader; 027 028import java.util.HashMap; 029import java.util.List; 030import java.util.Map; 031 032import static java.util.Arrays.asList; 033import static java.util.Collections.emptyList; 034import static org.fcrepo.camel.FcrepoHeaders.FCREPO_AGENT; 035import static org.fcrepo.camel.FcrepoHeaders.FCREPO_DATE_TIME; 036import static org.fcrepo.camel.FcrepoHeaders.FCREPO_RESOURCE_TYPE; 037import static org.fcrepo.camel.FcrepoHeaders.FCREPO_URI; 038 039/** 040 * Test the route validation logic. 041 * 042 * @author Aaron Coburn 043 * @author Demian Katz 044 * @since 2015-04-10 045 */ 046@RunWith(SpringJUnit4ClassRunner.class) 047@ContextConfiguration(classes = {RouteTest.ContextConfig.class}, loader = AnnotationConfigContextLoader.class) 048public class RouteValidationTest { 049 private final String EVENT_NS = "https://www.w3.org/ns/activitystreams#"; 050 private final String INDEXABLE = "http://fedora.info/definitions/v4/indexing#Indexable"; 051 private static final String baseURL = "http://localhost/rest"; 052 private static final String httpURL = "http://localhost/http_endpoint"; 053 private static final String fileID = "/file1"; 054 private static final String eventDate = "2015-04-06T22:45:20Z"; 055 private static final String userID = "bypassAdmin"; 056 private static final String userAgent = "curl/7.37.1"; 057 private static final String auditContainer = "/audit"; 058 private static final String inputStream = "seda:foo"; 059 private static final String reindexStream = "seda:bar"; 060 061 @Autowired 062 private CamelContext camelContext; 063 064 @Produce("direct:start") 065 protected ProducerTemplate template; 066 067 @BeforeClass 068 public static void beforeClass() { 069 System.setProperty("http.enabled", "true"); 070 System.setProperty("http.filter.containers", baseURL + auditContainer); 071 System.setProperty("http.input.stream", inputStream); 072 System.setProperty("http.reindex.stream", reindexStream); 073 System.setProperty("error.maxRedeliveries", "10"); 074 System.setProperty("fcrepo.baseUrl", baseURL); 075 System.setProperty("http.baseUrl", ""); 076 System.setProperty("http.reindex.stream", "seda:reindex"); 077 } 078 079 @DirtiesContext 080 @Test 081 public void testEmptyBaseUrlBehavior() throws Exception { 082 // If no base URL is provided, we cannot send messages... 083 System.setProperty("http.baseUrl", ""); 084 final List<String> eventTypes = asList(EVENT_NS + "Delete"); 085 086 final var context = camelContext.adapt(ModelCamelContext.class); 087 AdviceWith.adviceWith(context, "FcrepoHttpSend", a -> { 088 a.mockEndpointsAndSkip(httpURL); 089 a.mockEndpointsAndSkip("direct:http.baseurl.missing"); 090 }); 091 092 final var sendEndpoint = MockEndpoint.resolve(camelContext, "mock:http:localhost/http_endpoint"); 093 sendEndpoint.expectedMessageCount(0); 094 final var errorEndpoint = MockEndpoint.resolve(camelContext, "mock:direct:http.baseurl.missing"); 095 errorEndpoint.expectedMessageCount(1); 096 template.sendBodyAndHeaders(inputStream, "{}", 097 createEvent(baseURL + fileID, eventTypes)); 098 099 MockEndpoint.assertIsSatisfied(sendEndpoint, errorEndpoint); 100 } 101 102 private static Map<String, Object> createEvent(final String identifier, final List<String> eventTypes) { 103 return createEvent(identifier, eventTypes, emptyList()); 104 } 105 106 private static Map<String, Object> createEvent(final String identifier, final List<String> eventTypes, 107 final List<String> resourceTypes) { 108 final Map<String, Object> headers = new HashMap<>(); 109 headers.put(FCREPO_URI, identifier); 110 headers.put(FCREPO_DATE_TIME, eventDate); 111 headers.put(FCREPO_AGENT, asList(userID, userAgent)); 112 // The HttpRouter expects to find org.fcrepo.jms.eventtype and move it into 113 // FCREPO_EVENT_TYPE (or set a default) 114 if (eventTypes.size() > 0) { 115 headers.put("org.fcrepo.jms.eventtype", eventTypes.get(0)); 116 } 117 headers.put(FCREPO_RESOURCE_TYPE, resourceTypes); 118 return headers; 119 } 120 121 @Configuration 122 @ComponentScan(resourcePattern = "**/Fcrepo*.class") 123 static class ContextConfig extends CamelConfiguration { 124 125 @Bean 126 public RouteBuilder route() { 127 return new HttpRouter(); 128 } 129 } 130}