Search in sources :

Example 21 with DynamicObject

use of com.formkiq.stacks.common.objects.DynamicObject in project formkiq-core by formkiq.

the class ApiDocumentsRequestTest method testHandlePostDocuments12.

/**
 * POST /documents gutenburg content-type with IAM Role.
 *
 * @throws Exception an error has occurred
 */
@SuppressWarnings("unchecked")
@Test
public void testHandlePostDocuments12() throws Exception {
    for (String siteId : Arrays.asList(DEFAULT_SITE_ID, UUID.randomUUID().toString())) {
        // given
        // when
        DynamicObject obj = handleRequest("/request-post-documents05.json", siteId, null, null);
        // then
        DynamicObject body = new DynamicObject(fromJson(obj.getString("body"), Map.class));
        assertHeaders(obj.getMap("headers"));
        assertEquals("201.0", obj.getString("statusCode"));
        assertNotNull(body.getString("documentId"));
        assertNull(body.getString("uploadUrl"));
        assertEquals("[]", body.getList("documents").toString());
        String key = getKey(siteId, body);
        String documentId = body.getString("documentId");
        assertTrue(getLogger().containsString("s3 putObject " + key + " into bucket " + STAGE_BUCKET_NAME));
        assertNotNull(documentId);
        DynamicObject verifyS3 = verifyS3(key, false, null);
        assertNotNull(verifyS3.getString("documentId"));
        assertNotNull(verifyS3.getString("userId"));
        assertNull(verifyS3.getString("uploadUrl"));
        assertEquals("text/html", verifyS3.getString("contentType"));
        try (S3Client s3 = getS3().buildClient()) {
            assertEquals("text/html", getS3().getObjectMetadata(s3, STAGE_BUCKET_NAME, key).getContentType());
        }
    }
}
Also used : DynamicObject(com.formkiq.stacks.common.objects.DynamicObject) S3Client(software.amazon.awssdk.services.s3.S3Client) Map(java.util.Map) Test(org.junit.jupiter.api.Test)

Example 22 with DynamicObject

use of com.formkiq.stacks.common.objects.DynamicObject 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 23 with DynamicObject

use of com.formkiq.stacks.common.objects.DynamicObject in project formkiq-core by formkiq.

the class ApiDocumentsRequestTest method testHandlePostDocuments01.

/**
 * POST /documents request non Base64 body.
 *
 * @throws Exception an error has occurred
 */
@SuppressWarnings("unchecked")
@Test
public void testHandlePostDocuments01() throws Exception {
    for (String siteId : Arrays.asList(DEFAULT_SITE_ID, UUID.randomUUID().toString())) {
        // given
        // when
        DynamicObject obj = handleRequest("/request-post-documents-documentid01.json", siteId, null, null);
        // then
        DynamicObject body = new DynamicObject(fromJson(obj.getString("body"), Map.class));
        assertHeaders(obj.getMap("headers"));
        assertEquals("201.0", obj.getString("statusCode"));
        assertNotNull(body.getString("documentId"));
        assertNull(body.getString("next"));
        assertNull(body.getString("previous"));
        String key = getKey(siteId, body);
        String documentId = body.getString("documentId");
        assertTrue(getLogger().containsString("s3 putObject " + key + " into bucket " + STAGE_BUCKET_NAME));
        assertNotNull(documentId);
        verifyS3(key, true, "a0dac80d-18b3-472b-88da-79e75082b662@formkiq.com");
    }
}
Also used : DynamicObject(com.formkiq.stacks.common.objects.DynamicObject) Map(java.util.Map) Test(org.junit.jupiter.api.Test)

Example 24 with DynamicObject

use of com.formkiq.stacks.common.objects.DynamicObject in project formkiq-core by formkiq.

the class ApiDocumentsRequestTest method testHandleGetDocuments01.

/**
 * Get /documents request.
 *
 * @throws Exception an error has occurred
 */
