Search in sources :

Example 51 with ApiGatewayRequestEvent

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

the class ApiDocumentsRequestTest method testHandleDeleteDocument02.

/**
 * DELETE /documents request.
 *
 * @throws Exception an error has occurred
 */
@Test
public void testHandleDeleteDocument02() throws Exception {
    for (String siteId : Arrays.asList(null, UUID.randomUUID().toString())) {
        // given
        String filename = "test.txt";
        try (S3Client s3 = getS3().buildClient()) {
            getS3().putObject(s3, BUCKET_NAME, filename, "testdata".getBytes(StandardCharsets.UTF_8), null);
        }
        ApiGatewayRequestEvent event = toRequestEvent("/request-delete-documents-documentid02.json");
        addParameter(event, "siteId", siteId);
        expectDeleteDocument(event, filename);
    }
}
Also used : ApiGatewayRequestEvent(com.formkiq.lambda.apigateway.ApiGatewayRequestEvent) S3Client(software.amazon.awssdk.services.s3.S3Client) Test(org.junit.jupiter.api.Test)

Example 52 with ApiGatewayRequestEvent

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

the class ApiDocumentsRequestTest method testHandleOptionsDocuments01.

/**
 * Options /documents request.
 *
 * @throws Exception an error has occurred
 */
@SuppressWarnings("unchecked")
@Test
public void testHandleOptionsDocuments01() throws Exception {
    for (String siteId : Arrays.asList(null, UUID.randomUUID().toString())) {
        // given
        Date date = new Date();
        String username = UUID.randomUUID() + "@formkiq.com";
        String documentId = UUID.randomUUID().toString();
        DocumentItemDynamoDb item = new DocumentItemDynamoDb(documentId, date, username);
        ApiGatewayRequestEvent event = toRequestEvent("/request-options-documents.json");
        addParameter(event, "siteId", siteId);
        getDocumentService().saveDocument(siteId, item, new ArrayList<>());
        // 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")));
    }
}
Also used : ApiGatewayRequestEvent(com.formkiq.lambda.apigateway.ApiGatewayRequestEvent) Date(java.util.Date) LocalDate(java.time.LocalDate) DocumentItemDynamoDb(com.formkiq.stacks.dynamodb.DocumentItemDynamoDb) Test(org.junit.jupiter.api.Test)

Example 53 with ApiGatewayRequestEvent

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

the class ApiDocumentsRequestTest method testHandleGetDocuments12.

/**
 * Test user with 'Finance' role with siteid.
 *
 * @throws Exception an error has occurred
 */
@SuppressWarnings("unchecked")
@Test
public void testHandleGetDocuments12() throws Exception {
    // given
    String siteId = "Finance";
    ApiGatewayRequestEvent event = toRequestEvent("/request-get-documents03.json");
    addParameter(event, "siteId", siteId);
    setCognitoGroup(event, siteId);
    // 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));
    List<DynamicObject> documents = resp.getList("documents");
    assertEquals(0, documents.size());
}
Also used : DynamicObject(com.formkiq.stacks.common.objects.DynamicObject) ApiGatewayRequestEvent(com.formkiq.lambda.apigateway.ApiGatewayRequestEvent) Map(java.util.Map) Test(org.junit.jupiter.api.Test)

Example 54 with ApiGatewayRequestEvent

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

the class ApiDocumentsRequestTest method testHandleDeleteDocument03.

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

Example 55 with ApiGatewayRequestEvent

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

the class ApiDocumentsRequestTest method testHandleGetDocuments09.

/**
 * Test user with User role with siteid.
 *
 * @throws Exception an error has occurred
 */
@SuppressWarnings("unchecked")
@Test
public void testHandleGetDocuments09() throws Exception {
    // given
    String siteId = UUID.randomUUID().toString();
    ApiGatewayRequestEvent event = toRequestEvent("/request-get-documents03.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("403.0", String.valueOf(m.get("statusCode")));
    assertEquals(getHeaders(), "\"headers\":" + GsonUtil.getInstance().toJson(m.get("headers")));
    ApiResponseError resp = fromJson(m.get("body"), ApiResponseError.class);
    assertEquals("Access Denied", resp.getMessage());
}
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