Search in sources :

Example 81 with DynamicObject

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

the class ApiDocumentsRequestTest method testHandlePostDocuments02.

/**
 * POST /documents request Base64 body.
 *
 * @throws Exception an error has occurred
 */
@SuppressWarnings("unchecked")
@Test
public void testHandlePostDocuments02() throws Exception {
    for (String siteId : Arrays.asList(null, UUID.randomUUID().toString())) {
        // given
        ApiGatewayRequestEvent event = toRequestEvent("/request-post-documents-documentid02.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("201.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));
        assertNotNull(resp.get("documentId"));
        assertNull(resp.get("next"));
        assertNull(resp.get("previous"));
        String key = siteId != null ? siteId + "/" + resp.get("documentId") + ".fkb64" : resp.get("documentId") + ".fkb64";
        assertTrue(getLogger().containsString("s3 putObject " + key + " into bucket " + STAGE_BUCKET_NAME));
        assertNotNull(UUID.fromString(resp.getString("documentId")));
        try (S3Client s3 = getS3().buildClient()) {
            assertTrue(getS3().getObjectMetadata(s3, STAGE_BUCKET_NAME, key).isObjectExists());
            String content = getS3().getContentAsString(s3, STAGE_BUCKET_NAME, key, null);
            DynamicObject obj = new DynamicObject(fromJson(content, Map.class));
            assertTrue(obj.hasString("documentId"));
            assertTrue(obj.hasString("content"));
            assertEquals("7b87c0e2-5f20-403b-bcc6-d35cb491d3b7@formkiq.com", obj.getString("userId"));
        }
    }
}
Also used : DynamicObject(com.formkiq.stacks.common.objects.DynamicObject) ApiGatewayRequestEvent(com.formkiq.lambda.apigateway.ApiGatewayRequestEvent) S3Client(software.amazon.awssdk.services.s3.S3Client) Map(java.util.Map) Test(org.junit.jupiter.api.Test)

Example 82 with DynamicObject

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

the class ApiDocumentsRequestTest method testHandlePostDocuments05.

/**
 * POST /documents request Invalid JSON body.
 *
 * @throws Exception an error has occurred
 */
