use of com.formkiq.stacks.dynamodb.DocumentTag in project formkiq-core by formkiq.
the class ApiDocumentsTagsRequestTest method testHandleDeleteTagDocument01.
/**
* DELETE /documents/{documentId}/tags/{tagKey} request with Tag Value.
*
* @throws Exception an error has occurred
*/
@SuppressWarnings("unchecked")
@Test
public void testHandleDeleteTagDocument01() throws Exception {
for (String siteId : Arrays.asList(null, UUID.randomUUID().toString())) {
// given
Date now = new Date();
String documentId = UUID.randomUUID().toString();
String tagKey = "category";
String userId = "jsmith";
ApiGatewayRequestEvent event = toRequestEvent("/request-delete-documents-documentid-tags01.json");
addParameter(event, "siteId", siteId);
setPathParameter(event, "documentId", documentId);
DocumentTag tag = new DocumentTag(documentId, tagKey, tagKey, now, userId);
tag.setInsertedDate(new Date());
getDocumentService().addTags(siteId, documentId, Arrays.asList(tag), null);
assertEquals(1, getDocumentService().findDocumentTags(siteId, documentId, null, MAX_RESULTS).getResults().size());
// 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")));
ApiMessageResponse resp = GsonUtil.getInstance().fromJson(m.get("body"), ApiMessageResponse.class);
assertEquals("Removed 'category' from document '" + documentId + "'.", resp.getMessage());
assertNull(resp.getNext());
assertNull(resp.getPrevious());
PaginationResults<DocumentTag> tags = getDocumentService().findDocumentTags(siteId, documentId, null, MAX_RESULTS);
assertEquals(0, tags.getResults().size());
String expected = "response: {" + getHeaders() + ",\"body\":\"{\\\"message\\\":\\\"Removed 'category' from document '" + documentId + "'.\\\"}\"," + "\"statusCode\":200}";
assertTrue(getLogger().containsString(expected));
}
}
use of com.formkiq.stacks.dynamodb.DocumentTag 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.DocumentTag in project formkiq-core by formkiq.
the class DocumentTagRequestHandler method get.
@Override
public ApiRequestHandlerResponse get(final LambdaLogger logger, final ApiGatewayRequestEvent event, final ApiAuthorizer authorizer, final AwsServiceCache awsservice) throws Exception {
String documentId = event.getPathParameters().get("documentId");
String tagKey = event.getPathParameters().get("tagKey");
String siteId = authorizer.getSiteId();
DocumentTag tag = awsservice.documentService().findDocumentTag(siteId, documentId, tagKey);
if (tag == null) {
throw new NotFoundException("Tag " + tagKey + " not found.");
}
ApiDocumentTagItemResponse resp = new ApiDocumentTagItemResponse();
resp.setKey(tagKey);
resp.setValue(tag.getValue());
resp.setValues(tag.getValues());
resp.setInsertedDate(tag.getInsertedDate());
resp.setUserId(tag.getUserId());
resp.setType(tag.getType() != null ? tag.getType().name().toLowerCase() : null);
resp.setDocumentId(tag.getDocumentId());
return new ApiRequestHandlerResponse(SC_OK, resp);
}
use of com.formkiq.stacks.dynamodb.DocumentTag in project formkiq-core by formkiq.
the class DocumentTagsRequestHandler method post.
@Override
public ApiRequestHandlerResponse post(final LambdaLogger logger, final ApiGatewayRequestEvent event, final ApiAuthorizer authorizer, final AwsServiceCache awsservice) throws Exception {
DocumentTag tag = fromBodyToObject(logger, event, DocumentTag.class);
DocumentTags tags = fromBodyToObject(logger, event, DocumentTags.class);
boolean tagValid = isValid(tag);
boolean tagsValid = isValid(tags);
if (!tagValid && !tagsValid) {
throw new BadException("invalid json body");
}
if (!tagsValid) {
tags = new DocumentTags();
tags.setTags(Arrays.asList(tag));
}
tags.getTags().forEach(t -> {
t.setType(DocumentTagType.USERDEFINED);
t.setInsertedDate(new Date());
t.setUserId(getCallingCognitoUsername(event));
});
String documentId = event.getPathParameters().get("documentId");
String siteId = authorizer.getSiteId();
awsservice.documentService().deleteDocumentTag(siteId, documentId, "untagged");
awsservice.documentService().addTags(siteId, documentId, tags.getTags(), null);
ApiResponse resp = tagsValid ? new ApiMessageResponse("Created Tags.") : new ApiMessageResponse("Created Tag '" + tag.getKey() + "'.");
return new ApiRequestHandlerResponse(SC_CREATED, resp);
}
use of com.formkiq.stacks.dynamodb.DocumentTag in project formkiq-core by formkiq.
the class DocumentTagsRequestHandler method get.
@Override
public ApiRequestHandlerResponse get(final LambdaLogger logger, final ApiGatewayRequestEvent event, final ApiAuthorizer authorizer, final AwsServiceCache awsservice) throws Exception {
CacheService cacheService = awsservice.documentCacheService();
ApiPagination pagination = getPagination(cacheService, event);
int limit = pagination != null ? pagination.getLimit() : getLimit(logger, event);
PaginationMapToken ptoken = pagination != null ? pagination.getStartkey() : null;
String siteId = authorizer.getSiteId();
String documentId = event.getPathParameters().get("documentId");
PaginationResults<DocumentTag> results = awsservice.documentService().findDocumentTags(siteId, documentId, ptoken, limit);
results.getResults().forEach(r -> r.setDocumentId(null));
ApiPagination current = createPagination(cacheService, event, pagination, results.getToken(), limit);
List<DocumentTag> tags = subList(results.getResults(), limit);
List<ApiDocumentTagItemResponse> list = tags.stream().map(t -> {
ApiDocumentTagItemResponse r = new ApiDocumentTagItemResponse();
r.setDocumentId(t.getDocumentId());
r.setInsertedDate(t.getInsertedDate());
r.setKey(t.getKey());
r.setValue(t.getValue());
r.setValues(t.getValues());
r.setUserId(t.getUserId());
r.setType(t.getType() != null ? t.getType().name().toLowerCase() : null);
return r;
}).collect(Collectors.toList());
ApiDocumentTagsItemResponse resp = new ApiDocumentTagsItemResponse();
resp.setTags(list);
resp.setPrevious(current.getPrevious());
resp.setNext(current.hasNext() ? current.getNext() : null);
return new ApiRequestHandlerResponse(SC_OK, resp);
}
Aggregations