use of com.formkiq.stacks.dynamodb.DocumentItemDynamoDb in project formkiq-core by formkiq.
the class ApiDocumentsRequestTest method createTestData.
/**
* Create MAX_RESULTS + 2 Documents test data.
*
* @param prefix {@link String}
* @param testdatacount int
*/
private void createTestData(final String prefix, final int testdatacount) {
String userId = "jsmith";
final int min10 = 10;
LocalDateTime nowLocalDate = LocalDateTime.now(ZoneOffset.UTC).plusMinutes(min10);
final int max = testdatacount;
for (int i = 0; i < max; i++) {
nowLocalDate = nowLocalDate.plus(1, ChronoUnit.MINUTES);
Date d = Date.from(nowLocalDate.atZone(ZoneOffset.UTC).toInstant());
getDocumentService().saveDocument(prefix, new DocumentItemDynamoDb("doc_" + i, d, userId), new ArrayList<>());
}
}
use of com.formkiq.stacks.dynamodb.DocumentItemDynamoDb in project formkiq-core by formkiq.
the class ApiDocumentsRequestTest method testHandleGetDocuments01.
/**
* Get /documents request.
*
* @throws Exception an error has occurred
*/
@SuppressWarnings("unchecked")
@Test
public void testHandleGetDocuments01() throws Exception {
for (String siteId : Arrays.asList(null, UUID.randomUUID().toString())) {
// given
Date date = new Date();
final long contentLength = 1000L;
String username = UUID.randomUUID() + "@formkiq.com";
String documentId = UUID.randomUUID().toString();
DocumentItemDynamoDb item = new DocumentItemDynamoDb(documentId, date, username);
item.setContentLength(Long.valueOf(contentLength));
ApiGatewayRequestEvent event = toRequestEvent("/request-get-documents.json");
addParameter(event, "siteId", siteId);
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")));
DynamicObject resp = new DynamicObject(fromJson(m.get("body"), Map.class));
List<DynamicObject> documents = resp.getList("documents");
assertEquals(1, documents.size());
assertNotNull(documents.get(0).get("documentId"));
assertNotNull(documents.get(0).get("insertedDate"));
assertNotNull(documents.get(0).get("userId"));
assertEquals("1000.0", documents.get(0).get("contentLength").toString());
}
}
use of com.formkiq.stacks.dynamodb.DocumentItemDynamoDb in project formkiq-core by formkiq.
the class ApiDocumentsRequestTest method testHandleGetDocuments04.
/**
* Get /documents request with date / tz. Date after
*
* @throws Exception an error has occurred
*/
@SuppressWarnings("unchecked")
@Test
public void testHandleGetDocuments04() throws Exception {
for (String siteId : Arrays.asList(null, UUID.randomUUID().toString())) {
// given
Date date = new Date();
String username = UUID.randomUUID() + "@formkiq.com";
String documentId = UUID.randomUUID().toString();
DocumentItemDynamoDb item = new DocumentItemDynamoDb(documentId, date, username);
ApiGatewayRequestEvent event = toRequestEvent("/request-get-documents-tz.json");
addParameter(event, "siteId", siteId);
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")));
DynamicObject resp = new DynamicObject(fromJson(m.get("body"), Map.class));
List<DynamicObject> documents = resp.getList("documents");
assertEquals(0, documents.size());
}
}
use of com.formkiq.stacks.dynamodb.DocumentItemDynamoDb 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());
}
}
}
use of com.formkiq.stacks.dynamodb.DocumentItemDynamoDb in project formkiq-core by formkiq.
the class ApiDocumentsSearchRequestTest method testHandleSearchRequest03.
/**
* Valid POST search by eq tagValue.
*
* @throws Exception an error has occurred
*/
@SuppressWarnings("unchecked")
@Test
public void testHandleSearchRequest03() throws Exception {
for (String op : Arrays.asList("eq", "eqOr")) {
for (String siteId : Arrays.asList(null, UUID.randomUUID().toString())) {
// given
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);
DocumentSearch s = new DocumentSearch().query(new DocumentSearchQuery().tag(new DocumentSearchTag().key("category").eq("person")).documentIds(Arrays.asList(documentId)));
event.setBody(GsonUtil.getInstance().toJson(s));
event.setIsBase64Encoded(Boolean.FALSE);
if ("eqOr".equals(op)) {
s.query().tag().eq(null).eqOr(Arrays.asList("person"));
}
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));
// 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("person", matchedTag.get("value"));
assertNull(resp.get("next"));
assertNull(resp.get("previous"));
}
}
}
Aggregations