Search in sources :

Example 36 with DynamicObject

use of com.formkiq.stacks.common.objects.DynamicObject in project formkiq-core by formkiq.

the class ConfigServiceImplTest method testConfig04.

/**
 * Test Finding Default Config.
 */
@Test
public void testConfig04() {
    // given
    Map<String, Object> map = new HashMap<>();
    map.put(DOCUMENT_TIME_TO_LIVE, "" + UUID.randomUUID().toString());
    map.put(MAX_WEBHOOKS, "" + UUID.randomUUID().toString());
    map.put(MAX_DOCUMENTS, "" + UUID.randomUUID().toString());
    DynamicObject obj = new DynamicObject(map);
    this.service.save(null, obj);
    for (String siteId : Arrays.asList(null, UUID.randomUUID().toString(), DEFAULT_SITE_ID)) {
        // when
        DynamicObject config = this.service.get(siteId);
        // then
        final int count = 5;
        assertEquals(count, config.keySet().size());
        assertEquals("configs#", config.getString("PK"));
        assertEquals("default", config.getString("SK"));
        assertEquals(map.get(DOCUMENT_TIME_TO_LIVE), config.getString(DOCUMENT_TIME_TO_LIVE));
        assertEquals(map.get(MAX_WEBHOOKS), config.getString(MAX_WEBHOOKS));
        assertEquals(map.get(MAX_DOCUMENTS), config.getString(MAX_DOCUMENTS));
    }
}
Also used : DynamicObject(com.formkiq.stacks.common.objects.DynamicObject) HashMap(java.util.HashMap) DynamicObject(com.formkiq.stacks.common.objects.DynamicObject) Test(org.junit.jupiter.api.Test)

Example 37 with DynamicObject

use of com.formkiq.stacks.common.objects.DynamicObject in project formkiq-core by formkiq.

the class ConfigServiceImplTest method testConfig01.

/**
 * Test Finding Config.
 */
@Test
public void testConfig01() {
    // given
    for (String siteId : Arrays.asList(null, UUID.randomUUID().toString(), DEFAULT_SITE_ID)) {
        Map<String, Object> map = new HashMap<>();
        map.put(DOCUMENT_TIME_TO_LIVE, "" + UUID.randomUUID().toString());
        map.put(MAX_WEBHOOKS, "" + UUID.randomUUID().toString());
        map.put(MAX_DOCUMENTS, "" + UUID.randomUUID().toString());
        DynamicObject obj = new DynamicObject(map);
        this.service.save(siteId, obj);
        // when
        DynamicObject config = this.service.get(siteId);
        // then
        final int count = 5;
        assertEquals(count, config.keySet().size());
        assertEquals("configs#", config.getString("PK"));
        if (siteId != null) {
            assertEquals(siteId, config.getString("SK"));
        } else {
            assertEquals("default", config.getString("SK"));
        }
        assertEquals(map.get(DOCUMENT_TIME_TO_LIVE), config.getString(DOCUMENT_TIME_TO_LIVE));
        assertEquals(map.get(MAX_WEBHOOKS), config.getString(MAX_WEBHOOKS));
        assertEquals(map.get(MAX_DOCUMENTS), config.getString(MAX_DOCUMENTS));
    }
}
Also used : DynamicObject(com.formkiq.stacks.common.objects.DynamicObject) HashMap(java.util.HashMap) DynamicObject(com.formkiq.stacks.common.objects.DynamicObject) Test(org.junit.jupiter.api.Test)

Example 38 with DynamicObject

use of com.formkiq.stacks.common.objects.DynamicObject in project formkiq-core by formkiq.

the class DocumentIdRequestHandler method patch.

@Override
public ApiRequestHandlerResponse patch(final LambdaLogger logger, final ApiGatewayRequestEvent event, final ApiAuthorizer authorizer, final AwsServiceCache awsservice) throws Exception {
    boolean isUpdate = event.getHttpMethod().equalsIgnoreCase("patch") && event.getPathParameters().containsKey("documentId");
    String siteId = authorizer.getSiteId();
    String documentId = UUID.randomUUID().toString();
    if (isUpdate) {
        documentId = event.getPathParameters().get("documentId");
        if (awsservice.documentService().findDocument(siteId, documentId) == null) {
            throw new NotFoundException("Document " + documentId + " not found.");
        }
    }
    String maxDocumentCount = null;
    DynamicObject item = fromBodyToDynamicObject(logger, event);
    updateContentType(event, item);
    List<DynamicObject> documents = item.getList("documents");
    if (!isUpdate) {
        if (!item.hasString("content") && item.getList("documents").isEmpty()) {
            throw new BadException("Invalid JSON body.");
        }
        maxDocumentCount = this.restrictionMaxDocuments.getValue(awsservice, siteId);
        if (maxDocumentCount != null && this.restrictionMaxDocuments.enforced(awsservice, siteId, maxDocumentCount)) {
            throw new BadException("Max Number of Documents reached");
        }
    }
    addFieldsToObject(event, awsservice, siteId, documentId, item, documents);
    item.put("documents", documents);
    logger.log("setting userId: " + item.getString("userId") + " contentType: " + item.getString("contentType"));
    putObjectToStaging(logger, awsservice, maxDocumentCount, siteId, item);
    Map<String, String> uploadUrls = generateUploadUrls(awsservice, siteId, documentId, item, documents);
    Map<String, Object> map = buildResponse(siteId, documentId, documents, uploadUrls);
    ApiResponseStatus status = isUpdate ? SC_OK : SC_CREATED;
    return new ApiRequestHandlerResponse(status, new ApiMapResponse(map));
}
Also used : DynamicObject(com.formkiq.stacks.common.objects.DynamicObject) NotFoundException(com.formkiq.lambda.apigateway.exception.NotFoundException) DynamicObject(com.formkiq.stacks.common.objects.DynamicObject) ApiResponseStatus(com.formkiq.lambda.apigateway.ApiResponseStatus) ApiRequestHandlerResponse(com.formkiq.lambda.apigateway.ApiRequestHandlerResponse) BadException(com.formkiq.lambda.apigateway.exception.BadException) ApiMapResponse(com.formkiq.lambda.apigateway.ApiMapResponse)

