RM-4361: generalised getRecordContent() method + test for binary content

This commit is contained in:
Kristijan Conkas
2017-01-05 12:17:25 +00:00
parent a2e661f741
commit b12a2b9241
3 changed files with 47 additions and 57 deletions

View File

@@ -31,20 +31,18 @@ import static com.jayway.restassured.RestAssured.given;
import static org.alfresco.rest.rm.community.util.ParameterCheck.mandatoryString; import static org.alfresco.rest.rm.community.util.ParameterCheck.mandatoryString;
import com.jayway.restassured.response.Response; import com.jayway.restassured.response.Response;
import com.jayway.restassured.response.ResponseBody;
import org.alfresco.rest.core.RMRestWrapper; import org.alfresco.rest.core.RMRestWrapper;
import org.alfresco.rest.core.RestRequest;
import org.alfresco.rest.model.RestHtmlResponse;
import org.alfresco.rest.rm.community.requests.RMModelRequest; import org.alfresco.rest.rm.community.requests.RMModelRequest;
import org.springframework.context.annotation.Scope; import org.springframework.context.annotation.Scope;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
/** /**
*Records REST API Wrapper * Records REST API Wrapper
* *
*@author Rodica Sutu * @author Rodica Sutu
*@since 2.6 * @since 2.6
*/ */
@Component @Component
@Scope (value = "prototype") @Scope (value = "prototype")
@@ -62,7 +60,7 @@ public class RecordsAPI extends RMModelRequest
* Get the content for the electronic record * Get the content for the electronic record
* *
* @param recordId The id of the electronic record * @param recordId The id of the electronic record
* @return The content for the given record id * @return {@link ResponseBody} representing content for the given record id
* @throws Exception for the following cases: * @throws Exception for the following cases:
* <ul> * <ul>
* <li>{@code recordId} has no content</li> * <li>{@code recordId} has no content</li>
@@ -71,40 +69,16 @@ public class RecordsAPI extends RMModelRequest
* <li>{@code recordId} does not exist</li> * <li>{@code recordId} does not exist</li>
* </ul> * </ul>
*/ */
//FIXME Add a generic method to support retrieving binary content public ResponseBody<?> getRecordContent(String recordId) throws Exception
public <T> T getRecordContentText(String recordId) throws Exception
{ {
mandatoryString("recordId", recordId); mandatoryString("recordId", recordId);
Response response = given().auth().basic(getRMRestWrapper().getTestUser().getUsername(), Response response = given()
getRMRestWrapper().getTestUser().getPassword() .auth().basic(getRMRestWrapper().getTestUser().getUsername(),
) getRMRestWrapper().getTestUser().getPassword())
.when()
.get("records/{recordId}/content", recordId) .get("records/{recordId}/content", recordId)
.andReturn(); .andReturn();
getRMRestWrapper().setStatusCode(Integer.toString(response.getStatusCode())); getRMRestWrapper().setStatusCode(Integer.toString(response.getStatusCode()));
return response.getBody();
return (T) response.getBody().prettyPrint();
}
/**
* Get the content RestHtmlResponse(Response header and body) for the electronic record
*
* @param recordId The id of the electronic record
* @return The body and the header for the record
* @throws Exception for the following cases:
* <ul>
* <li>{@code recordId} has no content</li>
* <li> {@code recordId} is not a valid format, or is not a record</li>
* <li>authentication fails</li>
* <li>{@code recordId} does not exist</li>
* </ul>
*/
//FIXME Add a generic method to support retrieving binary content as we might end up
//FIXME with too many methods for differents content types
public RestHtmlResponse getRecordContent(String recordId) throws Exception
{
mandatoryString("recordId", recordId);
RestRequest request = RestRequest.simpleRequest(HttpMethod.GET, "records/{recordId}/content", recordId);
return getRMRestWrapper().processHtmlResponse(request);
} }
} }

View File

