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.integration;
019
020import org.apache.http.HttpResponse;
021import org.apache.http.client.HttpClient;
022import org.apache.http.client.methods.HttpPost;
023import org.apache.http.impl.client.HttpClientBuilder;
024import org.junit.runner.RunWith;
025import org.slf4j.Logger;
026import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
027
028import java.io.IOException;
029
030import static java.lang.Integer.MAX_VALUE;
031import static java.lang.Integer.parseInt;
032import static java.lang.System.getProperty;
033import static javax.ws.rs.core.Response.Status.CREATED;
034import static org.junit.Assert.assertEquals;
035import static org.slf4j.LoggerFactory.getLogger;
036
037/**
038 * <p>Abstract AbstractResourceIT class.</p>
039 *
040 * @author cbeer
041 * @author ajs6f
042 */
043@RunWith(SpringJUnit4ClassRunner.class)
044public abstract class AbstractResourceIT {
045
046    protected static Logger logger = getLogger(AbstractResourceIT.class);
047
048    protected static final int SERVER_PORT = parseInt(getProperty("fcrepo.dynamic.test.port",
049                                                                         "8080"));
050    protected static final String HOSTNAME = "localhost";
051
052    protected static final String serverAddress = "http://" + HOSTNAME + ":"
053            + SERVER_PORT;
054
055    protected static HttpClient client;
056
057    public AbstractResourceIT() {
058        final HttpClientBuilder b =
059            HttpClientBuilder.create().setMaxConnPerRoute(MAX_VALUE)
060                    .setMaxConnTotal(MAX_VALUE);
061        client = b.build();
062    }
063
064    protected static HttpPost postObjMethod(final String pid) {
065        return new HttpPost(serverAddress + pid);
066    }
067
068    protected HttpResponse createObject(final String pid) throws IOException {
069        final HttpPost httpPost = postObjMethod("/");
070        if (pid.length() > 0) {
071            httpPost.addHeader("Slug", pid);
072        }
073        final HttpResponse response = client.execute(httpPost);
074        assertEquals(CREATED.getStatusCode(), response.getStatusLine().getStatusCode());
075        return response;
076    }
077}