Search in sources :

Example 31 with ApiGatewayRequestEvent

use of com.formkiq.lambda.apigateway.ApiGatewayRequestEvent in project formkiq-core by formkiq.

the class ApiWebhookIdRequestTest method testGetWebhook01.

/**
 * GET /webhooks/{webhookId}.
 *
 * @throws Exception an error has occurred
 */
@SuppressWarnings("unchecked")
@Test
public void testGetWebhook01() throws Exception {
    putSsmParameter("/formkiq/" + FORMKIQ_APP_ENVIRONMENT + "/api/DocumentsPublicHttpUrl", "http://localhost:8080");
    for (String siteId : Arrays.asList(null, UUID.randomUUID().toString())) {
        // given
        ApiGatewayRequestEvent event = toRequestEvent("/request-post-webhooks01.json");
        addParameter(event, "siteId", siteId);
        String response = handleRequest(event);
        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);
        assertNotNull(result.get("id"));
        String id = result.get("id").toString();
        event = toRequestEvent("/request-get-webhooks-webhookid01.json");
        setPathParameter(event, "webhookId", id);
        addParameter(event, "siteId", siteId);
        // when
        response = handleRequest(event);
        // then
        m = GsonUtil.getInstance().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")));
        result = GsonUtil.getInstance().fromJson(m.get("body"), Map.class);
        if (siteId == null) {
            assertEquals("default", result.get("siteId"));
        } else {
            assertNotNull(result.get("siteId"));
            assertNotEquals("default", result.get("siteId"));
        }
        assertNotNull(result.get("id"));
        id = result.get("id").toString();
        assertNotNull(result.get("insertedDate"));
        assertEquals("john smith", result.get("name"));
        assertEquals("test@formkiq.com", result.get("userId"));
        verifyUrl(siteId, id, result, true);
    }
}
Also used : ApiGatewayRequestEvent(com.formkiq.lambda.apigateway.ApiGatewayRequestEvent) Map(java.util.Map) Test(org.junit.jupiter.api.Test)

Example 32 with ApiGatewayRequestEvent

use of com.formkiq.lambda.apigateway.ApiGatewayRequestEvent in project formkiq-core by formkiq.

the class ApiWebhookIdRequestTest method testGetWebhook02.

/**
 * GET /webhooks/{webhookId} with PRIVATE.
 *
 * @throws Exception an error has occurred
 */
@SuppressWarnings("unchecked")
@Test
public void testGetWebhook02() throws Exception {
    putSsmParameter("/formkiq/" + FORMKIQ_APP_ENVIRONMENT + "/api/DocumentsPublicHttpUrl", "http://localhost:8080");
    for (String siteId : Arrays.asList(null, UUID.randomUUID().toString())) {
        // given
        ApiGatewayRequestEvent event = toRequestEvent("/request-post-webhooks01.json");
        addParameter(event, "siteId", siteId);
        event.setBody("{\"name\":\"john smith\",\"enabled\":\"private\"}");
        String response = handleRequest(event);
        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);
        assertNotNull(result.get("id"));
        String id = result.get("id").toString();
        event = toRequestEvent("/request-get-webhooks-webhookid01.json");
        setPathParameter(event, "webhookId", id);
        addParameter(event, "siteId", siteId);
        // when
        response = handleRequest(event);
        // then
        m = GsonUtil.getInstance().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")));
        result = GsonUtil.getInstance().fromJson(m.get("body"), Map.class);
        if (siteId == null) {
            assertEquals("default", result.get("siteId"));
        } else {
            assertNotNull(result.get("siteId"));
            assertNotEquals("default", result.get("siteId"));
        }
        assertNotNull(result.get("id"));
        id = result.get("id").toString();
        assertNotNull(result.get("insertedDate"));
        assertEquals("john smith", result.get("name"));
        assertEquals("test@formkiq.com", result.get("userId"));
        verifyUrl(siteId, id, result, false);
    }
}
Also used : ApiGatewayRequestEvent(com.formkiq.lambda.apigateway.ApiGatewayRequestEvent) Map(java.util.Map) Test(org.junit.jupiter.api.Test)

