use of com.formkiq.stacks.dynamodb.DocumentItem in project formkiq-core by formkiq.
the class StagingS3CreateTest method testFkB64Extension05.
/**
* Test .fkb64 file with multiple documents.
*
* @throws IOException IOException
*/
@Test
public void testFkB64Extension05() throws IOException {
String documentId0 = "0d1a788d-9a70-418a-8c33-a9ee9c1a0173";
String documentId1 = "24af57ca-f61d-4ff8-b8a0-d7666073560e";
String documentId2 = "f2416702-6b3c-4d29-a217-82a43b16b964";
ZonedDateTime nowDate = ZonedDateTime.ofInstant(new Date().toInstant(), ZoneId.systemDefault());
for (String siteId : Arrays.asList(null, UUID.randomUUID().toString())) {
String key = createDatabaseKey(siteId, documentId0 + ".fkb64");
Map<String, Object> map = loadFileAsMap(this, "/objectcreate-event4.json", UUID1, key);
try (S3Client c = s3.buildClient()) {
String json = loadFile(this, "/document_multiple.fkb64");
s3.putObject(c, STAGING_BUCKET, key, json.getBytes(UTF_8), null, null);
// when
handleRequest(map);
// then
assertEquals(1, service.findDocumentsByDate(siteId, nowDate, null, MAX_RESULTS).getResults().size());
DocumentItem i = service.findDocument(siteId, documentId0, true, null, MAX_RESULTS).getResult();
assertNull(i.getContentType());
verifyBelongsToDocument(i, documentId0, Arrays.asList(documentId1, documentId2));
List<DocumentTag> tags = service.findDocumentTags(siteId, documentId0, null, MAX_RESULTS).getResults();
assertEquals(1, tags.size());
assertEquals("formName", tags.get(0).getKey());
assertEquals("Job Application Form", tags.get(0).getValue());
String k = createDatabaseKey(siteId, i.getDocumentId());
assertFalse(s3.getObjectMetadata(c, DOCUMENTS_BUCKET, k).isObjectExists());
i = service.findDocument(siteId, documentId1, true, null, MAX_RESULTS).getResult();
assertEquals("application/json", i.getContentType());
assertEquals(documentId0, i.getBelongsToDocumentId());
tags = service.findDocumentTags(siteId, documentId1, null, MAX_RESULTS).getResults();
assertEquals(1, tags.size());
assertEquals("formData", tags.get(0).getKey());
assertEquals("", tags.get(0).getValue());
k = createDatabaseKey(siteId, i.getDocumentId());
assertEquals("application/json", s3.getObjectMetadata(c, DOCUMENTS_BUCKET, k).getContentType());
i = service.findDocument(siteId, documentId2);
assertEquals(documentId0, i.getBelongsToDocumentId());
assertNull(i.getContentType());
tags = service.findDocumentTags(siteId, documentId2, null, MAX_RESULTS).getResults();
assertEquals(2, tags.size());
assertEquals("attachmentField", tags.get(0).getKey());
assertEquals("resume", tags.get(0).getValue());
assertEquals("category", tags.get(1).getKey());
assertEquals("", tags.get(1).getValue());
k = createDatabaseKey(siteId, i.getDocumentId());
assertFalse(s3.getObjectMetadata(c, DOCUMENTS_BUCKET, k).isObjectExists());
}
}
}
use of com.formkiq.stacks.dynamodb.DocumentItem in project formkiq-core by formkiq.
the class StagingS3CreateTest method testCopyFile.
/**
* Test CopyFile.
*
* @param documentId {@link String}
* @param expectedPath {@link String}
* @throws IOException IOException
*/
private void testCopyFile(final String documentId, final String expectedPath) throws IOException {
for (String siteId : Arrays.asList(null, UUID.randomUUID().toString())) {
// given
this.logger.reset();
String key = createDatabaseKey(siteId, documentId);
Map<String, String> metadata = new HashMap<>();
metadata.put("Content-Type", "application/pdf");
metadata.put("userId", "1234");
Map<String, Object> map = loadFileAsMap(this, "/objectcreate-event1.json", UUID1, key);
try (S3Client c = s3.buildClient()) {
s3.putObject(c, STAGING_BUCKET, key, "testdata".getBytes(UTF_8), "application/pdf", metadata);
// when
handleRequest(map);
// then
String destDocumentId = findDocumentIdFromLogger(siteId);
assertTrue(this.logger.containsString("Removing " + createDatabaseKey(siteId, documentId) + " from bucket example-bucket."));
assertTrue(this.logger.containsString("handling 1 record(s)."));
assertTrue(this.logger.containsString("Copying " + key + " from bucket example-bucket to " + createDatabaseKey(siteId, destDocumentId) + " in bucket " + DOCUMENTS_BUCKET + "."));
assertTrue(this.logger.containsString("Removing " + key + " from bucket example-bucket."));
assertFalse(s3.getObjectMetadata(c, STAGING_BUCKET, documentId).isObjectExists());
DocumentItem item = service.findDocument(siteId, destDocumentId);
assertNotNull(item);
assertEquals("8", item.getContentLength().toString());
assertEquals("application/pdf", item.getContentType());
assertEquals("ef654c40ab4f1747fc699915d4f70902", item.getChecksum());
assertNotNull(item.getInsertedDate());
assertEquals(item.getPath(), expectedPath);
assertEquals("1234", item.getUserId());
List<DocumentTag> tags = service.findDocumentTags(siteId, item.getDocumentId(), null, MAX_RESULTS).getResults();
int i = 0;
int count = 2;
if (item.getPath() != null) {
count++;
assertEquals("path", tags.get(i).getKey());
assertEquals(DocumentTagType.SYSTEMDEFINED, tags.get(i++).getType());
}
assertEquals(count, tags.size());
assertEquals("untagged", tags.get(i).getKey());
assertEquals(DocumentTagType.SYSTEMDEFINED, tags.get(i++).getType());
assertEquals("userId", tags.get(i).getKey());
assertEquals(DocumentTagType.SYSTEMDEFINED, tags.get(i++).getType());
}
}
}
use of com.formkiq.stacks.dynamodb.DocumentItem in project formkiq-core by formkiq.
the class AwsResourceTest method testAddDeleteFile02.
/**
* Test using a Presigned URL to Adding a file and then deleting it.
*
* @throws Exception Exception
*/
@Test(timeout = TEST_TIMEOUT)
public void testAddDeleteFile02() throws Exception {
// given
final Long contentLength = Long.valueOf(36);
String key = UUID.randomUUID().toString();
String contentType = "text/plain";
DocumentItem item = new DocumentItemDynamoDb(key, new Date(), "test");
try (S3Client s3 = getS3Service().buildClient()) {
// when
getDocumentService().saveDocument(null, item, null);
key = writeToDocuments(s3, key, contentType);
// then
verifyFileExistsInDocumentsS3(s3, key, contentType);
item = getDocumentService().findDocument(null, key);
while (true) {
if (contentType.equals(item.getContentType())) {
assertEquals(contentType, item.getContentType());
assertEquals(contentLength, item.getContentLength());
break;
}
item = getDocumentService().findDocument(null, key);
}
getS3Service().deleteObject(s3, getDocumentsbucketname(), key);
}
}
use of com.formkiq.stacks.dynamodb.DocumentItem in project formkiq-core by formkiq.
the class DocumentIdRequestHandler method get.
@Override
public ApiRequestHandlerResponse get(final LambdaLogger logger, final ApiGatewayRequestEvent event, final ApiAuthorizer authorizer, final AwsServiceCache awsservice) throws Exception {
String siteId = authorizer.getSiteId();
int limit = getLimit(logger, event);
ApiPagination token = getPagination(awsservice.documentCacheService(), event);
String documentId = event.getPathParameters().get("documentId");
ApiPagination pagination = getPagination(awsservice.documentCacheService(), event);
PaginationResult<DocumentItem> presult = awsservice.documentService().findDocument(siteId, documentId, true, token != null ? token.getStartkey() : null, limit);
DocumentItem result = presult.getResult();
if (result == null) {
throw new NotFoundException("Document " + documentId + " not found.");
}
ApiPagination current = createPagination(awsservice.documentCacheService(), event, pagination, presult.getToken(), limit);
DynamicDocumentItem item = new DocumentItemToDynamicDocumentItem().apply(result);
item.put("siteId", siteId != null ? siteId : DEFAULT_SITE_ID);
item.put("previous", current.getPrevious());
item.put("next", current.hasNext() ? current.getNext() : null);
return new ApiRequestHandlerResponse(SC_OK, new ApiMapResponse(item));
}
use of com.formkiq.stacks.dynamodb.DocumentItem in project formkiq-core by formkiq.
the class DocumentsRequestHandler method get.
@Override
public ApiRequestHandlerResponse get(final LambdaLogger logger, final ApiGatewayRequestEvent event, final ApiAuthorizer authorizer, final AwsServiceCache awsservice) throws Exception {
ApiPagination pagination = getPagination(awsservice.documentCacheService(), event);
final int limit = pagination != null ? pagination.getLimit() : getLimit(logger, event);
final PaginationMapToken ptoken = pagination != null ? pagination.getStartkey() : null;
String tz = getParameter(event, "tz");
String dateString = getParameter(event, "date");
if (StringUtils.isBlank(dateString)) {
if (StringUtils.isNotBlank(tz)) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
ZoneOffset offset = DateUtil.getZoneOffset(tz);
sdf.setTimeZone(TimeZone.getTimeZone(offset));
dateString = sdf.format(new Date());
} else {
dateString = this.df.format(new Date());
}
}
ZonedDateTime date = transformToDate(logger, awsservice, dateString, tz);
String siteId = authorizer.getSiteId();
final PaginationResults<DocumentItem> results = awsservice.documentService().findDocumentsByDate(siteId, date, ptoken, limit);
ApiPagination current = createPagination(awsservice.documentCacheService(), event, pagination, results.getToken(), limit);
List<DocumentItem> documents = subList(results.getResults(), limit);
List<DynamicDocumentItem> items = documents.stream().map(m -> new DocumentItemToDynamicDocumentItem().apply(m)).collect(Collectors.toList());
items.forEach(i -> i.put("siteId", siteId != null ? siteId : DEFAULT_SITE_ID));
Map<String, Object> map = new HashMap<>();
map.put("documents", items);
map.put("previous", current.getPrevious());
map.put("next", current.hasNext() ? current.getNext() : null);
return new ApiRequestHandlerResponse(SC_OK, new ApiMapResponse(map));
}
Aggregations