Merged HEAD (5.2) to 5.2.N (5.2.1)

126526 jkaabimofrad: Merged FILE-FOLDER-API (5.2.0) to HEAD (5.2)
      123086 jkaabimofrad: Added support to allow the REST API tests to set the request headers.


git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/BRANCHES/DEV/5.2.N/root@126870 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Ancuta Morarasu
2016-05-11 11:59:22 +00:00
parent 4c026f6713
commit 70dbbd521d
4 changed files with 338 additions and 4 deletions

View File

@@ -26,8 +26,10 @@
package org.alfresco.rest.api.tests;
import static org.alfresco.rest.api.tests.util.RestApiUtil.toJsonAsString;
import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.fail;
import org.alfresco.repo.content.MimetypeMap;
import org.alfresco.repo.tenant.TenantUtil;
import org.alfresco.rest.api.Nodes;
@@ -39,6 +41,7 @@ import org.alfresco.rest.api.tests.RepoService.TestSite;
import org.alfresco.rest.api.tests.client.HttpResponse;
import org.alfresco.rest.api.tests.client.PublicApiClient;
import org.alfresco.rest.api.tests.client.PublicApiHttpClient.BinaryPayload;
import org.alfresco.rest.api.tests.client.PublicApiHttpClient.RequestBuilder;
import org.alfresco.rest.api.tests.client.RequestContext;
import org.alfresco.rest.api.tests.client.data.Document;
import org.alfresco.rest.api.tests.client.data.Folder;
@@ -63,6 +66,9 @@ import java.util.Map;
*/
public abstract class AbstractBaseApiTest extends EnterpriseTestApi
{
public static final String LAST_MODIFIED_HEADER = "Last-Modified";
public static final String IF_MODIFIED_SINCE_HEADER = "If-Modified-Since";
private static final String RESOURCE_PREFIX = "publicapi/upload/";
private static final String URL_NODES = "nodes/";
@@ -186,6 +192,22 @@ public abstract class AbstractBaseApiTest extends EnterpriseTestApi
return response;
}
protected HttpResponse getSingle(String url, String runAsUser, String entityId, Map<String, String> params, Map<String, String> headers, int expectedStatus) throws Exception
{
RequestBuilder requestBuilder = httpClient.new GetRequestBuilder()
.setRequestContext(new RequestContext(runAsUser))
.setScope(getScope())
.setEntityCollectionName(url)
.setEntityId(entityId)
.setParams(params)
.setHeaders(headers);
HttpResponse response = publicApiClient.execute(requestBuilder);
checkStatus(expectedStatus, response.getStatusCode());
return response;
}
protected HttpResponse put(String url, String runAsUser, String entityId, String body, String queryString, int expectedStatus) throws Exception
{
publicApiClient.setRequestContext(new RequestContext(runAsUser));

View File

@@ -160,12 +160,13 @@ public class HttpResponse
if (contentType.startsWith("text/plain") || contentType.startsWith("application/json"))
{
sb.append(getResponse());
sb.append("\n");
}
else
else if(getResponseAsBytes() != null)
{
sb.append(" << ").append(getResponseAsBytes().length).append(" bytes >>");
sb.append("\n");
}
sb.append("\n");
}
return sb.toString();

View File

