Search in sources :

Example 6 with DocumentItem

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

the class StagingS3CreateTest method testFkB64Extension05.

/**
 * Test .fkb64 file with multiple documents.
 *
 * @throws IOException IOException
 */
@Test
public void testFkB64Extension05() throws IOException {
    String documentId0 = "0d1a788d-9a70-418a-8c33-a9ee9c1a0173";
    String documentId1 = "24af57ca-f61d-4ff8-b8a0-d7666073560e";
    String documentId2 = "f2416702-6b3c-4d29-a217-82a43b16b964";
    ZonedDateTime nowDate = ZonedDateTime.ofInstant(new Date().toInstant(), ZoneId.systemDefault());
    for (String siteId : Arrays.asList(null, UUID.randomUUID().toString())) {
        String key = createDatabaseKey(siteId, documentId0 + ".fkb64");
        Map<String, Object> map = loadFileAsMap(this, "/objectcreate-event4.json", UUID1, key);
        try (S3Client c = s3.buildClient()) {
            String json = loadFile(this, "/document_multiple.fkb64");
            s3.putObject(c, STAGING_BUCKET, key, json.getBytes(UTF_8), null, null);
            // when
            handleRequest(map);
            // then
            assertEquals(1, service.findDocumentsByDate(siteId, nowDate, null, MAX_RESULTS).getResults().size());
            DocumentItem i = service.findDocument(siteId, documentId0, true, null, MAX_RESULTS).getResult();
            assertNull(i.getContentType());
            verifyBelongsToDocument(i, documentId0, Arrays.asList(documentId1, documentId2));
            List<DocumentTag> tags = service.findDocumentTags(siteId, documentId0, null, MAX_RESULTS).getResults();
            assertEquals(1, tags.size());
            assertEquals("formName", tags.get(0).getKey());
            assertEquals("Job Application Form", tags.get(0).getValue());
            String k = createDatabaseKey(siteId, i.getDocumentId());
            assertFalse(s3.getObjectMetadata(c, DOCUMENTS_BUCKET, k).isObjectExists());
            i = service.findDocument(siteId, documentId1, true, null, MAX_RESULTS).getResult();
            assertEquals("application/json", i.getContentType());
            assertEquals(documentId0, i.getBelongsToDocumentId());
            tags = service.findDocumentTags(siteId, documentId1, null, MAX_RESULTS).getResults();
            assertEquals(1, tags.size());
            assertEquals("formData", tags.get(0).getKey());
            assertEquals("", tags.get(0).getValue());
            k = createDatabaseKey(siteId, i.getDocumentId());
            assertEquals("application/json", s3.getObjectMetadata(c, DOCUMENTS_BUCKET, k).getContentType());
            i = service.findDocument(siteId, documentId2);
            assertEquals(documentId0, i.getBelongsToDocumentId());
            assertNull(i.getContentType());
            tags = service.findDocumentTags(siteId, documentId2, null, MAX_RESULTS).getResults();
            assertEquals(2, tags.size());
            assertEquals("attachmentField", tags.get(0).getKey());
            assertEquals("resume", tags.get(0).getValue());
            assertEquals("category", tags.get(1).getKey());
            assertEquals("", tags.get(1).getValue());
            k = createDatabaseKey(siteId, i.getDocumentId());
            assertFalse(s3.getObjectMetadata(c, DOCUMENTS_BUCKET, k).isObjectExists());
        }
    }
}
Also used : DocumentTag(com.formkiq.stacks.dynamodb.DocumentTag) ZonedDateTime(java.time.ZonedDateTime) 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) Date(java.util.Date) Test(org.junit.jupiter.api.Test)

Example 7 with DocumentItem

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

the class StagingS3CreateTest method testCopyFile.

/**
 * Test CopyFile.
 *
 * @param documentId {@link String}
 * @param expectedPath {@link String}
 * @throws IOException IOException
 */
