use of com.formkiq.stacks.dynamodb.DocumentItemDynamoDb in project formkiq-core by formkiq.
the class ApiDocumentsTagsRequestTest method testHandleGetTags04.
/**
* GET /documents/{documentId}/tags/{tagKey} request. Tag and values found.
*
* @throws Exception an error has occurred
*/
@SuppressWarnings("unchecked")
@Test
public void testHandleGetTags04() throws Exception {
for (String siteId : Arrays.asList(null, UUID.randomUUID().toString())) {
// given
String documentId = UUID.randomUUID().toString();
DocumentItemDynamoDb item = new DocumentItemDynamoDb(documentId, new Date(), "jsmith");
DocumentTag tag = new DocumentTag(documentId, "category", null, new Date(), "jsmith");
tag.setValues(Arrays.asList("abc", "xyz"));
getDocumentService().saveDocument(siteId, item, Arrays.asList(tag));
ApiGatewayRequestEvent event = toRequestEvent("/request-get-documents-documentid-tags01.json");
addParameter(event, "siteId", siteId);
setPathParameter(event, "documentId", documentId);
// when
String response = handleRequest(event);
// then
Map<String, String> m = GsonUtil.getInstance().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")));
ApiDocumentTagItemResponse resp = GsonUtil.getInstance().fromJson(m.get("body"), ApiDocumentTagItemResponse.class);
assertEquals("category", resp.getKey());
assertNull(resp.getValue());
assertEquals("[abc, xyz]", resp.getValues().toString());
}
}
use of com.formkiq.stacks.dynamodb.DocumentItemDynamoDb in project formkiq-core by formkiq.
the class ApiDocumentsTagsRequestTest method testHandleGetTags03.
/**
* GET /documents/{documentId}/tags/{tagKey} request. Tag and NO value found.
*
* @throws Exception an error has occurred
*/
@SuppressWarnings("unchecked")
@Test
public void testHandleGetTags03() throws Exception {
for (String siteId : Arrays.asList(null, UUID.randomUUID().toString())) {
// given
String documentId = UUID.randomUUID().toString();
DocumentItemDynamoDb item = new DocumentItemDynamoDb(documentId, new Date(), "jsmith");
DocumentTag tag = new DocumentTag(documentId, "category", null, new Date(), "jsmith");
getDocumentService().saveDocument(siteId, item, Arrays.asList(tag));
ApiGatewayRequestEvent event = toRequestEvent("/request-get-documents-documentid-tags01.json");
addParameter(event, "siteId", siteId);
setPathParameter(event, "documentId", documentId);
// when
String response = handleRequest(event);
// then
Map<String, String> m = GsonUtil.getInstance().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")));
ApiDocumentTagItemResponse resp = GsonUtil.getInstance().fromJson(m.get("body"), ApiDocumentTagItemResponse.class);
assertEquals("category", resp.getKey());
assertEquals("", resp.getValue());
}
}
use of com.formkiq.stacks.dynamodb.DocumentItemDynamoDb in project formkiq-core by formkiq.
the class DocumentsUploadRequestHandler method get.
@Override
public ApiRequestHandlerResponse get(final LambdaLogger logger, final ApiGatewayRequestEvent event, final ApiAuthorizer authorizer, final AwsServiceCache awsservice) throws Exception {
boolean documentExists = false;
Date date = new Date();
String documentId = UUID.randomUUID().toString();
String username = getCallingCognitoUsername(event);
DocumentItem item = new DocumentItemDynamoDb(documentId, date, username);
List<DocumentTag> tags = new ArrayList<>();
Map<String, String> query = event.getQueryStringParameters();
String siteId = authorizer.getSiteId();
DocumentService service = awsservice.documentService();
if (query != null && query.containsKey("path")) {
String path = query.get("path");
path = URLDecoder.decode(path, StandardCharsets.UTF_8.toString());
item.setPath(path);
tags.add(new DocumentTag(documentId, "path", path, date, username, DocumentTagType.SYSTEMDEFINED));
}
String urlstring = generatePresignedUrl(awsservice, logger, siteId, documentId, query);
logger.log("generated presign url: " + urlstring + " for document " + documentId);
if (!documentExists) {
tags.add(new DocumentTag(documentId, "untagged", "true", date, username, DocumentTagType.SYSTEMDEFINED));
String value = this.restrictionMaxDocuments.getValue(awsservice, siteId);
if (!this.restrictionMaxDocuments.enforced(awsservice, siteId, value)) {
logger.log("saving document: " + item.getDocumentId() + " on path " + item.getPath());
service.saveDocument(siteId, item, tags);
if (value != null) {
awsservice.documentCountService().incrementDocumentCount(siteId);
}
} else {
throw new BadException("Max Number of Documents reached");
}
}
return new ApiRequestHandlerResponse(SC_OK, new ApiUrlResponse(urlstring, documentId));
}
use of com.formkiq.stacks.dynamodb.DocumentItemDynamoDb in project formkiq-core by formkiq.
the class ApiDocumentsTagsRequestTest method testHandlePutTags06.
/**
* PUT /documents/{documentId}/tags/{tagKey} change VALUES to VALUE request.
*
* @throws Exception an error has occurred
*/
@Test
public void testHandlePutTags06() throws Exception {
for (String siteId : Arrays.asList(null, UUID.randomUUID().toString())) {
// given
String documentId = UUID.randomUUID().toString();
String userId = "jsmith";
final String expected = "{" + getHeaders() + "," + "\"body\":\"" + "{\\\"message\\\":\\\"Updated tag 'category' on document '" + documentId + "'.\\\"}\"" + ",\"statusCode\":200}";
DocumentTag tag = new DocumentTag(null, "category", null, new Date(), userId);
tag.setValues(Arrays.asList("abc", "xyz"));
getDocumentService().saveDocument(siteId, new DocumentItemDynamoDb(documentId, new Date(), userId), Arrays.asList(tag));
ApiGatewayRequestEvent event = toRequestEvent("/request-put-documents-documentid-tags01.json");
addParameter(event, "siteId", siteId);
setPathParameter(event, "documentId", documentId);
// when
String response = handleRequest(event);
// then
assertEquals(expected, response);
PaginationResults<DocumentTag> tags = getDocumentService().findDocumentTags(siteId, documentId, null, MAX_RESULTS);
assertEquals(1, tags.getResults().size());
assertEquals("category", tags.getResults().get(0).getKey());
assertEquals("active", tags.getResults().get(0).getValue());
assertNull(tags.getResults().get(0).getValues());
assertEquals("8a73dfef-26d3-43d8-87aa-b3ec358e43ba@formkiq.com", tags.getResults().get(0).getUserId());
}
}
use of com.formkiq.stacks.dynamodb.DocumentItemDynamoDb in project formkiq-core by formkiq.
the class ApiDocumentsUploadRequestTest method testHandleGetDocumentsUpload03.
/**
* Valid PUT generate upload document signed url for existing document.
*
* @throws Exception an error has occurred
*/
@Test
public void testHandleGetDocumentsUpload03() throws Exception {
for (String siteId : Arrays.asList(null, UUID.randomUUID().toString())) {
// given
Date date = new Date();
String documentId = UUID.randomUUID().toString();
DocumentItemDynamoDb item = new DocumentItemDynamoDb(documentId, date, "jsmith");
ApiGatewayRequestEvent event = toRequestEvent("/request-get-documents-upload-documentid01.json");
addParameter(event, "siteId", siteId);
setPathParameter(event, "documentId", documentId);
// when
getDocumentService().saveDocument(siteId, item, new ArrayList<>());
String response = handleRequest(event);
// then
ApiUrlResponse resp = expectResponse(response);
assertTrue(getLogger().containsString("generated presign url: " + this.localstack.getEndpointOverride(Service.S3).toString() + "/testbucket/"));
assertTrue(getLogger().containsString("for document " + resp.getDocumentId()));
assertEquals(documentId, resp.getDocumentId());
}
}
Aggregations