Example 33 with ApiGatewayRequestEvent

use of com.formkiq.lambda.apigateway.ApiGatewayRequestEvent 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)

Example 34 with ApiGatewayRequestEvent

use of com.formkiq.lambda.apigateway.ApiGatewayRequestEvent in project formkiq-core by formkiq.

the class ApiWebhooksRequestTest method testPostWebhooks05.

/**
 * POST /webhooks with Config WEBHOOK_TIME_TO_LIVE.
 *
 * @throws Exception an error has occurred
 */
@SuppressWarnings("unchecked")
@Test
public void testPostWebhooks05() throws Exception {
    // given
    putSsmParameter("/formkiq/" + FORMKIQ_APP_ENVIRONMENT + "/api/DocumentsPublicHttpUrl", "http://localhost:8080");
    ApiGatewayRequestEvent eventPost = toRequestEvent("/request-post-webhooks01.json");
    eventPost.setBody("{\"name\":\"john smith\"}");
    ApiGatewayRequestEvent event = toRequestEvent("/request-get-webhooks01.json");
    String siteId = null;
    String ttl = "87400";
    getAwsServices().configService().save(siteId, new DynamicObject(Map.of(WEBHOOK_TIME_TO_LIVE, ttl)));
    // when
    String responsePost = handleRequest(eventPost);
    final String response = handleRequest(event);
    // then
    Map<String, String> m = GsonUtil.getInstance().fromJson(responsePost, Map.class);
    assertEquals("200.0", String.valueOf(m.get("statusCode")));
    Map<String, Object> result = GsonUtil.getInstance().fromJson(m.get("body"), Map.class);
    final String id = verifyPostWebhooks05(result);
    m = GsonUtil.getInstance().fromJson(response, Map.class);
    assertEquals("200.0", String.valueOf(m.get("statusCode")));
    result = GsonUtil.getInstance().fromJson(m.get("body"), Map.class);
    List<Map<String, Object>> list = (List<Map<String, Object>>) result.get("webhooks");
    assertEquals(1, list.size());
    verifyPostWebhooks05(list.get(0));
    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);
    assertEquals(now.getDayOfMonth(), ld.getDayOfMonth());
    // given
    ttl = "-87400";
    getAwsServices().configService().save(siteId, new DynamicObject(Map.of(WEBHOOK_TIME_TO_LIVE, ttl)));
    // when
    responsePost = handleRequest(eventPost);
    // then
    m = GsonUtil.getInstance().fromJson(responsePost, Map.class);
    assertEquals("200.0", String.valueOf(m.get("statusCode")));
    result = GsonUtil.getInstance().fromJson(m.get("body"), Map.class);
    assertEquals("false", result.get("enabled"));
    assertNotNull(result.get("ttl"));
}
Also used : DynamicObject(com.formkiq.stacks.common.objects.DynamicObject) ZonedDateTime(java.time.ZonedDateTime) ApiGatewayRequestEvent(com.formkiq.lambda.apigateway.ApiGatewayRequestEvent) DynamicObject(com.formkiq.stacks.common.objects.DynamicObject) List(java.util.List) Map(java.util.Map) WebhooksService(com.formkiq.stacks.dynamodb.WebhooksService) Test(org.junit.jupiter.api.Test)

Example 35 with ApiGatewayRequestEvent

use of com.formkiq.lambda.apigateway.ApiGatewayRequestEvent in project formkiq-core by formkiq.

the class ApiWebhooksRequestTest method testPostWebhooks02.

/**
 * POST /webhooks with TAGS.
 * Test TTL.
 *
 * @throws Exception an error has occurred
 */