@@ -33,8 +33,8 @@ import static org.alfresco.rest.rm.community.model.fileplancomponents.FilePlanCo
import static org.alfresco.rest.rm.community.model.fileplancomponents.FilePlanComponentAspects.ASPECTS_CLOSED_RECORD; import static org.alfresco.rest.rm.community.model.fileplancomponents.FilePlanComponentAspects.ASPECTS_CLOSED_RECORD;
import static org.alfresco.rest.rm.community.model.fileplancomponents.FilePlanComponentType.RECORD_CATEGORY_TYPE; import static org.alfresco.rest.rm.community.model.fileplancomponents.FilePlanComponentType.RECORD_CATEGORY_TYPE;
import static org.alfresco.rest.rm.community.model.fileplancomponents.FilePlanComponentType.RECORD_FOLDER_TYPE; import static org.alfresco.rest.rm.community.model.fileplancomponents.FilePlanComponentType.RECORD_FOLDER_TYPE;
import static org.alfresco.rest.rm.community.model.fileplancomponents.FilePlanComponentType.UNFILED_RECORD_FOLDER_TYPE;
import static org.alfresco.rest.rm.community.model.fileplancomponents.FilePlanComponentType.RECORD_TYPE; import static org.alfresco.rest.rm.community.model.fileplancomponents.FilePlanComponentType.RECORD_TYPE;
import static org.alfresco.rest.rm.community.model.fileplancomponents.FilePlanComponentType.UNFILED_RECORD_FOLDER_TYPE;
import static org.alfresco.rest.rm.community.utils.FilePlanComponentsUtil.createFilePlanComponentModel; import static org.alfresco.rest.rm.community.utils.FilePlanComponentsUtil.createFilePlanComponentModel;
import static org.alfresco.rest.rm.community.utils.RMSiteUtil.createStandardRMSiteModel; import static org.alfresco.rest.rm.community.utils.RMSiteUtil.createStandardRMSiteModel;
import static org.alfresco.utility.data.RandomData.getRandomAlphanumeric; import static org.alfresco.utility.data.RandomData.getRandomAlphanumeric;
@@ -45,11 +45,6 @@ import static org.testng.Assert.assertTrue;
import java.util.List; import java.util.List;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.nio.charset.Charset;
import org.alfresco.rest.RestTest; import org.alfresco.rest.RestTest;
import org.alfresco.rest.core.RestAPIFactory; import org.alfresco.rest.core.RestAPIFactory;
import org.alfresco.rest.rm.community.model.fileplancomponents.FilePlanComponent; import org.alfresco.rest.rm.community.model.fileplancomponents.FilePlanComponent;

View File