@SuppressWarnings("unchecked")
@Test
public void testHandlePostDocuments05() throws Exception {
    for (String siteId : Arrays.asList(null, UUID.randomUUID().toString())) {
        // given
        ApiGatewayRequestEvent event = toRequestEvent("/request-post-documents-documentid05.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 83 with DynamicObject

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

the class ApiDocumentsRequestTest method testHandleGetDocuments06.

/**
 * Get /documents request with tz. Date after
 *
 * @throws Exception an error has occurred
 */
@SuppressWarnings("unchecked")
@Test
public void testHandleGetDocuments06() throws Exception {
    for (String siteId : Arrays.asList(null, UUID.randomUUID().toString())) {
        // given
        ZoneOffset zone = ZoneOffset.of("-05:00");
        ZonedDateTime zdate = ZonedDateTime.now(zone);
        Date date = Date.from(zdate.toInstant());
        String username = UUID.randomUUID() + "@formkiq.com";
        String documentId = UUID.randomUUID().toString();
        DocumentItemDynamoDb item = new DocumentItemDynamoDb(documentId, date, username);
        ApiGatewayRequestEvent event = toRequestEvent("/request-get-documents-tz02.json");
        addParameter(event, "siteId", siteId);
        getDocumentService().saveDocument(siteId, item, new ArrayList<>());
        Thread.sleep(ONE_SECOND);
        // 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());
    }
}
Also used : DynamicObject(com.formkiq.stacks.common.objects.DynamicObject) ZonedDateTime(java.time.ZonedDateTime) ApiGatewayRequestEvent(com.formkiq.lambda.apigateway.ApiGatewayRequestEvent) Map(java.util.Map) Date(java.util.Date) LocalDate(java.time.LocalDate) ZoneOffset(java.time.ZoneOffset) DocumentItemDynamoDb(com.formkiq.stacks.dynamodb.DocumentItemDynamoDb) Test(org.junit.jupiter.api.Test)

Example 84 with DynamicObject

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

the class ApiDocumentsSearchRequestTest method testHandleSearchRequest09.

/**
 * Valid POST search by eq tagValue with > 10 Document.
 *
 * @throws Exception an error has occurred
 */
@SuppressWarnings("unchecked")
@Test
public void testHandleSearchRequest09() throws Exception {
    for (String siteId : Arrays.asList(null, UUID.randomUUID().toString())) {
        // given
        final int count = 100;
        final String tagKey = "category";
        final String tagvalue = "person";
        final String username = "jsmith";
        List<String> documentIds = new ArrayList<>();
        for (int i = 0; i < count; i++) {
            String documentId = UUID.randomUUID().toString();
            Date now = new Date();
            documentIds.add(documentId);
            DocumentTag item = new DocumentTag(documentId, tagKey, tagvalue, now, username);
            item.setUserId(UUID.randomUUID().toString());
            getDocumentService().saveDocument(siteId, new DocumentItemDynamoDb(documentId, now, username), Arrays.asList(item));
        }
        ApiGatewayRequestEvent event = toRequestEvent("/request-post-search01.json");
        addParameter(event, "siteId", siteId);
        event.setIsBase64Encoded(Boolean.FALSE);
        QueryRequest q = new QueryRequest().query(new SearchQuery().tag(new SearchTagCriteria().key(tagKey).eq(tagvalue)).documentsIds(documentIds));
        event.setBody(GsonUtil.getInstance().toJson(q));
        // 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(count, documents.size());
        // given not search by documentIds should be limited to 10
        q = new QueryRequest().query(new SearchQuery().tag(new SearchTagCriteria().key(tagKey).eq(tagvalue)).documentsIds(null));
        event.setBody(GsonUtil.getInstance().toJson(q));
        // when
        response = handleRequest(event);
        // then
        final int ten = 10;
        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")));
        resp = new DynamicObject(fromJson(m.get("body"), Map.class));
        documents = resp.getList("documents");
        assertEquals(ten, documents.size());
    }
}
Also used : SearchQuery(com.formkiq.stacks.dynamodb.SearchQuery) DocumentSearchQuery(com.formkiq.stacks.client.models.DocumentSearchQuery) DocumentTag(com.formkiq.stacks.dynamodb.DocumentTag) ArrayList(java.util.ArrayList) SearchTagCriteria(com.formkiq.stacks.dynamodb.SearchTagCriteria) Date(java.util.Date) DynamicObject(com.formkiq.stacks.common.objects.DynamicObject) ApiGatewayRequestEvent(com.formkiq.lambda.apigateway.ApiGatewayRequestEvent) Map(java.util.Map) DocumentItemDynamoDb(com.formkiq.stacks.dynamodb.DocumentItemDynamoDb) Test(org.junit.jupiter.api.Test)

Example 85 with DynamicObject

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

the class ApiDocumentsSearchRequestTest method testHandleSearchRequest04.

/**
 * Valid POST search by eq tagValues.
 *
 * @throws Exception an error has occurred
 */
@SuppressWarnings("unchecked")
@Test
public void testHandleSearchRequest04() throws Exception {
    for (String siteId : Arrays.asList(null, UUID.randomUUID().toString())) {
        // given
        String documentId = UUID.randomUUID().toString();
        String tagKey = "category";
        String username = "jsmith";
        Date now = new Date();
        ApiGatewayRequestEvent event = toRequestEvent("/request-post-search01.json");
        addParameter(event, "siteId", siteId);
        event.setBody("ewogICJxdWVyeSI6IHsKICAgICJ0YWciOiB7CiAgICAgICJrZXkiOiAiY2F0Z" + "WdvcnkiLAogICAgICAiZXEiOiAieHl6IgogICAgfQogIH0KfQ==");
        DocumentTag item = new DocumentTag(documentId, tagKey, null, now, username);
        item.setValues(Arrays.asList("abc", "xyz"));
        item.setUserId(UUID.randomUUID().toString());
        getDocumentService().saveDocument(siteId, new DocumentItemDynamoDb(documentId, now, username), Arrays.asList(item));
        // 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());
        assertEquals(documentId, documents.get(0).get("documentId"));
        assertEquals(username, documents.get(0).get("userId"));
        assertNotNull(documents.get(0).get("insertedDate"));
        Map<String, Object> matchedTag = (Map<String, Object>) documents.get(0).get("matchedTag");
        assertEquals(MATCH_COUNT, matchedTag.size());
        assertEquals("USERDEFINED", matchedTag.get("type"));
        assertEquals("category", matchedTag.get("key"));
        assertEquals("xyz", matchedTag.get("value"));
        assertNull(resp.get("next"));
        assertNull(resp.get("previous"));
    }
}
Also used : DocumentTag(com.formkiq.stacks.dynamodb.DocumentTag) DynamicObject(com.formkiq.stacks.common.objects.DynamicObject) ApiGatewayRequestEvent(com.formkiq.lambda.apigateway.ApiGatewayRequestEvent) DynamicObject(com.formkiq.stacks.common.objects.DynamicObject) Map(java.util.Map) Date(java.util.Date) DocumentItemDynamoDb(com.formkiq.stacks.dynamodb.DocumentItemDynamoDb) 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