Search in sources :

Example 26 with DocumentItemDynamoDb

use of com.formkiq.stacks.dynamodb.DocumentItemDynamoDb in project formkiq-core by formkiq.

the class DocumentIdUrlGetRequestHandlerTest method testHandleGetDocumentContent04.

/**
 * /documents/{documentId}/url request.
 *
 * Tests No Content-Type, Content-Type matches DocumentItem's Content Type and Document Format
 * exists.
 *
 * @throws Exception an error has occurred
 */
@SuppressWarnings("unchecked")
@Test
public void testHandleGetDocumentContent04() throws Exception {
    for (String siteId : Arrays.asList(null, UUID.randomUUID().toString())) {
        // given
        String documentId = UUID.randomUUID().toString();
        ApiGatewayRequestEvent event = toRequestEvent("/request-get-documents-documentid-url01.json");
        addParameter(event, "siteId", siteId);
        setPathParameter(event, "documentId", documentId);
        addHeader(event, "Content-Type", "application/pdf");
        String userId = "jsmith";
        DocumentItemDynamoDb item = new DocumentItemDynamoDb(documentId, new Date(), userId);
        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("404.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) DocumentItemDynamoDb(com.formkiq.stacks.dynamodb.DocumentItemDynamoDb) Test(org.junit.jupiter.api.Test)

Example 27 with DocumentItemDynamoDb

use of com.formkiq.stacks.dynamodb.DocumentItemDynamoDb in project formkiq-core by formkiq.

the class ApiDocumentsRequestTest method testHandleGetDocuments05.

/**
 * Get /documents request with date / tz.
 *
 * @throws Exception an error has occurred
 */
@SuppressWarnings("unchecked")
@Test
public void testHandleGetDocuments05() throws Exception {
    for (String siteId : Arrays.asList(null, UUID.randomUUID().toString())) {
        // given
        Date date = DateUtil.toDateFromString("2019-08-15", "0500");
        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(1, 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 DocumentItemDynamoDb

use of com.formkiq.stacks.dynamodb.DocumentItemDynamoDb 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 29 with DocumentItemDynamoDb

use of com.formkiq.stacks.dynamodb.DocumentItemDynamoDb 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 30 with DocumentItemDynamoDb

use of com.formkiq.stacks.dynamodb.DocumentItemDynamoDb 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

DocumentItemDynamoDb (com.formkiq.stacks.dynamodb.DocumentItemDynamoDb)32 Date (java.util.Date)32 ApiGatewayRequestEvent (com.formkiq.lambda.apigateway.ApiGatewayRequestEvent)28 Test (org.junit.jupiter.api.Test)27 DocumentTag (com.formkiq.stacks.dynamodb.DocumentTag)13 DynamicObject (com.formkiq.stacks.common.objects.DynamicObject)12 Map (java.util.Map)12 LocalDate (java.time.LocalDate)7 DocumentItem (com.formkiq.stacks.dynamodb.DocumentItem)6 DynamicDocumentItem (com.formkiq.stacks.dynamodb.DynamicDocumentItem)4 ArrayList (java.util.ArrayList)4 DocumentSearchQuery (com.formkiq.stacks.client.models.DocumentSearchQuery)3 DocumentItemToDynamicDocumentItem (com.formkiq.stacks.dynamodb.DocumentItemToDynamicDocumentItem)3 ApiRequestHandlerResponse (com.formkiq.lambda.apigateway.ApiRequestHandlerResponse)2 BadException (com.formkiq.lambda.apigateway.exception.BadException)2 ApiUrlResponse (com.formkiq.stacks.api.ApiUrlResponse)2 DocumentSearch (com.formkiq.stacks.client.models.DocumentSearch)2 DocumentSearchTag (com.formkiq.stacks.client.models.DocumentSearchTag)2 DocumentService (com.formkiq.stacks.dynamodb.DocumentService)2 S3Client (software.amazon.awssdk.services.s3.S3Client)2