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.auth.integration;
007
008import com.github.benmanes.caffeine.cache.Cache;
009import com.github.benmanes.caffeine.cache.Caffeine;
010import com.google.common.base.Strings;
011import org.apache.http.HttpResponse;
012import org.apache.http.client.HttpClient;
013import org.apache.http.client.methods.HttpPost;
014import org.apache.http.client.methods.HttpUriRequest;
015import org.apache.http.impl.client.HttpClientBuilder;
016import org.apache.http.util.EntityUtils;
017import org.junit.Before;
018import org.junit.runner.RunWith;
019import org.slf4j.Logger;
020import org.slf4j.LoggerFactory;
021import org.springframework.context.annotation.Bean;
022import org.springframework.context.annotation.Configuration;
023import org.springframework.test.context.ContextConfiguration;
024import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
025
026import java.io.IOException;
027import java.util.Objects;
028import java.util.Optional;
029import java.util.concurrent.TimeUnit;
030
031import static java.lang.Integer.parseInt;
032
033import org.fcrepo.kernel.api.auth.ACLHandle;
034
035/**
036 * <p>Abstract AbstractResourceIT class.</p>
037 *
038 * @author gregjan
039 */
040@RunWith(SpringJUnit4ClassRunner.class)
041@ContextConfiguration("/spring-test/test-container.xml")
042public abstract class AbstractResourceIT {
043
044    private Logger logger;
045
046    @Before
047    public void setLogger() {
048        logger = LoggerFactory.getLogger(this.getClass());
049    }
050
051    @Configuration
052    static class TestConfig {
053        @Bean
054        public Cache<String, Optional<ACLHandle>> authHandleCache() {
055            return Caffeine.newBuilder().weakValues().expireAfterAccess(10, TimeUnit.SECONDS)
056                    .maximumSize(10).build();
057        }
058    }
059
060    private static final int SERVER_PORT = parseInt(Objects.requireNonNullElse(
061            Strings.emptyToNull(System.getProperty("fcrepo.dynamic.test.port")), "8080"));
062
063    private static final String HOSTNAME = "localhost";
064
065    protected static final String serverAddress = "http://" + HOSTNAME +
066            ":" + SERVER_PORT + "/";
067
068    private static HttpClient client;
069
070    public AbstractResourceIT() {
071        client =
072            HttpClientBuilder.create().setMaxConnPerRoute(5).setMaxConnTotal(
073                    Integer.MAX_VALUE).build();
074    }
075
076    protected static HttpPost postObjMethod(final String pid) {
077        return new HttpPost(serverAddress + pid);
078    }
079
080    protected static HttpPost postObjMethod(final String pid,
081            final String query) {
082        if (query.equals("")) {
083            return new HttpPost(serverAddress + pid);
084        }
085        return new HttpPost(serverAddress + pid + "?" + query);
086    }
087
088    protected HttpResponse execute(final HttpUriRequest method)
089            throws IOException {
090        logger.debug("Executing: " + method.getMethod() + " to " +
091                method.getURI());
092        return client.execute(method);
093    }
094
095    protected int getStatus(final HttpUriRequest method)
096            throws IOException {
097        final HttpResponse response = execute(method);
098        final int result = response.getStatusLine().getStatusCode();
099        if (!(result > 199) || !(result < 400)) {
100            logger.warn(EntityUtils.toString(response.getEntity()));
101        }
102        return result;
103    }
104}