Search in sources :

Example 21 with DynamicDocumentItem

use of com.formkiq.stacks.dynamodb.DynamicDocumentItem in project formkiq-core by formkiq.

the class StagingS3CreateTest method testFkB64Extension04.

/**
 * Test .fkb64 file without DocumentId.
 *
 * @throws IOException IOException
 */
@Test
public void testFkB64Extension04() throws IOException {
    DynamicDocumentItem item = createDocumentItem();
    item.setDocumentId(null);
    for (String siteId : Arrays.asList(null, UUID.randomUUID().toString())) {
        processFkB64File(siteId, item);
    }
}
Also used : DynamicDocumentItem(com.formkiq.stacks.dynamodb.DynamicDocumentItem) Test(org.junit.jupiter.api.Test)

Example 22 with DynamicDocumentItem

use of com.formkiq.stacks.dynamodb.DynamicDocumentItem in project formkiq-core by formkiq.

the class StagingS3CreateTest method createDocumentItem.

/**
 * Create {@link DynamicDocumentItem}.
 *
 * @return {@link DynamicDocumentItem}
 */
private DynamicDocumentItem createDocumentItem() {
    String content = "This is a test";
    DynamicDocumentItem item = new DynamicDocumentItem(Collections.emptyMap());
    item.setDocumentId(UUID.randomUUID().toString());
    item.put("content", Base64.getEncoder().encodeToString(content.getBytes(UTF_8)));
    item.setContentLength(Long.valueOf(content.length()));
    item.setContentType("plain/text");
    item.setInsertedDate(new Date());
    item.setPath("test.txt");
    item.setUserId("joe");
    return item;
}
Also used : DynamicDocumentItem(com.formkiq.stacks.dynamodb.DynamicDocumentItem) Date(java.util.Date)

Example 23 with DynamicDocumentItem

use of com.formkiq.stacks.dynamodb.DynamicDocumentItem in project formkiq-core by formkiq.

the class StagingS3CreateTest method processFkB64File.

/**
 * Test .fkb64 file.
 *
 * @param siteId {@link String}
 * @param docitem {@link DynamicDocumentItem}
 * @throws IOException IOException
 */
private void processFkB64File(final String siteId, final DynamicDocumentItem docitem) throws IOException {
    // given
    String documentId = UUID.randomUUID().toString();
    this.logger.reset();
    String key = createDatabaseKey(siteId, documentId + ".fkb64");
    Map<String, Object> map = loadFileAsMap(this, "/objectcreate-event4.json", UUID1, key);
    try (S3Client c = s3.buildClient()) {
        byte[] content = this.gson.toJson(docitem).getBytes(UTF_8);
        s3.putObject(c, STAGING_BUCKET, key, content, null, null);
        // when
        handleRequest(map);
        // then
        String destDocumentId = findDocumentIdFromLogger(siteId);
        if (destDocumentId != null) {
            assertTrue(this.logger.containsString("Removing " + key + " from bucket example-bucket."));
            assertTrue(this.logger.containsString("handling 1 record(s)."));
            assertTrue(this.logger.containsString("Inserted " + docitem.getPath() + " into bucket documentsbucket as " + createDatabaseKey(siteId, destDocumentId)));
            assertFalse(s3.getObjectMetadata(c, STAGING_BUCKET, documentId).isObjectExists());
            DocumentItem item = service.findDocument(siteId, destDocumentId);
            assertEquals("20", item.getContentLength().toString());
            assertEquals("plain/text", item.getContentType());
            assertTrue(item.getChecksum() != null && item.getInsertedDate() != null);
            assertEquals("test.txt", item.getPath());
            assertEquals("joe", item.getUserId());
            boolean hasTags = docitem.containsKey("tags");
            List<DocumentTag> tags = service.findDocumentTags(siteId, destDocumentId, null, MAX_RESULTS).getResults();
            int tagcount = hasTags ? docitem.getList("tags").size() + 2 : 2 + 1;
            assertEquals(tagcount, tags.size());
            DocumentTag ptag = findTag(tags, "path");
            assertEquals(destDocumentId, ptag.getDocumentId());
            assertEquals(docitem.getPath(), ptag.getValue());
            assertNotNull(ptag.getInsertedDate());
            assertEquals(DocumentTagType.SYSTEMDEFINED, ptag.getType());
            assertEquals(docitem.getUserId(), ptag.getUserId());
            if (hasTags) {
                for (DynamicObject tag : docitem.getList("tags")) {
                    DocumentTag dtag = findTag(tags, tag.getString("key"));
                    assertEquals(tag.getString("value"), dtag.getValue());
                }
            } else {
                ptag = findTag(tags, "untagged");
                assertEquals(destDocumentId, ptag.getDocumentId());
                assertEquals("true", ptag.getValue());
                assertNotNull(ptag.getInsertedDate());
                assertEquals(DocumentTagType.SYSTEMDEFINED, ptag.getType());
                assertEquals(docitem.getUserId(), ptag.getUserId());
            }
        }
    }
}
Also used : DocumentTag(com.formkiq.stacks.dynamodb.DocumentTag) DynamicObject(com.formkiq.stacks.common.objects.DynamicObject) DocumentItem(com.formkiq.stacks.dynamodb.DocumentItem) DynamicDocumentItem(com.formkiq.stacks.dynamodb.DynamicDocumentItem) DynamicObject(com.formkiq.stacks.common.objects.DynamicObject) S3Client(software.amazon.awssdk.services.s3.S3Client)

