Search in sources :

Example 16 with MultiPartRequest

use of org.alfresco.rest.api.tests.util.MultiPartBuilder.MultiPartRequest 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\":"));
}
Also used : FormData(org.springframework.extensions.webscripts.servlet.FormData) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MultiPartResourceAction(org.alfresco.rest.framework.resource.actions.interfaces.MultiPartResourceAction) MultiPartRequest(org.alfresco.rest.api.tests.util.MultiPartBuilder.MultiPartRequest) ResourceWithMetadata(org.alfresco.rest.framework.core.ResourceWithMetadata) File(java.io.File) FileData(org.alfresco.rest.api.tests.util.MultiPartBuilder.FileData) PrintWriter(java.io.PrintWriter) Test(org.junit.Test)

Example 17 with MultiPartRequest

use of org.alfresco.rest.api.tests.util.MultiPartBuilder.MultiPartRequest 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);
    extractor.setLocator(locator);
    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());
}
Also used : WebScriptRequest(org.springframework.extensions.webscripts.WebScriptRequest) FormData(org.springframework.extensions.webscripts.servlet.FormData) HashMap(java.util.HashMap) UnsupportedResourceOperationException(org.alfresco.rest.framework.core.exceptions.UnsupportedResourceOperationException) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) Params(org.alfresco.rest.framework.resource.parameters.Params) MultiPartRequest(org.alfresco.rest.api.tests.util.MultiPartBuilder.MultiPartRequest) Match(org.springframework.extensions.webscripts.Match) ResourceWebScriptPost(org.alfresco.rest.framework.webscripts.ResourceWebScriptPost) File(java.io.File) FileData(org.alfresco.rest.api.tests.util.MultiPartBuilder.FileData) PrintWriter(java.io.PrintWriter) Test(org.junit.Test)

Aggregations

File (java.io.File)17 FileData (org.alfresco.rest.api.tests.util.MultiPartBuilder.FileData)17 MultiPartRequest (org.alfresco.rest.api.tests.util.MultiPartBuilder.MultiPartRequest)17 Test (org.junit.Test)17 HttpResponse (org.alfresco.rest.api.tests.client.HttpResponse)14 Document (org.alfresco.rest.api.tests.client.data.Document)14 MultiPartBuilder (org.alfresco.rest.api.tests.util.MultiPartBuilder)13 ContentInfo (org.alfresco.rest.api.tests.client.data.ContentInfo)10 RandomAccessFile (java.io.RandomAccessFile)9 AbstractSingleNetworkSiteTest (org.alfresco.rest.AbstractSingleNetworkSiteTest)9 HashMap (java.util.HashMap)7 NodesEntityResource (org.alfresco.rest.api.nodes.NodesEntityResource)6 Rendition (org.alfresco.rest.api.tests.client.data.Rendition)6 RestApiUtil.toJsonAsString (org.alfresco.rest.api.tests.util.RestApiUtil.toJsonAsString)6 ArrayList (java.util.ArrayList)3 LinkedHashMap (java.util.LinkedHashMap)3 ExpectedPaging (org.alfresco.rest.api.tests.client.PublicApiClient.ExpectedPaging)3 Paging (org.alfresco.rest.api.tests.client.PublicApiClient.Paging)3 Folder (org.alfresco.rest.api.tests.client.data.Folder)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2