Search in sources :

Example 21 with DocumentItemDynamoDb

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

the class ApiRequestHandlerTest method testHandleGetRequest05.

/**
 * Get Document Request, Document found.
 *
 * @throws Exception an error has occurred
 */
@SuppressWarnings("unchecked")
@Test
public void testHandleGetRequest05() throws Exception {
    // given
    Date date = new Date();
    String documentId = "1a1d1938-451e-4e20-bf95-e0e7a749505a";
    String userId = "jsmith";
    DocumentItem item = new DocumentItemDynamoDb(documentId, date, userId);
    getDocumentService().saveDocument(null, item, new ArrayList<>());
    ApiGatewayRequestEvent event = toRequestEvent("/request-get-documents-documentid02.json");
    // 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")));
    DynamicObject resp = new DynamicObject(fromJson(m.get("body"), Map.class));
    assertEquals(documentId, resp.get("documentId"));
    assertEquals(userId, resp.get("userId"));
    assertNotNull(documentId, resp.get("insertedDate"));
    assertEquals(DEFAULT_SITE_ID, resp.get("siteId"));
    assertNull(resp.get("next"));
    assertNull(resp.get("previous"));
}
Also used : DynamicObject(com.formkiq.stacks.common.objects.DynamicObject) DocumentItemToDynamicDocumentItem(com.formkiq.stacks.dynamodb.DocumentItemToDynamicDocumentItem) DynamicDocumentItem(com.formkiq.stacks.dynamodb.DynamicDocumentItem) DocumentItem(com.formkiq.stacks.dynamodb.DocumentItem) ApiGatewayRequestEvent(com.formkiq.lambda.apigateway.ApiGatewayRequestEvent) Map(java.util.Map) Date(java.util.Date) DocumentItemDynamoDb(com.formkiq.stacks.dynamodb.DocumentItemDynamoDb) Test(org.junit.jupiter.api.Test)

Example 22 with DocumentItemDynamoDb

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

the class ApiRequestHandlerTest method testHandleGetRequest06.

/**
 * Get Document Request, Document with sub docs found.
 *
 * @throws Exception an error has occurred
 */
@SuppressWarnings("unchecked")
@Test
public void testHandleGetRequest06() throws Exception {
    // given
    Date date = new Date();
    String documentId0 = "1a1d1938-451e-4e20-bf95-e0e7a749505a";
    String documentId1 = UUID.randomUUID().toString();
    String userId = "jsmith";
    DocumentItem item = new DocumentItemDynamoDb(documentId0, date, userId);
    DocumentItem citem = new DocumentItemDynamoDb(documentId1, date, userId);
    DynamicDocumentItem doc = new DocumentItemToDynamicDocumentItem().apply(item);
    doc.put("documents", Arrays.asList(new DocumentItemToDynamicDocumentItem().apply(citem)));
    getDocumentService().saveDocumentItemWithTag(null, doc);
    ApiGatewayRequestEvent event = toRequestEvent("/request-get-documents-documentid02.json");
    // 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")));
    DynamicObject resp = new DynamicObject(fromJson(m.get("body"), Map.class));
    assertEquals(documentId0, resp.get("documentId"));
    assertEquals(userId, resp.get("userId"));
    assertNotNull(resp.get("insertedDate"));
    assertEquals(DEFAULT_SITE_ID, resp.get("siteId"));
    assertNull(resp.get("next"));
    assertNull(resp.get("previous"));
    List<Map<String, Object>> children = (List<Map<String, Object>>) resp.get("documents");
    assertEquals(1, children.size());
    assertEquals(documentId1, children.get(0).get("documentId"));
    assertNotNull(userId, children.get(0).get("userId"));
    assertNotNull(children.get(0).get("belongsToDocumentId"));
    assertNotNull(children.get(0).get("insertedDate"));
    assertNull(children.get(0).get("siteId"));
}
Also used : DocumentItemToDynamicDocumentItem(com.formkiq.stacks.dynamodb.DocumentItemToDynamicDocumentItem) Date(java.util.Date) DocumentItemToDynamicDocumentItem(com.formkiq.stacks.dynamodb.DocumentItemToDynamicDocumentItem) DynamicDocumentItem(com.formkiq.stacks.dynamodb.DynamicDocumentItem) DynamicObject(com.formkiq.stacks.common.objects.DynamicObject) DocumentItemToDynamicDocumentItem(com.formkiq.stacks.dynamodb.DocumentItemToDynamicDocumentItem) DynamicDocumentItem(com.formkiq.stacks.dynamodb.DynamicDocumentItem) DocumentItem(com.formkiq.stacks.dynamodb.DocumentItem) ApiGatewayRequestEvent(com.formkiq.lambda.apigateway.ApiGatewayRequestEvent) ArrayList(java.util.ArrayList) List(java.util.List) DynamicObject(com.formkiq.stacks.common.objects.DynamicObject) Map(java.util.Map) DocumentItemDynamoDb(com.formkiq.stacks.dynamodb.DocumentItemDynamoDb) Test(org.junit.jupiter.api.Test)

Example 23 with DocumentItemDynamoDb

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

