Search in sources :

Example 26 with ApiGatewayRequestEvent

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

the class ApiRequestHandlerTest method testHandleGetRequest04.

/**
 * unknown resource.
 *
 * @throws Exception an error has occurred
 */
@Test
public void testHandleGetRequest04() throws Exception {
    for (String siteId : Arrays.asList(null, UUID.randomUUID().toString())) {
        // given
        ApiGatewayRequestEvent event = toRequestEvent("/request-invalid-resource.json");
        addParameter(event, "siteId", siteId);
        String expected = "{" + getHeaders() + ",\"body\":\"{\\\"message\\\":\\\"/unknown not found\\\"}\"," + "\"statusCode\":404}";
        // when
        String response = handleRequest(event);
        // then
        assertEquals(expected, response);
        assertTrue(getLogger().containsString("response: " + expected));
    }
}
Also used : ApiGatewayRequestEvent(com.formkiq.lambda.apigateway.ApiGatewayRequestEvent) Test(org.junit.jupiter.api.Test)

Example 27 with ApiGatewayRequestEvent

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

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

the class ApiRequestHandlerTest method testHandleGetRequest01.

/**
 * Get Document Request, Document not found.
 *
 * @throws Exception an error has occurred
 */
@Test
public void testHandleGetRequest01() throws Exception {
    for (String siteId : Arrays.asList(null, UUID.randomUUID().toString())) {
        // given
        ApiGatewayRequestEvent event = toRequestEvent("/request-get-documents-documentid01.json");
        addParameter(event, "siteId", siteId);
        String expected = "{" + getHeaders() + ",\"body\":\"{\\\"message\\\":\\\"Document 142 not found.\\\"}\"," + "\"statusCode\":404}";
        // when
        String response = handleRequest(event);
        // then
        assertEquals(expected, response);
        assertTrue(getLogger().containsString("response: " + expected));
    }
}
Also used : ApiGatewayRequestEvent(com.formkiq.lambda.apigateway.ApiGatewayRequestEvent) Test(org.junit.jupiter.api.Test)

Example 29 with ApiGatewayRequestEvent

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

the class ApiWebhookIdRequestTest method testPatchWebhook01.

/**
 * PATCH /webhooks/{webhookId}.
 *
 * @throws Exception an error has occurred
 */
@SuppressWarnings("unchecked")
@Test
public void testPatchWebhook01() throws Exception {
    Date date = new Date();
    putSsmParameter("/formkiq/" + FORMKIQ_APP_ENVIRONMENT + "/api/DocumentsPublicHttpUrl", "http://localhost:8080");
    for (String siteId : Arrays.asList(null, UUID.randomUUID().toString())) {
        // given
        String id = getAwsServices().webhookService().saveWebhook(siteId, "test", "joe", date, "true");
        ApiGatewayRequestEvent event = toRequestEvent("/request-patch-webhooks-webhookid01.json");
        setPathParameter(event, "webhookId", id);
        addParameter(event, "siteId", siteId);
        event.setBody("{\"name\":\"john smith2\",\"enabled\":false}");
        // when
        String response = handleRequest(event);
        // then
        Map<String, Object> 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("{\"message\":\"'" + id + "' object updated\"}", m.get("body"));
        // given
        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);
        assertEquals(mapsize, m.size());
        assertEquals("200.0", String.valueOf(m.get("statusCode")));
        assertEquals(getHeaders(), "\"headers\":" + GsonUtil.getInstance().toJson(m.get("headers")));
        Map<String, Object> result = GsonUtil.getInstance().fromJson(m.get("body").toString(), 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 smith2", result.get("name"));
        assertEquals("joe", result.get("userId"));
        assertEquals("false", result.get("enabled"));
        verifyUrl(siteId, id, result, true);
    }
}
Also used : ApiGatewayRequestEvent(com.formkiq.lambda.apigateway.ApiGatewayRequestEvent) Map(java.util.Map) Date(java.util.Date) Test(org.junit.jupiter.api.Test)

Example 30 with ApiGatewayRequestEvent

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

the class ApiWebhookIdRequestTest method testDeleteWebhooks03.

/**
 * Delete /webhooks/{webhookId} and webhook tags.
 *
 * @throws Exception an error has occurred
 */
@SuppressWarnings("unchecked")
@Test
public void testDeleteWebhooks03() throws Exception {
    // given
    ApiGatewayRequestEvent req = toRequestEvent("/request-post-webhooks01.json");
    req.setBody("{\"name\":\"john smith\",tags:[{key:\"dynamodb\"}]}");
    String response = handleRequest(req);
    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"));
    String id = result.get("id").toString();
    ApiGatewayRequestEvent event = toRequestEvent("/request-delete-webhooks-webhookid01.json");
    setPathParameter(event, "webhookId", id);
    // 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")));
    event = toRequestEvent("/request-get-webhooks-webhookid-tags01.json");
    setPathParameter(event, "webhookId", id);
    // when
    response = handleRequest(event);
    // then
    m = GsonUtil.getInstance().fromJson(response, Map.class);
    result = GsonUtil.getInstance().fromJson(m.get("body"), Map.class);
    assertEquals(0, ((List<Object>) result.get("tags")).size());
}
Also used : ApiGatewayRequestEvent(com.formkiq.lambda.apigateway.ApiGatewayRequestEvent) Map(java.util.Map) 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