Search in sources :

Example 1 with DocumentFormat

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

the class DocumentsS3UpdateTest method testHandleRequest02.

/**
 * Update Document Request with existing Tags.
 *
 * @throws Exception Exception
 */
@Test
@Timeout(unit = TimeUnit.MILLISECONDS, value = TEST_TIMEOUT)
public void testHandleRequest02() throws Exception {
    for (String siteId : Arrays.asList(null, UUID.randomUUID().toString())) {
        // given
        this.logger.reset();
        String key = createDatabaseKey(siteId, BUCKET_KEY);
        final Map<String, Object> map = loadFileAsMap(this, "/objectupdate-event1.json", BUCKET_KEY, key);
        DynamicDocumentItem doc = new DynamicDocumentItem(Map.of());
        doc.setInsertedDate(new Date());
        doc.setDocumentId(BUCKET_KEY);
        doc.setUserId("asd");
        doc.setPath("test.txt");
        DynamicDocumentTag tag = new DynamicDocumentTag(Map.of("documentId", BUCKET_KEY, "key", "person", "value", "category", "insertedDate", new Date(), "userId", "asd"));
        doc.put("tags", Arrays.asList(tag));
        service.saveDocumentItemWithTag(siteId, doc);
        DocumentFormat format = new DocumentFormat();
        format.setContentType("application/pdf");
        format.setDocumentId(BUCKET_KEY);
        format.setInsertedDate(new Date());
        format.setUserId("asd");
        service.saveDocumentFormat(siteId, format);
        addS3File(key, "pdf", true, "testdata");
        // when
        final DocumentItem item = handleRequest(siteId, BUCKET_KEY, map);
        // then
        PaginationResults<DocumentTag> tags = service.findDocumentTags(siteId, BUCKET_KEY, null, MAX_RESULTS);
        final int size = 5;
        int i = 0;
        assertEquals(size, tags.getResults().size());
        assertEquals("CLAMAV_SCAN_STATUS", tags.getResults().get(i).getKey());
        assertEquals("GOOD", tags.getResults().get(i).getValue());
        assertEquals(DocumentTagType.SYSTEMDEFINED, tags.getResults().get(i++).getType());
        assertEquals("path", tags.getResults().get(i).getKey());
        assertEquals(DocumentTagType.SYSTEMDEFINED, tags.getResults().get(i++).getType());
        assertEquals("person", tags.getResults().get(i).getKey());
        assertEquals(DocumentTagType.USERDEFINED, tags.getResults().get(i++).getType());
        assertEquals("12345", tags.getResults().get(i).getValue());
        assertEquals(DocumentTagType.USERDEFINED, tags.getResults().get(i).getType());
        assertEquals("12345", tags.getResults().get(i++).getValue());
        assertEquals(DocumentTagType.SYSTEMDEFINED, tags.getResults().get(i).getType());
        assertEquals("asd", tags.getResults().get(i++).getValue());
        assertEquals(0, service.findDocumentFormats(siteId, BUCKET_KEY, null, MAX_RESULTS).getResults().size());
        verifyDocumentSaved(siteId, item, "pdf", "8");
        assertPublishSnsMessage(siteId, sqsDocumentEventUrl, "update", false, false);
    }
}
Also used : DocumentFormat(com.formkiq.stacks.dynamodb.DocumentFormat) DynamicDocumentItem(com.formkiq.stacks.dynamodb.DynamicDocumentItem) DynamicDocumentTag(com.formkiq.stacks.dynamodb.DynamicDocumentTag) DocumentTag(com.formkiq.stacks.dynamodb.DocumentTag) DynamicDocumentTag(com.formkiq.stacks.dynamodb.DynamicDocumentTag) DocumentItem(com.formkiq.stacks.dynamodb.DocumentItem) DynamicDocumentItem(com.formkiq.stacks.dynamodb.DynamicDocumentItem) Date(java.util.Date) Test(org.junit.jupiter.api.Test) Timeout(org.junit.jupiter.api.Timeout)

Example 2 with DocumentFormat

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

the class DocumentIdUrlRequestHandler method getS3Url.

/**
 * Get S3 URL.
 *
 * @param logger {@link LambdaLogger}
 * @param authorizer {@link ApiAuthorizer}
 * @param awsservice {@link AwsServiceCache}
 * @param event {@link ApiGatewayRequestEvent}
 * @param documentId {@link String}
 * @param item {@link DocumentItem}
 * @param versionId {@link String}
 * @return {@link URL}
 */
