Search in sources :

Example 1 with DynamicObject

use of com.formkiq.stacks.common.objects.DynamicObject in project formkiq-core by formkiq.

the class ApiDocumentsUploadRequestTest method testHandleGetDocumentsUpload06.

/**
 * Content-Length > Max Content Length.
 *
 * @throws Exception an error has occurred
 */
@SuppressWarnings("unchecked")
@Test
public void testHandleGetDocumentsUpload06() throws Exception {
    String maxContentLengthBytes = "2783034";
    getAwsServices().configService().save(null, new DynamicObject(Map.of(ConfigService.MAX_DOCUMENT_SIZE_BYTES, maxContentLengthBytes)));
    for (String siteId : Arrays.asList(null, UUID.randomUUID().toString())) {
        if (siteId != null) {
            getAwsServices().configService().save(siteId, new DynamicObject(Map.of(ConfigService.MAX_DOCUMENT_SIZE_BYTES, maxContentLengthBytes)));
        }
        // given
        ApiGatewayRequestEvent event = toRequestEvent("/request-get-documents-upload-documentid.json");
        addParameter(event, "siteId", siteId);
        addParameter(event, "contentLength", "2783035");
        // when
        String response = handleRequest(event);
        // then
        Map<String, String> m = GsonUtil.getInstance().fromJson(response, Map.class);
        final int mapsize = 3;
        assertEquals(mapsize, m.size());
        assertEquals("400.0", String.valueOf(m.get("statusCode")));
        assertEquals("{\"message\":\"'contentLength' cannot exceed 2783034 bytes\"}", m.get("body"));
    }
}
Also used : DynamicObject(com.formkiq.stacks.common.objects.DynamicObject) ApiGatewayRequestEvent(com.formkiq.lambda.apigateway.ApiGatewayRequestEvent) Test(org.junit.jupiter.api.Test)

Example 2 with DynamicObject

use of com.formkiq.stacks.common.objects.DynamicObject in project formkiq-core by formkiq.

the class ApiDocumentsUploadRequestTest method testHandleGetDocumentsUpload07.

/**
 * Content-Length is required.
 *
 * @throws Exception an error has occurred
 */
@SuppressWarnings("unchecked")
@Test
public void testHandleGetDocumentsUpload07() throws Exception {
    String maxContentLengthBytes = "2783034";
    getAwsServices().configService().save(null, new DynamicObject(Map.of(ConfigService.MAX_DOCUMENT_SIZE_BYTES, maxContentLengthBytes)));
    for (String siteId : Arrays.asList(null, UUID.randomUUID().toString())) {
        if (siteId != null) {
            getAwsServices().configService().save(siteId, new DynamicObject(Map.of(ConfigService.MAX_DOCUMENT_SIZE_BYTES, maxContentLengthBytes)));
        }
        // given
        ApiGatewayRequestEvent event = toRequestEvent("/request-get-documents-upload-documentid.json");
        addParameter(event, "siteId", siteId);
        // when
        String response = handleRequest(event);
        // then
        Map<String, String> m = GsonUtil.getInstance().fromJson(response, Map.class);
        final int mapsize = 3;
        assertEquals(mapsize, m.size());
        assertEquals("400.0", String.valueOf(m.get("statusCode")));
        assertEquals("{\"message\":\"'contentLength' is required\"}", m.get("body"));
    }
}
Also used : DynamicObject(com.formkiq.stacks.common.objects.DynamicObject) ApiGatewayRequestEvent(com.formkiq.lambda.apigateway.ApiGatewayRequestEvent) Test(org.junit.jupiter.api.Test)

Example 3 with DynamicObject

use of com.formkiq.stacks.common.objects.DynamicObject in project formkiq-core by formkiq.

the class ApiPublicWebhooksRequestTest method testPostWebhooks12.

/**
 * Post /public/webhooks with Config 'DocumentTimeToLive'.
 *
 * @throws Exception an error has occurred
 */
