Search in sources :

Example 21 with DocumentTag

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));
    }
}
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 22 with DocumentTag

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

Example 23 with DocumentTag

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);
}
Also used : DocumentTag(com.formkiq.stacks.dynamodb.DocumentTag) ApiDocumentTagItemResponse(com.formkiq.stacks.api.ApiDocumentTagItemResponse) NotFoundException(com.formkiq.lambda.apigateway.exception.NotFoundException) ApiRequestHandlerResponse(com.formkiq.lambda.apigateway.ApiRequestHandlerResponse)

Example 24 with DocumentTag

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);
}
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)

Example 25 with DocumentTag

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);
}
Also used : SC_CREATED(com.formkiq.lambda.apigateway.ApiResponseStatus.SC_CREATED) Arrays(java.util.Arrays) DocumentTags(com.formkiq.stacks.dynamodb.DocumentTags) Date(java.util.Date) AwsServiceCache(com.formkiq.lambda.apigateway.AwsServiceCache) ApiAuthorizer(com.formkiq.lambda.apigateway.ApiAuthorizer) ApiResponse(com.formkiq.lambda.apigateway.ApiResponse) ApiGatewayRequestEventUtil(com.formkiq.lambda.apigateway.ApiGatewayRequestEventUtil) LambdaLogger(com.amazonaws.services.lambda.runtime.LambdaLogger) DocumentTag(com.formkiq.stacks.dynamodb.DocumentTag) SC_OK(com.formkiq.lambda.apigateway.ApiResponseStatus.SC_OK) PaginationMapToken(com.formkiq.stacks.dynamodb.PaginationMapToken) ApiDocumentTagsItemResponse(com.formkiq.stacks.api.ApiDocumentTagsItemResponse) ApiGatewayRequestHandler(com.formkiq.lambda.apigateway.ApiGatewayRequestHandler) ApiPagination(com.formkiq.lambda.apigateway.ApiPagination) ApiDocumentTagItemResponse(com.formkiq.stacks.api.ApiDocumentTagItemResponse) Collectors(java.util.stream.Collectors) BadException(com.formkiq.lambda.apigateway.exception.BadException) List(java.util.List) ApiGatewayRequestEvent(com.formkiq.lambda.apigateway.ApiGatewayRequestEvent) ApiRequestHandlerResponse(com.formkiq.lambda.apigateway.ApiRequestHandlerResponse) CacheService(com.formkiq.stacks.dynamodb.CacheService) ApiMessageResponse(com.formkiq.lambda.apigateway.ApiMessageResponse) Collections(java.util.Collections) DocumentTagType(com.formkiq.stacks.dynamodb.DocumentTagType) PaginationResults(com.formkiq.stacks.dynamodb.PaginationResults) ApiPagination(com.formkiq.lambda.apigateway.ApiPagination) DocumentTag(com.formkiq.stacks.dynamodb.DocumentTag) ApiDocumentTagItemResponse(com.formkiq.stacks.api.ApiDocumentTagItemResponse) ApiRequestHandlerResponse(com.formkiq.lambda.apigateway.ApiRequestHandlerResponse) PaginationMapToken(com.formkiq.stacks.dynamodb.PaginationMapToken) ApiDocumentTagsItemResponse(com.formkiq.stacks.api.ApiDocumentTagsItemResponse) CacheService(com.formkiq.stacks.dynamodb.CacheService)

Aggregations

DocumentTag (com.formkiq.stacks.dynamodb.DocumentTag)46 Date (java.util.Date)34 Test (org.junit.jupiter.api.Test)34 ApiGatewayRequestEvent (com.formkiq.lambda.apigateway.ApiGatewayRequestEvent)31 DocumentItemDynamoDb (com.formkiq.stacks.dynamodb.DocumentItemDynamoDb)13 ApiMessageResponse (com.formkiq.lambda.apigateway.ApiMessageResponse)11 DynamicObject (com.formkiq.stacks.common.objects.DynamicObject)10 DocumentItem (com.formkiq.stacks.dynamodb.DocumentItem)10 ApiRequestHandlerResponse (com.formkiq.lambda.apigateway.ApiRequestHandlerResponse)9 DynamicDocumentItem (com.formkiq.stacks.dynamodb.DynamicDocumentItem)9 BadException (com.formkiq.lambda.apigateway.exception.BadException)7 Map (java.util.Map)7 ApiResponse (com.formkiq.lambda.apigateway.ApiResponse)5 NotFoundException (com.formkiq.lambda.apigateway.exception.NotFoundException)5 DynamicDocumentTag (com.formkiq.stacks.dynamodb.DynamicDocumentTag)5 Timeout (org.junit.jupiter.api.Timeout)5 DocumentService (com.formkiq.stacks.dynamodb.DocumentService)4 S3Client (software.amazon.awssdk.services.s3.S3Client)4 LambdaLogger (com.amazonaws.services.lambda.runtime.LambdaLogger)3 DocumentSearchQuery (com.formkiq.stacks.client.models.DocumentSearchQuery)3