use of com.formkiq.stacks.common.objects.DynamicObject in project formkiq-core by formkiq.
the class ApiDocumentsRequestTest method testHandlePostDocuments02.
/**
* POST /documents request Base64 body.
*
* @throws Exception an error has occurred
*/
@SuppressWarnings("unchecked")
@Test
public void testHandlePostDocuments02() throws Exception {
for (String siteId : Arrays.asList(null, UUID.randomUUID().toString())) {
// given
ApiGatewayRequestEvent event = toRequestEvent("/request-post-documents-documentid02.json");
addParameter(event, "siteId", siteId);
// when
String response = handleRequest(event);
// then
Map<String, String> m = fromJson(response, Map.class);
final int mapsize = 3;
assertEquals(mapsize, m.size());
assertEquals("201.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));
assertNotNull(resp.get("documentId"));
assertNull(resp.get("next"));
assertNull(resp.get("previous"));
String key = siteId != null ? siteId + "/" + resp.get("documentId") + ".fkb64" : resp.get("documentId") + ".fkb64";
assertTrue(getLogger().containsString("s3 putObject " + key + " into bucket " + STAGE_BUCKET_NAME));
assertNotNull(UUID.fromString(resp.getString("documentId")));
try (S3Client s3 = getS3().buildClient()) {
assertTrue(getS3().getObjectMetadata(s3, STAGE_BUCKET_NAME, key).isObjectExists());
String content = getS3().getContentAsString(s3, STAGE_BUCKET_NAME, key, null);
DynamicObject obj = new DynamicObject(fromJson(content, Map.class));
assertTrue(obj.hasString("documentId"));
assertTrue(obj.hasString("content"));
assertEquals("7b87c0e2-5f20-403b-bcc6-d35cb491d3b7@formkiq.com", obj.getString("userId"));
}
}
}
use of com.formkiq.stacks.common.objects.DynamicObject in project formkiq-core by formkiq.
the class ApiDocumentsRequestTest method testHandlePostDocuments05.
/**
* POST /documents request Invalid JSON body.
*
* @throws Exception an error has occurred
*/
@SuppressWarnings("unchecked")
@Test
public void testHandlePostDocuments05() throws Exception {
for (String siteId : Arrays.asList(null, UUID.randomUUID().toString())) {
// given
ApiGatewayRequestEvent event = toRequestEvent("/request-post-documents-documentid05.json");
addParameter(event, "siteId", siteId);
// when
String response = handleRequest(event);
// then
Map<String, String> m = fromJson(response, Map.class);
final int mapsize = 3;
assertEquals(mapsize, m.size());
assertEquals("400.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));
assertNull(resp.get("documentId"));
assertNull(resp.get("next"));
assertNull(resp.get("previous"));
}
}
use of com.formkiq.stacks.common.objects.DynamicObject in project formkiq-core by formkiq.
the class ApiDocumentsRequestTest method testHandleGetDocuments06.
/**
* Get /documents request with tz. Date after
*
* @throws Exception an error has occurred
*/
@SuppressWarnings("unchecked")
@Test
public void testHandleGetDocuments06() throws Exception {
for (String siteId : Arrays.asList(null, UUID.randomUUID().toString())) {
// given
ZoneOffset zone = ZoneOffset.of("-05:00");
ZonedDateTime zdate = ZonedDateTime.now(zone);
Date date = Date.from(zdate.toInstant());
String username = UUID.randomUUID() + "@formkiq.com";
String documentId = UUID.randomUUID().toString();
DocumentItemDynamoDb item = new DocumentItemDynamoDb(documentId, date, username);
ApiGatewayRequestEvent event = toRequestEvent("/request-get-documents-tz02.json");
addParameter(event, "siteId", siteId);
getDocumentService().saveDocument(siteId, item, new ArrayList<>());
Thread.sleep(ONE_SECOND);
// 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));
List<DynamicObject> documents = resp.getList("documents");
assertEquals(1, documents.size());
}
}
use of com.formkiq.stacks.common.objects.DynamicObject in project formkiq-core by formkiq.
the class ApiDocumentsSearchRequestTest method testHandleSearchRequest09.
/**
* Valid POST search by eq tagValue with > 10 Document.
*
* @throws Exception an error has occurred
*/
@SuppressWarnings("unchecked")
@Test
public void testHandleSearchRequest09() throws Exception {
for (String siteId : Arrays.asList(null, UUID.randomUUID().toString())) {
// given
final int count = 100;
final String tagKey = "category";
final String tagvalue = "person";
final String username = "jsmith";
List<String> documentIds = new ArrayList<>();
for (int i = 0; i < count; i++) {
String documentId = UUID.randomUUID().toString();
Date now = new Date();
documentIds.add(documentId);
DocumentTag item = new DocumentTag(documentId, tagKey, tagvalue, now, username);
item.setUserId(UUID.randomUUID().toString());
getDocumentService().saveDocument(siteId, new DocumentItemDynamoDb(documentId, now, username), Arrays.asList(item));
}
ApiGatewayRequestEvent event = toRequestEvent("/request-post-search01.json");
addParameter(event, "siteId", siteId);
event.setIsBase64Encoded(Boolean.FALSE);
QueryRequest q = new QueryRequest().query(new SearchQuery().tag(new SearchTagCriteria().key(tagKey).eq(tagvalue)).documentsIds(documentIds));
event.setBody(GsonUtil.getInstance().toJson(q));
// 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));
List<DynamicObject> documents = resp.getList("documents");
assertEquals(count, documents.size());
// given not search by documentIds should be limited to 10
q = new QueryRequest().query(new SearchQuery().tag(new SearchTagCriteria().key(tagKey).eq(tagvalue)).documentsIds(null));
event.setBody(GsonUtil.getInstance().toJson(q));
// when
response = handleRequest(event);
// then
final int ten = 10;
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")));
resp = new DynamicObject(fromJson(m.get("body"), Map.class));
documents = resp.getList("documents");
assertEquals(ten, documents.size());
}
}
use of com.formkiq.stacks.common.objects.DynamicObject in project formkiq-core by formkiq.
the class ApiDocumentsSearchRequestTest method testHandleSearchRequest04.
/**
* Valid POST search by eq tagValues.
*
* @throws Exception an error has occurred
*/
@SuppressWarnings("unchecked")
@Test
public void testHandleSearchRequest04() throws Exception {
for (String siteId : Arrays.asList(null, UUID.randomUUID().toString())) {
// given
String documentId = UUID.randomUUID().toString();
String tagKey = "category";
String username = "jsmith";
Date now = new Date();
ApiGatewayRequestEvent event = toRequestEvent("/request-post-search01.json");
addParameter(event, "siteId", siteId);
event.setBody("ewogICJxdWVyeSI6IHsKICAgICJ0YWciOiB7CiAgICAgICJrZXkiOiAiY2F0Z" + "WdvcnkiLAogICAgICAiZXEiOiAieHl6IgogICAgfQogIH0KfQ==");
DocumentTag item = new DocumentTag(documentId, tagKey, null, now, username);
item.setValues(Arrays.asList("abc", "xyz"));
item.setUserId(UUID.randomUUID().toString());
getDocumentService().saveDocument(siteId, new DocumentItemDynamoDb(documentId, now, username), Arrays.asList(item));
// 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));
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("USERDEFINED", matchedTag.get("type"));
assertEquals("category", matchedTag.get("key"));
assertEquals("xyz", matchedTag.get("value"));
assertNull(resp.get("next"));
assertNull(resp.get("previous"));
}
}
Aggregations