@SuppressWarnings("unchecked")
@Test
public void testPostWebhooks02() throws Exception {
    // given
    putSsmParameter("/formkiq/" + FORMKIQ_APP_ENVIRONMENT + "/api/DocumentsPublicHttpUrl", "http://localhost:8080");
    for (String siteId : Arrays.asList(null, UUID.randomUUID().toString())) {
        ApiGatewayRequestEvent event = toRequestEvent("/request-post-webhooks01.json");
        addParameter(event, "siteId", siteId);
        event.setBody("{\"name\":\"joe smith\",\"tags\":" + "[{\"key\":\"category\",\"value\":\"person\"},{\"key\":\"day\",\"value\":\"today\"}]}");
        // 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);
        if (siteId != null) {
            assertEquals(siteId, result.get("siteId"));
        } else {
            assertEquals("default", result.get("siteId"));
        }
        assertNotNull(result.get("id"));
        final String id = result.get("id").toString();
        assertNotNull(result.get("insertedDate"));
        assertEquals("joe smith", result.get("name"));
        assertEquals("test@formkiq.com", result.get("userId"));
        verifyUrl(siteId, id, result);
        WebhooksService webhookService = getAwsServices().webhookService();
        DynamicObject obj = webhookService.findWebhook(siteId, id);
        assertEquals("joe smith", obj.getString("path"));
        assertEquals("test@formkiq.com", obj.getString("userId"));
        PaginationResults<DynamicObject> tags = webhookService.findTags(siteId, id, null);
        assertEquals(2, tags.getResults().size());
        DynamicObject tag = tags.getResults().get(0);
        assertEquals(id, tag.getString("documentId"));
        assertEquals("USERDEFINED", tag.getString("type"));
        assertEquals("test@formkiq.com", tag.getString("userId"));
        assertEquals("category", tag.getString("tagKey"));
        assertEquals("person", tag.getString("tagValue"));
        assertNotNull(tag.getString("inserteddate"));
        tag = tags.getResults().get(1);
        assertEquals(id, tag.getString("documentId"));
        assertEquals("USERDEFINED", tag.getString("type"));
        assertEquals("test@formkiq.com", tag.getString("userId"));
        assertEquals("day", tag.getString("tagKey"));
        assertEquals("today", tag.getString("tagValue"));
        assertNotNull(tag.getString("inserteddate"));
    }
}
Also used : DynamicObject(com.formkiq.stacks.common.objects.DynamicObject) ApiGatewayRequestEvent(com.formkiq.lambda.apigateway.ApiGatewayRequestEvent) DynamicObject(com.formkiq.stacks.common.objects.DynamicObject) WebhooksService(com.formkiq.stacks.dynamodb.WebhooksService) Test(org.junit.jupiter.api.Test)

Aggregations

ApiGatewayRequestEvent (com.formkiq.lambda.apigateway.ApiGatewayRequestEvent)131 Test (org.junit.jupiter.api.Test)121 Date (java.util.Date)49 DynamicObject (com.formkiq.stacks.common.objects.DynamicObject)46 Map (java.util.Map)45 DocumentTag (com.formkiq.stacks.dynamodb.DocumentTag)34 DocumentItemDynamoDb (com.formkiq.stacks.dynamodb.DocumentItemDynamoDb)28 HashMap (java.util.HashMap)13 List (java.util.List)11 S3Client (software.amazon.awssdk.services.s3.S3Client)10 ZonedDateTime (java.time.ZonedDateTime)9 ApiMessageResponse (com.formkiq.lambda.apigateway.ApiMessageResponse)8 LambdaLogger (com.amazonaws.services.lambda.runtime.LambdaLogger)7 ApiAuthorizer (com.formkiq.lambda.apigateway.ApiAuthorizer)7 ApiGatewayRequestEventUtil (com.formkiq.lambda.apigateway.ApiGatewayRequestEventUtil)7 ApiGatewayRequestHandler (com.formkiq.lambda.apigateway.ApiGatewayRequestHandler)7 ApiRequestHandlerResponse (com.formkiq.lambda.apigateway.ApiRequestHandlerResponse)7 SC_OK (com.formkiq.lambda.apigateway.ApiResponseStatus.SC_OK)7 AwsServiceCache (com.formkiq.lambda.apigateway.AwsServiceCache)7 LocalDate (java.time.LocalDate)6