Search in sources :

Example 26 with DynamicObject

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

the class ApiDocumentsRequestTest method expectNextPage.

/**
 * Expect Next Page of Data.
 *
 * @param next {@link String}
 * @throws IOException IOException
 */
@SuppressWarnings("unchecked")
private void expectNextPage(final String next) throws IOException {
    // given
    try (InputStream in = toStream("/request-get-documents-next.json")) {
        String input = IoUtils.toUtf8String(in).replaceAll("\\{\\{next\\}\\}", next);
        final InputStream instream2 = new ByteArrayInputStream(input.getBytes(StandardCharsets.UTF_8));
        ByteArrayOutputStream outstream = new ByteArrayOutputStream();
        // when
        getHandler().handleRequest(instream2, outstream, getMockContext());
        // then
        String response = new String(outstream.toByteArray(), "UTF-8");
        Map<String, String> m = fromJson(response, Map.class);
        DynamicObject resp = new DynamicObject(fromJson(m.get("body"), Map.class));
        assertNull(resp.get("next"));
        assertNotNull(resp.get("previous"));
        List<DynamicObject> documents = resp.getList("documents");
        assertEquals(2, documents.size());
        assertEquals("doc_10", documents.get(0).getString("documentId"));
        assertNotNull(documents.get(0).getString("siteId"));
        assertEquals("doc_11", documents.get(1).getString("documentId"));
        assertNotNull(documents.get(1).getString("siteId"));
    }
}
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)

Example 27 with DynamicObject

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

the class ApiDocumentsRequestTest method testHandleGetDocuments04.

/**
 * Get /documents request with date / tz. Date after
 *
 * @throws Exception an error has occurred
 */
@SuppressWarnings("unchecked")
@Test
public void testHandleGetDocuments04() 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-get-documents-tz.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(0, documents.size());
    }
}
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 28 with DynamicObject

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

the class ApiDocumentsSearchRequestTest method testHandleSearchRequest07.

/**
 * Valid POST search by eq/eqOr tagValue and valid/invalid DocumentId.
 *
 * @throws Exception an error has occurred
 */
@SuppressWarnings("unchecked")
@Test
public void testHandleSearchRequest07() throws Exception {
    for (String op : Arrays.asList("eq", "eqOr")) {
        for (String siteId : Arrays.asList(null, UUID.randomUUID().toString())) {
            // given
            final String documentId = UUID.randomUUID().toString();
            final String tagKey = "category";
            final String tagvalue = "person";
            final String username = "jsmith";
            final Date now = new Date();
            ApiGatewayRequestEvent event = toRequestEvent("/request-post-search01.json");
            addParameter(event, "siteId", siteId);
            event.setIsBase64Encoded(Boolean.FALSE);
            DocumentSearch s = new DocumentSearch().query(new DocumentSearchQuery().tag(new DocumentSearchTag().key("category").eq("person")).documentIds(Arrays.asList(documentId)));
            event.setBody(GsonUtil.getInstance().toJson(s));
            if ("eqOr".equals(op)) {
                s.query().tag().eq(null).eqOr(Arrays.asList("person"));
            }
            for (String v : Arrays.asList("", "!")) {
                DocumentTag item = new DocumentTag(documentId + v, tagKey, tagvalue + v, now, username);
                item.setUserId(UUID.randomUUID().toString());
                getDocumentService().saveDocument(siteId, new DocumentItemDynamoDb(documentId + v, now, username), Arrays.asList(item));
            }
            // when
            String response = handleRequest(event);
            // then
            Map<String, String> m = fromJson(response, Map.class);
            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("category", matchedTag.get("key"));
            assertEquals("person", matchedTag.get("value"));
            assertEquals("USERDEFINED", matchedTag.get("type"));
            assertNull(resp.get("next"));
            assertNull(resp.get("previous"));
            // given - invalid document id
            s.query().documentIds(Arrays.asList("123"));
            event.setBody(GsonUtil.getInstance().toJson(s));
            // when
            response = handleRequest(event);
            // then
            m = fromJson(response, Map.class);
            resp = new DynamicObject(fromJson(m.get("body"), Map.class));
            documents = resp.getList("documents");
            assertEquals(0, documents.size());
        }
    }
}
Also used : DocumentTag(com.formkiq.stacks.dynamodb.DocumentTag) DocumentSearchTag(com.formkiq.stacks.client.models.DocumentSearchTag) DocumentSearch(com.formkiq.stacks.client.models.DocumentSearch) Date(java.util.Date) DocumentSearchQuery(com.formkiq.stacks.client.models.DocumentSearchQuery) DynamicObject(com.formkiq.stacks.common.objects.DynamicObject) ApiGatewayRequestEvent(com.formkiq.lambda.apigateway.ApiGatewayRequestEvent) DynamicObject(com.formkiq.stacks.common.objects.DynamicObject) Map(java.util.Map) DocumentItemDynamoDb(com.formkiq.stacks.dynamodb.DocumentItemDynamoDb) Test(org.junit.jupiter.api.Test)

