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.fixity.integration; 007 008import org.apache.camel.CamelContext; 009import org.apache.camel.EndpointInject; 010import org.apache.camel.Produce; 011import org.apache.camel.ProducerTemplate; 012import org.apache.camel.builder.AdviceWith; 013import org.apache.camel.component.mock.MockEndpoint; 014import org.apache.camel.model.ModelCamelContext; 015import org.apache.camel.spring.javaconfig.CamelConfiguration; 016import org.apache.camel.support.builder.Namespaces; 017import org.apache.commons.codec.digest.DigestUtils; 018import org.apache.jena.vocabulary.RDF; 019import org.fcrepo.camel.fixity.FcrepoFixityConfig; 020import org.fcrepo.client.FcrepoClient; 021import org.fcrepo.client.FcrepoResponse; 022import org.junit.BeforeClass; 023import org.junit.Test; 024import org.junit.runner.RunWith; 025import org.slf4j.Logger; 026import org.slf4j.LoggerFactory; 027import org.springframework.beans.factory.annotation.Autowired; 028import org.springframework.context.annotation.ComponentScan; 029import org.springframework.context.annotation.Configuration; 030import org.springframework.test.context.ContextConfiguration; 031import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 032import org.springframework.test.context.support.AnnotationConfigContextLoader; 033 034import java.net.URI; 035 036import static junit.framework.TestCase.assertTrue; 037import static org.apache.camel.component.mock.MockEndpoint.assertIsSatisfied; 038import static org.apache.camel.util.ObjectHelper.loadResourceAsStream; 039import static org.fcrepo.camel.FcrepoHeaders.FCREPO_URI; 040import static org.fcrepo.client.FcrepoClient.client; 041 042/** 043 * Test the route workflow. 044 * 045 * @author Aaron Coburn 046 * @since 2015-06-18 047 */ 048@RunWith(SpringJUnit4ClassRunner.class) 049@ContextConfiguration(classes = {RouteIT.ContextConfig.class}, loader = AnnotationConfigContextLoader.class) 050public class RouteIT { 051 private static Logger LOGGER = LoggerFactory.getLogger(RouteIT.class); 052 private static final String REPOSITORY = "http://fedora.info/definitions/v4/repository#"; 053 054 private static String FEDORA_USERNAME = "fedoraAdmin"; 055 private static String FEDORA_PASSWORD = "fedoraAdmin"; 056 057 private static final String binary = "binary.txt"; 058 private static String fullPath = ""; 059 @EndpointInject("mock:result") 060 protected MockEndpoint resultEndpoint; 061 @Produce("direct:start") 062 protected ProducerTemplate template; 063 @Autowired 064 private CamelContext camelContext; 065 066 @Autowired 067 private FcrepoFixityConfig config; 068 069 @BeforeClass 070 public static void beforeClass() throws Exception { 071 final String webPort = System.getProperty("fcrepo.dynamic.test.port", "8080"); 072 final String jmsPort = System.getProperty("fcrepo.dynamic.jms.port", "61616"); 073 074 final FcrepoClient client = client().throwExceptionOnFailure() 075 .credentials(FEDORA_USERNAME, FEDORA_PASSWORD).build(); 076 final FcrepoResponse res = client.post(URI.create("http://localhost:" + webPort + "/fcrepo/rest")) 077 .body(loadResourceAsStream(binary), "text/plain").perform(); 078 fullPath = res.getLocation().toString(); 079 080 System.setProperty("fixity.failure", "mock:failure"); 081 System.setProperty("fixity.success", "mock:success"); 082 System.setProperty("fixity.input.stream", "direct:start"); 083 084 System.setProperty("fcrepo.baseUrl", "http://localhost:" + webPort + "/fcrepo/rest"); 085 System.setProperty("jms.brokerUrl", "tcp://localhost:" + jmsPort); 086 System.setProperty("fixity.enabled", "true"); 087 088 } 089 090 @Test 091 public void testFixityOnBinary() throws Exception { 092 final String webPort = System.getProperty("fcrepo.dynamic.test.port", "8080"); 093 final String path = fullPath.replaceFirst("http://localhost:[0-9]+/fcrepo/rest", ""); 094 final String fcrepoEndpoint = "mock:fcrepo:http://localhost:" + webPort + "/fcrepo/rest"; 095 final Namespaces ns = new Namespaces("rdf", RDF.uri); 096 ns.add("fedora", REPOSITORY); 097 ns.add("premis", "http://www.loc.gov/premis/rdf/v1#"); 098 099 final var digest = DigestUtils.sha512Hex(loadResourceAsStream(binary)); 100 101 LOGGER.info("digest={}", digest); 102 final var context = camelContext.adapt(ModelCamelContext.class); 103 AdviceWith.adviceWith(context, "FcrepoFixity", a -> { 104 a.mockEndpoints("*"); 105 }); 106 107 final var fcrepoEndpointObj = MockEndpoint.resolve(camelContext, fcrepoEndpoint); 108 fcrepoEndpointObj.expectedMessageCount(2); 109 final var successEndpoint = MockEndpoint.resolve(camelContext, "mock:success"); 110 successEndpoint.expectedMessageCount(1); 111 template.sendBodyAndHeader("direct:start", "", FCREPO_URI, 112 config.getFcrepoBaseUrl() + path); 113 114 assertIsSatisfied(fcrepoEndpointObj, successEndpoint); 115 116 final String body = successEndpoint.assertExchangeReceived(0).getIn().getBody(String.class); 117 118 LOGGER.info("body={}", body); 119 assertTrue(body.contains( 120 "<premis:hasSize rdf:datatype=\"http://www.w3.org/2001/XMLSchema#long\">74</premis:hasSize>")); 121 assertTrue(body.contains( 122 "<premis:hasMessageDigest rdf:resource=\"urn:sha-512:" + digest + "\"/>")); 123 } 124 125 @Configuration 126 @ComponentScan("org.fcrepo.camel") 127 static class ContextConfig extends CamelConfiguration { 128 129 } 130}