use of com.formkiq.stacks.dynamodb.DocumentItem in project formkiq-core by formkiq.
the class ApiRequestHandlerTest method testHandleGetRequest02.
/**
* Get Document Request, Document found.
*
* @throws Exception an error has occurred
*/
@SuppressWarnings("unchecked")
@Test
public void testHandleGetRequest02() throws Exception {
for (String siteId : Arrays.asList(null, UUID.randomUUID().toString())) {
// given
Date date = new Date();
String documentId = UUID.randomUUID().toString();
String userId = "jsmith";
DocumentItem item = new DocumentItemDynamoDb(documentId, date, userId);
getDocumentService().saveDocument(siteId, item, new ArrayList<>());
ApiGatewayRequestEvent event = toRequestEvent("/request-get-documents-documentid01.json");
addParameter(event, "siteId", siteId);
setPathParameter(event, "documentId", documentId);
// 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.getString("documentId"));
assertEquals(userId, resp.getString("userId"));
assertNotNull(resp.get("insertedDate"));
assertNull(resp.get("next"));
assertNull(resp.get("previous"));
}
}
use of com.formkiq.stacks.dynamodb.DocumentItem in project formkiq-core by formkiq.
the class DocumentsS3Update method processS3File.
/**
* Process S3 File.
*
* @param logger {@link LambdaLogger}
* @param create boolean
* @param s3bucket {@link String}
* @param s3key {@link String}
* @param debug boolean
* @throws FileNotFoundException FileNotFoundException
*/
private void processS3File(final LambdaLogger logger, final boolean create, final String s3bucket, final String s3key, final boolean debug) throws FileNotFoundException {
String key = urlDecode(s3key);
String siteId = getSiteId(key);
String documentId = resetDatabaseKey(siteId, key);
try (S3Client s3 = this.s3service.buildClient()) {
S3ObjectMetadata resp = this.s3service.getObjectMetadata(s3, s3bucket, key);
if (!resp.isObjectExists()) {
throw new FileNotFoundException("Object " + documentId + " not found in bucket " + s3bucket);
}
String contentType = resp.getContentType();
Long contentLength = resp.getContentLength();
DocumentItem item = this.service.findDocument(siteId, documentId);
if (item != null) {
DynamicDocumentItem doc = new DocumentItemToDynamicDocumentItem().apply(item);
if (contentType != null && contentType.length() > 0) {
doc.setContentType(contentType);
}
doc.setChecksum(resp.getEtag());
if (contentLength != null) {
doc.setContentLength(contentLength);
}
logger.log("saving document " + createDatabaseKey(siteId, item.getDocumentId()));
List<DynamicDocumentTag> tags = getObjectTags(s3, item, s3bucket, key);
doc.put("tags", tags);
if (debug) {
logger.log("original " + this.gson.toJson(item));
logger.log("new " + this.gson.toJson(doc));
}
this.service.saveDocumentItemWithTag(siteId, doc);
this.service.deleteDocumentFormats(siteId, item.getDocumentId());
String content = getContent(s3bucket, key, s3, resp, doc);
sendSnsMessage(logger, create ? "create" : "update", siteId, doc, s3bucket, key, content);
} else {
logger.log("Cannot find document " + documentId + " in site " + siteId);
}
}
}
use of com.formkiq.stacks.dynamodb.DocumentItem in project formkiq-core by formkiq.
the class DocumentsS3UpdateTest method testHandleRequest02.
/**
* Update Document Request with existing Tags.
*
* @throws Exception Exception
*/
@Test
@Timeout(unit = TimeUnit.MILLISECONDS, value = TEST_TIMEOUT)
public void testHandleRequest02() throws Exception {
for (String siteId : Arrays.asList(null, UUID.randomUUID().toString())) {
// given
this.logger.reset();
String key = createDatabaseKey(siteId, BUCKET_KEY);
final Map<String, Object> map = loadFileAsMap(this, "/objectupdate-event1.json", BUCKET_KEY, key);
DynamicDocumentItem doc = new DynamicDocumentItem(Map.of());
doc.setInsertedDate(new Date());
doc.setDocumentId(BUCKET_KEY);
doc.setUserId("asd");
doc.setPath("test.txt");
DynamicDocumentTag tag = new DynamicDocumentTag(Map.of("documentId", BUCKET_KEY, "key", "person", "value", "category", "insertedDate", new Date(), "userId", "asd"));
doc.put("tags", Arrays.asList(tag));
service.saveDocumentItemWithTag(siteId, doc);
DocumentFormat format = new DocumentFormat();
format.setContentType("application/pdf");
format.setDocumentId(BUCKET_KEY);
format.setInsertedDate(new Date());
format.setUserId("asd");
service.saveDocumentFormat(siteId, format);
addS3File(key, "pdf", true, "testdata");
// when
final DocumentItem item = handleRequest(siteId, BUCKET_KEY, map);
// then
PaginationResults<DocumentTag> tags = service.findDocumentTags(siteId, BUCKET_KEY, null, MAX_RESULTS);
final int size = 5;
int i = 0;
assertEquals(size, tags.getResults().size());
assertEquals("CLAMAV_SCAN_STATUS", tags.getResults().get(i).getKey());
assertEquals("GOOD", tags.getResults().get(i).getValue());
assertEquals(DocumentTagType.SYSTEMDEFINED, tags.getResults().get(i++).getType());
assertEquals("path", tags.getResults().get(i).getKey());
assertEquals(DocumentTagType.SYSTEMDEFINED, tags.getResults().get(i++).getType());
assertEquals("person", tags.getResults().get(i).getKey());
assertEquals(DocumentTagType.USERDEFINED, tags.getResults().get(i++).getType());
assertEquals("12345", tags.getResults().get(i).getValue());
assertEquals(DocumentTagType.USERDEFINED, tags.getResults().get(i).getType());
assertEquals("12345", tags.getResults().get(i++).getValue());
assertEquals(DocumentTagType.SYSTEMDEFINED, tags.getResults().get(i).getType());
assertEquals("asd", tags.getResults().get(i++).getValue());
assertEquals(0, service.findDocumentFormats(siteId, BUCKET_KEY, null, MAX_RESULTS).getResults().size());
verifyDocumentSaved(siteId, item, "pdf", "8");
assertPublishSnsMessage(siteId, sqsDocumentEventUrl, "update", false, false);
}
}
use of com.formkiq.stacks.dynamodb.DocumentItem in project formkiq-core by formkiq.
the class DocumentsS3UpdateTest method testHandleRequest01.
/**
* Create Document Request without existing Tags/Formats.
*
* @throws Exception Exception
*/
@Test
@Timeout(unit = TimeUnit.MILLISECONDS, value = TEST_TIMEOUT)
public void testHandleRequest01() throws Exception {
for (String siteId : Arrays.asList(null, UUID.randomUUID().toString())) {
// given
this.logger.reset();
String key = createDatabaseKey(siteId, BUCKET_KEY);
final Map<String, Object> map = loadFileAsMap(this, "/objectcreate-event1.json", BUCKET_KEY, key);
DynamicDocumentItem doc = new DynamicDocumentItem(Map.of());
doc.setInsertedDate(new Date());
doc.setDocumentId(BUCKET_KEY);
doc.setUserId("joe");
doc.setPath("test.txt");
service.saveDocumentItemWithTag(siteId, doc);
addS3File(key, "pdf", false, "testdata");
// when
final DocumentItem item = handleRequest(siteId, BUCKET_KEY, map);
// then
PaginationResults<DocumentTag> tags = service.findDocumentTags(siteId, BUCKET_KEY, null, MAX_RESULTS);
final int count = 3;
assertEquals(count, tags.getResults().size());
int i = 0;
assertEquals("path", tags.getResults().get(i).getKey());
assertEquals("test.txt", tags.getResults().get(i).getValue());
assertEquals(BUCKET_KEY, tags.getResults().get(i).getDocumentId());
assertEquals(DocumentTagType.SYSTEMDEFINED, tags.getResults().get(i).getType());
assertEquals("joe", tags.getResults().get(i).getUserId());
assertNotNull(tags.getResults().get(i++).getInsertedDate());
assertEquals("untagged", tags.getResults().get(i).getKey());
assertEquals("true", tags.getResults().get(i).getValue());
assertEquals(BUCKET_KEY, tags.getResults().get(i).getDocumentId());
assertEquals(DocumentTagType.SYSTEMDEFINED, tags.getResults().get(i).getType());
assertEquals("joe", tags.getResults().get(i).getUserId());
assertNotNull(tags.getResults().get(i++).getInsertedDate());
assertEquals("userId", tags.getResults().get(i).getKey());
assertEquals("joe", tags.getResults().get(i).getValue());
assertEquals(BUCKET_KEY, tags.getResults().get(i).getDocumentId());
assertEquals(DocumentTagType.SYSTEMDEFINED, tags.getResults().get(i).getType());
assertEquals("joe", tags.getResults().get(i).getUserId());
assertNotNull(tags.getResults().get(i++).getInsertedDate());
assertEquals(0, service.findDocumentFormats(siteId, BUCKET_KEY, null, MAX_RESULTS).getResults().size());
verifyDocumentSaved(siteId, item, "pdf", "8");
assertPublishSnsMessage(siteId, sqsDocumentEventUrl, "create", false, false);
}
}
use of com.formkiq.stacks.dynamodb.DocumentItem in project formkiq-core by formkiq.
the class DocumentsS3UpdateTest method testHandleRequest06.
/**
* Create Document Request with Text Content.
*
* @throws Exception Exception
*/
@Test
@Timeout(unit = TimeUnit.MILLISECONDS, value = TEST_TIMEOUT)
public void testHandleRequest06() throws Exception {
for (String siteId : Arrays.asList(null, UUID.randomUUID().toString())) {
// given
this.logger.reset();
String key = createDatabaseKey(siteId, BUCKET_KEY);
final Map<String, Object> map = loadFileAsMap(this, "/objectcreate-event1.json", BUCKET_KEY, key);
DynamicDocumentItem doc = new DynamicDocumentItem(Map.of());
doc.setInsertedDate(new Date());
doc.setDocumentId(BUCKET_KEY);
doc.setUserId("joe");
doc.setPath("test.txt");
service.saveDocumentItemWithTag(siteId, doc);
addS3File(key, "text/plain", false, "testdata");
// when
final DocumentItem item = handleRequest(siteId, BUCKET_KEY, map);
// then
verifyDocumentSaved(siteId, item, "text/plain", "8");
assertPublishSnsMessage(siteId, sqsDocumentEventUrl, "create", false, false);
}
}
Aggregations