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.auth.integration;
019
020import org.apache.http.HttpResponse;
021import org.apache.http.client.HttpClient;
022import org.apache.http.client.methods.HttpPost;
023import org.apache.http.client.methods.HttpPut;
024import org.apache.http.client.methods.HttpUriRequest;
025import org.apache.http.entity.StringEntity;
026import org.apache.http.impl.client.HttpClientBuilder;
027import org.apache.http.util.EntityUtils;
028import org.junit.Before;
029import org.junit.runner.RunWith;
030import org.slf4j.Logger;
031import org.slf4j.LoggerFactory;
032import org.springframework.test.context.ContextConfiguration;
033import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
034
035import java.io.IOException;
036import java.io.UnsupportedEncodingException;
037import java.util.UUID;
038
039/**
040 * <p>Abstract AbstractResourceIT class.</p>
041 *
042 * @author gregjan
043 */
044@RunWith(SpringJUnit4ClassRunner.class)
045@ContextConfiguration("/spring-test/test-container.xml")
046public abstract class AbstractResourceIT {
047
048    protected Logger logger;
049
050    @Before
051    public void setLogger() {
052        logger = LoggerFactory.getLogger(this.getClass());
053    }
054
055    protected static final int SERVER_PORT = Integer.parseInt(System
056            .getProperty("fcrepo.dynamic.test.port", "8080"));
057
058    protected static final String HOSTNAME = "localhost";
059
060    protected static final String serverAddress = "http://" + HOSTNAME +
061            ":" + SERVER_PORT + "/";
062
063    protected static HttpClient client;
064
065    public AbstractResourceIT() {
066        client =
067            HttpClientBuilder.create().setMaxConnPerRoute(5).setMaxConnTotal(
068                    Integer.MAX_VALUE).build();
069    }
070
071    protected static HttpPost postObjMethod(final String pid) {
072        return new HttpPost(serverAddress + pid);
073    }
074
075    protected static HttpPut putObjMethod(final String pid) {
076        return new HttpPut(serverAddress + pid);
077    }
078
079    protected static HttpPost postObjMethod(final String pid,
080            final String query) {
081        if (query.equals("")) {
082            return new HttpPost(serverAddress + pid);
083        }
084        return new HttpPost(serverAddress + pid + "?" + query);
085    }
086
087    protected static HttpPost postDSMethod(final String pid,
088            final String ds, final String content)
089            throws UnsupportedEncodingException {
090        final HttpPost post =
091                new HttpPost(serverAddress + pid + "/" + ds +
092                        "/jcr:content");
093        post.setEntity(new StringEntity(content));
094        return post;
095    }
096
097    protected static HttpPut putDSMethod(final String pid,
098            final String ds, final String content)
099            throws UnsupportedEncodingException {
100        final HttpPut put =
101                new HttpPut(serverAddress + pid + "/" + ds +
102                        "/jcr:content");
103
104        put.setEntity(new StringEntity(content));
105        return put;
106    }
107
108    protected HttpResponse execute(final HttpUriRequest method)
109            throws IOException {
110        logger.debug("Executing: " + method.getMethod() + " to " +
111                method.getURI());
112        return client.execute(method);
113    }
114
115    protected int getStatus(final HttpUriRequest method)
116            throws IOException {
117        final HttpResponse response = execute(method);
118        final int result = response.getStatusLine().getStatusCode();
119        if (!(result > 199) || !(result < 400)) {
120            logger.warn(EntityUtils.toString(response.getEntity()));
121        }
122        return result;
123    }
124
125    /**
126     * Gets a random (but valid) pid for use in testing. This pid is guaranteed
127     * to be unique within runs of this application.
128     *
129     * @return  A string containing the new Pid
130     */
131    protected static String getRandomUniquePid() {
132        return UUID.randomUUID().toString();
133    }
134}