use of com.formkiq.stacks.dynamodb.DocumentTag 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.DocumentTag 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.DocumentTag 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.DocumentTag 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.DocumentTag in project formkiq-core by formkiq.
the class ApiDocumentsSearchRequestTest method testHandleSearchRequest07.
/**
* Valid POST search by eq/eqOr tagValue and valid/invalid DocumentId.
*
* @throws Exception an error has occurred
*/
@SuppressWarnings("unchecked")
@Test
public void testHandleSearchRequest07() throws Exception {
for (String op : Arrays.asList("eq", "eqOr")) {
for (String siteId : Arrays.asList(null, UUID.randomUUID().toString())) {
// given
final String documentId = UUID.randomUUID().toString();
final String tagKey = "category";
final String tagvalue = "person";
final String username = "jsmith";
final Date now = new Date();
ApiGatewayRequestEvent event = toRequestEvent("/request-post-search01.json");
addParameter(event, "siteId", siteId);
event.setIsBase64Encoded(Boolean.FALSE);
DocumentSearch s = new DocumentSearch().query(new DocumentSearchQuery().tag(new DocumentSearchTag().key("category").eq("person")).documentIds(Arrays.asList(documentId)));
event.setBody(GsonUtil.getInstance().toJson(s));
if ("eqOr".equals(op)) {
s.query().tag().eq(null).eqOr(Arrays.asList("person"));
}
for (String v : Arrays.asList("", "!")) {
DocumentTag item = new DocumentTag(documentId + v, tagKey, tagvalue + v, now, username);
item.setUserId(UUID.randomUUID().toString());
getDocumentService().saveDocument(siteId, new DocumentItemDynamoDb(documentId + v, now, username), Arrays.asList(item));
}
// when
String response = handleRequest(event);
// then
Map<String, String> m = fromJson(response, Map.class);
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("category", matchedTag.get("key"));
assertEquals("person", matchedTag.get("value"));
assertEquals("USERDEFINED", matchedTag.get("type"));
assertNull(resp.get("next"));
assertNull(resp.get("previous"));
// given - invalid document id
s.query().documentIds(Arrays.asList("123"));
event.setBody(GsonUtil.getInstance().toJson(s));
// when
response = handleRequest(event);
// then
m = fromJson(response, Map.class);
resp = new DynamicObject(fromJson(m.get("body"), Map.class));
documents = resp.getList("documents");
assertEquals(0, documents.size());
}
}
}
Aggregations