diff --git a/rm-automation/rm-automation-community-rest-api/pom.xml b/rm-automation/rm-automation-community-rest-api/pom.xml index 3198796e12..7ecbbea085 100644 --- a/rm-automation/rm-automation-community-rest-api/pom.xml +++ b/rm-automation/rm-automation-community-rest-api/pom.xml @@ -30,6 +30,17 @@ ${project.parent.parent.basedir}/license/description.ftl + + org.apache.maven.plugins + maven-jar-plugin + + + + test-jar + + + + diff --git a/rm-automation/rm-automation-community-rest-api/src/main/java/org/alfresco/rest/core/RMRestWrapper.java b/rm-automation/rm-automation-community-rest-api/src/main/java/org/alfresco/rest/core/RMRestWrapper.java index c59bc2defe..d955094c73 100644 --- a/rm-automation/rm-automation-community-rest-api/src/main/java/org/alfresco/rest/core/RMRestWrapper.java +++ b/rm-automation/rm-automation-community-rest-api/src/main/java/org/alfresco/rest/core/RMRestWrapper.java @@ -26,10 +26,19 @@ */ package org.alfresco.rest.core; +import com.jayway.restassured.builder.RequestSpecBuilder; + +import org.alfresco.rest.exception.EmptyJsonResponseException; +import org.alfresco.rest.model.RestErrorModel; +import org.alfresco.rest.model.RestHtmlResponse; +import org.alfresco.rest.model.RestSiteModel; +import org.alfresco.rest.model.RestSiteModelsCollection; +import org.alfresco.rest.requests.coreAPI.RestCoreAPI; import org.alfresco.rest.rm.community.requests.igCoreAPI.RestIGCoreAPI; +import org.alfresco.utility.model.UserModel; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.context.annotation.Primary; import org.springframework.context.annotation.Scope; +import org.springframework.http.HttpStatus; import org.springframework.stereotype.Service; /** @@ -38,11 +47,13 @@ import org.springframework.stereotype.Service; * @author Tuna Aksoy * @since 2.6 */ -@Primary @Service @Scope(value = "prototype") -public class RMRestWrapper extends RestWrapper +public class RMRestWrapper { + /** The class that wraps the ReST APIs from core. */ + @Autowired + private RestWrapper restWrapper; @Autowired private RMRestProperties rmRestProperties; @@ -51,6 +62,131 @@ public class RMRestWrapper extends RestWrapper return new RestIGCoreAPI(this, rmRestProperties); } + /** Get the core class that wraps the ReST APIs. */ + public RestWrapper getRestWrapper() + { + return restWrapper; + } + + /** Authenticate specific user to Alfresco REST API */ + public void authenticateUser(UserModel userModel) + { + restWrapper.authenticateUser(userModel); + } + + /** Get the last error thrown (if any). */ + public RestErrorModel assertLastError() + { + return restWrapper.assertLastError(); + } + + /** Process responses for a collection of models as {@link RestSiteModelsCollection}. */ + public T processModels(Class classz, RestRequest simpleRequest) + { + try + { + return restWrapper.processModels(classz, simpleRequest); + } + catch (Exception e) + { + // TODO Hopefully remove this check when TAS stops using checked exceptions. + // See https://gitlab.alfresco.com/tas/alfresco-tas-restapi-test/merge_requests/392 + throw new RuntimeException(e); + } + } + + /** Process responses for a single model as {@link RestSiteModel}. */ + public T processModel(Class classz, RestRequest restRequest) + { + try + { + return restWrapper.processModel(classz, restRequest); + } + catch (Exception e) + { + // TODO Hopefully remove this check when TAS stops using checked exceptions. + // See https://gitlab.alfresco.com/tas/alfresco-tas-restapi-test/merge_requests/392 + throw new RuntimeException(e); + } + } + + /** Process a response that has no body - basically will need only the status code from it. */ + public void processEmptyModel(RestRequest simpleRequest) + { + try + { + restWrapper.processEmptyModel(simpleRequest); + } + catch (EmptyJsonResponseException e) + { + // TODO Hopefully remove this check when TAS stops using checked exceptions. + // See https://gitlab.alfresco.com/tas/alfresco-tas-restapi-test/merge_requests/392 + throw new RuntimeException(e); + } + } + + /** Get the most recently returned status code. */ + public String getStatusCode() + { + return restWrapper.getStatusCode(); + } + + /** Set the status code. This should only be needed when calling APIs without using the TAS framework. */ + public void setStatusCode(String statusCode) + { + restWrapper.setStatusCode(statusCode); + } + + /** Assert that a specific status code is returned. */ + public void assertStatusCodeIs(HttpStatus statusCode) + { + restWrapper.assertStatusCodeIs(statusCode); + } + + /** @return A parameters string that you could pass on the request ?param=value */ + public String getParameters() + { + return restWrapper.getParameters(); + } + + /** Create a {@link UserModel} for a new test user. */ + public UserModel getTestUser() + { + return restWrapper.getTestUser(); + } + + /** Get the Alfresco Core API. */ + public RestCoreAPI withCoreAPI() + { + return restWrapper.withCoreAPI(); + } + + /** + * You can handle the request sent to server by calling this method. + * If for example you want to sent multipart form data you can use:
+     * restClient.configureRequestSpec()
+     *              .addMultiPart("filedata", Utility.getResourceTestDataFile("restapi-resource"))
+     *              .addFormParam("renditions", "doclib")
+     *              .addFormParam("autoRename", true);
+     *
+     * restClient.withCoreAPI().usingNode(ContentModel.my()).createNode();
+     * 
This will create the node using the multipart data defined. + */ + public RequestSpecBuilder configureRequestSpec() + { + return restWrapper.configureRequestSpec(); + } + + /** + * Process a response that returns a html + * + * @throws EmptyJsonResponseException If there is no response from the server. + */ + public RestHtmlResponse processHtmlResponse(RestRequest simpleRequest) + { + return restWrapper.processHtmlResponse(simpleRequest); + } + /** * @return the rmRestProperties */ diff --git a/rm-automation/rm-automation-community-rest-api/src/main/java/org/alfresco/rest/core/RestAPIFactory.java b/rm-automation/rm-automation-community-rest-api/src/main/java/org/alfresco/rest/core/RestAPIFactory.java index fd8882e7ba..41370c7a55 100644 --- a/rm-automation/rm-automation-community-rest-api/src/main/java/org/alfresco/rest/core/RestAPIFactory.java +++ b/rm-automation/rm-automation-community-rest-api/src/main/java/org/alfresco/rest/core/RestAPIFactory.java @@ -26,6 +26,8 @@ */ package org.alfresco.rest.core; +import javax.annotation.Resource; + import org.alfresco.rest.requests.Node; import org.alfresco.rest.requests.coreAPI.RestCoreAPI; import org.alfresco.rest.rm.community.requests.igCoreAPI.FilePlanComponentAPI; @@ -54,7 +56,7 @@ public class RestAPIFactory @Autowired private DataUser dataUser; - @Autowired + @Resource(name = "RMRestWrapper") private RMRestWrapper rmRestWrapper; /** @@ -65,6 +67,11 @@ public class RestAPIFactory return this.rmRestWrapper; } + public void setRmRestWrapper(RMRestWrapper rmRestWrapper) + { + this.rmRestWrapper = rmRestWrapper; + } + private RestIGCoreAPI getRestIGCoreAPI(UserModel userModel) { getRmRestWrapper().authenticateUser(userModel != null ? userModel : dataUser.getAdminUser()); diff --git a/rm-automation/rm-automation-community-rest-api/src/main/java/org/alfresco/rest/rm/community/requests/RMModelRequest.java b/rm-automation/rm-automation-community-rest-api/src/main/java/org/alfresco/rest/rm/community/requests/RMModelRequest.java index b56bcad897..bf0648a60c 100644 --- a/rm-automation/rm-automation-community-rest-api/src/main/java/org/alfresco/rest/rm/community/requests/RMModelRequest.java +++ b/rm-automation/rm-automation-community-rest-api/src/main/java/org/alfresco/rest/rm/community/requests/RMModelRequest.java @@ -42,17 +42,17 @@ public abstract class RMModelRequest extends ModelRequest /** * @return the rmRestWrapper */ - protected RMRestWrapper getRMRestWrapper() + public RMRestWrapper getRMRestWrapper() { return this.rmRestWrapper; } /** - * @param restWrapper + * @param rmRestWrapper */ public RMModelRequest(RMRestWrapper rmRestWrapper) { - super(rmRestWrapper); + super(rmRestWrapper.getRestWrapper()); this.rmRestWrapper = rmRestWrapper; } } diff --git a/rm-automation/rm-automation-community-rest-api/src/main/java/org/alfresco/rest/rm/community/requests/igCoreAPI/FilePlanComponentAPI.java b/rm-automation/rm-automation-community-rest-api/src/main/java/org/alfresco/rest/rm/community/requests/igCoreAPI/FilePlanComponentAPI.java index 88b3c31c8a..afd8c15de0 100644 --- a/rm-automation/rm-automation-community-rest-api/src/main/java/org/alfresco/rest/rm/community/requests/igCoreAPI/FilePlanComponentAPI.java +++ b/rm-automation/rm-automation-community-rest-api/src/main/java/org/alfresco/rest/rm/community/requests/igCoreAPI/FilePlanComponentAPI.java @@ -26,6 +26,8 @@ */ package org.alfresco.rest.rm.community.requests.igCoreAPI; +import static com.jayway.restassured.RestAssured.basic; +import static com.jayway.restassured.RestAssured.given; import static org.alfresco.rest.core.RestRequest.requestWithBody; import static org.alfresco.rest.core.RestRequest.simpleRequest; import static org.alfresco.rest.rm.community.model.fileplancomponents.FilePlanComponentType.CONTENT_TYPE; diff --git a/rm-automation/rm-automation-community-rest-api/src/main/java/org/alfresco/rest/rm/community/requests/igCoreAPI/FilesAPI.java b/rm-automation/rm-automation-community-rest-api/src/main/java/org/alfresco/rest/rm/community/requests/igCoreAPI/FilesAPI.java index 83df47ab11..afa93ac2f8 100644 --- a/rm-automation/rm-automation-community-rest-api/src/main/java/org/alfresco/rest/rm/community/requests/igCoreAPI/FilesAPI.java +++ b/rm-automation/rm-automation-community-rest-api/src/main/java/org/alfresco/rest/rm/community/requests/igCoreAPI/FilesAPI.java @@ -65,7 +65,7 @@ public class FilesAPI extends RMModelRequest public FilePlanComponent declareAsRecord(String fileId, String parameters) throws Exception { mandatoryString("fileId", fileId); - + return getRMRestWrapper().processModel(FilePlanComponent.class, simpleRequest( POST, "/files/{fileId}/declare?{parameters}", @@ -73,7 +73,7 @@ public class FilesAPI extends RMModelRequest parameters )); } - + /** * A no-parameter version of {@link FilesAPI#declareAsRecord} * @param fileId The Id of a file to declare as record diff --git a/rm-automation/rm-automation-community-rest-api/src/main/java/org/alfresco/rest/rm/community/requests/igCoreAPI/RecordsAPI.java b/rm-automation/rm-automation-community-rest-api/src/main/java/org/alfresco/rest/rm/community/requests/igCoreAPI/RecordsAPI.java index a11219e193..be85682079 100644 --- a/rm-automation/rm-automation-community-rest-api/src/main/java/org/alfresco/rest/rm/community/requests/igCoreAPI/RecordsAPI.java +++ b/rm-automation/rm-automation-community-rest-api/src/main/java/org/alfresco/rest/rm/community/requests/igCoreAPI/RecordsAPI.java @@ -26,6 +26,7 @@ */ package org.alfresco.rest.rm.community.requests.igCoreAPI; +import static com.jayway.restassured.RestAssured.given; import static org.alfresco.rest.core.RestRequest.requestWithBody; import static org.alfresco.rest.core.RestRequest.simpleRequest; import static org.alfresco.rest.rm.community.util.ParameterCheck.mandatoryObject; diff --git a/rm-automation/rm-automation-community-rest-api/src/main/java/org/alfresco/rest/rm/community/util/PojoUtility.java b/rm-automation/rm-automation-community-rest-api/src/main/java/org/alfresco/rest/rm/community/util/PojoUtility.java index e865118d79..ba4f7960c7 100644 --- a/rm-automation/rm-automation-community-rest-api/src/main/java/org/alfresco/rest/rm/community/util/PojoUtility.java +++ b/rm-automation/rm-automation-community-rest-api/src/main/java/org/alfresco/rest/rm/community/util/PojoUtility.java @@ -26,12 +26,8 @@ */ package org.alfresco.rest.rm.community.util; -import java.io.IOException; - import com.fasterxml.jackson.annotation.JsonInclude.Include; -import com.fasterxml.jackson.core.JsonGenerationException; import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.JsonMappingException; import com.fasterxml.jackson.databind.ObjectMapper; import org.alfresco.rest.rm.community.model.fileplancomponents.FilePlanComponent; @@ -48,9 +44,8 @@ public class PojoUtility * Converting object to JSON string * * @param model The java object model to convert - * @throws JsonProcessingException Throws exceptions if the given object doesn't match to the POJO class model */ - public static String toJson(Object model) throws JsonProcessingException + public static String toJson(Object model) { ObjectMapper mapper = new ObjectMapper(); //include only values that differ from default settings to be included @@ -60,15 +55,7 @@ public class PojoUtility //return the json object return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(model); } - catch (JsonGenerationException e) - { - return e.toString(); - } - catch (JsonMappingException e) - { - return e.toString(); - } - catch (IOException e) + catch (JsonProcessingException e) { return e.toString(); } @@ -81,7 +68,7 @@ public class PojoUtility * @param model The java object model to convert * @throws JsonProcessingException Throws exceptions if the given object doesn't match to the POJO class model */ - public static String toJsonElectronicRecord(Object model) throws JsonProcessingException + public static String toJsonElectronicRecord(Object model) { ObjectMapper mapper = new ObjectMapper(); @@ -93,20 +80,10 @@ public class PojoUtility mapper.setSerializationInclusion(Include.NON_DEFAULT); try { - //return the json object return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(model); - } - catch (JsonGenerationException e) - { - return e.toString(); - } - catch (JsonMappingException e) - { - return e.toString(); - } - catch (IOException e) + catch (JsonProcessingException e) { return e.toString(); } diff --git a/rm-automation/rm-automation-community-rest-api/src/test/java/org/alfresco/rest/rm/community/base/BaseRMRestTest.java b/rm-automation/rm-automation-community-rest-api/src/test/java/org/alfresco/rest/rm/community/base/BaseRMRestTest.java index ebef9ce32c..cb25fcd074 100644 --- a/rm-automation/rm-automation-community-rest-api/src/test/java/org/alfresco/rest/rm/community/base/BaseRMRestTest.java +++ b/rm-automation/rm-automation-community-rest-api/src/test/java/org/alfresco/rest/rm/community/base/BaseRMRestTest.java @@ -100,7 +100,7 @@ public class BaseRMRestTest extends RestTest */ protected void assertStatusCode(HttpStatus statusCode) { - getRestAPIFactory().getRmRestWrapper().assertStatusCodeIs(statusCode); + restAPIFactory.getRmRestWrapper().assertStatusCodeIs(statusCode); } /** @@ -145,7 +145,7 @@ public class BaseRMRestTest extends RestTest */ public void createRMSiteIfNotExists() throws Exception { - RMSiteAPI rmSiteAPI = getRestAPIFactory().getRMSiteAPI(); + RMSiteAPI rmSiteAPI = restAPIFactory.getRMSiteAPI(); // Check RM site doesn't exist if (!rmSiteAPI.existsRMSite()) @@ -294,16 +294,16 @@ public class BaseRMRestTest extends RestTest assertTrue(aspects.contains(RECORD_TYPE)); // a record mustn't be closed assertFalse(aspects.contains(ASPECTS_CLOSED_RECORD)); - + // add closed record aspect aspects.add(ASPECTS_CLOSED_RECORD); - - FilePlanComponent updatedComponent = filePlanComponentsAPI.updateFilePlanComponent(FilePlanComponent.builder().aspectNames(aspects).build(), + + FilePlanComponent updatedComponent = filePlanComponentsAPI.updateFilePlanComponent(FilePlanComponent.builder().aspectNames(aspects).build(), recordToClose.getId()); assertStatusCode(OK); return updatedComponent; } - + /** * Helper method to create a randomly-named / structure in file plan *