Search in sources :

Example 1 with AddDocumentRequest

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

the class DocumentsRequestTest method testPost03.

/**
 * Save new File as 'USERS' group.
 *
 * @throws Exception Exception
 */
@Test(timeout = TEST_TIMEOUT)
public void testPost03() throws Exception {
    // given
    final String siteId = "finance";
    AuthenticationResultType token = login(USER_EMAIL, USER_PASSWORD);
    FormKiqClientV1 client = createHttpClient(token);
    AddDocument post = new AddDocument();
    post.content("dummy data", StandardCharsets.UTF_8);
    post.contentType("application/pdf");
    AddDocumentRequest req = new AddDocumentRequest().document(post);
    // when
    final HttpResponse<String> responseNoSiteId = client.addDocumentAsHttpResponse(req);
    final HttpResponse<String> responseSiteId = client.addDocumentAsHttpResponse(req.siteId(siteId));
    // then
    assertEquals(STATUS_CREATED, responseNoSiteId.statusCode());
    assertRequestCorsHeaders(responseNoSiteId.headers());
    assertTrue(responseNoSiteId.body().startsWith("{\"documentId\":\""));
    assertEquals(STATUS_FORBIDDEN, responseSiteId.statusCode());
    assertRequestCorsHeaders(responseSiteId.headers());
    assertEquals("{\"message\":\"Access Denied\"}", responseSiteId.body());
}
Also used : AddDocument(com.formkiq.stacks.client.models.AddDocument) AuthenticationResultType(software.amazon.awssdk.services.cognitoidentityprovider.model.AuthenticationResultType) AddDocumentRequest(com.formkiq.stacks.client.requests.AddDocumentRequest) FormKiqClientV1(com.formkiq.stacks.client.FormKiqClientV1) Test(org.junit.Test)

Example 2 with AddDocumentRequest

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

the class DocumentsRequestTest method testPost06.

/**
 * Post /documents, MAX DocumentGreater than allowed.
 *
 * @throws Exception Exception
 */
@Test(timeout = TEST_TIMEOUT)
public void testPost06() throws Exception {
    // given
    getConfigService().save(SITEID1, new DynamicObject(Map.of(MAX_DOCUMENTS, "1")));
    AddDocument post = new AddDocument();
    post.content("dummy data", StandardCharsets.UTF_8);
    post.contentType("application/pdf");
    AddDocumentRequest req = new AddDocumentRequest().document(post).siteId(SITEID1);
    FormKiqClientV1 c = getFormKiqClients().get(0);
    HttpResponse<String> response = c.addDocumentAsHttpResponse(req);
    assertEquals(STATUS_CREATED, response.statusCode());
    for (FormKiqClientV1 client : getFormKiqClients()) {
        // when
        response = client.addDocumentAsHttpResponse(req);
        // then
        assertEquals(STATUS_BAD_REQUEST, response.statusCode());
        assertEquals("{\"message\":\"Max Number of Documents reached\"}", response.body());
        assertRequestCorsHeaders(response.headers());
    }
}
Also used : DynamicObject(com.formkiq.stacks.common.objects.DynamicObject) AddDocument(com.formkiq.stacks.client.models.AddDocument) AddDocumentRequest(com.formkiq.stacks.client.requests.AddDocumentRequest) FormKiqClientV1(com.formkiq.stacks.client.FormKiqClientV1) Test(org.junit.Test)

Example 3 with AddDocumentRequest

use of com.formkiq.stacks.client.requests.AddDocumentRequest 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 4 with AddDocumentRequest

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

the class DocumentsRequestTest method testPost05.

/**
 * Save new File as 'FINANCE' group.
 *
 * @throws Exception Exception
 */
@Test(timeout = TEST_TIMEOUT)
public void testPost05() throws Exception {
    // given
    final String siteId = "finance";
    AuthenticationResultType utoken = login(USER_EMAIL, USER_PASSWORD);
    AuthenticationResultType ftoken = login(FINANCE_EMAIL, USER_PASSWORD);
    FormKiqClientV1 uclient = createHttpClient(utoken);
    FormKiqClientV1 fclient = createHttpClient(ftoken);
    AddDocument post = new AddDocument();
    post.content("dummy data", StandardCharsets.UTF_8);
    post.contentType("application/pdf");
    AddDocumentRequest req = new AddDocumentRequest().document(post);
    // when
    final HttpResponse<String> responseNoSiteId = uclient.addDocumentAsHttpResponse(req);
    final HttpResponse<String> responseSiteId = fclient.addDocumentAsHttpResponse(req.siteId(siteId));
    // then
    assertEquals(STATUS_CREATED, responseNoSiteId.statusCode());
    assertRequestCorsHeaders(responseNoSiteId.headers());
    assertTrue(responseNoSiteId.body().startsWith("{\"documentId\":\""));
    assertEquals(STATUS_CREATED, responseSiteId.statusCode());
    assertRequestCorsHeaders(responseSiteId.headers());
    Map<String, Object> map = toMap(responseSiteId.body());
    assertNotNull(map.get("documentId"));
    assertEquals(siteId, map.get("siteId"));
}
Also used : AddDocument(com.formkiq.stacks.client.models.AddDocument) DynamicObject(com.formkiq.stacks.common.objects.DynamicObject) AuthenticationResultType(software.amazon.awssdk.services.cognitoidentityprovider.model.AuthenticationResultType) AddDocumentRequest(com.formkiq.stacks.client.requests.AddDocumentRequest) FormKiqClientV1(com.formkiq.stacks.client.FormKiqClientV1) Test(org.junit.Test)

