From 70dbbd521d3959ce59d10e53d956dcba9e02ab8f Mon Sep 17 00:00:00 2001 From: Ancuta Morarasu Date: Wed, 11 May 2016 11:59:22 +0000 Subject: [PATCH] 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 --- .../rest/api/tests/AbstractBaseApiTest.java | 26 +- .../rest/api/tests/client/HttpResponse.java | 5 +- .../api/tests/client/PublicApiClient.java | 14 + .../api/tests/client/PublicApiHttpClient.java | 297 ++++++++++++++++++ 4 files changed, 338 insertions(+), 4 deletions(-) diff --git a/source/test-java/org/alfresco/rest/api/tests/AbstractBaseApiTest.java b/source/test-java/org/alfresco/rest/api/tests/AbstractBaseApiTest.java index 05783f5a84..a68a485c41 100644 --- a/source/test-java/org/alfresco/rest/api/tests/AbstractBaseApiTest.java +++ b/source/test-java/org/alfresco/rest/api/tests/AbstractBaseApiTest.java @@ -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 params, Map 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)); diff --git a/source/test-java/org/alfresco/rest/api/tests/client/HttpResponse.java b/source/test-java/org/alfresco/rest/api/tests/client/HttpResponse.java index 65d065eca3..4050f1ffa9 100644 --- a/source/test-java/org/alfresco/rest/api/tests/client/HttpResponse.java +++ b/source/test-java/org/alfresco/rest/api/tests/client/HttpResponse.java @@ -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(); diff --git a/source/test-java/org/alfresco/rest/api/tests/client/PublicApiClient.java b/source/test-java/org/alfresco/rest/api/tests/client/PublicApiClient.java index 6bfe478b71..056b797c1f 100644 --- a/source/test-java/org/alfresco/rest/api/tests/client/PublicApiClient.java +++ b/source/test-java/org/alfresco/rest/api/tests/client/PublicApiClient.java @@ -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 params) throws IOException { HttpResponse response = get("", params); diff --git a/source/test-java/org/alfresco/rest/api/tests/client/PublicApiHttpClient.java b/source/test-java/org/alfresco/rest/api/tests/client/PublicApiHttpClient.java index 34906532da..7f3ca1eb1a 100644 --- a/source/test-java/org/alfresco/rest/api/tests/client/PublicApiHttpClient.java +++ b/source/test-java/org/alfresco/rest/api/tests/client/PublicApiHttpClient.java @@ -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 + { + private RequestContext requestContext; + private String scope; + private int version = 1; + private String entityCollectionName; + private Object entityId; + private String relationCollectionName; + private Object relationshipEntityId; + private Map params; + private Map headers; + + public abstract T getHttpMethod() throws IOException; + + protected void setRequestHeaderIfAny(T methodBase) + { + if (headers != null) + { + for (Entry 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 getParams() + { + return params; + } + + public RequestBuilder setParams(Map params) + { + this.params = params; + return this; + } + + public Map getHeaders() + { + return headers; + } + + public RequestBuilder setHeaders(Map headers) + { + this.headers = headers; + return this; + } + } + + public class GetRequestBuilder extends RequestBuilder + { + 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 + { + @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 extends RequestBuilder + { + 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 + { + + @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 + { + 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; + } + } }