private void testCopyFile(final String documentId, final String expectedPath) throws IOException {
    for (String siteId : Arrays.asList(null, UUID.randomUUID().toString())) {
        // given
        this.logger.reset();
        String key = createDatabaseKey(siteId, documentId);
        Map<String, String> metadata = new HashMap<>();
        metadata.put("Content-Type", "application/pdf");
        metadata.put("userId", "1234");
        Map<String, Object> map = loadFileAsMap(this, "/objectcreate-event1.json", UUID1, key);
        try (S3Client c = s3.buildClient()) {
            s3.putObject(c, STAGING_BUCKET, key, "testdata".getBytes(UTF_8), "application/pdf", metadata);
            // when
            handleRequest(map);
            // then
            String destDocumentId = findDocumentIdFromLogger(siteId);
            assertTrue(this.logger.containsString("Removing " + createDatabaseKey(siteId, documentId) + " from bucket example-bucket."));
            assertTrue(this.logger.containsString("handling 1 record(s)."));
            assertTrue(this.logger.containsString("Copying " + key + " from bucket example-bucket to " + createDatabaseKey(siteId, destDocumentId) + " in bucket " + DOCUMENTS_BUCKET + "."));
            assertTrue(this.logger.containsString("Removing " + key + " from bucket example-bucket."));
            assertFalse(s3.getObjectMetadata(c, STAGING_BUCKET, documentId).isObjectExists());
            DocumentItem item = service.findDocument(siteId, destDocumentId);
            assertNotNull(item);
            assertEquals("8", item.getContentLength().toString());
            assertEquals("application/pdf", item.getContentType());
            assertEquals("ef654c40ab4f1747fc699915d4f70902", item.getChecksum());
            assertNotNull(item.getInsertedDate());
            assertEquals(item.getPath(), expectedPath);
            assertEquals("1234", item.getUserId());
            List<DocumentTag> tags = service.findDocumentTags(siteId, item.getDocumentId(), null, MAX_RESULTS).getResults();
            int i = 0;
            int count = 2;
            if (item.getPath() != null) {
                count++;
                assertEquals("path", tags.get(i).getKey());
                assertEquals(DocumentTagType.SYSTEMDEFINED, tags.get(i++).getType());
            }
            assertEquals(count, tags.size());
            assertEquals("untagged", tags.get(i).getKey());
            assertEquals(DocumentTagType.SYSTEMDEFINED, tags.get(i++).getType());
            assertEquals("userId", tags.get(i).getKey());
            assertEquals(DocumentTagType.SYSTEMDEFINED, tags.get(i++).getType());
        }
    }
}
Also used : DocumentTag(com.formkiq.stacks.dynamodb.DocumentTag) HashMap(java.util.HashMap) 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 8 with DocumentItem

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

the class AwsResourceTest method testAddDeleteFile02.

/**
 * Test using a Presigned URL to Adding a file and then deleting it.
 *
 * @throws Exception Exception
 */
@Test(timeout = TEST_TIMEOUT)
public void testAddDeleteFile02() throws Exception {
    // given
    final Long contentLength = Long.valueOf(36);
    String key = UUID.randomUUID().toString();
    String contentType = "text/plain";
    DocumentItem item = new DocumentItemDynamoDb(key, new Date(), "test");
    try (S3Client s3 = getS3Service().buildClient()) {
        // when
        getDocumentService().saveDocument(null, item, null);
        key = writeToDocuments(s3, key, contentType);
        // then
        verifyFileExistsInDocumentsS3(s3, key, contentType);
        item = getDocumentService().findDocument(null, key);
        while (true) {
            if (contentType.equals(item.getContentType())) {
                assertEquals(contentType, item.getContentType());
                assertEquals(contentLength, item.getContentLength());
                break;
            }
            item = getDocumentService().findDocument(null, key);
        }
        getS3Service().deleteObject(s3, getDocumentsbucketname(), key);
    }
}
Also used : DynamicDocumentItem(com.formkiq.stacks.dynamodb.DynamicDocumentItem) DocumentItem(com.formkiq.stacks.dynamodb.DocumentItem) S3Client(software.amazon.awssdk.services.s3.S3Client) Date(java.util.Date) DocumentItemDynamoDb(com.formkiq.stacks.dynamodb.DocumentItemDynamoDb) Test(org.junit.Test)

Example 9 with DocumentItem

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

the class DocumentIdRequestHandler method get.

