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.auth.AuthScope;
038import org.apache.http.auth.UsernamePasswordCredentials;
039import org.apache.http.client.methods.HttpPost;
040import org.apache.http.impl.client.BasicCredentialsProvider;
041import org.apache.http.impl.client.CloseableHttpClient;
042import org.apache.http.impl.client.HttpClients;
043import org.apache.http.util.EntityUtils;
044import org.fcrepo.camel.FcrepoComponent;
045
046import org.junit.Test;
047import org.slf4j.Logger;
048
049/**
050 * Test the route workflow.
051 *
052 * @author Aaron Coburn
053 * @since 2016-07-21
054 */
055public class RouteIT extends CamelBlueprintTestSupport {
056
057    private static final Logger LOGGER = getLogger(RouteIT.class);
058
059    private static String FEDORA_USERNAME = "fedoraAdmin";
060    private static String FEDORA_PASSWORD = "fedoraAdmin";
061
062    @EndpointInject(uri = "mock:result")
063    protected MockEndpoint resultEndpoint;
064
065    @Produce(uri = "direct:start")
066    protected ProducerTemplate template;
067
068    @Override
069    protected String getBlueprintDescriptor() {
070        return "/OSGI-INF/blueprint/blueprint-test.xml";
071    }
072
073    @Override
074    protected void addServicesOnStartup(final Map<String, KeyValueHolder<Object, Dictionary>> services) {
075        final String fcrepoPort = System.getProperty("fcrepo.dynamic.test.port", "8080");
076        final FcrepoComponent component = new FcrepoComponent();
077
078        component.setBaseUrl("http://localhost:" + fcrepoPort + "/fcrepo/rest");
079        component.setAuthUsername(FEDORA_USERNAME);
080        component.setAuthPassword(FEDORA_PASSWORD);
081        services.put("fcrepo", asService(component, "osgi.jndi.service.name", "fcrepo/Camel"));
082    }
083
084    @Override
085    public boolean isUseRouteBuilder() {
086        return false;
087    }
088
089    @Test
090    public void testFcrepoService() throws Exception {
091        final String webPort = System.getProperty("fcrepo.dynamic.test.port", "8080");
092
093        final String baseUrl = "http://localhost:" + webPort + "/fcrepo/rest";
094        final String url1 = post(baseUrl).replace(baseUrl, "");
095        final String url2 = post(baseUrl).replace(baseUrl, "");
096
097        final Map<String, Object> headers = new HashMap<>();
098        headers.put(FCREPO_BASE_URL, baseUrl);
099        headers.put(FCREPO_IDENTIFIER, url1);
100        template.sendBodyAndHeaders(null, headers);
101
102        headers.put(FCREPO_IDENTIFIER, url2);
103        template.sendBodyAndHeaders(null, headers);
104
105        resultEndpoint.expectedMessageCount(2);
106        assertMockEndpointsSatisfied();
107    }
108
109    private String post(final String url) {
110        try {
111            final BasicCredentialsProvider provider = new BasicCredentialsProvider();
112            provider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(FEDORA_USERNAME, FEDORA_PASSWORD));
113            final CloseableHttpClient httpclient = HttpClients.custom().setDefaultCredentialsProvider(provider).build();
114
115            final HttpPost httppost = new HttpPost(url);
116            final HttpResponse response = httpclient.execute(httppost);
117            assertEquals(SC_CREATED, response.getStatusLine().getStatusCode());
118            return EntityUtils.toString(response.getEntity(), "UTF-8");
119        } catch (IOException ex) {
120            LOGGER.debug("Unable to extract HttpEntity response into an InputStream: ", ex);
121            return "";
122        }
123    }
124}