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.httpforwarding.integration;
007
008import org.apache.camel.Exchange;
009import org.apache.http.client.methods.HttpPost;
010import org.apache.http.entity.StringEntity;
011import org.apache.http.impl.client.CloseableHttpClient;
012import org.apache.http.impl.client.HttpClients;
013import org.fcrepo.client.FcrepoClient;
014
015import static org.fcrepo.client.FcrepoClient.client;
016
017/**
018 * Utility functions for the integration tests
019 *
020 * @author acoburn
021 * @since 2015-04-21
022 */
023public class TestUtils {
024
025    private static String FEDORA_USERNAME = "fedoraAdmin";
026    private static String FEDORA_PASSWORD = "fedoraAdmin";
027
028    /**
029     * Get an event for a given URL
030     *
031     * @param subject the URL
032     * @param eventType the event type
033     * @return the event message as JSON
034     */
035    public static String getEvent(final String subject, final String eventType) {
036        return "{\n" +
037                "\"id\": \"urn:uuid:3c834a8f-5638-4412-aa4b-35ea80416a18\", \n" +
038                "\"type\" : [ \"http://www.w3.org/ns/prov#Activity\" ,\n" +
039                "             \"" + eventType + "\" ],\n" +
040                "\"name\": \"resource event\",\n" +
041                "        \"published\": \"2016-05-19T17:17:43-04:00Z\",\n" +
042                "        \"actor\": [{\n" +
043                "   \"type\": [\"Person\"],\n" +
044                "    \"id\": \"info:fedora/fedoraAdmin\"\n" +
045                "}, {\n" +
046                "    \"type\": [\"Application\"],\n" +
047                "    \"name\": \"CLAW client/1.0\"\n" +
048                "}],\n" +
049                "        \n" +
050                "\"object\" : {\n" +
051                "    \"id\" : \"" + subject + "\" ,\n" +
052                "            \"type\" : [\n" +
053                "    \"http://www.w3.org/ns/prov#Entity\" ,\n" +
054                "            \"http://fedora.info/definitions/v4/repository#Resource\" ,\n" +
055                "            \"http://fedora.info/definitions/v4/repository#Container\" ,\n" +
056                "            \"http://www.w3.org/ns/ldp#RDFSource\",\n" +
057                "           \"http://www.w3.org/ns/ldp#BasicContainer\" ],\n" +
058                "    \"isPartOf\" : \"http://localhost/rest\"\n" +
059                "},\n" +
060                " \n" +
061                "\"@context\": [\"https://www.w3.org/ns/activitystreams\", {\n" +
062                "       \"prov\": \"http://www.w3.org/ns/prov#\",\n" +
063                "        \"dcterms\": \"http://purl.org/dc/terms/\",\n" +
064                "       \"type\": \"@type\",\n" +
065                "        \"id\": \"@id\",\n" +
066                "        \"isPartOf\": {\n" +
067                "    \"@id\": \"dcterms:isPartOf\",\n" +
068                "            \"@type\": \"@id\"\n" +
069                "}}]\n" +
070                "}";
071    }
072
073    /**
074     * Post data to the provided URL
075     *
076     * @param url string containing URL
077     * @param content string containing data
078     * @param mimeType string containing MIMe type of content
079     * @throws Exception in the event of an HTTP client failure
080     */
081    public static void httpPost(final String url, final String content, final String mimeType) throws Exception {
082        final CloseableHttpClient httpClient = HttpClients.createDefault();
083        final HttpPost post = new HttpPost(url);
084        post.addHeader(Exchange.CONTENT_TYPE, mimeType);
085        post.setEntity(new StringEntity(content));
086        httpClient.execute(post);
087    }
088
089    /**
090     * Create a new FcrepoClient instance with authentication.
091     * @return the newly created client
092     */
093    public static FcrepoClient createClient() {
094        return client().throwExceptionOnFailure()
095                .authScope("localhost")
096                .credentials(FEDORA_USERNAME, FEDORA_PASSWORD).build();
097    }
098
099    private TestUtils() {
100        // prevent instantiation
101    }
102}