Search in sources :

Example 16 with DocumentItemDynamoDb

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());
    }
}
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 17 with DocumentItemDynamoDb

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());
    }
}
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 18 with DocumentItemDynamoDb

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));
}
Also used : DocumentTag(com.formkiq.stacks.dynamodb.DocumentTag) ApiUrlResponse(com.formkiq.stacks.api.ApiUrlResponse) ArrayList(java.util.ArrayList) BadException(com.formkiq.lambda.apigateway.exception.BadException) Date(java.util.Date) DocumentService(com.formkiq.stacks.dynamodb.DocumentService) DocumentItem(com.formkiq.stacks.dynamodb.DocumentItem) ApiRequestHandlerResponse(com.formkiq.lambda.apigateway.ApiRequestHandlerResponse) DocumentItemDynamoDb(com.formkiq.stacks.dynamodb.DocumentItemDynamoDb)

Example 19 with DocumentItemDynamoDb

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());
    }
}
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 20 with DocumentItemDynamoDb

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

Aggregations

DocumentItemDynamoDb (com.formkiq.stacks.dynamodb.DocumentItemDynamoDb)32 Date (java.util.Date)32 ApiGatewayRequestEvent (com.formkiq.lambda.apigateway.ApiGatewayRequestEvent)28 Test (org.junit.jupiter.api.Test)27 DocumentTag (com.formkiq.stacks.dynamodb.DocumentTag)13 DynamicObject (com.formkiq.stacks.common.objects.DynamicObject)12 Map (java.util.Map)12 LocalDate (java.time.LocalDate)7 DocumentItem (com.formkiq.stacks.dynamodb.DocumentItem)6 DynamicDocumentItem (com.formkiq.stacks.dynamodb.DynamicDocumentItem)4 ArrayList (java.util.ArrayList)4 DocumentSearchQuery (com.formkiq.stacks.client.models.DocumentSearchQuery)3 DocumentItemToDynamicDocumentItem (com.formkiq.stacks.dynamodb.DocumentItemToDynamicDocumentItem)3 ApiRequestHandlerResponse (com.formkiq.lambda.apigateway.ApiRequestHandlerResponse)2 BadException (com.formkiq.lambda.apigateway.exception.BadException)2 ApiUrlResponse (com.formkiq.stacks.api.ApiUrlResponse)2 DocumentSearch (com.formkiq.stacks.client.models.DocumentSearch)2 DocumentSearchTag (com.formkiq.stacks.client.models.DocumentSearchTag)2 DocumentService (com.formkiq.stacks.dynamodb.DocumentService)2 S3Client (software.amazon.awssdk.services.s3.S3Client)2