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.HttpUriRequest; 024import org.apache.http.impl.client.HttpClientBuilder; 025import org.apache.http.util.EntityUtils; 026import org.junit.Before; 027import org.junit.runner.RunWith; 028import org.slf4j.Logger; 029import org.slf4j.LoggerFactory; 030import org.springframework.test.context.ContextConfiguration; 031import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 032 033import java.io.IOException; 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 private static final int SERVER_PORT = Integer.parseInt(System 052 .getProperty("fcrepo.dynamic.test.port", "8080")); 053 054 private static final String HOSTNAME = "localhost"; 055 056 protected static final String serverAddress = "http://" + HOSTNAME + 057 ":" + SERVER_PORT + "/"; 058 059 private static HttpClient client; 060 061 public AbstractResourceIT() { 062 client = 063 HttpClientBuilder.create().setMaxConnPerRoute(5).setMaxConnTotal( 064 Integer.MAX_VALUE).build(); 065 } 066 067 protected static HttpPost postObjMethod(final String pid) { 068 return new HttpPost(serverAddress + pid); 069 } 070 071 protected static HttpPost postObjMethod(final String pid, 072 final String query) { 073 if (query.equals("")) { 074 return new HttpPost(serverAddress + pid); 075 } 076 return new HttpPost(serverAddress + pid + "?" + query); 077 } 078 079 protected HttpResponse execute(final HttpUriRequest method) 080 throws IOException { 081 logger.debug("Executing: " + method.getMethod() + " to " + 082 method.getURI()); 083 return client.execute(method); 084 } 085 086 protected int getStatus(final HttpUriRequest method) 087 throws IOException { 088 final HttpResponse response = execute(method); 089 final int result = response.getStatusLine().getStatusCode(); 090 if (!(result > 199) || !(result < 400)) { 091 logger.warn(EntityUtils.toString(response.getEntity())); 092 } 093 return result; 094 } 095}