use of com.formkiq.lambda.apigateway.ApiMessageResponse in project formkiq-core by formkiq.
the class ApiDocumentsTagsRequestTest method testHandleDeleteTagValue02.
/**
* DELETE /documents/{documentId}/tags/{tagKey}/{tagValue} request with Tag Values.
*
* @throws Exception an error has occurred
*/
@SuppressWarnings("unchecked")
@Test
public void testHandleDeleteTagValue02() throws Exception {
for (String siteId : Arrays.asList(null, UUID.randomUUID().toString())) {
// given
final Date now = new Date();
String documentId = UUID.randomUUID().toString();
String tagKey = "category";
final String userId = "jsmith";
ApiGatewayRequestEvent event = toRequestEvent("/request-delete-documents-documentid-tag-value01.json");
addParameter(event, "siteId", siteId);
setPathParameter(event, "documentId", documentId);
setPathParameter(event, "tagKey", tagKey);
setPathParameter(event, "tagValue", "xyz");
DocumentTag tag = new DocumentTag(documentId, tagKey, null, now, userId);
tag.setValues(Arrays.asList("abc", "xyz"));
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 Tag from document '" + documentId + "'.", resp.getMessage());
assertNull(resp.getNext());
assertNull(resp.getPrevious());
PaginationResults<DocumentTag> tags = getDocumentService().findDocumentTags(siteId, documentId, null, MAX_RESULTS);
assertEquals(1, tags.getResults().size());
assertEquals("abc", tags.getResults().get(0).getValue());
assertNull(tags.getResults().get(0).getValues());
}
}
use of com.formkiq.lambda.apigateway.ApiMessageResponse in project formkiq-core by formkiq.
the class ApiDocumentsTagsRequestTest method testHandleDeleteTagDocument03.
/**
* DELETE /documents/{documentId}/tags/{tagKey} request with Tag Values.
*
* @throws Exception an error has occurred
*/
@SuppressWarnings("unchecked")
@Test
public void testHandleDeleteTagDocument03() 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, null, now, userId);
tag.setValues(Arrays.asList("abc", "xyz"));
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.lambda.apigateway.ApiMessageResponse 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.lambda.apigateway.ApiMessageResponse in project formkiq-core by formkiq.
the class DocumentIdRequestHandler method delete.
@Override
public ApiRequestHandlerResponse delete(final LambdaLogger logger, final ApiGatewayRequestEvent event, final ApiAuthorizer authorizer, final AwsServiceCache awsservice) throws Exception {
String documentBucket = awsservice.documents3bucket();
String documentId = event.getPathParameters().get("documentId");
logger.log("deleting object " + documentId + " from bucket '" + documentBucket + "'");
try {
S3Service s3Service = awsservice.s3Service();
try (S3Client s3 = s3Service.buildClient()) {
S3ObjectMetadata md = s3Service.getObjectMetadata(s3, documentBucket, documentId);
if (md.isObjectExists()) {
s3Service.deleteObject(s3, documentBucket, documentId);
ApiResponse resp = new ApiMessageResponse("'" + documentId + "' object deleted");
return new ApiRequestHandlerResponse(SC_OK, resp);
}
}
throw new NotFoundException("Document " + documentId + " not found.");
} catch (S3Exception e) {
if (e.statusCode() == SC_NOT_FOUND.getStatusCode()) {
throw new NotFoundException("Document " + documentId + " not found.");
}
throw e;
}
}
use of com.formkiq.lambda.apigateway.ApiMessageResponse 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);
}
Aggregations