@Override
public ApiRequestHandlerResponse get(final LambdaLogger logger, final ApiGatewayRequestEvent event, final ApiAuthorizer authorizer, final AwsServiceCache awsservice) throws Exception {
    String siteId = authorizer.getSiteId();
    int limit = getLimit(logger, event);
    ApiPagination token = getPagination(awsservice.documentCacheService(), event);
    String documentId = event.getPathParameters().get("documentId");
    ApiPagination pagination = getPagination(awsservice.documentCacheService(), event);
    PaginationResult<DocumentItem> presult = awsservice.documentService().findDocument(siteId, documentId, true, token != null ? token.getStartkey() : null, limit);
    DocumentItem result = presult.getResult();
    if (result == null) {
        throw new NotFoundException("Document " + documentId + " not found.");
    }
    ApiPagination current = createPagination(awsservice.documentCacheService(), event, pagination, presult.getToken(), limit);
    DynamicDocumentItem item = new DocumentItemToDynamicDocumentItem().apply(result);
    item.put("siteId", siteId != null ? siteId : DEFAULT_SITE_ID);
    item.put("previous", current.getPrevious());
    item.put("next", current.hasNext() ? current.getNext() : null);
    return new ApiRequestHandlerResponse(SC_OK, new ApiMapResponse(item));
}
Also used : ApiPagination(com.formkiq.lambda.apigateway.ApiPagination) DynamicDocumentItem(com.formkiq.stacks.dynamodb.DynamicDocumentItem) DocumentItemToDynamicDocumentItem(com.formkiq.stacks.dynamodb.DocumentItemToDynamicDocumentItem) DocumentItemToDynamicDocumentItem(com.formkiq.stacks.dynamodb.DocumentItemToDynamicDocumentItem) DynamicDocumentItem(com.formkiq.stacks.dynamodb.DynamicDocumentItem) DocumentItemToDynamicDocumentItem(com.formkiq.stacks.dynamodb.DocumentItemToDynamicDocumentItem) DocumentItem(com.formkiq.stacks.dynamodb.DocumentItem) NotFoundException(com.formkiq.lambda.apigateway.exception.NotFoundException) ApiRequestHandlerResponse(com.formkiq.lambda.apigateway.ApiRequestHandlerResponse) ApiMapResponse(com.formkiq.lambda.apigateway.ApiMapResponse)

Example 10 with DocumentItem

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

the class DocumentsRequestHandler method get.

