From 039fe044064efe28b4d77f44734cc69d8e29d309 Mon Sep 17 00:00:00 2001 From: Kristijan Conkas Date: Wed, 7 Dec 2016 15:44:52 +0000 Subject: [PATCH] RM-4405: electronic records client API implementation --- .../rm-automation-community-rest-api/pom.xml | 5 ++ .../requests/FilePlanComponentAPI.java | 86 +++++++++++++++++-- 2 files changed, 84 insertions(+), 7 deletions(-) diff --git a/rm-automation/rm-automation-community-rest-api/pom.xml b/rm-automation/rm-automation-community-rest-api/pom.xml index ab99e46c1e..81a3e2c542 100644 --- a/rm-automation/rm-automation-community-rest-api/pom.xml +++ b/rm-automation/rm-automation-community-rest-api/pom.xml @@ -33,6 +33,11 @@ + + com.jayway.restassured + rest-assured + 2.9.0 + org.alfresco.tas restapi-test diff --git a/rm-automation/rm-automation-community-rest-api/src/main/java/org/alfresco/rest/rm/community/requests/FilePlanComponentAPI.java b/rm-automation/rm-automation-community-rest-api/src/main/java/org/alfresco/rest/rm/community/requests/FilePlanComponentAPI.java index f90d9e767c..643bef70ff 100644 --- a/rm-automation/rm-automation-community-rest-api/src/main/java/org/alfresco/rest/rm/community/requests/FilePlanComponentAPI.java +++ b/rm-automation/rm-automation-community-rest-api/src/main/java/org/alfresco/rest/rm/community/requests/FilePlanComponentAPI.java @@ -26,6 +26,8 @@ */ package org.alfresco.rest.rm.community.requests; +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; @@ -35,11 +37,20 @@ import static org.springframework.http.HttpMethod.DELETE; import static org.springframework.http.HttpMethod.GET; import static org.springframework.http.HttpMethod.POST; import static org.springframework.http.HttpMethod.PUT; +import static org.testng.Assert.fail; + +import java.io.File; + +import com.google.common.io.Resources; +import com.jayway.restassured.response.Response; import org.alfresco.rest.core.RestAPI; import org.alfresco.rest.rm.community.model.fileplancomponents.FilePlanComponent; +import org.alfresco.rest.rm.community.model.fileplancomponents.FilePlanComponentType; import org.alfresco.rest.rm.community.model.fileplancomponents.FilePlanComponentsCollection; +import org.alfresco.utility.model.UserModel; import org.springframework.context.annotation.Scope; +import org.springframework.http.HttpStatus; import org.springframework.stereotype.Component; /** @@ -118,15 +129,76 @@ public class FilePlanComponentAPI extends RestAPI public FilePlanComponent createFilePlanComponent(FilePlanComponent filePlanComponentModel, String parentId) throws Exception { mandatoryObject("filePlanComponentProperties", filePlanComponentModel); - mandatoryString("parentId", parentId); + return doCreateFilePlanComponent(toJson(filePlanComponentModel), parentId); + } + + /** + * Create electronic record from file resource + * @param electronicRecordModel {@link FilePlanComponent} for electronic record to be created + * @param fileName the name of the resource file + * @param parentId parent container id + * @return newly created {@link FilePlanComponent} + * @throws Exception if operation failed + */ + public FilePlanComponent createElectronicRecord(FilePlanComponent electronicRecordModel, String fileName, String parentId) throws Exception + { + return createElectronicRecord(electronicRecordModel, new File(Resources.getResource(fileName).getFile()), parentId); + } + + /** + * Create electronic record from file resource + * @param electronicRecordModel {@link FilePlanComponent} for electronic record to be created + * @param recordContent {@link File} pointing to the content of the electronic record to be created + * @param parentId parent container id + * @return newly created {@link FilePlanComponent} + * @throws Exception if operation failed + */ + public FilePlanComponent createElectronicRecord(FilePlanComponent electronicRecordModel, File recordContent, String parentId) throws Exception + { + mandatoryObject("filePlanComponentProperties", electronicRecordModel); + if (!electronicRecordModel.getNodeType().equals(FilePlanComponentType.CONTENT_TYPE.toString())) + { + fail("Only electronic records are supported"); + } + + /* + * RestWrapper adds some headers which break multipart/form-data uploads and also assumes json POST requests. + * Upload the file using RestAssured library. + */ + UserModel currentUser = usingRestWrapper().getTestUser(); + Response response = given() + .auth().basic(currentUser.getUsername(), currentUser.getPassword()) + .multiPart("nodeBodyCreate", toJson(electronicRecordModel), "application/json") + .multiPart("filedata", recordContent, "application/octet-stream") + .expect() + .statusCode(HttpStatus.CREATED.value()) + .when() + .post("fileplan-components/{fileplanComponentId}/children?{parameters}", parentId, getParameters()) + .andReturn(); + usingRestWrapper().setStatusCode(Integer.toString(response.getStatusCode())); + LOG.info("electronic record created: " + response.getBody().prettyPrint()); + + /* return a FilePlanComponent object representing Response */ + return response.jsonPath().getObject("entry", FilePlanComponent.class); + } + + /** + * Helper method for handling low-level fileplan component creation requests + * @param requestBody + * @param parentId + * @return Newly created {@link FilePlanComponent} + * @throws Exception + */ + private FilePlanComponent doCreateFilePlanComponent(String requestBody, String parentId) throws Exception + { + mandatoryString("parentId", parentId); return usingRestWrapper().processModel(FilePlanComponent.class, requestWithBody( - POST, - toJson(filePlanComponentModel), - "fileplan-components/{fileplanComponentId}/children?{parameters}", - parentId, - getParameters() - )); + POST, + requestBody, + "fileplan-components/{fileplanComponentId}/children?{parameters}", + parentId, + getParameters())); } /**