001/* 002 * Copyright 2016 DuraSpace, Inc. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.fcrepo.camel.service; 017 018import static org.apache.http.HttpStatus.SC_CREATED; 019import static org.fcrepo.camel.FcrepoHeaders.FCREPO_BASE_URL; 020import static org.fcrepo.camel.FcrepoHeaders.FCREPO_IDENTIFIER; 021import static org.slf4j.LoggerFactory.getLogger; 022 023import java.io.IOException; 024import java.util.Dictionary; 025import java.util.HashMap; 026import java.util.Map; 027 028import org.apache.camel.EndpointInject; 029import org.apache.camel.Produce; 030import org.apache.camel.ProducerTemplate; 031import org.apache.camel.component.mock.MockEndpoint; 032import org.apache.camel.test.blueprint.CamelBlueprintTestSupport; 033import org.apache.camel.util.KeyValueHolder; 034import org.apache.http.HttpResponse; 035import org.apache.http.client.methods.HttpPost; 036import org.apache.http.impl.client.CloseableHttpClient; 037import org.apache.http.impl.client.HttpClients; 038import org.apache.http.util.EntityUtils; 039import org.fcrepo.camel.FcrepoComponent; 040 041import org.junit.Test; 042import org.slf4j.Logger; 043 044/** 045 * Test the route workflow. 046 * 047 * @author Aaron Coburn 048 * @since 2016-07-21 049 */ 050public class RouteIT extends CamelBlueprintTestSupport { 051 052 private static final Logger LOGGER = getLogger(RouteIT.class); 053 054 @EndpointInject(uri = "mock:result") 055 protected MockEndpoint resultEndpoint; 056 057 @Produce(uri = "direct:start") 058 protected ProducerTemplate template; 059 060 @Override 061 protected String getBlueprintDescriptor() { 062 return "/OSGI-INF/blueprint/blueprint-test.xml"; 063 } 064 065 @Override 066 protected void addServicesOnStartup(final Map<String, KeyValueHolder<Object, Dictionary>> services) { 067 final String fcrepoPort = System.getProperty("fcrepo.dynamic.test.port", "8080"); 068 final FcrepoComponent component = new FcrepoComponent(); 069 070 component.setBaseUrl("http://localhost:" + fcrepoPort + "/fcrepo/rest"); 071 072 services.put("fcrepo", asService(component, "osgi.jndi.service.name", "fcrepo/Camel")); 073 } 074 075 @Override 076 public boolean isUseRouteBuilder() { 077 return false; 078 } 079 080 @Test 081 public void testFcrepoService() throws Exception { 082 final String webPort = System.getProperty("fcrepo.dynamic.test.port", "8080"); 083 084 final String baseUrl = "http://localhost:" + webPort + "/fcrepo/rest"; 085 final String url1 = post(baseUrl).replace(baseUrl, ""); 086 final String url2 = post(baseUrl).replace(baseUrl, ""); 087 088 final Map<String, Object> headers = new HashMap<>(); 089 headers.put(FCREPO_BASE_URL, baseUrl); 090 headers.put(FCREPO_IDENTIFIER, url1); 091 template.sendBodyAndHeaders(null, headers); 092 093 headers.put(FCREPO_IDENTIFIER, url2); 094 template.sendBodyAndHeaders(null, headers); 095 096 resultEndpoint.expectedMessageCount(2); 097 assertMockEndpointsSatisfied(); 098 } 099 100 private String post(final String url) { 101 final CloseableHttpClient httpclient = HttpClients.createDefault(); 102 try { 103 final HttpPost httppost = new HttpPost(url); 104 final HttpResponse response = httpclient.execute(httppost); 105 assertEquals(SC_CREATED, response.getStatusLine().getStatusCode()); 106 return EntityUtils.toString(response.getEntity(), "UTF-8"); 107 } catch (IOException ex) { 108 LOGGER.debug("Unable to extract HttpEntity response into an InputStream: ", ex); 109 return ""; 110 } 111 } 112}