@Override
public ApiRequestHandlerResponse get(final LambdaLogger logger, final ApiGatewayRequestEvent event, final ApiAuthorizer authorizer, final AwsServiceCache awsservice) throws Exception {
    ApiPagination pagination = getPagination(awsservice.documentCacheService(), event);
    final int limit = pagination != null ? pagination.getLimit() : getLimit(logger, event);
    final PaginationMapToken ptoken = pagination != null ? pagination.getStartkey() : null;
    String tz = getParameter(event, "tz");
    String dateString = getParameter(event, "date");
    if (StringUtils.isBlank(dateString)) {
        if (StringUtils.isNotBlank(tz)) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            ZoneOffset offset = DateUtil.getZoneOffset(tz);
            sdf.setTimeZone(TimeZone.getTimeZone(offset));
            dateString = sdf.format(new Date());
        } else {
            dateString = this.df.format(new Date());
        }
    }
    ZonedDateTime date = transformToDate(logger, awsservice, dateString, tz);
    String siteId = authorizer.getSiteId();
    final PaginationResults<DocumentItem> results = awsservice.documentService().findDocumentsByDate(siteId, date, ptoken, limit);
    ApiPagination current = createPagination(awsservice.documentCacheService(), event, pagination, results.getToken(), limit);
    List<DocumentItem> documents = subList(results.getResults(), limit);
    List<DynamicDocumentItem> items = documents.stream().map(m -> new DocumentItemToDynamicDocumentItem().apply(m)).collect(Collectors.toList());
    items.forEach(i -> i.put("siteId", siteId != null ? siteId : DEFAULT_SITE_ID));
    Map<String, Object> map = new HashMap<>();
    map.put("documents", items);
    map.put("previous", current.getPrevious());
    map.put("next", current.hasNext() ? current.getNext() : null);
    return new ApiRequestHandlerResponse(SC_OK, new ApiMapResponse(map));
}
Also used : StringUtils(software.amazon.awssdk.utils.StringUtils) Date(java.util.Date) ZonedDateTime(java.time.ZonedDateTime) DEFAULT_SITE_ID(com.formkiq.stacks.dynamodb.SiteIdKeyGenerator.DEFAULT_SITE_ID) ApiMapResponse(com.formkiq.lambda.apigateway.ApiMapResponse) SimpleDateFormat(java.text.SimpleDateFormat) HashMap(java.util.HashMap) AwsServiceCache(com.formkiq.lambda.apigateway.AwsServiceCache) ApiAuthorizer(com.formkiq.lambda.apigateway.ApiAuthorizer) DynamicDocumentItem(com.formkiq.stacks.dynamodb.DynamicDocumentItem) ApiGatewayRequestEventUtil(com.formkiq.lambda.apigateway.ApiGatewayRequestEventUtil) LambdaLogger(com.amazonaws.services.lambda.runtime.LambdaLogger) Map(java.util.Map) ZoneOffset(java.time.ZoneOffset) SC_OK(com.formkiq.lambda.apigateway.ApiResponseStatus.SC_OK) PaginationMapToken(com.formkiq.stacks.dynamodb.PaginationMapToken) DocumentItemToDynamicDocumentItem(com.formkiq.stacks.dynamodb.DocumentItemToDynamicDocumentItem) TimeZone(java.util.TimeZone) ApiGatewayRequestHandler(com.formkiq.lambda.apigateway.ApiGatewayRequestHandler) ApiPagination(com.formkiq.lambda.apigateway.ApiPagination) ZoneRulesException(java.time.zone.ZoneRulesException) Collectors(java.util.stream.Collectors) BadException(com.formkiq.lambda.apigateway.exception.BadException) List(java.util.List) DateUtil(com.formkiq.stacks.dynamodb.DateUtil) ApiGatewayRequestEvent(com.formkiq.lambda.apigateway.ApiGatewayRequestEvent) ApiRequestHandlerResponse(com.formkiq.lambda.apigateway.ApiRequestHandlerResponse) PaginationResults(com.formkiq.stacks.dynamodb.PaginationResults) DocumentItem(com.formkiq.stacks.dynamodb.DocumentItem) ApiPagination(com.formkiq.lambda.apigateway.ApiPagination) HashMap(java.util.HashMap) DocumentItemToDynamicDocumentItem(com.formkiq.stacks.dynamodb.DocumentItemToDynamicDocumentItem) Date(java.util.Date) ZoneOffset(java.time.ZoneOffset) DynamicDocumentItem(com.formkiq.stacks.dynamodb.DynamicDocumentItem) DocumentItemToDynamicDocumentItem(com.formkiq.stacks.dynamodb.DocumentItemToDynamicDocumentItem) ZonedDateTime(java.time.ZonedDateTime) DynamicDocumentItem(com.formkiq.stacks.dynamodb.DynamicDocumentItem) DocumentItemToDynamicDocumentItem(com.formkiq.stacks.dynamodb.DocumentItemToDynamicDocumentItem) DocumentItem(com.formkiq.stacks.dynamodb.DocumentItem) ApiRequestHandlerResponse(com.formkiq.lambda.apigateway.ApiRequestHandlerResponse) ApiMapResponse(com.formkiq.lambda.apigateway.ApiMapResponse) PaginationMapToken(com.formkiq.stacks.dynamodb.PaginationMapToken) SimpleDateFormat(java.text.SimpleDateFormat)

Aggregations

DocumentItem (com.formkiq.stacks.dynamodb.DocumentItem)25 DynamicDocumentItem (com.formkiq.stacks.dynamodb.DynamicDocumentItem)20 Date (java.util.Date)17 Test (org.junit.jupiter.api.Test)14 DocumentTag (com.formkiq.stacks.dynamodb.DocumentTag)10 Timeout (org.junit.jupiter.api.Timeout)9 DocumentItemToDynamicDocumentItem (com.formkiq.stacks.dynamodb.DocumentItemToDynamicDocumentItem)7 DynamicDocumentTag (com.formkiq.stacks.dynamodb.DynamicDocumentTag)7 S3Client (software.amazon.awssdk.services.s3.S3Client)7 ApiRequestHandlerResponse (com.formkiq.lambda.apigateway.ApiRequestHandlerResponse)6 DynamicObject (com.formkiq.stacks.common.objects.DynamicObject)6 DocumentItemDynamoDb (com.formkiq.stacks.dynamodb.DocumentItemDynamoDb)6 ApiGatewayRequestEvent (com.formkiq.lambda.apigateway.ApiGatewayRequestEvent)5 Map (java.util.Map)5 NotFoundException (com.formkiq.lambda.apigateway.exception.NotFoundException)4 ArrayList (java.util.ArrayList)4 ApiMapResponse (com.formkiq.lambda.apigateway.ApiMapResponse)3 BadException (com.formkiq.lambda.apigateway.exception.BadException)3 ApiUrlResponse (com.formkiq.stacks.api.ApiUrlResponse)3 DocumentService (com.formkiq.stacks.dynamodb.DocumentService)3