org.fcrepo.it.BasicIT.java Source code

Java tutorial

Introduction

Here is the source code for org.fcrepo.it.BasicIT.java

Source

/*
 * Copyright 2015 DuraSpace, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.fcrepo.it;

/**
 * An access exception has occurred.
 *
 * @author yinlinchen
 * @since 2016-04-06
 */
import static java.lang.Integer.MAX_VALUE;

import java.io.IOException;

import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPatch;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClientBuilder;
import org.junit.Assert;
import org.junit.Test;

public class BasicIT {

    public static String FEDORA_CONTEXT = System.getProperty("fcrepo.context");

    private static String CARGO_PORT = "8080";

    private static final String serverHost = "localhost";

    private static HttpClient client = createClient();

    private static HttpClient createClient() {
        return HttpClientBuilder.create().setMaxConnPerRoute(MAX_VALUE).setMaxConnTotal(MAX_VALUE).build();
    }

    private static int getStatusCode(final HttpRequestBase request) throws IOException {
        try {
            return client.execute(request).getStatusLine().getStatusCode();
        } finally {
            request.releaseConnection();
        }
    }

    private static String getFedoraBaseUrl() {
        String endpoint = "/rest";
        if (FEDORA_CONTEXT == null || FEDORA_CONTEXT.isEmpty()) {
            FEDORA_CONTEXT = "";
            endpoint = "rest";
        }

        return "http://" + serverHost + ":" + CARGO_PORT + "/" + FEDORA_CONTEXT + endpoint;
    }

    private static String getURIForPid(final String pid) {
        return getFedoraBaseUrl() + pid;
    }

    private static void setTitle(final String pid, final String title) throws IOException {
        final String sparqlUpdate = "prefix dc: <http://purl.org/dc/elements/1.1/>" + " insert data { <"
                + getURIForPid(pid) + "> dc:title '" + title + "' . }";
        updateProperties(pid, sparqlUpdate);
    }

    private static void updateProperties(final String pid, final String sparql) throws IOException {
        final HttpPatch patch = new HttpPatch(getURIForPid(pid));
        patch.setHeader("Content-type", "application/sparql-update");
        patch.setEntity(new StringEntity(sparql));
        Assert.assertEquals(204, getStatusCode(patch));
    }

    @Test
    public void testFedoraIsRunning() throws IOException {
        Assert.assertEquals("Fedora must be running!", 200, getStatusCode(new HttpGet(getFedoraBaseUrl())));
    }

    private static final String pid101 = "/objects/101";

    private static final String content1 = "@prefix dc: <http://purl.org/dc/elements/1.1/> .\n"
            + "@prefix pcdm: <http://pcdm.org/models#> .\n" + "@prefix ldp: <http://www.w3.org/ns/ldp#> .\n\n"
            + "<> a ldp:Container, pcdm:Object ;\n" + "dc:title \"An Object\" .";

    @Test
    public void testAddContainer() throws IOException {

        // Create a container with title
        putDummyDatastream(pid101, "text/turtle");
        try {
            Thread.sleep(5000);
        } catch (final InterruptedException e) {
            e.printStackTrace();
        }

    }

    private static void putDummyDatastream(final String path, final String mimeType) throws IOException {
        final HttpPut put = new HttpPut(getURIForPid(path));
        put.setHeader("Content-type", mimeType);
        put.setEntity(new StringEntity(content1));
        Assert.assertEquals(201, getStatusCode(put));
    }

}