Search in sources :

Example 1 with GetDocumentRequest

use of com.formkiq.stacks.client.requests.GetDocumentRequest in project formkiq-core by formkiq.

the class DocumentsRequestTest method testPost01.

/**
 * Save new File.
 *
 * @throws Exception Exception
 */
@Test(timeout = TEST_TIMEOUT)
public void testPost01() throws Exception {
    for (FormKiqClientV1 client : getFormKiqClients()) {
        // given
        AddDocument post = new AddDocument().contentType("text/plain").content("test data", StandardCharsets.UTF_8);
        AddDocumentRequest req = new AddDocumentRequest().document(post);
        // when
        HttpResponse<String> response = client.addDocumentAsHttpResponse(req);
        assertEquals(STATUS_CREATED, response.statusCode());
        Map<String, Object> map = toMap(response);
        // given
        String documentId = map.get("documentId").toString();
        // when - fetch document
        while (true) {
            map = fetchDocument(client, documentId);
            if (map.containsKey("contentType")) {
                assertTrue(map.get("contentType").toString().startsWith("text/plain"));
                break;
            }
            Thread.sleep(ONE_SECOND);
        }
        // given
        UpdateDocument updateDocument = new UpdateDocument().content("dummy data", StandardCharsets.UTF_8).contentType("application/pdf");
        UpdateDocumentRequest request = new UpdateDocumentRequest().document(updateDocument);
        request.documentId(documentId);
        // when - patch document
        response = client.updateDocumentAsHttpResponse(request);
        assertEquals(STATUS_OK, response.statusCode());
        assertRequestCorsHeaders(response.headers());
        // when - check content type changed
        while (true) {
            map = fetchDocument(client, documentId);
            if (map.containsKey("contentLength") && "application/pdf".equals(map.get("contentType").toString())) {
                assertEquals("application/pdf", map.get("contentType").toString());
                assertNotNull(map.get("contentLength"));
                break;
            }
            Thread.sleep(ONE_SECOND);
        }
        // given
        DeleteDocumentRequest delRequest = new DeleteDocumentRequest().documentId(documentId);
        GetDocumentRequest getRequest = new GetDocumentRequest().documentId(documentId);
        // when - delete document
        response = client.deleteDocumentAsHttpResponse(delRequest);
        assertEquals(STATUS_OK, response.statusCode());
        assertRequestCorsHeaders(response.headers());
        while (true) {
            // when - fetch document
            response = client.getDocumentAsHttpResponse(getRequest);
            // then
            if (STATUS_NOT_FOUND == response.statusCode()) {
                assertEquals(STATUS_NOT_FOUND, response.statusCode());
                assertRequestCorsHeaders(response.headers());
                break;
            }
            Thread.sleep(ONE_SECOND);
        }
    }
}
Also used : UpdateDocument(com.formkiq.stacks.client.models.UpdateDocument) DeleteDocumentRequest(com.formkiq.stacks.client.requests.DeleteDocumentRequest) AddDocument(com.formkiq.stacks.client.models.AddDocument) DynamicObject(com.formkiq.stacks.common.objects.DynamicObject) UpdateDocumentRequest(com.formkiq.stacks.client.requests.UpdateDocumentRequest) AddDocumentRequest(com.formkiq.stacks.client.requests.AddDocumentRequest) GetDocumentRequest(com.formkiq.stacks.client.requests.GetDocumentRequest) FormKiqClientV1(com.formkiq.stacks.client.FormKiqClientV1) Test(org.junit.Test)

Example 2 with GetDocumentRequest

use of com.formkiq.stacks.client.requests.GetDocumentRequest in project formkiq-core by formkiq.

the class DocumentsRequestTest method fetchDocument.

/**
 * Fetch Document.
 *
 * @param client {@link FormKiqClientV1}
 * @param documentId {@link String}
 * @return {@link Map}
 * @throws IOException IOException
 * @throws InterruptedException InterruptedException
 * @throws URISyntaxException URISyntaxException
 */
private Map<String, Object> fetchDocument(final FormKiqClientV1 client, final String documentId) throws IOException, InterruptedException, URISyntaxException {
    Map<String, Object> map = null;
    GetDocumentRequest request = new GetDocumentRequest().documentId(documentId);
    while (true) {
        HttpResponse<String> response = client.getDocumentAsHttpResponse(request);
        if (STATUS_OK == response.statusCode()) {
            assertEquals(STATUS_OK, response.statusCode());
            map = toMap(response);
            assertRequestCorsHeaders(response.headers());
            assertEquals(documentId, map.get("documentId"));
            assertEquals(DEFAULT_SITE_ID, map.get("siteId"));
            break;
        }
        Thread.sleep(ONE_SECOND);
    }
    return map;
}
Also used : DynamicObject(com.formkiq.stacks.common.objects.DynamicObject) GetDocumentRequest(com.formkiq.stacks.client.requests.GetDocumentRequest)

Aggregations

GetDocumentRequest (com.formkiq.stacks.client.requests.GetDocumentRequest)2 DynamicObject (com.formkiq.stacks.common.objects.DynamicObject)2 FormKiqClientV1 (com.formkiq.stacks.client.FormKiqClientV1)1 AddDocument (com.formkiq.stacks.client.models.AddDocument)1 UpdateDocument (com.formkiq.stacks.client.models.UpdateDocument)1 AddDocumentRequest (com.formkiq.stacks.client.requests.AddDocumentRequest)1 DeleteDocumentRequest (com.formkiq.stacks.client.requests.DeleteDocumentRequest)1 UpdateDocumentRequest (com.formkiq.stacks.client.requests.UpdateDocumentRequest)1 Test (org.junit.Test)1