the class ApiDocumentsPatchRequestTest method testHandlePatchDocuments01.

/**
 * POST /documents request Base64 body.
 *
 * @throws Exception an error has occurred
 */
@Test
public void testHandlePatchDocuments01() throws Exception {
    for (String siteId : Arrays.asList(null, UUID.randomUUID().toString())) {
        // given
        String userId = "jsmith";
        String documentId = UUID.randomUUID().toString();
        getDocumentService().saveDocument(siteId, new DocumentItemDynamoDb(documentId, new Date(), userId), new ArrayList<>());
        ApiGatewayRequestEvent event = toRequestEvent("/request-patch-documents-documentid01.json");
        addParameter(event, "siteId", siteId);
        setPathParameter(event, "documentId", documentId);
        // when
        String response = handleRequest(event);
        // then
        assert200Response(siteId, response);
        assertTrue(getLogger().containsString("setting userId: 8a73dfef-26d3-43d8-87aa-b3ec358e43ba@formkiq.com " + "contentType: application/pdf"));
    }
}
Also used : ApiGatewayRequestEvent(com.formkiq.lambda.apigateway.ApiGatewayRequestEvent) Date(java.util.Date) DocumentItemDynamoDb(com.formkiq.stacks.dynamodb.DocumentItemDynamoDb) Test(org.junit.jupiter.api.Test)

Example 24 with DocumentItemDynamoDb

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

the class DocumentIdContentGetRequestHandlerTest method testReturnContent.

/**
 * Test Content is returned.
 * @param contentType {@link String}
 * @throws Exception Exception
 */
@SuppressWarnings("unchecked")
private void testReturnContent(final String contentType) throws Exception {
    for (String siteId : Arrays.asList(null, UUID.randomUUID().toString())) {
        // given
        String documentId = UUID.randomUUID().toString();
        String content = "this is a test";
        String s3key = createS3Key(siteId, documentId);
        try (S3Client s3 = getS3().buildClient()) {
            getS3().putObject(s3, BUCKET_NAME, s3key, content.getBytes(StandardCharsets.UTF_8), null);
        }
        ApiGatewayRequestEvent event = toRequestEvent("/request-get-documents-documentid-content01.json");
        addParameter(event, "siteId", siteId);
        setPathParameter(event, "documentId", documentId);
        String userId = "jsmith";
        DocumentItemDynamoDb item = new DocumentItemDynamoDb(documentId, new Date(), userId);
        item.setContentType(contentType);
        getDocumentService().saveDocument(siteId, item, new ArrayList<>());
        // when
        String response = handleRequest(event);
        // then
        Map<String, Object> m = fromJson(response, Map.class);
        final int mapsize = 3;
        assertEquals(mapsize, m.size());
        assertEquals("200.0", String.valueOf(m.get("statusCode")));
        Map<String, Object> body = fromJson(m.get("body").toString(), Map.class);
        assertEquals(content, body.get("content"));
        assertEquals(contentType, body.get("contentType"));
        assertEquals("false", body.get("isBase64").toString());
    }
}
Also used : ApiGatewayRequestEvent(com.formkiq.lambda.apigateway.ApiGatewayRequestEvent) S3Client(software.amazon.awssdk.services.s3.S3Client) Date(java.util.Date) DocumentItemDynamoDb(com.formkiq.stacks.dynamodb.DocumentItemDynamoDb)

Example 25 with DocumentItemDynamoDb

use of com.formkiq.stacks.dynamodb.DocumentItemDynamoDb 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

DocumentItemDynamoDb (com.formkiq.stacks.dynamodb.DocumentItemDynamoDb)32 Date (java.util.Date)32 ApiGatewayRequestEvent (com.formkiq.lambda.apigateway.ApiGatewayRequestEvent)28 Test (org.junit.jupiter.api.Test)27 DocumentTag (com.formkiq.stacks.dynamodb.DocumentTag)13 DynamicObject (com.formkiq.stacks.common.objects.DynamicObject)12 Map (java.util.Map)12 LocalDate (java.time.LocalDate)7 DocumentItem (com.formkiq.stacks.dynamodb.DocumentItem)6 DynamicDocumentItem (com.formkiq.stacks.dynamodb.DynamicDocumentItem)4 ArrayList (java.util.ArrayList)4 DocumentSearchQuery (com.formkiq.stacks.client.models.DocumentSearchQuery)3 DocumentItemToDynamicDocumentItem (com.formkiq.stacks.dynamodb.DocumentItemToDynamicDocumentItem)3 ApiRequestHandlerResponse (com.formkiq.lambda.apigateway.ApiRequestHandlerResponse)2 BadException (com.formkiq.lambda.apigateway.exception.BadException)2 ApiUrlResponse (com.formkiq.stacks.api.ApiUrlResponse)2 DocumentSearch (com.formkiq.stacks.client.models.DocumentSearch)2 DocumentSearchTag (com.formkiq.stacks.client.models.DocumentSearchTag)2 DocumentService (com.formkiq.stacks.dynamodb.DocumentService)2 S3Client (software.amazon.awssdk.services.s3.S3Client)2