use of org.alfresco.rest.api.tests.util.MultiPartBuilder.FileData in project alfresco-remote-api by Alfresco.
the class NodeApiTest method testGuessMimeTypeAndEncoding.
/**
* Tests guess mimetype & guess encoding when uploading file/content (create or update) - also empty file/content
* <p>POST:</p>
* {@literal <host>:<port>/alfresco/api/-default-/public/alfresco/versions/1/nodes/<nodeId>/children}
* <p>PUT:</p>
* {@literal <host>:<port>/alfresco/api/-default-/public/alfresco/versions/1/nodes/<nodeId>/content}
*/
@Test
public void testGuessMimeTypeAndEncoding() throws Exception {
setRequestContext(user1);
String fId = createFolder(getMyNodeId(), "test-folder-guess-" + RUNID).getId();
// create empty files
Document d = new Document();
d.setName("my doc");
d.setNodeType(TYPE_CM_CONTENT);
HttpResponse response = post(getNodeChildrenUrl(fId), toJsonAsStringNonNull(d), 201);
Document documentResp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
assertEquals(MimetypeMap.MIMETYPE_BINARY, documentResp.getContent().getMimeType());
assertEquals("Binary File (Octet Stream)", documentResp.getContent().getMimeTypeName());
assertEquals(0L, documentResp.getContent().getSizeInBytes().longValue());
assertEquals("UTF-8", documentResp.getContent().getEncoding());
d = new Document();
d.setName("my doc.txt");
d.setNodeType(TYPE_CM_CONTENT);
response = post(getNodeChildrenUrl(fId), toJsonAsStringNonNull(d), 201);
documentResp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
assertEquals(MimetypeMap.MIMETYPE_TEXT_PLAIN, documentResp.getContent().getMimeType());
assertEquals("Plain Text", documentResp.getContent().getMimeTypeName());
assertEquals(0L, documentResp.getContent().getSizeInBytes().longValue());
assertEquals("UTF-8", documentResp.getContent().getEncoding());
d = new Document();
d.setName("my doc.pdf");
d.setNodeType(TYPE_CM_CONTENT);
response = post(getNodeChildrenUrl(fId), toJsonAsStringNonNull(d), 201);
documentResp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
assertEquals(MimetypeMap.MIMETYPE_PDF, documentResp.getContent().getMimeType());
assertEquals("Adobe PDF Document", documentResp.getContent().getMimeTypeName());
assertEquals(0L, documentResp.getContent().getSizeInBytes().longValue());
assertEquals("UTF-8", documentResp.getContent().getEncoding());
// upload files
String fileName = "quick-2.pdf";
File file = getResourceFile(fileName);
MultiPartBuilder multiPartBuilder = MultiPartBuilder.create().setFileData(new FileData(fileName, file));
MultiPartRequest reqBody = multiPartBuilder.build();
response = post(getNodeChildrenUrl(fId), reqBody.getBody(), null, reqBody.getContentType(), 201);
Document document = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
ContentInfo contentInfo = document.getContent();
assertEquals(MimetypeMap.MIMETYPE_PDF, contentInfo.getMimeType());
assertEquals("UTF-8", contentInfo.getEncoding());
fileName = "quick-2.pdf";
file = getResourceFile(fileName);
multiPartBuilder = MultiPartBuilder.create().setFileData(// note: we've deliberately dropped the file ext here
new FileData("quick-2", file));
reqBody = multiPartBuilder.build();
response = post(getNodeChildrenUrl(fId), reqBody.getBody(), null, reqBody.getContentType(), 201);
document = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
contentInfo = document.getContent();
assertEquals(MimetypeMap.MIMETYPE_PDF, contentInfo.getMimeType());
assertEquals("UTF-8", contentInfo.getEncoding());
fileName = "example-1.txt";
file = getResourceFile(fileName);
multiPartBuilder = MultiPartBuilder.create().setFileData(new FileData(fileName, file));
reqBody = multiPartBuilder.build();
response = post(getNodeChildrenUrl(fId), reqBody.getBody(), null, reqBody.getContentType(), 201);
document = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
contentInfo = document.getContent();
assertEquals(MimetypeMap.MIMETYPE_TEXT_PLAIN, contentInfo.getMimeType());
assertEquals("ISO-8859-1", contentInfo.getEncoding());
fileName = "example-2.txt";
file = getResourceFile(fileName);
multiPartBuilder = MultiPartBuilder.create().setFileData(new FileData(fileName, file));
reqBody = multiPartBuilder.build();
response = post(getNodeChildrenUrl(fId), reqBody.getBody(), null, reqBody.getContentType(), 201);
document = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
contentInfo = document.getContent();
assertEquals(MimetypeMap.MIMETYPE_TEXT_PLAIN, contentInfo.getMimeType());
assertEquals("UTF-8", contentInfo.getEncoding());
fileName = "shift-jis.txt";
file = getResourceFile(fileName);
multiPartBuilder = MultiPartBuilder.create().setFileData(new FileData(fileName, file));
reqBody = multiPartBuilder.build();
response = post(getNodeChildrenUrl(fId), reqBody.getBody(), null, reqBody.getContentType(), 201);
document = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
contentInfo = document.getContent();
assertEquals(MimetypeMap.MIMETYPE_TEXT_PLAIN, contentInfo.getMimeType());
assertEquals("Shift_JIS", contentInfo.getEncoding());
fileName = "example-1.xml";
file = getResourceFile(fileName);
multiPartBuilder = MultiPartBuilder.create().setFileData(new FileData(fileName, file));
reqBody = multiPartBuilder.build();
response = post(getNodeChildrenUrl(fId), reqBody.getBody(), null, reqBody.getContentType(), 201);
document = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
contentInfo = document.getContent();
assertEquals(MimetypeMap.MIMETYPE_XML, contentInfo.getMimeType());
assertEquals("ISO-8859-1", contentInfo.getEncoding());
fileName = "example-2.xml";
file = getResourceFile(fileName);
multiPartBuilder = MultiPartBuilder.create().setFileData(new FileData(fileName, file));
reqBody = multiPartBuilder.build();
response = post(getNodeChildrenUrl(fId), reqBody.getBody(), null, reqBody.getContentType(), 201);
document = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
contentInfo = document.getContent();
assertEquals(MimetypeMap.MIMETYPE_XML, contentInfo.getMimeType());
assertEquals("UTF-8", contentInfo.getEncoding());
// upload file, rename and then update file
fileName = "quick.pdf";
file = getResourceFile(fileName);
multiPartBuilder = MultiPartBuilder.create().setFileData(new FileData(fileName, file));
reqBody = multiPartBuilder.build();
response = post(getNodeChildrenUrl(fId), reqBody.getBody(), null, reqBody.getContentType(), 201);
document = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
String docId = document.getId();
contentInfo = document.getContent();
assertEquals(MimetypeMap.MIMETYPE_PDF, contentInfo.getMimeType());
assertEquals("UTF-8", contentInfo.getEncoding());
// rename (mimeType remains unchanged, binary has not changed)
Document dUpdate = new Document();
dUpdate.setName("quick.docx");
response = put(URL_NODES, docId, toJsonAsStringNonNull(dUpdate), null, 200);
document = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
contentInfo = document.getContent();
assertEquals(MimetypeMap.MIMETYPE_PDF, contentInfo.getMimeType());
assertEquals("UTF-8", contentInfo.getEncoding());
fileName = "quick.docx";
file = getResourceFile(fileName);
BinaryPayload payload = new BinaryPayload(file);
response = putBinary(getNodeContentUrl(docId), payload, null, null, 200);
document = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
contentInfo = document.getContent();
assertEquals(MimetypeMap.MIMETYPE_OPENXML_WORDPROCESSING, contentInfo.getMimeType());
assertEquals("UTF-8", contentInfo.getEncoding());
// additional test
fileName = "CMIS-Delete.json";
file = getResourceFile(fileName);
multiPartBuilder = MultiPartBuilder.create().setFileData(new FileData("special-" + GUID.generate(), file));
reqBody = multiPartBuilder.build();
response = post(getNodeChildrenUrl(fId), reqBody.getBody(), null, reqBody.getContentType(), 201);
document = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
contentInfo = document.getContent();
assertEquals(MimetypeMap.MIMETYPE_TEXT_PLAIN, contentInfo.getMimeType());
assertEquals("UTF-8", contentInfo.getEncoding());
// cleanup
deleteNode(fId);
}
use of org.alfresco.rest.api.tests.util.MultiPartBuilder.FileData in project alfresco-remote-api by Alfresco.
the class ParamsExtractorTests method testMultiPartPostExtractor.
@Test
public void testMultiPartPostExtractor() throws Exception {
ResourceWebScriptPost extractor = new ResourceWebScriptPost();
extractor.setAssistant(assistant);
Map<String, String> templateVars = new HashMap<String, String>();
WebScriptRequest request = mock(WebScriptRequest.class);
when(request.getServiceMatch()).thenReturn(new Match(null, templateVars, null));
File file = TempFileProvider.createTempFile("ParamsExtractorTests-", ".txt");
PrintWriter writer = new PrintWriter(file);
writer.println("Multipart Mock test.");
writer.close();
MultiPartRequest reqBody = MultiPartBuilder.create().setFileData(new FileData(file.getName(), file, MimetypeMap.MIMETYPE_TEXT_PLAIN)).build();
MockHttpServletRequest mockRequest = new MockHttpServletRequest("POST", "");
mockRequest.setContent(reqBody.getBody());
mockRequest.setContentType(reqBody.getContentType());
when(request.getContentType()).thenReturn("multipart/form-data");
when(request.parseContent()).thenReturn(new FormData(mockRequest));
Params params = extractor.extractParams(mockEntity(), request);
assertNotNull(params);
Object passed = params.getPassedIn();
assertNotNull(passed);
assertTrue(FormData.class.isAssignableFrom(passed.getClass()));
FormData formData = (FormData) passed;
assertTrue(formData.getIsMultiPart());
// No entity id for POST
templateVars.put(ResourceLocator.ENTITY_ID, "1234");
try {
params = extractor.extractParams(mockEntity(), request);
fail("Should not get here. No entity id for POST");
} catch (UnsupportedResourceOperationException uoe) {
assertNotNull(uoe);
}
params = extractor.extractParams(mockRelationship(), request);
assertNotNull(params);
assertEquals("1234", params.getEntityId());
passed = params.getPassedIn();
assertNotNull(passed);
assertTrue(FormData.class.isAssignableFrom(passed.getClass()));
formData = (FormData) passed;
assertTrue(formData.getIsMultiPart());
}
use of org.alfresco.rest.api.tests.util.MultiPartBuilder.FileData in project alfresco-remote-api by Alfresco.
the class SerializeTests method testInvokeMultiPartEntity.
@Test
public void testInvokeMultiPartEntity() throws IOException {
ResourceWithMetadata entityResource = locator.locateEntityResource(api, "multiparttest", HttpMethod.POST);
assertNotNull(entityResource);
MultiPartResourceAction.Create<?> resource = (MultiPartResourceAction.Create<?>) entityResource.getResource();
File file = TempFileProvider.createTempFile("ParamsExtractorTests-", ".txt");
PrintWriter writer = new PrintWriter(file);
writer.println("Multipart Mock test2.");
writer.close();
MultiPartRequest reqBody = MultiPartBuilder.create().setFileData(new FileData(file.getName(), file, MimetypeMap.MIMETYPE_TEXT_PLAIN)).build();
MockHttpServletRequest mockRequest = new MockHttpServletRequest("POST", "");
mockRequest.setContent(reqBody.getBody());
mockRequest.setContentType(reqBody.getContentType());
String out = writeResponse(helper.processAdditionsToTheResponse(mock(WebScriptResponse.class), api, null, NOT_USED, resource.create(new FormData(mockRequest), NOT_USED, callBack)));
assertTrue("There must be json output", StringUtils.startsWith(out, "{\"entry\":"));
}
Aggregations