Search in sources :

Example 1 with WebhooksService

use of com.formkiq.stacks.dynamodb.WebhooksService 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 2 with WebhooksService

use of com.formkiq.stacks.dynamodb.WebhooksService 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 3 with WebhooksService

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

Example 4 with WebhooksService

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

the class WebhooksIdRequestHandler method patch.

@Override
public ApiRequestHandlerResponse patch(final LambdaLogger logger, final ApiGatewayRequestEvent event, final ApiAuthorizer authorizer, final AwsServiceCache awsServices) throws Exception {
    String siteId = authorizer.getSiteId();
    String id = getPathParameter(event, "webhookId");
    WebhooksService webhookService = awsServices.webhookService();
    if (webhookService.findWebhook(siteId, id) == null) {
        throw new NotFoundException("Webhook 'id' not found");
    }
    DynamicObject obj = fromBodyToDynamicObject(logger, event);
    Map<String, Object> map = new HashMap<>();
    if (obj.containsKey("name")) {
        map.put("name", obj.getString("name"));
    }
    if (obj.containsKey("enabled")) {
        map.put("enabled", obj.getBoolean("enabled"));
    }
    Date ttlDate = null;
    if (obj.containsKey("ttl")) {
        ZonedDateTime now = ZonedDateTime.now(ZoneOffset.UTC).plusSeconds(Long.parseLong(obj.getString("ttl")));
        ttlDate = Date.from(now.toInstant());
        map.put("TimeToLive", ttlDate);
    }
    webhookService.updateWebhook(siteId, id, new DynamicObject(map));
    if (ttlDate != null) {
        webhookService.updateTimeToLive(siteId, id, ttlDate);
    }
    return new ApiRequestHandlerResponse(SC_OK, new ApiMessageResponse("'" + id + "' object updated"));
}
Also used : ApiMessageResponse(com.formkiq.lambda.apigateway.ApiMessageResponse) DynamicObject(com.formkiq.stacks.common.objects.DynamicObject) HashMap(java.util.HashMap) ZonedDateTime(java.time.ZonedDateTime) NotFoundException(com.formkiq.lambda.apigateway.exception.NotFoundException) DynamicObject(com.formkiq.stacks.common.objects.DynamicObject) ApiRequestHandlerResponse(com.formkiq.lambda.apigateway.ApiRequestHandlerResponse) WebhooksService(com.formkiq.stacks.dynamodb.WebhooksService) Date(java.util.Date)

Example 5 with WebhooksService

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

the class ApiWebhooksRequestTest method testPostWebhooks03.

/**
 * POST /webhooks with NO SiteId Set.
 *
 * @throws Exception an error has occurred
 */
@SuppressWarnings("unchecked")
@Test
public void testPostWebhooks03() throws Exception {
    // given
    putSsmParameter("/formkiq/" + FORMKIQ_APP_ENVIRONMENT + "/api/DocumentsPublicHttpUrl", "http://localhost:8080");
    String siteId = UUID.randomUUID().toString();
    ApiGatewayRequestEvent event = toRequestEvent("/request-post-webhooks01.json");
    setCognitoGroup(event, siteId);
    event.setBody("{\"name\":\"joe smith\"}");
    // 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(siteId, 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"));
}
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

DynamicObject (com.formkiq.stacks.common.objects.DynamicObject)5 WebhooksService (com.formkiq.stacks.dynamodb.WebhooksService)5 ApiGatewayRequestEvent (com.formkiq.lambda.apigateway.ApiGatewayRequestEvent)4 Test (org.junit.jupiter.api.Test)4 ZonedDateTime (java.time.ZonedDateTime)3 Date (java.util.Date)2 Map (java.util.Map)2 ApiMessageResponse (com.formkiq.lambda.apigateway.ApiMessageResponse)1 ApiRequestHandlerResponse (com.formkiq.lambda.apigateway.ApiRequestHandlerResponse)1 NotFoundException (com.formkiq.lambda.apigateway.exception.NotFoundException)1 DocumentTag (com.formkiq.stacks.dynamodb.DocumentTag)1 HashMap (java.util.HashMap)1 List (java.util.List)1