@SuppressWarnings("unchecked")
@Test
public void testPostWebhooks12() throws Exception {
    // given
    createApiRequestHandler(getMap());
    for (String siteId : Arrays.asList(null, UUID.randomUUID().toString())) {
        String name = UUID.randomUUID().toString();
        getAwsServices().configService().save(siteId, new DynamicObject(Map.of(DOCUMENT_TIME_TO_LIVE, "1000")));
        String id = getAwsServices().webhookService().saveWebhook(siteId, name, "joe", null, "true");
        ApiGatewayRequestEvent event = toRequestEvent("/request-post-public-webhooks01.json", siteId, id);
        // when
        String response = handleRequest(event);
        // then
        Map<String, String> m = fromJson(response, Map.class);
        verifyHeaders(m, "200.0");
        Map<String, Object> body = fromJson(m.get("body"), Map.class);
        String documentId = body.get("documentId").toString();
        assertNotNull(documentId);
        // verify s3 file
        try (S3Client s3 = getS3().buildClient()) {
            String key = createDatabaseKey(siteId, documentId + FORMKIQ_DOC_EXT);
            String json = getS3().getContentAsString(s3, STAGE_BUCKET_NAME, key, null);
            Map<String, Object> map = fromJson(json, Map.class);
            assertEquals(documentId, map.get("documentId"));
            assertEquals("webhook/" + name, map.get("userId"));
            assertEquals("webhooks/" + id, map.get("path"));
            assertEquals("{\"name\":\"john smith\"}", map.get("content"));
            assertEquals("1000", map.get("TimeToLive"));
        }
    }
}
Also used : DynamicObject(com.formkiq.stacks.common.objects.DynamicObject) ApiGatewayRequestEvent(com.formkiq.lambda.apigateway.ApiGatewayRequestEvent) DynamicObject(com.formkiq.stacks.common.objects.DynamicObject) S3Client(software.amazon.awssdk.services.s3.S3Client) Test(org.junit.jupiter.api.Test)

Example 4 with DynamicObject

use of com.formkiq.stacks.common.objects.DynamicObject in project formkiq-core by formkiq.

the class ApiRequestHandlerTest method testHandleGetRequest02.

/**
 * Get Document Request, Document found.
 *
 * @throws Exception an error has occurred
 */