Example 24 with DynamicDocumentItem

use of com.formkiq.stacks.dynamodb.DynamicDocumentItem in project formkiq-core by formkiq.

the class AwsResourceTest method testAddDeleteFile03.

/**
 * Test Adding a file directly to Documents Buckekt and then deleting it.
 *
 * @throws Exception Exception
 */
@Test(timeout = TEST_TIMEOUT)
public void testAddDeleteFile03() throws Exception {
    // given
    final int statusCode = 200;
    HttpClient http = HttpClient.newHttpClient();
    String key = UUID.randomUUID().toString();
    String createQueue = "createtest-" + UUID.randomUUID();
    String documentQueueUrl = createSqsQueue(createQueue).queueUrl();
    String subscriptionDocumentArn = subscribeToSns(getSnsDocumentEventArn(), documentQueueUrl);
    String contentType = "text/plain";
    String content = "test content";
    try {
        try (S3Client s3 = getS3Service().buildClient()) {
            DynamicDocumentItem doc = new DynamicDocumentItem(Map.of("documentId", key, "insertedDate", new Date(), "userId", "joe"));
            getDocumentService().saveDocumentItemWithTag(null, doc);
            // when
            URL url = getS3Service().presignPutUrl(getDocumentsbucketname(), key, Duration.ofHours(1));
            HttpResponse<String> put = http.send(HttpRequest.newBuilder(url.toURI()).header("Content-Type", contentType).method("PUT", BodyPublishers.ofString(content)).build(), BodyHandlers.ofString());
            // then
            assertEquals(statusCode, put.statusCode());
            verifyFileExistsInDocumentsS3(s3, key, contentType);
            assertSnsMessage(documentQueueUrl, "create");
            // when
            getS3Service().deleteObject(s3, getDocumentsbucketname(), key);
            // then
            assertSnsMessage(documentQueueUrl, "delete");
        }
    } finally {
        getSnsService().unsubscribe(subscriptionDocumentArn);
        getSqsService().deleteQueue(documentQueueUrl);
    }
}
Also used : DynamicDocumentItem(com.formkiq.stacks.dynamodb.DynamicDocumentItem) HttpClient(java.net.http.HttpClient) S3Client(software.amazon.awssdk.services.s3.S3Client) Date(java.util.Date) URL(java.net.URL) Test(org.junit.Test)

Example 25 with DynamicDocumentItem

use of com.formkiq.stacks.dynamodb.DynamicDocumentItem in project formkiq-core by formkiq.