@@ -35,6 +35,7 @@ import static org.alfresco.rest.rm.community.model.fileplancomponents.FilePlanCo
import static org.alfresco.rest.rm.community.model.fileplancomponents.FilePlanComponentType.CONTENT_TYPE; import static org.alfresco.rest.rm.community.model.fileplancomponents.FilePlanComponentType.CONTENT_TYPE;
import static org.alfresco.rest.rm.community.model.fileplancomponents.FilePlanComponentType.NON_ELECTRONIC_RECORD_TYPE; import static org.alfresco.rest.rm.community.model.fileplancomponents.FilePlanComponentType.NON_ELECTRONIC_RECORD_TYPE;
import static org.alfresco.rest.rm.community.model.fileplancomponents.FilePlanComponentType.RECORD_FOLDER_TYPE; import static org.alfresco.rest.rm.community.model.fileplancomponents.FilePlanComponentType.RECORD_FOLDER_TYPE;
import static org.alfresco.rest.rm.community.utils.FilePlanComponentsUtil.IMAGE_FILE;
import static org.alfresco.rest.rm.community.utils.FilePlanComponentsUtil.createTempFile; import static org.alfresco.rest.rm.community.utils.FilePlanComponentsUtil.createTempFile;
import static org.alfresco.utility.data.RandomData.getRandomAlphanumeric; import static org.alfresco.utility.data.RandomData.getRandomAlphanumeric;
import static org.springframework.http.HttpStatus.BAD_REQUEST; import static org.springframework.http.HttpStatus.BAD_REQUEST;
@@ -45,9 +46,13 @@ import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.fail; import static org.testng.Assert.fail;
import static org.testng.AssertJUnit.assertTrue; import static org.testng.AssertJUnit.assertTrue;
import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
import com.google.common.io.Resources;
import org.alfresco.rest.rm.community.base.BaseRMRestTest; import org.alfresco.rest.rm.community.base.BaseRMRestTest;
import org.alfresco.rest.rm.community.base.TestData; import org.alfresco.rest.rm.community.base.TestData;
import org.alfresco.rest.rm.community.model.fileplancomponents.FilePlanComponent; import org.alfresco.rest.rm.community.model.fileplancomponents.FilePlanComponent;
@@ -55,6 +60,7 @@ import org.alfresco.rest.rm.community.model.fileplancomponents.FilePlanComponent
import org.alfresco.rest.rm.community.model.fileplancomponents.FilePlanComponentProperties; import org.alfresco.rest.rm.community.model.fileplancomponents.FilePlanComponentProperties;
import org.alfresco.rest.rm.community.model.fileplancomponents.FilePlanComponentsCollection; import org.alfresco.rest.rm.community.model.fileplancomponents.FilePlanComponentsCollection;
import org.alfresco.rest.rm.community.requests.igCoreAPI.FilePlanComponentAPI; import org.alfresco.rest.rm.community.requests.igCoreAPI.FilePlanComponentAPI;
import org.apache.commons.codec.digest.DigestUtils;
import org.testng.annotations.DataProvider; import org.testng.annotations.DataProvider;
import org.testng.annotations.Test; import org.testng.annotations.Test;
@@ -77,7 +83,7 @@ public class ReadRecordTests extends BaseRMRestTest
.nodeType(CONTENT_TYPE.toString()) .nodeType(CONTENT_TYPE.toString())
.content(FilePlanComponentContent.builder().mimeType("text/plain").build()) .content(FilePlanComponentContent.builder().mimeType("text/plain").build())
.build(); .build();
private FilePlanComponent nonelectronicRecord = FilePlanComponent.builder() private FilePlanComponent nonelectronicRecord = FilePlanComponent.builder()
.properties(FilePlanComponentProperties.builder() .properties(FilePlanComponentProperties.builder()
.description(NONELECTRONIC_RECORD_NAME) .description(NONELECTRONIC_RECORD_NAME)
@@ -200,32 +206,48 @@ public class ReadRecordTests extends BaseRMRestTest
public void readRecordContent() throws Exception public void readRecordContent() throws Exception
{ {
String RECORD_ELECTRONIC = "Record " + getRandomAlphanumeric(); String RECORD_ELECTRONIC = "Record " + getRandomAlphanumeric();
String RELATIVE_PATH = "/"+CATEGORY_NAME+ getRandomAlphanumeric()+"/folder"; String RECORD_ELECTRONIC_BINARY = "Binary Record" + getRandomAlphanumeric();
String RELATIVE_PATH = "/" + CATEGORY_NAME + getRandomAlphanumeric() + "/folder";
// create the containers from the relativePath
FilePlanComponentAPI filePlanComponentAPI = getRestAPIFactory().getFilePlanComponentsAPI(); FilePlanComponentAPI filePlanComponentAPI = getRestAPIFactory().getFilePlanComponentsAPI();
//create the containers from the relativePath
FilePlanComponent recordFolder = FilePlanComponent.builder() FilePlanComponent recordFolder = FilePlanComponent.builder()
.name(FOLDER_NAME) .name(FOLDER_NAME)
.nodeType(RECORD_FOLDER_TYPE.toString()) .nodeType(RECORD_FOLDER_TYPE.toString())
.relativePath(RELATIVE_PATH) .relativePath(RELATIVE_PATH)
.build(); .build();
String folderId = filePlanComponentAPI.createFilePlanComponent(recordFolder,FILE_PLAN_ALIAS.toString()).getId(); String folderId = filePlanComponentAPI.createFilePlanComponent(recordFolder, FILE_PLAN_ALIAS.toString()).getId();
//
FilePlanComponent record = FilePlanComponent.builder() // text file as an electronic record
FilePlanComponent recordText = FilePlanComponent.builder()
.name(RECORD_ELECTRONIC) .name(RECORD_ELECTRONIC)
.nodeType(CONTENT_TYPE.toString()) .nodeType(CONTENT_TYPE.toString())
.build(); .build();
String recordId = filePlanComponentAPI.createElectronicRecord(record, createTempFile(RECORD_ELECTRONIC, RECORD_ELECTRONIC), folderId).getId(); String recordId = filePlanComponentAPI.createElectronicRecord(recordText, createTempFile(RECORD_ELECTRONIC, RECORD_ELECTRONIC), folderId).getId();
assertEquals(getRestAPIFactory().getRecordsAPI().getRecordContent(recordId).asString(), RECORD_ELECTRONIC);
assertEquals(getRestAPIFactory().getRecordsAPI().getRecordContentText(recordId),RECORD_ELECTRONIC);
// Check status code // Check status code
assertStatusCode(OK); assertStatusCode(OK);
// binary file as an electronic record
FilePlanComponent recordBinary = FilePlanComponent.builder()
.name(RECORD_ELECTRONIC_BINARY)
.nodeType(CONTENT_TYPE.toString())
.build();
String binaryRecordId = filePlanComponentAPI.createElectronicRecord(recordBinary, IMAGE_FILE, folderId).getId();
// binary content, therefore compare respective SHA1 checksums in order to verify this is identical content
assertEquals(
DigestUtils.sha1(getRestAPIFactory().getRecordsAPI().getRecordContent(binaryRecordId).asInputStream()),
DigestUtils.sha1(new FileInputStream(new File(Resources.getResource(IMAGE_FILE).getFile()))));
assertStatusCode(OK);
// electronic record with no content
FilePlanComponent recordNoContent = FilePlanComponent.builder() FilePlanComponent recordNoContent = FilePlanComponent.builder()
.name(RECORD_ELECTRONIC) .name(RECORD_ELECTRONIC)
.nodeType(CONTENT_TYPE.toString()) .nodeType(CONTENT_TYPE.toString())
.build(); .build();
String recordNoContentId = filePlanComponentAPI.createFilePlanComponent(recordNoContent,folderId).getId(); String recordNoContentId = filePlanComponentAPI.createFilePlanComponent(recordNoContent,folderId).getId();
assertTrue(getRestAPIFactory().getRecordsAPI().getRecordContentText(recordNoContentId).toString().isEmpty()); assertTrue(getRestAPIFactory().getRecordsAPI().getRecordContent(recordNoContentId).asString().isEmpty());
assertStatusCode(OK); assertStatusCode(OK);
} }
/** /**
@@ -245,12 +267,11 @@ public class ReadRecordTests extends BaseRMRestTest
.relativePath("/"+CATEGORY_NAME+getRandomAlphanumeric()+"/"+FOLDER_NAME) .relativePath("/"+CATEGORY_NAME+getRandomAlphanumeric()+"/"+FOLDER_NAME)
.build(); .build();
String nonElectronicRecord= filePlanComponentAPI.createFilePlanComponent(record,FILE_PLAN_ALIAS.toString()).getId(); String nonElectronicRecord = filePlanComponentAPI.createFilePlanComponent(record,FILE_PLAN_ALIAS.toString()).getId();
assertTrue(getRestAPIFactory().getRecordsAPI().getRecordContentText(nonElectronicRecord).toString().isEmpty()); assertTrue(getRestAPIFactory().getRecordsAPI().getRecordContent(nonElectronicRecord).asString().isEmpty());
assertStatusCode(OK); assertStatusCode(OK);
} }
/** /**
@@ -266,7 +287,7 @@ public class ReadRecordTests extends BaseRMRestTest
) )
public void readContentFromInvalidContainers(String container) throws Exception public void readContentFromInvalidContainers(String container) throws Exception
{ {
getRestAPIFactory().getRecordsAPI().getRecordContentText(container).toString(); getRestAPIFactory().getRecordsAPI().getRecordContent(container).asString();
assertStatusCode(BAD_REQUEST); assertStatusCode(BAD_REQUEST);
} }