use of com.formkiq.stacks.dynamodb.DocumentItemDynamoDb in project formkiq-core by formkiq.
the class ApiRequestHandlerTest method testHandleGetRequest05.
/**
* Get Document Request, Document found.
*
* @throws Exception an error has occurred
*/
@SuppressWarnings("unchecked")
@Test
public void testHandleGetRequest05() throws Exception {
// given
Date date = new Date();
String documentId = "1a1d1938-451e-4e20-bf95-e0e7a749505a";
String userId = "jsmith";
DocumentItem item = new DocumentItemDynamoDb(documentId, date, userId);
getDocumentService().saveDocument(null, item, new ArrayList<>());
ApiGatewayRequestEvent event = toRequestEvent("/request-get-documents-documentid02.json");
// 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));
assertEquals(documentId, resp.get("documentId"));
assertEquals(userId, resp.get("userId"));
assertNotNull(documentId, resp.get("insertedDate"));
assertEquals(DEFAULT_SITE_ID, resp.get("siteId"));
assertNull(resp.get("next"));
assertNull(resp.get("previous"));
}
use of com.formkiq.stacks.dynamodb.DocumentItemDynamoDb in project formkiq-core by formkiq.
the class ApiRequestHandlerTest method testHandleGetRequest06.
/**
* Get Document Request, Document with sub docs found.
*
* @throws Exception an error has occurred
*/
@SuppressWarnings("unchecked")
@Test
public void testHandleGetRequest06() throws Exception {
// given
Date date = new Date();
String documentId0 = "1a1d1938-451e-4e20-bf95-e0e7a749505a";
String documentId1 = UUID.randomUUID().toString();
String userId = "jsmith";
DocumentItem item = new DocumentItemDynamoDb(documentId0, date, userId);
DocumentItem citem = new DocumentItemDynamoDb(documentId1, date, userId);
DynamicDocumentItem doc = new DocumentItemToDynamicDocumentItem().apply(item);
doc.put("documents", Arrays.asList(new DocumentItemToDynamicDocumentItem().apply(citem)));
getDocumentService().saveDocumentItemWithTag(null, doc);
ApiGatewayRequestEvent event = toRequestEvent("/request-get-documents-documentid02.json");
// 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));
assertEquals(documentId0, resp.get("documentId"));
assertEquals(userId, resp.get("userId"));
assertNotNull(resp.get("insertedDate"));
assertEquals(DEFAULT_SITE_ID, resp.get("siteId"));
assertNull(resp.get("next"));
assertNull(resp.get("previous"));
List<Map<String, Object>> children = (List<Map<String, Object>>) resp.get("documents");
assertEquals(1, children.size());
assertEquals(documentId1, children.get(0).get("documentId"));
assertNotNull(userId, children.get(0).get("userId"));
assertNotNull(children.get(0).get("belongsToDocumentId"));
assertNotNull(children.get(0).get("insertedDate"));
assertNull(children.get(0).get("siteId"));
}
use of com.formkiq.stacks.dynamodb.DocumentItemDynamoDb in project formkiq-core by formkiq.
the class ApiDocumentsPatchRequestTest method testHandlePatchDocuments01.
/**
* POST /documents request Base64 body.
*
* @throws Exception an error has occurred
*/
@Test
public void testHandlePatchDocuments01() throws Exception {
for (String siteId : Arrays.asList(null, UUID.randomUUID().toString())) {
// given
String userId = "jsmith";
String documentId = UUID.randomUUID().toString();
getDocumentService().saveDocument(siteId, new DocumentItemDynamoDb(documentId, new Date(), userId), new ArrayList<>());
ApiGatewayRequestEvent event = toRequestEvent("/request-patch-documents-documentid01.json");
addParameter(event, "siteId", siteId);
setPathParameter(event, "documentId", documentId);
// when
String response = handleRequest(event);
// then
assert200Response(siteId, response);
assertTrue(getLogger().containsString("setting userId: 8a73dfef-26d3-43d8-87aa-b3ec358e43ba@formkiq.com " + "contentType: application/pdf"));
}
}
use of com.formkiq.stacks.dynamodb.DocumentItemDynamoDb in project formkiq-core by formkiq.
the class DocumentIdContentGetRequestHandlerTest method testReturnContent.
/**
* Test Content is returned.
* @param contentType {@link String}
* @throws Exception Exception
*/
@SuppressWarnings("unchecked")
private void testReturnContent(final String contentType) throws Exception {
for (String siteId : Arrays.asList(null, UUID.randomUUID().toString())) {
// given
String documentId = UUID.randomUUID().toString();
String content = "this is a test";
String s3key = createS3Key(siteId, documentId);
try (S3Client s3 = getS3().buildClient()) {
getS3().putObject(s3, BUCKET_NAME, s3key, content.getBytes(StandardCharsets.UTF_8), null);
}
ApiGatewayRequestEvent event = toRequestEvent("/request-get-documents-documentid-content01.json");
addParameter(event, "siteId", siteId);
setPathParameter(event, "documentId", documentId);
String userId = "jsmith";
DocumentItemDynamoDb item = new DocumentItemDynamoDb(documentId, new Date(), userId);
item.setContentType(contentType);
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);
assertEquals(content, body.get("content"));
assertEquals(contentType, body.get("contentType"));
assertEquals("false", body.get("isBase64").toString());
}
}
use of com.formkiq.stacks.dynamodb.DocumentItemDynamoDb in project formkiq-core by formkiq.
the class DocumentIdUrlGetRequestHandlerTest method testHandleGetDocumentContent01.
/**
* /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 testHandleGetDocumentContent01() throws Exception {
for (String contentType : Arrays.asList(null, "application/pdf", "text/plain")) {
for (String siteId : Arrays.asList(null, UUID.randomUUID().toString())) {
// given
String documentId = UUID.randomUUID().toString();
String userId = "jsmith";
if (contentType != null) {
DocumentFormat format = new DocumentFormat();
format.setContentType(contentType);
format.setDocumentId(documentId);
format.setInsertedDate(new Date());
format.setUserId(userId);
getDocumentService().saveDocumentFormat(siteId, format);
}
ApiGatewayRequestEvent event = toRequestEvent("/request-get-documents-documentid-url01.json");
addParameter(event, "siteId", siteId);
setPathParameter(event, "documentId", documentId);
addHeader(event, "Content-Type", contentType);
DocumentItemDynamoDb item = new DocumentItemDynamoDb(documentId, new Date(), userId);
if ("text/plain".equals(contentType)) {
item.setContentType(contentType);
}
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")));
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=172800"));
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));
}
}
}
}
Aggregations