Search in sources :

Example 16 with DynamicObject

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

the class ApiDocumentsRequestTest method testHandlePostDocuments10.

/**
 * POST /documents with sub documents.
 *
 * @throws Exception an error has occurred
 */
@SuppressWarnings("unchecked")
@Test
public void testHandlePostDocuments10() throws Exception {
    for (String siteId : Arrays.asList(DEFAULT_SITE_ID, UUID.randomUUID().toString())) {
        // given
        String username = UUID.randomUUID() + "@formkiq.com";
        // when
        DynamicObject obj = handleRequest("/request-post-documents03.json", siteId, username, "Admins");
        // 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"));
        assertNotNull(body.getString("uploadUrl"));
        List<DynamicObject> documents = body.getList("documents");
        final int count = 3;
        assertEquals(count, documents.size());
        int i = 0;
        assertNull(documents.get(i).getString("uploadUrl"));
        assertNotNull(documents.get(i++).getString("documentId"));
        assertNotNull(documents.get(i).getString("uploadUrl"));
        assertNotNull(documents.get(i++).getString("documentId"));
        assertNotNull(documents.get(i).getString("uploadUrl"));
        assertNotNull(documents.get(i++).getString("documentId"));
        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, false, username);
    }
}
Also used : DynamicObject(com.formkiq.stacks.common.objects.DynamicObject) Map(java.util.Map) Test(org.junit.jupiter.api.Test)

Example 17 with DynamicObject

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

the class ApiDocumentsRequestTest method testHandleGetDocuments07.

/**
 * Get /documents request for previous date.
 *
 * @throws Exception an error has occurred
 */
@SuppressWarnings("unchecked")
@Test
public void testHandleGetDocuments07() throws Exception {
    for (String siteId : Arrays.asList(null, UUID.randomUUID().toString())) {
        // given
        Date date0 = new Date();
        final int year = 2015;
        final int dayOfMonth = 21;
        final long contentLength = 1111L;
        LocalDate localDate = LocalDate.of(year, Month.MARCH, dayOfMonth);
        Date date1 = Date.from(localDate.atStartOfDay(ZoneOffset.UTC).toInstant());
        String username = UUID.randomUUID() + "@formkiq.com";
        String documentId0 = UUID.randomUUID().toString();
        String documentId1 = UUID.randomUUID().toString();
        DocumentItemDynamoDb item0 = new DocumentItemDynamoDb(documentId0, date0, username);
        item0.setContentLength(Long.valueOf(contentLength));
        DocumentItemDynamoDb item1 = new DocumentItemDynamoDb(documentId1, date1, username);
        item1.setContentLength(Long.valueOf(contentLength));
        ApiGatewayRequestEvent event = toRequestEvent("/request-get-documents-yesterday.json");
        addParameter(event, "siteId", siteId);
        getDocumentService().saveDocument(siteId, item0, new ArrayList<>());
        getDocumentService().saveDocument(siteId, item1, new ArrayList<>());
        // when
        String response = handleRequest(event);
        // then
        final int mapsize = 3;
        Map<String, String> m = 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")));
        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("1111.0", documents.get(0).get("contentLength").toString());
    }
}
Also used : DynamicObject(com.formkiq.stacks.common.objects.DynamicObject) ApiGatewayRequestEvent(com.formkiq.lambda.apigateway.ApiGatewayRequestEvent) LocalDate(java.time.LocalDate) 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 18 with DynamicObject

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

the class ApiDocumentsRequestTest method testHandleGetDocuments11.

/**
 * Test user with 'Finance' role with no siteid and belongs to multiple groups.
 *
 * @throws Exception an error has occurred
 */
@SuppressWarnings("unchecked")
@Test
public void testHandleGetDocuments11() throws Exception {
    // given
    String siteId = null;
    ApiGatewayRequestEvent event = toRequestEvent("/request-get-documents03.json");
    addParameter(event, "siteId", siteId);
    setCognitoGroup(event, "Finance", "Bleh");
    // 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 19 with DynamicObject

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

the class ApiDocumentsRequestTest method testHandlePostDocuments04.

/**
 * POST /documents request Invalid JSON body.
 *
 * @throws Exception an error has occurred
 */
@SuppressWarnings("unchecked")
@Test
public void testHandlePostDocuments04() throws Exception {
    for (String siteId : Arrays.asList(null, UUID.randomUUID().toString())) {
        // given
        ApiGatewayRequestEvent event = toRequestEvent("/request-post-documents-documentid04.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("400.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));
        assertNull(resp.get("documentId"));
        assertNull(resp.get("next"));
        assertNull(resp.get("previous"));
    }
}
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 20 with DynamicObject

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

the class ApiDocumentsRequestTest method testHandleGetDocuments02.

/**
 * Get /documents request with next parameter.
 *
 * @throws Exception an error has occurred
 */
@SuppressWarnings("unchecked")
@Test
public void testHandleGetDocuments02() throws Exception {
    for (String siteId : Arrays.asList(null, UUID.randomUUID().toString())) {
        // given
        ByteArrayOutputStream outstream = new ByteArrayOutputStream();
        createTestData(siteId, DocumentService.MAX_RESULTS + 2);
        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"));
            assertNotNull(resp.get("next"));
            expectNextPage(resp.getString("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