the class ApiDocumentsTagsRequestTest method testHandlePostDocumentTags06.

/**
 * POST /documents/{documentId}/tags testing "untagged" gets removed.
 *
 * @throws Exception an error has occurred
 */
@Test
public void testHandlePostDocumentTags06() throws Exception {
    for (String siteId : Arrays.asList(null, UUID.randomUUID().toString())) {
        // given
        final String documentId = "test" + UUID.randomUUID().toString() + ".pdf";
        String username = UUID.randomUUID() + "@formkiq.com";
        DynamicDocumentItem doc = new DynamicDocumentItem(Map.of("documentId", documentId, "userId", username, "insertedDate", new Date()));
        getDocumentService().saveDocumentItemWithTag(siteId, doc);
        PaginationResults<DocumentTag> tags = getDocumentService().findDocumentTags(siteId, documentId, null, MAX_RESULTS);
        assertEquals(2, tags.getResults().size());
        assertEquals("untagged", tags.getResults().get(0).getKey());
        assertEquals("userId", tags.getResults().get(1).getKey());
        final String tagname = "category";
        final String tagvalue = "";
        ApiGatewayRequestEvent event = toRequestEvent("/request-post-documents-documentid-tags03.json");
        addParameter(event, "siteId", siteId);
        setPathParameter(event, "documentId", documentId);
        String expected = "{" + getHeaders() + ",\"body\":\"" + "{\\\"message\\\":\\\"Created Tag 'category'.\\\"}\",\"statusCode\":201}";
        // when
        String response = handleRequest(event);
        // then
        assertEquals(expected, response);
        tags = getDocumentService().findDocumentTags(siteId, documentId, null, MAX_RESULTS);
        assertEquals(2, tags.getResults().size());
        assertEquals(tagname, tags.getResults().get(0).getKey());
        assertEquals(tagvalue, tags.getResults().get(0).getValue());
        assertEquals("userId", tags.getResults().get(1).getKey());
        assertEquals(username, tags.getResults().get(1).getValue());
        assertTrue(getLogger().containsString("response: " + expected));
    }
}
Also used : DynamicDocumentItem(com.formkiq.stacks.dynamodb.DynamicDocumentItem) DocumentTag(com.formkiq.stacks.dynamodb.DocumentTag) ApiGatewayRequestEvent(com.formkiq.lambda.apigateway.ApiGatewayRequestEvent) Date(java.util.Date) Test(org.junit.jupiter.api.Test)

Aggregations

DynamicDocumentItem (com.formkiq.stacks.dynamodb.DynamicDocumentItem)27 Date (java.util.Date)18 Test (org.junit.jupiter.api.Test)15 DocumentItem (com.formkiq.stacks.dynamodb.DocumentItem)14 Timeout (org.junit.jupiter.api.Timeout)9 DocumentTag (com.formkiq.stacks.dynamodb.DocumentTag)7 DynamicDocumentTag (com.formkiq.stacks.dynamodb.DynamicDocumentTag)6 S3Client (software.amazon.awssdk.services.s3.S3Client)6 DocumentItemToDynamicDocumentItem (com.formkiq.stacks.dynamodb.DocumentItemToDynamicDocumentItem)5 DynamicObject (com.formkiq.stacks.common.objects.DynamicObject)4 DynamoDbClient (software.amazon.awssdk.services.dynamodb.DynamoDbClient)4 AttributeValue (software.amazon.awssdk.services.dynamodb.model.AttributeValue)4 ApiGatewayRequestEvent (com.formkiq.lambda.apigateway.ApiGatewayRequestEvent)3 ApiMapResponse (com.formkiq.lambda.apigateway.ApiMapResponse)3 ApiPagination (com.formkiq.lambda.apigateway.ApiPagination)3 ApiRequestHandlerResponse (com.formkiq.lambda.apigateway.ApiRequestHandlerResponse)3 SimpleDateFormat (java.text.SimpleDateFormat)3 HashMap (java.util.HashMap)3 List (java.util.List)3 Map (java.util.Map)3