private URL getS3Url(final LambdaLogger logger, final ApiAuthorizer authorizer, final AwsServiceCache awsservice, final ApiGatewayRequestEvent event, final DocumentItem item, final String documentId, final String versionId) {
    URL url = null;
    String contentType = getContentType(event);
    String siteId = authorizer.getSiteId();
    int hours = getDurationHours(event);
    Duration duration = Duration.ofHours(hours);
    if (awsservice.debug()) {
        logger.log("Finding S3 Url for 'Content-Type' " + contentType);
    }
    if (contentType == null || contentType.equals(item.getContentType())) {
        logger.log("Found default format " + contentType + " for siteId: " + siteId + " documentId: " + documentId);
        String s3key = createS3Key(siteId, documentId);
        url = awsservice.s3Service().presignGetUrl(awsservice.documents3bucket(), s3key, duration, versionId);
    } else {
        Optional<DocumentFormat> format = awsservice.documentService().findDocumentFormat(siteId, documentId, contentType);
        if (format.isPresent()) {
            if (awsservice.debug()) {
                logger.log("Found format " + contentType + " for siteId: " + siteId + " documentId: " + documentId);
            }
            String s3key = createS3Key(siteId, documentId, contentType);
            url = awsservice.s3Service().presignGetUrl(awsservice.documents3bucket(), s3key, duration, versionId);
        } else if (awsservice.debug()) {
            logger.log("Cannot find format " + contentType + " for siteId: " + siteId + " documentId: " + documentId);
        }
    }
    return url;
}
Also used : DocumentFormat(com.formkiq.stacks.dynamodb.DocumentFormat) Duration(java.time.Duration) URL(java.net.URL)

Example 3 with DocumentFormat

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

the class DocumentIdUrlGetRequestHandlerTest method testHandleGetDocumentContent01.

/**
 * /documents/{documentId}/url request.
 *
 * Tests No Content-Type, Content-Type matches DocumentItem's Content Type and Document Format
 * exists.
 *
 * @throws Exception an error has occurred
 */
@SuppressWarnings("unchecked")
@Test
public void testHandleGetDocumentContent01() throws Exception {
    for (String contentType : Arrays.asList(null, "application/pdf", "text/plain")) {
        for (String siteId : Arrays.asList(null, UUID.randomUUID().toString())) {
            // given
            String documentId = UUID.randomUUID().toString();
            String userId = "jsmith";
            if (contentType != null) {
                DocumentFormat format = new DocumentFormat();
                format.setContentType(contentType);
                format.setDocumentId(documentId);
                format.setInsertedDate(new Date());
                format.setUserId(userId);
                getDocumentService().saveDocumentFormat(siteId, format);
            }
            ApiGatewayRequestEvent event = toRequestEvent("/request-get-documents-documentid-url01.json");
            addParameter(event, "siteId", siteId);
            setPathParameter(event, "documentId", documentId);
            addHeader(event, "Content-Type", contentType);
            DocumentItemDynamoDb item = new DocumentItemDynamoDb(documentId, new Date(), userId);
            if ("text/plain".equals(contentType)) {
                item.setContentType(contentType);
            }
            getDocumentService().saveDocument(siteId, item, new ArrayList<>());
            // when
            String response = handleRequest(event);
            // then
            Map<String, String> m = fromJson(response, Map.class);
            final int mapsize = 3;
            assertEquals(mapsize, m.size());
            assertEquals("200.0", String.valueOf(m.get("statusCode")));
            assertEquals(getHeaders(), "\"headers\":" + GsonUtil.getInstance().toJson(m.get("headers")));
            ApiUrlResponse resp = fromJson(m.get("body"), ApiUrlResponse.class);
            assertTrue(resp.getUrl().contains("X-Amz-Algorithm=AWS4-HMAC-SHA256"));
            assertTrue(resp.getUrl().contains("X-Amz-Expires=172800"));
            assertTrue(resp.getUrl().contains(AWS_REGION.toString()));
            assertNull(resp.getNext());
            assertNull(resp.getPrevious());
            if (siteId != null) {
                assertTrue(resp.getUrl().startsWith(this.localstack.getEndpointOverride(Service.S3).toString() + "/testbucket/" + siteId + "/" + documentId));
            } else {
                assertTrue(resp.getUrl().startsWith(this.localstack.getEndpointOverride(Service.S3).toString() + "/testbucket/" + documentId));
            }
        }
    }
}
Also used : DocumentFormat(com.formkiq.stacks.dynamodb.DocumentFormat) ApiGatewayRequestEvent(com.formkiq.lambda.apigateway.ApiGatewayRequestEvent) Date(java.util.Date) DocumentItemDynamoDb(com.formkiq.stacks.dynamodb.DocumentItemDynamoDb) Test(org.junit.jupiter.api.Test)

Aggregations

DocumentFormat (com.formkiq.stacks.dynamodb.DocumentFormat)3 Date (java.util.Date)2 Test (org.junit.jupiter.api.Test)2 ApiGatewayRequestEvent (com.formkiq.lambda.apigateway.ApiGatewayRequestEvent)1 DocumentItem (com.formkiq.stacks.dynamodb.DocumentItem)1 DocumentItemDynamoDb (com.formkiq.stacks.dynamodb.DocumentItemDynamoDb)1 DocumentTag (com.formkiq.stacks.dynamodb.DocumentTag)1 DynamicDocumentItem (com.formkiq.stacks.dynamodb.DynamicDocumentItem)1 DynamicDocumentTag (com.formkiq.stacks.dynamodb.DynamicDocumentTag)1 URL (java.net.URL)1 Duration (java.time.Duration)1 Timeout (org.junit.jupiter.api.Timeout)1