Search in sources :

Example 6 with DocumentItemDynamoDb

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

the class DocumentIdContentGetRequestHandlerTest method testHandleGetDocumentContent01.

/**
 * /documents/{documentId}/content request.
 *
 * Tests S3 URL is returned.
 *
 * @throws Exception an error has occurred
 */
@SuppressWarnings("unchecked")
@Test
public void testHandleGetDocumentContent01() throws Exception {
    for (String siteId : Arrays.asList(null, UUID.randomUUID().toString())) {
        // given
        String documentId = UUID.randomUUID().toString();
        String userId = "jsmith";
        ApiGatewayRequestEvent event = toRequestEvent("/request-get-documents-documentid-content01.json");
        addParameter(event, "siteId", siteId);
        setPathParameter(event, "documentId", documentId);
        DocumentItemDynamoDb item = new DocumentItemDynamoDb(documentId, new Date(), userId);
        getDocumentService().saveDocument(siteId, item, new ArrayList<>());
        // when
        String response = handleRequest(event);
        // then
        Map<String, Object> m = fromJson(response, Map.class);
        final int mapsize = 3;
        assertEquals(mapsize, m.size());
        assertEquals("200.0", String.valueOf(m.get("statusCode")));
        Map<String, Object> body = fromJson(m.get("body").toString(), Map.class);
        String url = body.get("contentUrl").toString();
        assertTrue(url.contains("X-Amz-Algorithm=AWS4-HMAC-SHA256"));
        assertTrue(url.contains("X-Amz-Expires="));
        assertTrue(url.contains(AWS_REGION.toString()));
        assertEquals("application/octet-stream", body.get("contentType"));
        if (siteId != null) {
            assertTrue(url.startsWith(this.localstack.getEndpointOverride(Service.S3).toString() + "/testbucket/" + siteId + "/" + documentId));
        } else {
            assertTrue(url.startsWith(this.localstack.getEndpointOverride(Service.S3).toString() + "/testbucket/" + documentId));
        }
    }
}
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 7 with DocumentItemDynamoDb

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

the class DocumentIdUrlGetRequestHandlerTest method testHandleGetDocumentContent02.

/**
 * /documents/{documentId}/url request w/ duration.
 *
 * @throws Exception an error has occurred
 */
@SuppressWarnings("unchecked")
@Test
public void testHandleGetDocumentContent02() throws Exception {
    for (String siteId : Arrays.asList(null, UUID.randomUUID().toString())) {
        // given
        String documentId = UUID.randomUUID().toString();
        ApiGatewayRequestEvent event = toRequestEvent("/request-get-documents-documentid-url02.json");
        addParameter(event, "siteId", siteId);
        setPathParameter(event, "documentId", documentId);
        String userId = "jsmith";
        getDocumentService().saveDocument(siteId, new DocumentItemDynamoDb(documentId, new Date(), userId), 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")));
        ApiUrlResponse resp = fromJson(m.get("body"), ApiUrlResponse.class);
        assertTrue(resp.getUrl().contains("X-Amz-Algorithm=AWS4-HMAC-SHA256"));
        assertTrue(resp.getUrl().contains("X-Amz-Expires=28800"));
        assertTrue(resp.getUrl().contains(AWS_REGION.toString()));
        assertNull(resp.getNext());
        assertNull(resp.getPrevious());
        if (siteId != null) {
            assertTrue(resp.getUrl().startsWith(this.localstack.getEndpointOverride(Service.S3).toString() + "/testbucket/" + siteId + "/" + documentId));
        } else {
            assertTrue(resp.getUrl().startsWith(this.localstack.getEndpointOverride(Service.S3).toString() + "/testbucket/" + documentId));
        }
    }
}
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 8 with DocumentItemDynamoDb

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

the class ApiDocumentsPatchRequestTest method testHandlePatchDocuments03.

/**
 * POST /documents request non Base64 body.
 *
 * @throws Exception an error has occurred
 */
@Test
public void testHandlePatchDocuments03() throws Exception {
    for (String siteId : Arrays.asList(null, UUID.randomUUID().toString())) {
        // given
        String documentId = UUID.randomUUID().toString();
        String userId = "jsmith";
        getDocumentService().saveDocument(siteId, new DocumentItemDynamoDb(documentId, new Date(), userId), new ArrayList<>());
        ApiGatewayRequestEvent event = toRequestEvent("/request-patch-documents-documentid02.json");
        addParameter(event, "siteId", siteId);
        setPathParameter(event, "documentId", documentId);
        // when
        String response = handleRequest(event);
        // then
        assert200Response(siteId, response);
    }
}
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 9 with DocumentItemDynamoDb

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

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

the class ApiDocumentsRequestTest method testHandleOptionsDocuments01.

/**
 * Options /documents request.
 *
 * @throws Exception an error has occurred
 */
@SuppressWarnings("unchecked")
@Test
public void testHandleOptionsDocuments01() 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-options-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")));
    }
}
Also used : ApiGatewayRequestEvent(com.formkiq.lambda.apigateway.ApiGatewayRequestEvent) Date(java.util.Date) LocalDate(java.time.LocalDate) 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