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.service; 007 008import org.apache.camel.EndpointInject; 009import org.apache.camel.Produce; 010import org.apache.camel.ProducerTemplate; 011import org.apache.camel.builder.RouteBuilder; 012import org.apache.camel.component.mock.MockEndpoint; 013import org.apache.camel.spring.javaconfig.CamelConfiguration; 014import org.apache.http.HttpResponse; 015import org.apache.http.auth.AuthScope; 016import org.apache.http.auth.UsernamePasswordCredentials; 017import org.apache.http.client.methods.HttpPost; 018import org.apache.http.impl.client.BasicCredentialsProvider; 019import org.apache.http.impl.client.CloseableHttpClient; 020import org.apache.http.impl.client.HttpClients; 021import org.apache.http.util.EntityUtils; 022import org.fcrepo.camel.processor.EventProcessor; 023import org.junit.BeforeClass; 024import org.junit.Test; 025import org.junit.runner.RunWith; 026import org.slf4j.Logger; 027import org.springframework.context.annotation.Bean; 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.io.IOException; 035import java.util.HashMap; 036import java.util.Map; 037 038import static org.apache.camel.component.mock.MockEndpoint.assertIsSatisfied; 039import static org.apache.http.HttpStatus.SC_CREATED; 040import static org.fcrepo.camel.FcrepoHeaders.FCREPO_BASE_URL; 041import static org.fcrepo.camel.FcrepoHeaders.FCREPO_IDENTIFIER; 042import static org.junit.Assert.assertEquals; 043import static org.slf4j.LoggerFactory.getLogger; 044 045/** 046 * Test the route workflow. 047 * 048 * @author Aaron Coburn 049 * @since 2016-07-21 050 */ 051@RunWith(SpringJUnit4ClassRunner.class) 052@ContextConfiguration(classes = {ContextConfig.class}, loader = AnnotationConfigContextLoader.class) 053public class RouteIT { 054 055 private static final Logger LOGGER = getLogger(RouteIT.class); 056 057 private static String FEDORA_USERNAME = "fedoraAdmin"; 058 private static String FEDORA_PASSWORD = "fedoraAdmin"; 059 private static String FEDORA_BASE_URL = ""; 060 @EndpointInject("mock:result") 061 protected MockEndpoint resultEndpoint; 062 063 @Produce("direct:start") 064 protected ProducerTemplate template; 065 066 @BeforeClass 067 public static void beforeClass() { 068 System.setProperty("fcrepo.authUsername", FEDORA_USERNAME); 069 System.setProperty("fcrepo.authPassword", FEDORA_PASSWORD); 070 final String fcrepoPort = System.getProperty("fcrepo.dynamic.test.port", "8080"); 071 FEDORA_BASE_URL = "http://localhost:" + fcrepoPort + "/fcrepo/rest"; 072 System.setProperty("fcrepo.baseUrl", FEDORA_BASE_URL); 073 } 074 075 @Test 076 public void testFcrepoService() throws Exception { 077 final String webPort = System.getProperty("fcrepo.dynamic.test.port", "8080"); 078 079 final String baseUrl = "http://localhost:" + webPort + "/fcrepo/rest"; 080 final String url1 = post(baseUrl).replace(baseUrl, ""); 081 final String url2 = post(baseUrl).replace(baseUrl, ""); 082 083 final Map<String, Object> headers = new HashMap<>(); 084 headers.put(FCREPO_BASE_URL, baseUrl); 085 headers.put(FCREPO_IDENTIFIER, url1); 086 template.sendBodyAndHeaders(null, headers); 087 088 headers.put(FCREPO_IDENTIFIER, url2); 089 template.sendBodyAndHeaders(null, headers); 090 091 resultEndpoint.expectedMessageCount(2); 092 assertIsSatisfied(resultEndpoint); 093 } 094 095 private String post(final String url) { 096 try { 097 final BasicCredentialsProvider provider = new BasicCredentialsProvider(); 098 provider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(FEDORA_USERNAME, FEDORA_PASSWORD)); 099 final CloseableHttpClient httpclient = HttpClients.custom().setDefaultCredentialsProvider(provider).build(); 100 101 final HttpPost httppost = new HttpPost(url); 102 final HttpResponse response = httpclient.execute(httppost); 103 assertEquals(SC_CREATED, response.getStatusLine().getStatusCode()); 104 return EntityUtils.toString(response.getEntity(), "UTF-8"); 105 } catch (IOException ex) { 106 LOGGER.debug("Unable to extract HttpEntity response into an InputStream: ", ex); 107 return ""; 108 } 109 } 110 111 112} 113 114@Configuration 115@ComponentScan("org.fcrepo.camel") 116class ContextConfig extends CamelConfiguration { 117 @Bean 118 public RouteBuilder route() { 119 return new RouteBuilder() { 120 public void configure() throws Exception { 121 from("direct:start") 122 .process(new EventProcessor()) 123 .to("fcrepo:localhost/rest") 124 .to("mock:result"); 125 } 126 }; 127 } 128}