@SuppressWarnings("unchecked")
@Test
public void testHandleGetDocuments01() throws Exception {
    for (String siteId : Arrays.asList(null, UUID.randomUUID().toString())) {
        // given
        Date date = new Date();
        final long contentLength = 1000L;
        String username = UUID.randomUUID() + "@formkiq.com";
        String documentId = UUID.randomUUID().toString();
        DocumentItemDynamoDb item = new DocumentItemDynamoDb(documentId, date, username);
        item.setContentLength(Long.valueOf(contentLength));
        ApiGatewayRequestEvent event = toRequestEvent("/request-get-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")));
        DynamicObject resp = new DynamicObject(fromJson(m.get("body"), Map.class));
        List<DynamicObject> documents = resp.getList("documents");
        assertEquals(1, documents.size());
        assertNotNull(documents.get(0).get("documentId"));
        assertNotNull(documents.get(0).get("insertedDate"));
        assertNotNull(documents.get(0).get("userId"));
        assertEquals("1000.0", documents.get(0).get("contentLength").toString());
    }
}
Also used : DynamicObject(com.formkiq.stacks.common.objects.DynamicObject) ApiGatewayRequestEvent(com.formkiq.lambda.apigateway.ApiGatewayRequestEvent) Map(java.util.Map) Date(java.util.Date) LocalDate(java.time.LocalDate) DocumentItemDynamoDb(com.formkiq.stacks.dynamodb.DocumentItemDynamoDb) Test(org.junit.jupiter.api.Test)

Example 25 with DynamicObject

use of com.formkiq.stacks.common.objects.DynamicObject in project formkiq-core by formkiq.

the class ApiDocumentsRequestTest method testHandleGetDocuments03.

/**
 * Get /documents request with Limit=MAX_RESULTS.
 *
 * @throws Exception an error has occurred
 */
@SuppressWarnings("unchecked")
@Test
public void testHandleGetDocuments03() throws Exception {
    for (String siteId : Arrays.asList(null, UUID.randomUUID().toString())) {
        // given
        ByteArrayOutputStream outstream = new ByteArrayOutputStream();
        createTestData(siteId, DocumentService.MAX_RESULTS);
        try (InputStream in = toStream("/request-get-documents-next.json")) {
            String input = IoUtils.toUtf8String(in);
            final InputStream instream = new ByteArrayInputStream(input.getBytes(StandardCharsets.UTF_8));
            // when
            getHandler().handleRequest(instream, outstream, getMockContext());
            // then
            String response = new String(outstream.toByteArray(), "UTF-8");
            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(DocumentService.MAX_RESULTS, documents.size());
            assertNull(resp.get("previous"));
            assertNull(resp.get("next"));
        }
    }
}
Also used : DynamicObject(com.formkiq.stacks.common.objects.DynamicObject) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Map(java.util.Map) Test(org.junit.jupiter.api.Test)

Aggregations

DynamicObject (com.formkiq.stacks.common.objects.DynamicObject)93 Test (org.junit.jupiter.api.Test)55 Map (java.util.Map)46 ApiGatewayRequestEvent (com.formkiq.lambda.apigateway.ApiGatewayRequestEvent)39 Date (java.util.Date)26 HashMap (java.util.HashMap)17 DocumentItemDynamoDb (com.formkiq.stacks.dynamodb.DocumentItemDynamoDb)12 DocumentTag (com.formkiq.stacks.dynamodb.DocumentTag)10 ZonedDateTime (java.time.ZonedDateTime)10 ApiRequestHandlerResponse (com.formkiq.lambda.apigateway.ApiRequestHandlerResponse)8 List (java.util.List)8 AttributeValue (software.amazon.awssdk.services.dynamodb.model.AttributeValue)7 S3Client (software.amazon.awssdk.services.s3.S3Client)7 ApiMapResponse (com.formkiq.lambda.apigateway.ApiMapResponse)6 BadException (com.formkiq.lambda.apigateway.exception.BadException)6 Collectors (java.util.stream.Collectors)6 NotFoundException (com.formkiq.lambda.apigateway.exception.NotFoundException)5 FormKiqClientV1 (com.formkiq.stacks.client.FormKiqClientV1)5 LocalDate (java.time.LocalDate)5 Test (org.junit.Test)5