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.audit.triplestore; 007 008import org.apache.camel.CamelContext; 009import org.apache.camel.EndpointInject; 010import org.apache.camel.Exchange; 011import org.apache.camel.Produce; 012import org.apache.camel.ProducerTemplate; 013import org.apache.camel.builder.AdviceWith; 014import org.apache.camel.component.mock.MockEndpoint; 015import org.apache.camel.model.ModelCamelContext; 016import org.apache.camel.spring.javaconfig.CamelConfiguration; 017import org.junit.BeforeClass; 018import org.junit.Test; 019import org.junit.runner.RunWith; 020import org.springframework.beans.factory.annotation.Autowired; 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 static org.apache.camel.component.mock.MockEndpoint.assertIsSatisfied; 029import static org.apache.camel.util.ObjectHelper.loadResourceAsStream; 030import static org.fcrepo.camel.audit.triplestore.AuditSparqlProcessor.AUDIT; 031import static org.fcrepo.camel.audit.triplestore.AuditSparqlProcessor.PREMIS; 032import static org.junit.Assert.assertTrue; 033 034/** 035 * Test the route workflow. 036 * 037 * @author escowles 038 * @author Aaron Coburn 039 * @author dbernstein 040 * @since 2015-04-10 041 */ 042@RunWith(SpringJUnit4ClassRunner.class) 043@ContextConfiguration(classes = {RouteTest.ContextConfig.class}, loader = AnnotationConfigContextLoader.class) 044public class RouteTest { 045 046 @EndpointInject("mock:result") 047 protected MockEndpoint resultEndpoint; 048 049 @Produce("direct:start") 050 protected ProducerTemplate template; 051 052 @Autowired 053 private CamelContext camelContext; 054 055 private static final String baseURL = "http://localhost/rest"; 056 private static final String fileID = "/file1"; 057 private static final String auditContainer = "/audit"; 058 059 060 @BeforeClass 061 public static void beforeClass() { 062 System.setProperty("audit.input.stream", "seda:foo"); 063 System.setProperty("audit.filter.containers", baseURL + auditContainer); 064 System.setProperty("audit.enabled", "true"); 065 } 066 067 @DirtiesContext 068 @Test 069 public void testWithoutJms() throws Exception { 070 071 final var context = camelContext.adapt(ModelCamelContext.class); 072 073 AdviceWith.adviceWith(context, "AuditFcrepoRouter", a -> { 074 a.replaceFromWith("direct:start"); 075 }); 076 077 AdviceWith.adviceWith(context, "AuditEventRouter", a -> { 078 a.mockEndpointsAndSkip("http*"); 079 a.weaveAddLast().to("mock:result"); 080 }); 081 082 resultEndpoint.expectedMessageCount(2); 083 resultEndpoint.expectedHeaderReceived(Exchange.CONTENT_TYPE, "application/x-www-form-urlencoded"); 084 resultEndpoint.expectedHeaderReceived(Exchange.HTTP_METHOD, "POST"); 085 resultEndpoint.expectedHeaderReceived(AuditHeaders.EVENT_BASE_URI, "http://example.com/event"); 086 087 template.sendBody(loadResourceAsStream("event_delete_binary.json")); 088 template.sendBody(loadResourceAsStream("event_delete_resource.json")); 089 090 assertIsSatisfied(resultEndpoint); 091 final String body = (String) resultEndpoint.assertExchangeReceived(0).getIn().getBody(); 092 assertTrue("Event type not found!", 093 body.contains("<" + PREMIS + "hasEventType> <" + AUDIT + "contentRemoval>")); 094 assertTrue("Object link not found!", 095 body.contains("<" + PREMIS + "hasEventRelatedObject> <" + baseURL + fileID + ">")); 096 } 097 098 @DirtiesContext 099 @Test 100 public void testFilterContainersWithoutJms() throws Exception { 101 102 final var context = camelContext.adapt(ModelCamelContext.class); 103 104 resultEndpoint.expectedMessageCount(0); 105 resultEndpoint.setAssertPeriod(1000); 106 107 AdviceWith.adviceWith(context, "AuditFcrepoRouter", a -> { 108 a.replaceFromWith("direct:start"); 109 }); 110 111 AdviceWith.adviceWith(context, "AuditEventRouter", a -> { 112 a.mockEndpointsAndSkip("http*"); 113 a.weaveAddLast().to("mock:result"); 114 }); 115 116 //send events that should be filtered 117 template.sendBody(loadResourceAsStream("event_audit_resource.json")); 118 template.sendBody(loadResourceAsStream("event_audit_update.json")); 119 120 assertIsSatisfied(resultEndpoint); 121 } 122 123 @Configuration 124 @ComponentScan(basePackages = "org.fcrepo.camel.audit") 125 static class ContextConfig extends CamelConfiguration { 126 127 } 128}