Search in sources :

Example 36 with ApiGatewayRequestEvent

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

the class ApiWebhooksRequestTest method testGetWebhooks01.

/**
 * Get /webhooks empty.
 *
 * @throws Exception an error has occurred
 */
@SuppressWarnings("unchecked")
@Test
public void testGetWebhooks01() throws Exception {
    // given
    ApiGatewayRequestEvent event = toRequestEvent("/request-get-webhooks01.json");
    addParameter(event, "siteId", UUID.randomUUID().toString());
    // 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("200.0", String.valueOf(m.get("statusCode")));
    assertEquals(getHeaders(), "\"headers\":" + GsonUtil.getInstance().toJson(m.get("headers")));
    assertEquals("{\"webhooks\":[]}", m.get("body"));
}
Also used : ApiGatewayRequestEvent(com.formkiq.lambda.apigateway.ApiGatewayRequestEvent) Test(org.junit.jupiter.api.Test)

Example 37 with ApiGatewayRequestEvent

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

the class ApiWebhooksRequestTest method testGetWebhooks02.

/**
 * POST/Get /webhooks.
 *
 * @throws Exception an error has occurred
 */
@SuppressWarnings("unchecked")
@Test
public void testGetWebhooks02() 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);
        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);
        if (siteId == null) {
            assertEquals("default", result.get("siteId"));
        } else {
            assertNotNull(result.get("siteId"));
            assertNotEquals("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("true", result.get("enabled"));
        verifyUrl(siteId, id, result);
        event = toRequestEvent("/request-get-webhooks01.json");
        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);
        List<Map<String, Object>> list = (List<Map<String, Object>>) result.get("webhooks");
        Optional<Map<String, Object>> o = list.stream().filter(l -> l.get("url").toString().contains(id)).findFirst();
        assertTrue(o.isPresent());
        final int expectedCount = 7;
        assertEquals(expectedCount, o.get().size());
        assertNotNull(o.get().get("insertedDate"));
        assertNotNull(o.get().get("id"));
        assertEquals("john smith", o.get().get("name"));
        assertEquals("true", o.get().get("enabled"));
        verifyUrl(siteId, id, o.get());
        assertEquals("test@formkiq.com", o.get().get("userId"));
    }
}
Also used : Arrays(java.util.Arrays) Date(java.util.Date) ZonedDateTime(java.time.ZonedDateTime) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Map(java.util.Map) DocumentTag(com.formkiq.stacks.dynamodb.DocumentTag) ZoneOffset(java.time.ZoneOffset) DynamicObject(com.formkiq.stacks.common.objects.DynamicObject) WEBHOOK_TIME_TO_LIVE(com.formkiq.stacks.dynamodb.ConfigService.WEBHOOK_TIME_TO_LIVE) Assert.assertNotNull(org.junit.Assert.assertNotNull) Assert.assertTrue(org.junit.Assert.assertTrue) FORMKIQ_APP_ENVIRONMENT(com.formkiq.stacks.api.TestServices.FORMKIQ_APP_ENVIRONMENT) UUID(java.util.UUID) Instant(java.time.Instant) Assert.assertNotEquals(org.junit.Assert.assertNotEquals) Test(org.junit.jupiter.api.Test) MAX_WEBHOOKS(com.formkiq.stacks.dynamodb.ConfigService.MAX_WEBHOOKS) List(java.util.List) Assert.assertNull(org.junit.Assert.assertNull) GsonUtil(com.formkiq.lambda.apigateway.util.GsonUtil) WebhooksService(com.formkiq.stacks.dynamodb.WebhooksService) ApiGatewayRequestEvent(com.formkiq.lambda.apigateway.ApiGatewayRequestEvent) Optional(java.util.Optional) Assert.assertEquals(org.junit.Assert.assertEquals) PaginationResults(com.formkiq.stacks.dynamodb.PaginationResults) ApiGatewayRequestEvent(com.formkiq.lambda.apigateway.ApiGatewayRequestEvent) DynamicObject(com.formkiq.stacks.common.objects.DynamicObject) List(java.util.List) Map(java.util.Map) Test(org.junit.jupiter.api.Test)

Example 38 with ApiGatewayRequestEvent

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

the class ApiWebhooksTagsRequestTest method testGetWebhooks02.

/**
 * Get /webhooks/{webhookId}/tags empty.
 *
 * @throws Exception an error has occurred
 */