@@ -43,6 +43,7 @@ import javax.servlet.http.HttpServletResponse;
import org.alfresco.cmis.client.impl.AlfrescoObjectFactoryImpl;
import org.alfresco.opencmis.CMISDispatcherRegistry.Binding;
import org.alfresco.rest.api.tests.client.PublicApiHttpClient.BinaryPayload;
import org.alfresco.rest.api.tests.client.PublicApiHttpClient.RequestBuilder;
import org.alfresco.rest.api.tests.client.data.Activities;
import org.alfresco.rest.api.tests.client.data.Activity;
import org.alfresco.rest.api.tests.client.data.CMISNode;
@@ -599,6 +600,19 @@ public class PublicApiClient
return response;
}
public HttpResponse execute(RequestBuilder requestBuilder) throws IOException
{
if (requestBuilder.getRequestContext() == null)
{
throw new RuntimeException("Must set a request context");
}
HttpResponse response = client.execute(requestBuilder);
logger.debug(response.toString());
return response;
}
public HttpResponse index(Map<String, String> params) throws IOException
{
HttpResponse response = get("", params);

View File

@@ -36,6 +36,7 @@ import java.net.URLEncoder;
import java.text.MessageFormat;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import org.alfresco.error.AlfrescoRuntimeException;
import org.alfresco.opencmis.CMISDispatcherRegistry.Binding;
@@ -53,6 +54,7 @@ import org.alfresco.util.Pair;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.HttpMethodBase;
import org.apache.commons.httpclient.methods.ByteArrayRequestEntity;
import org.apache.commons.httpclient.methods.DeleteMethod;
import org.apache.commons.httpclient.methods.GetMethod;
@@ -619,6 +621,11 @@ public class PublicApiHttpClient
return submitRequest(req, rq);
}
public HttpResponse execute(RequestBuilder requestBuilder) throws IOException
{
return submitRequest(requestBuilder.getHttpMethod(), requestBuilder.getRequestContext());
}
/*
* Encapsulates information relating to a rest api end point, generating and
* encoding urls based on the rest api implementation class.
@@ -977,4 +984,294 @@ public class PublicApiHttpClient
return charset;
}
}
/**
* @author Jamal Kaabi-Mofrad
*/
public abstract class RequestBuilder<T extends HttpMethod>
{
private RequestContext requestContext;
private String scope;
private int version = 1;
private String entityCollectionName;
private Object entityId;
private String relationCollectionName;
private Object relationshipEntityId;
private Map<String, String> params;
private Map<String, String> headers;
public abstract T getHttpMethod() throws IOException;
protected void setRequestHeaderIfAny(T methodBase)
{
if (headers != null)
{
for (Entry<String, String> headerNameValue : headers.entrySet())
{
methodBase.addRequestHeader(headerNameValue.getKey(), headerNameValue.getValue());
}
}
}
public RequestContext getRequestContext()
{
return requestContext;
}
public RequestBuilder setRequestContext(RequestContext requestContext)
{
this.requestContext = requestContext;
return this;
}
public String getScope()
{
return scope;
}
public RequestBuilder setScope(String scope)
{
this.scope = scope;
return this;
}
public int getVersion()
{
return version;
}
public RequestBuilder setVersion(int version)
{
this.version = version;
return this;
}
public String getEntityCollectionName()
{
return entityCollectionName;
}
public RequestBuilder setEntityCollectionName(String entityCollectionName)
{
this.entityCollectionName = entityCollectionName;
return this;
}
public Object getEntityId()
{
return entityId;
}
public RequestBuilder setEntityId(Object entityId)
{
this.entityId = entityId;
return this;
}
public String getRelationCollectionName()
{
return relationCollectionName;
}
public RequestBuilder setRelationCollectionName(String relationCollectionName)
{
this.relationCollectionName = relationCollectionName;
return this;
}
public Object getRelationshipEntityId()
{
return relationshipEntityId;
}
public RequestBuilder setRelationshipEntityId(Object relationshipEntityId)
{
this.relationshipEntityId = relationshipEntityId;
return this;
}
public Map<String, String> getParams()
{
return params;
}
public RequestBuilder setParams(Map<String, String> params)
{
this.params = params;
return this;
}
public Map<String, String> getHeaders()
{
return headers;
}
public RequestBuilder setHeaders(Map<String, String> headers)
{
this.headers = headers;
return this;
}
}
public class GetRequestBuilder extends RequestBuilder<GetMethod>
{
private String password;
public String getPassword()
{
return password;
}
public GetRequestBuilder setPassword(String password)
{
this.password = password;
return this;
}
@Override
public GetMethod getHttpMethod() throws IOException
{
RestApiEndpoint endpoint = new RestApiEndpoint(getRequestContext().getNetworkId(),
getScope(), getVersion(), getEntityCollectionName(),
getEntityId(), getRelationCollectionName(), getRelationshipEntityId(), getParams());
String url = endpoint.getUrl();
GetMethod req = new GetMethod(url);
setRequestHeaderIfAny(req);
return req;
}
}
public class DeleteRequestBuilder extends RequestBuilder<DeleteMethod>
{
@Override
public DeleteMethod getHttpMethod() throws IOException
{
RestApiEndpoint endpoint = new RestApiEndpoint(getRequestContext().getNetworkId(),
getScope(), getVersion(), getEntityCollectionName(),
getEntityId(), getRelationCollectionName(), getRelationshipEntityId(), getParams());
String url = endpoint.getUrl();
DeleteMethod req = new DeleteMethod(url);
setRequestHeaderIfAny(req);
return req;
}
}
public abstract class PostPutRequestBuilder<T extends HttpMethodBase> extends RequestBuilder<T>
{
private String bodyAsString;
private byte[] bodyAsByteArray;
private String contentType;
public String getBodyAsString()
{
return bodyAsString;
}
public PostPutRequestBuilder setBodyAsString(String bodyAsString)
{
this.bodyAsString = bodyAsString;
return this;
}
public byte[] getBodyAsByteArray()
{
return bodyAsByteArray;
}
public PostPutRequestBuilder setBodyAsByteArray(byte[] bodyAsByteArray)
{
this.bodyAsByteArray = bodyAsByteArray;
return this;
}
public String getContentType()
{
return contentType;
}
public PostPutRequestBuilder setContentType(String contentType)
{
this.contentType = contentType;
return this;
}
}
public class PostRequestBuilder extends PostPutRequestBuilder<PostMethod>
{
@Override
public PostMethod getHttpMethod() throws IOException
{
RestApiEndpoint endpoint = new RestApiEndpoint(getRequestContext().getNetworkId(),
getScope(), getVersion(), getEntityCollectionName(),
getEntityId(), getRelationCollectionName(), getRelationshipEntityId(), getParams());
String url = endpoint.getUrl();
PostMethod req = new PostMethod(url.toString());
String contentType = getContentType();
if (getBodyAsString() != null)
{
if (contentType == null || contentType.isEmpty())
{
contentType = "application/json";
}
StringRequestEntity requestEntity = new StringRequestEntity(getBodyAsString(), contentType, "UTF-8");
req.setRequestEntity(requestEntity);
}
else if (getBodyAsByteArray() != null)
{
if (contentType == null || contentType.isEmpty())
{
contentType = "application/octet-stream";
}
ByteArrayRequestEntity requestEntity = new ByteArrayRequestEntity(getBodyAsByteArray(), contentType);
req.setRequestEntity(requestEntity);
}
setRequestHeaderIfAny(req);
return req;
}
}
public class PutRequestBuilder extends PostPutRequestBuilder<PutMethod>
{
private BinaryPayload binaryPayload;
public BinaryPayload getBinaryPayload()
{
return binaryPayload;
}
public PutRequestBuilder setBinaryPayload(BinaryPayload binaryPayload)
{
this.binaryPayload = binaryPayload;
return this;
}
@Override
public PutMethod getHttpMethod() throws IOException
{
RestApiEndpoint endpoint = new RestApiEndpoint(getRequestContext().getNetworkId(),
getScope(), getVersion(), getEntityCollectionName(),
getEntityId(), getRelationCollectionName(), getRelationshipEntityId(), getParams());
String url = endpoint.getUrl();
PutMethod req = new PutMethod(url);
if (getBodyAsString() != null)
{
StringRequestEntity requestEntity = new StringRequestEntity(getBodyAsString(), "application/json", "UTF-8");
req.setRequestEntity(requestEntity);
}
else if (binaryPayload != null)
{
BinaryRequestEntity requestEntity = new BinaryRequestEntity(binaryPayload.getFile(), binaryPayload.getMimeType(),
binaryPayload.getCharset());
req.setRequestEntity(requestEntity);
}
setRequestHeaderIfAny(req);
return req;
}
}
}