Example 5 with AddDocumentRequest

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

the class DocumentsRequestTest method testPost07.

/**
 * Save document with subdocuments against private/public endpoints.
 *
 * @throws Exception Exception
 */
@Test(timeout = TEST_TIMEOUT)
public void testPost07() throws Exception {
    for (boolean enablePublicEndpoint : Arrays.asList(Boolean.FALSE, Boolean.TRUE)) {
        for (FormKiqClientV1 client : getFormKiqClients()) {
            // given
            AddDocument post = new AddDocument().tags(Arrays.asList(new DocumentTag().key("formName").value("Job Application Form"))).documents(Arrays.asList(new AddDocument().contentType("application/json").content("{\"firstName\": \"Jan\",\"lastName\": \"Doe\"}").tags(Arrays.asList(new DocumentTag().key("formData")))));
            AddDocumentRequest req = new AddDocumentRequest().document(post).enablePublicEndpoint(enablePublicEndpoint);
            // when
            AddDocumentResponse response = client.addDocument(req);
            // then
            assertNotNull(response.documentId());
            assertNotNull(response.uploadUrl());
            assertEquals(1, response.documents().size());
            assertNotNull(response.documents().get(0).documentId());
            assertNull(response.documents().get(0).uploadUrl());
            // given
            String content = "this is a test";
            // when
            HttpResponse<String> httpresponse = this.http.send(HttpRequest.newBuilder(new URI(response.uploadUrl())).header("Content-Type", MimeType.MIME_HTML.getContentType()).method("PUT", BodyPublishers.ofString(content)).build(), BodyHandlers.ofString());
            // then
            assertEquals(STATUS_OK, httpresponse.statusCode());
            // given
            String documentId = response.documentId();
            // when - fetch document
            final DocumentWithChildren documentc = getDocument(client, documentId, true);
            DocumentTags tags = getDocumentTags(client, documentId);
            assertEquals(2, tags.tags().size());
            assertEquals("formName", tags.tags().get(0).key());
            assertEquals("Job Application Form", tags.tags().get(0).value());
            assertEquals("userId", tags.tags().get(1).key());
            assertNotNull(tags.tags().get(1).value());
            // then
            assertNotNull(documentc);
            assertEquals(1, documentc.documents().size());
            assertEquals(response.documents().get(0).documentId(), documentc.documents().get(0).documentId());
            // given
            documentId = response.documents().get(0).documentId();
            // when
            DocumentWithChildren document = getDocument(client, documentId, false);
            // then
            assertNotNull(document);
            assertNull(document.documents());
            assertEquals(response.documentId(), document.belongsToDocumentId());
        }
    }
}
Also used : DocumentTag(com.formkiq.stacks.client.models.DocumentTag) DocumentWithChildren(com.formkiq.stacks.client.models.DocumentWithChildren) AddDocument(com.formkiq.stacks.client.models.AddDocument) AddDocumentRequest(com.formkiq.stacks.client.requests.AddDocumentRequest) URI(java.net.URI) AddDocumentResponse(com.formkiq.stacks.client.models.AddDocumentResponse) FormKiqClientV1(com.formkiq.stacks.client.FormKiqClientV1) DocumentTags(com.formkiq.stacks.client.models.DocumentTags) Test(org.junit.Test)

Aggregations

FormKiqClientV1 (com.formkiq.stacks.client.FormKiqClientV1)7 AddDocument (com.formkiq.stacks.client.models.AddDocument)7 AddDocumentRequest (com.formkiq.stacks.client.requests.AddDocumentRequest)7 Test (org.junit.Test)7 DynamicObject (com.formkiq.stacks.common.objects.DynamicObject)4 AuthenticationResultType (software.amazon.awssdk.services.cognitoidentityprovider.model.AuthenticationResultType)4 AddDocumentResponse (com.formkiq.stacks.client.models.AddDocumentResponse)1 DocumentTag (com.formkiq.stacks.client.models.DocumentTag)1 DocumentTags (com.formkiq.stacks.client.models.DocumentTags)1 DocumentWithChildren (com.formkiq.stacks.client.models.DocumentWithChildren)1 UpdateDocument (com.formkiq.stacks.client.models.UpdateDocument)1 DeleteDocumentRequest (com.formkiq.stacks.client.requests.DeleteDocumentRequest)1 GetDocumentRequest (com.formkiq.stacks.client.requests.GetDocumentRequest)1 UpdateDocumentRequest (com.formkiq.stacks.client.requests.UpdateDocumentRequest)1 URI (java.net.URI)1