Example 29 with DynamicObject

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

the class ApiDocumentsSearchRequestTest method testHandleSearchRequest03.

/**
 * Valid POST search by eq tagValue.
 *
 * @throws Exception an error has occurred
 */
@SuppressWarnings("unchecked")
@Test
public void testHandleSearchRequest03() throws Exception {
    for (String op : Arrays.asList("eq", "eqOr")) {
        for (String siteId : Arrays.asList(null, UUID.randomUUID().toString())) {
            // given
            String documentId = UUID.randomUUID().toString();
            final String tagKey = "category";
            final String tagvalue = "person";
            final String username = "jsmith";
            final Date now = new Date();
            ApiGatewayRequestEvent event = toRequestEvent("/request-post-search01.json");
            addParameter(event, "siteId", siteId);
            DocumentSearch s = new DocumentSearch().query(new DocumentSearchQuery().tag(new DocumentSearchTag().key("category").eq("person")).documentIds(Arrays.asList(documentId)));
            event.setBody(GsonUtil.getInstance().toJson(s));
            event.setIsBase64Encoded(Boolean.FALSE);
            if ("eqOr".equals(op)) {
                s.query().tag().eq(null).eqOr(Arrays.asList("person"));
            }
            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));
            // 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("person", matchedTag.get("value"));
            assertNull(resp.get("next"));
            assertNull(resp.get("previous"));
        }
    }
}
Also used : DocumentTag(com.formkiq.stacks.dynamodb.DocumentTag) DocumentSearchTag(com.formkiq.stacks.client.models.DocumentSearchTag) DocumentSearch(com.formkiq.stacks.client.models.DocumentSearch) Date(java.util.Date) DocumentSearchQuery(com.formkiq.stacks.client.models.DocumentSearchQuery) DynamicObject(com.formkiq.stacks.common.objects.DynamicObject) ApiGatewayRequestEvent(com.formkiq.lambda.apigateway.ApiGatewayRequestEvent) DynamicObject(com.formkiq.stacks.common.objects.DynamicObject) Map(java.util.Map) DocumentItemDynamoDb(com.formkiq.stacks.dynamodb.DocumentItemDynamoDb) Test(org.junit.jupiter.api.Test)

Example 30 with DynamicObject

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

the class ApiDocumentsSearchRequestTest method testHandleSearchRequest06.

/**
 * Valid POST search no results.
 *
 * @throws Exception an error has occurred
 */
@SuppressWarnings("unchecked")
@Test
public void testHandleSearchRequest06() throws Exception {
    for (String siteId : Arrays.asList(null, UUID.randomUUID().toString())) {
        // given
        ApiGatewayRequestEvent event = toRequestEvent("/request-post-search01.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("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)

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