Example 39 with DynamicObject

use of com.formkiq.stacks.common.objects.DynamicObject in project formkiq-core by formkiq.

the class PublicWebhooksRequestHandler method buildDynamicObject.

private DynamicObject buildDynamicObject(final AwsServiceCache awsservice, final String siteId, final String webhookId, final DynamicObject hook, final String body, final String contentType) {
    DynamicObject item = new DynamicObject(new HashMap<>());
    final String documentId = UUID.randomUUID().toString();
    if (contentType != null) {
        item.put("contentType", contentType);
    }
    item.put("content", body);
    item.put("documentId", documentId);
    item.put("userId", "webhook/" + hook.getOrDefault("path", "webhook"));
    item.put("path", "webhooks/" + webhookId);
    if (hook.containsKey("TimeToLive")) {
        item.put("TimeToLive", hook.get("TimeToLive"));
    } else {
        String ttl = awsservice.configService().get(siteId).getString(DOCUMENT_TIME_TO_LIVE);
        if (ttl != null) {
            item.put("TimeToLive", ttl);
        }
    }
    if (siteId != null) {
        item.put("siteId", siteId);
    }
    return item;
}
Also used : DynamicObject(com.formkiq.stacks.common.objects.DynamicObject)

Example 40 with DynamicObject

use of com.formkiq.stacks.common.objects.DynamicObject in project formkiq-core by formkiq.

the class SitesRequestHandler method get.

@Override
public ApiRequestHandlerResponse get(final LambdaLogger logger, final ApiGatewayRequestEvent event, final ApiAuthorizer authorizer, final AwsServiceCache awsservice) throws Exception {
    List<DynamicObject> sites = authorizer.getSiteIds().stream().map(siteId -> {
        DynamicObject config = awsservice.config(siteId);
        config.put("siteId", siteId != null ? siteId : DEFAULT_SITE_ID);
        return config;
    }).collect(Collectors.toList());
    sites.forEach(ob -> {
        ob.remove("PK");
        ob.remove("SK");
    });
    updateUploadEmail(logger, awsservice, authorizer, sites);
    return new ApiRequestHandlerResponse(SC_OK, new ApiMapResponse(Map.of("sites", sites)));
}
Also used : DynamicObject(com.formkiq.stacks.common.objects.DynamicObject) ApiGatewayRequestHandler(com.formkiq.lambda.apigateway.ApiGatewayRequestHandler) DEFAULT_SITE_ID(com.formkiq.stacks.dynamodb.SiteIdKeyGenerator.DEFAULT_SITE_ID) ApiMapResponse(com.formkiq.lambda.apigateway.ApiMapResponse) Random(java.util.Random) AwsServiceCache(com.formkiq.lambda.apigateway.AwsServiceCache) ApiAuthorizer(com.formkiq.lambda.apigateway.ApiAuthorizer) Collectors(java.util.stream.Collectors) List(java.util.List) ApiGatewayRequestEventUtil(com.formkiq.lambda.apigateway.ApiGatewayRequestEventUtil) LambdaLogger(com.amazonaws.services.lambda.runtime.LambdaLogger) Map(java.util.Map) ApiGatewayRequestEvent(com.formkiq.lambda.apigateway.ApiGatewayRequestEvent) ApiRequestHandlerResponse(com.formkiq.lambda.apigateway.ApiRequestHandlerResponse) SC_OK(com.formkiq.lambda.apigateway.ApiResponseStatus.SC_OK) DynamicObject(com.formkiq.stacks.common.objects.DynamicObject) ApiRequestHandlerResponse(com.formkiq.lambda.apigateway.ApiRequestHandlerResponse) ApiMapResponse(com.formkiq.lambda.apigateway.ApiMapResponse)

Aggregations

DynamicObject (com.formkiq.stacks.common.objects.DynamicObject)93 Test (org.junit.jupiter.api.Test)55 Map (java.util.Map)46 ApiGatewayRequestEvent (com.formkiq.lambda.apigateway.ApiGatewayRequestEvent)39 Date (java.util.Date)26 HashMap (java.util.HashMap)17 DocumentItemDynamoDb (com.formkiq.stacks.dynamodb.DocumentItemDynamoDb)12 DocumentTag (com.formkiq.stacks.dynamodb.DocumentTag)10 ZonedDateTime (java.time.ZonedDateTime)10 ApiRequestHandlerResponse (com.formkiq.lambda.apigateway.ApiRequestHandlerResponse)8 List (java.util.List)8 AttributeValue (software.amazon.awssdk.services.dynamodb.model.AttributeValue)7 S3Client (software.amazon.awssdk.services.s3.S3Client)7 ApiMapResponse (com.formkiq.lambda.apigateway.ApiMapResponse)6 BadException (com.formkiq.lambda.apigateway.exception.BadException)6 Collectors (java.util.stream.Collectors)6 NotFoundException (com.formkiq.lambda.apigateway.exception.NotFoundException)5 FormKiqClientV1 (com.formkiq.stacks.client.FormKiqClientV1)5 LocalDate (java.time.LocalDate)5 Test (org.junit.Test)5