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