@SuppressWarnings("unchecked")
@Test
public void testHandleGetRequest02() throws Exception {
    for (String siteId : Arrays.asList(null, UUID.randomUUID().toString())) {
        // given
        Date date = new Date();
        String documentId = UUID.randomUUID().toString();
        String userId = "jsmith";
        DocumentItem item = new DocumentItemDynamoDb(documentId, date, userId);
        getDocumentService().saveDocument(siteId, item, new ArrayList<>());
        ApiGatewayRequestEvent event = toRequestEvent("/request-get-documents-documentid01.json");
        addParameter(event, "siteId", siteId);
        setPathParameter(event, "documentId", documentId);
        // 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.getString("documentId"));
        assertEquals(userId, resp.getString("userId"));
        assertNotNull(resp.get("insertedDate"));
        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 5 with DynamicObject

use of com.formkiq.stacks.common.objects.DynamicObject in project formkiq-core by formkiq.

the class ApiWebhooksRequestTest method testPostWebhooks01.

/**
 * POST /webhooks.
 * Test TTL.
 *
 * @throws Exception an error has occurred
 */
@SuppressWarnings("unchecked")
@Test
public void testPostWebhooks01() throws Exception {
    // given
    putSsmParameter("/formkiq/" + FORMKIQ_APP_ENVIRONMENT + "/api/DocumentsPublicHttpUrl", "http://localhost:8080");
    ApiGatewayRequestEvent event = toRequestEvent("/request-post-webhooks01.json");
    String ttl = "90000";
    event.setBody("{\"name\":\"john smith\",\"ttl\":\"" + ttl + "\"}");
    // when
    String response = handleRequest(event);
    // then
    Map<String, String> m = GsonUtil.getInstance().fromJson(response, Map.class);
    assertEquals("200.0", String.valueOf(m.get("statusCode")));
    Map<String, Object> result = GsonUtil.getInstance().fromJson(m.get("body"), Map.class);
    assertEquals("default", result.get("siteId"));
    assertNotNull(result.get("id"));
    final String id = result.get("id").toString();
    assertNotNull(result.get("insertedDate"));
    assertEquals("john smith", result.get("name"));
    assertEquals("test@formkiq.com", result.get("userId"));
    assertEquals("http://localhost:8080/public/webhooks/" + id, result.get("url"));
    assertNotNull(result.get("ttl"));
    WebhooksService webhookService = getAwsServices().webhookService();
    DynamicObject obj = webhookService.findWebhook(null, id);
    long epoch = Long.parseLong(obj.getString("TimeToLive"));
    ZonedDateTime ld = Instant.ofEpochMilli(epoch * TO_MILLIS).atZone(ZoneOffset.UTC);
    ZonedDateTime now = ZonedDateTime.now(ZoneOffset.UTC).plusDays(1).plusHours(1);
    assertEquals(now.getDayOfMonth(), ld.getDayOfMonth());
    // given
    DocumentTag tag = new DocumentTag(id, "category", "person", new Date(), "joe");
    // when
    webhookService.addTags(null, id, Arrays.asList(tag), null);
    // then
    assertNull(webhookService.findTag(null, id, "category").getString("TimeToLive"));
    // update ttl/name
    // given
    ttl = "180000";
    event = toRequestEvent("/request-patch-webhooks-webhookid01.json");
    event.setPathParameters(Map.of("webhookId", id));
    event.setBody("{\"name\":\"john smith2\",\"ttl\":\"" + ttl + "\"}");
    // when
    response = handleRequest(event);
    // then
    m = GsonUtil.getInstance().fromJson(response, Map.class);
    assertEquals("200.0", String.valueOf(m.get("statusCode")));
    result = GsonUtil.getInstance().fromJson(m.get("body"), Map.class);
    assertEquals("'" + id + "' object updated", result.get("message"));
    obj = webhookService.findWebhook(null, id);
    assertEquals("john smith2", obj.get("path"));
    epoch = Long.parseLong(obj.getString("TimeToLive"));
    ld = Instant.ofEpochMilli(epoch * TO_MILLIS).atZone(ZoneOffset.UTC);
    now = ZonedDateTime.now(ZoneOffset.UTC).plusDays(2).plusHours(2);
    assertEquals(now.getDayOfMonth(), ld.getDayOfMonth());
    DynamicObject dtag = webhookService.findTag(null, id, "category");
    assertNotNull(dtag.getString("TimeToLive"));
    epoch = Long.parseLong(dtag.getString("TimeToLive"));
    ld = Instant.ofEpochMilli(epoch * TO_MILLIS).atZone(ZoneOffset.UTC);
    now = ZonedDateTime.now(ZoneOffset.UTC).plusDays(2);
    assertEquals(now.getDayOfMonth(), ld.getDayOfMonth());
}
Also used : DocumentTag(com.formkiq.stacks.dynamodb.DocumentTag) DynamicObject(com.formkiq.stacks.common.objects.DynamicObject) ZonedDateTime(java.time.ZonedDateTime) ApiGatewayRequestEvent(com.formkiq.lambda.apigateway.ApiGatewayRequestEvent) DynamicObject(com.formkiq.stacks.common.objects.DynamicObject) WebhooksService(com.formkiq.stacks.dynamodb.WebhooksService) Map(java.util.Map) Date(java.util.Date) Test(org.junit.jupiter.api.Test)

Aggregations

DynamicObject (com.formkiq.stacks.common.objects.DynamicObject)93 Test (org.junit.jupiter.api.Test)55 Map (java.util.Map)46 ApiGatewayRequestEvent (com.formkiq.lambda.apigateway.ApiGatewayRequestEvent)39 Date (java.util.Date)26 HashMap (java.util.HashMap)17 DocumentItemDynamoDb (com.formkiq.stacks.dynamodb.DocumentItemDynamoDb)12 DocumentTag (com.formkiq.stacks.dynamodb.DocumentTag)10 ZonedDateTime (java.time.ZonedDateTime)10 ApiRequestHandlerResponse (com.formkiq.lambda.apigateway.ApiRequestHandlerResponse)8 List (java.util.List)8 AttributeValue (software.amazon.awssdk.services.dynamodb.model.AttributeValue)7 S3Client (software.amazon.awssdk.services.s3.S3Client)7 ApiMapResponse (com.formkiq.lambda.apigateway.ApiMapResponse)6 BadException (com.formkiq.lambda.apigateway.exception.BadException)6 Collectors (java.util.stream.Collectors)6 NotFoundException (com.formkiq.lambda.apigateway.exception.NotFoundException)5 FormKiqClientV1 (com.formkiq.stacks.client.FormKiqClientV1)5 LocalDate (java.time.LocalDate)5 Test (org.junit.Test)5