Search in sources :

Example 1 with ApiMessageResponse

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());
    }
}
Also used : DocumentTag(com.formkiq.stacks.dynamodb.DocumentTag) ApiMessageResponse(com.formkiq.lambda.apigateway.ApiMessageResponse) ApiGatewayRequestEvent(com.formkiq.lambda.apigateway.ApiGatewayRequestEvent) Date(java.util.Date) Test(org.junit.jupiter.api.Test)

Example 2 with ApiMessageResponse

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));
    }
}
Also used : DocumentTag(com.formkiq.stacks.dynamodb.DocumentTag) ApiMessageResponse(com.formkiq.lambda.apigateway.ApiMessageResponse) ApiGatewayRequestEvent(com.formkiq.lambda.apigateway.ApiGatewayRequestEvent) Date(java.util.Date) Test(org.junit.jupiter.api.Test)

Example 3 with ApiMessageResponse

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));
    }
}
Also used : DocumentTag(com.formkiq.stacks.dynamodb.DocumentTag) ApiMessageResponse(com.formkiq.lambda.apigateway.ApiMessageResponse) ApiGatewayRequestEvent(com.formkiq.lambda.apigateway.ApiGatewayRequestEvent) Date(java.util.Date) Test(org.junit.jupiter.api.Test)

Example 4 with ApiMessageResponse

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;
    }
}
Also used : ApiMessageResponse(com.formkiq.lambda.apigateway.ApiMessageResponse) S3Exception(software.amazon.awssdk.services.s3.model.S3Exception) NotFoundException(com.formkiq.lambda.apigateway.exception.NotFoundException) S3ObjectMetadata(com.formkiq.aws.s3.S3ObjectMetadata) ApiRequestHandlerResponse(com.formkiq.lambda.apigateway.ApiRequestHandlerResponse) S3Client(software.amazon.awssdk.services.s3.S3Client) S3Service(com.formkiq.aws.s3.S3Service) ApiResponse(com.formkiq.lambda.apigateway.ApiResponse)

Example 5 with ApiMessageResponse

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);
}
Also used : DocumentTag(com.formkiq.stacks.dynamodb.DocumentTag) ApiMessageResponse(com.formkiq.lambda.apigateway.ApiMessageResponse) ApiRequestHandlerResponse(com.formkiq.lambda.apigateway.ApiRequestHandlerResponse) BadException(com.formkiq.lambda.apigateway.exception.BadException) Date(java.util.Date) ApiResponse(com.formkiq.lambda.apigateway.ApiResponse) DocumentTags(com.formkiq.stacks.dynamodb.DocumentTags)

Aggregations

ApiMessageResponse (com.formkiq.lambda.apigateway.ApiMessageResponse)14 DocumentTag (com.formkiq.stacks.dynamodb.DocumentTag)10 Date (java.util.Date)10 ApiRequestHandlerResponse (com.formkiq.lambda.apigateway.ApiRequestHandlerResponse)8 NotFoundException (com.formkiq.lambda.apigateway.exception.NotFoundException)7 ApiGatewayRequestEvent (com.formkiq.lambda.apigateway.ApiGatewayRequestEvent)6 ApiResponse (com.formkiq.lambda.apigateway.ApiResponse)6 Test (org.junit.jupiter.api.Test)6 BadException (com.formkiq.lambda.apigateway.exception.BadException)3 DocumentService (com.formkiq.stacks.dynamodb.DocumentService)3 DynamicObject (com.formkiq.stacks.common.objects.DynamicObject)2 S3ObjectMetadata (com.formkiq.aws.s3.S3ObjectMetadata)1 S3Service (com.formkiq.aws.s3.S3Service)1 DocumentTags (com.formkiq.stacks.dynamodb.DocumentTags)1 WebhooksService (com.formkiq.stacks.dynamodb.WebhooksService)1 ZonedDateTime (java.time.ZonedDateTime)1 HashMap (java.util.HashMap)1 S3Client (software.amazon.awssdk.services.s3.S3Client)1 S3Exception (software.amazon.awssdk.services.s3.model.S3Exception)1