001/**
002 * Copyright 2015 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.sword.integration;
017
018import org.apache.abdera.Abdera;
019import org.apache.abdera.model.Document;
020import org.apache.abdera.model.Service;
021import org.apache.http.HttpResponse;
022import org.apache.http.client.HttpClient;
023import org.apache.http.client.methods.HttpGet;
024import org.apache.http.client.methods.HttpPost;
025import org.apache.http.entity.StringEntity;
026import org.apache.http.impl.client.HttpClientBuilder;
027import org.fcrepo.sword.provider.SWORDServiceProvider;
028import org.junit.Before;
029import org.junit.Test;
030import org.junit.runner.RunWith;
031import org.springframework.test.context.ContextConfiguration;
032import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
033
034import java.io.IOException;
035import java.io.InputStream;
036
037import static org.fcrepo.sword.integration.Assert.assertStatusCode;
038
039/**
040 * @author claussni
041 */
042@RunWith(SpringJUnit4ClassRunner.class)
043@ContextConfiguration("/spring-test/test-container.xml")
044public abstract class BaseServiceProviderIT {
045
046    protected static final int SERVER_PORT = Integer.parseInt(System
047            .getProperty("fcrepo.dynamic.test.port", "8080"));
048
049    protected static final String HOSTNAME = "localhost";
050
051    protected static final String SERVICE_NAME = "fcr:sword";
052
053    protected static final String serverAddress =
054            String.format("http://%s:%s/%s", HOSTNAME, SERVER_PORT, SERVICE_NAME);
055
056    protected HttpClient httpClient;
057
058    protected Service serviceDocumentFromStream(final InputStream content) {
059        final Abdera abdera = new Abdera();
060        final Document<Service> serviceDocument = abdera.getParser().parse(content);
061        return serviceDocument.getRoot();
062    }
063
064    @Before
065    public void setupHttpClient() {
066        httpClient = HttpClientBuilder.create().build();
067    }
068
069    @Test
070    public void fedoraRepositoryIsResponding() throws IOException {
071        final HttpGet get = new HttpGet(String.format("http://%s:%s/", HOSTNAME, SERVER_PORT));
072        final HttpResponse response = httpClient.execute(get);
073        assertStatusCode(200, response);
074    }
075
076    protected HttpResponse requestServiceDocument() throws IOException {
077        final HttpGet get = new HttpGet(serverAddress);
078        get.setHeader("Content-Type", "application/svc+xml");
079        return httpClient.execute(get);
080    }
081
082    protected HttpResponse createWorkspaceNode(final String title) throws IOException {
083        final HttpPost post = new HttpPost(String.format("http://%s:%s/%s/",
084                HOSTNAME,
085                SERVER_PORT,
086                SWORDServiceProvider.SWORD_WORKSPACES_PATH));
087        post.setHeader("Content-Type", "text/turtle");
088        post.setEntity(new StringEntity(
089                String.format("PREFIX dc: <http://purl.org/dc/elements/1.1/>\n" +
090                        "<> dc:title \"%s\"", title)));
091        return httpClient.execute(post);
092    }
093}