@SuppressWarnings("unchecked")
@Test
public void testGetWebhooks02() throws Exception {
    // given
    String webhookId = getAwsServices().webhookService().saveWebhook(null, "testwebhook", "joe", null, "true");
    ApiGatewayRequestEvent event = toRequestEvent("/request-post-webhooks-webhookid-tags01.json");
    setPathParameter(event, "webhookId", webhookId);
    // 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("201.0", String.valueOf(m.get("statusCode")));
    assertEquals(getHeaders(), "\"headers\":" + GsonUtil.getInstance().toJson(m.get("headers")));
    assertEquals("{\"message\":\"Created Tag 'category'.\"}", m.get("body"));
    // given
    event = toRequestEvent("/request-get-webhooks-webhookid-tags01.json");
    setPathParameter(event, "webhookId", webhookId);
    // when
    response = handleRequest(event);
    // then
    m = GsonUtil.getInstance().fromJson(response, Map.class);
    assertEquals(mapsize, m.size());
    assertEquals("200.0", String.valueOf(m.get("statusCode")));
    assertEquals(getHeaders(), "\"headers\":" + GsonUtil.getInstance().toJson(m.get("headers")));
    Map<String, Object> body = fromJson(m.get("body"), Map.class);
    DynamicObject obj = new DynamicObject(body);
    List<DynamicObject> list = obj.getList("tags");
    assertEquals(1, list.size());
    assertEquals(webhookId, list.get(0).getString("webhookId"));
    assertEquals("USERDEFINED", list.get(0).getString("type"));
    assertEquals("8a73dfef-26d3-43d8-87aa-b3ec358e43ba@formkiq.com", list.get(0).getString("userId"));
    assertEquals("category", list.get(0).getString("key"));
    assertEquals("job", list.get(0).getString("value"));
    assertNotNull(list.get(0).getString("insertedDate"));
}
Also used : DynamicObject(com.formkiq.stacks.common.objects.DynamicObject) ApiGatewayRequestEvent(com.formkiq.lambda.apigateway.ApiGatewayRequestEvent) DynamicObject(com.formkiq.stacks.common.objects.DynamicObject) Map(java.util.Map) Test(org.junit.jupiter.api.Test)

Example 39 with ApiGatewayRequestEvent

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

the class DocumentIdContentGetRequestHandlerTest method testHandleGetDocumentContent01.

/**
 * /documents/{documentId}/content request.
 *
 * Tests S3 URL is returned.
 *
 * @throws Exception an error has occurred
 */
@SuppressWarnings("unchecked")
@Test
public void testHandleGetDocumentContent01() throws Exception {
    for (String siteId : Arrays.asList(null, UUID.randomUUID().toString())) {
        // given
        String documentId = UUID.randomUUID().toString();
        String userId = "jsmith";
        ApiGatewayRequestEvent event = toRequestEvent("/request-get-documents-documentid-content01.json");
        addParameter(event, "siteId", siteId);
        setPathParameter(event, "documentId", documentId);
        DocumentItemDynamoDb item = new DocumentItemDynamoDb(documentId, new Date(), userId);
        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);
        String url = body.get("contentUrl").toString();
        assertTrue(url.contains("X-Amz-Algorithm=AWS4-HMAC-SHA256"));
        assertTrue(url.contains("X-Amz-Expires="));
        assertTrue(url.contains(AWS_REGION.toString()));
        assertEquals("application/octet-stream", body.get("contentType"));
        if (siteId != null) {
            assertTrue(url.startsWith(this.localstack.getEndpointOverride(Service.S3).toString() + "/testbucket/" + siteId + "/" + documentId));
        } else {
            assertTrue(url.startsWith(this.localstack.getEndpointOverride(Service.S3).toString() + "/testbucket/" + documentId));
        }
    }
}
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 40 with ApiGatewayRequestEvent

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

the class DocumentIdUrlGetRequestHandlerTest method testHandleGetDocumentContent03.

/**
 * /documents/{documentId}/url request, document not found.
 *
 * @throws Exception an error has occurred
 */
@SuppressWarnings("unchecked")
@Test
public void testHandleGetDocumentContent03() throws Exception {
    for (String siteId : Arrays.asList(null, UUID.randomUUID().toString())) {
        // given
        ApiGatewayRequestEvent event = toRequestEvent("/request-get-documents-documentid-url01.json");
        addParameter(event, "siteId", siteId);
        // when
        String response = handleRequest(event);
        // then
        Map<String, String> m = fromJson(response, Map.class);
        final int mapsize = 3;
        assertEquals(mapsize, m.size());
        assertEquals("404.0", String.valueOf(m.get("statusCode")));
        assertEquals(getHeaders(), "\"headers\":" + GsonUtil.getInstance().toJson(m.get("headers")));
        ApiResponseError resp = fromJson(m.get("body"), ApiResponseError.class);
        assertEquals("Document 39da0052-3cb3-47a6-94c7-7b97cd43f1ee not found.", resp.getMessage());
        assertNull(resp.getNext());
        assertNull(resp.getPrevious());
    }
}
Also used : ApiGatewayRequestEvent(com.formkiq.lambda.apigateway.ApiGatewayRequestEvent) ApiResponseError(com.formkiq.lambda.apigateway.ApiResponseError) 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