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