Search in sources :

Example 31 with DynamicObject

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

the class ApiDocumentsSitesRequestTest method testHandleGetSites03.

/**
 * Get /sites with 'read' permissions SES support.
 *
 * @throws Exception an error has occurred
 */
@SuppressWarnings("unchecked")
@Test
public void testHandleGetSites03() throws Exception {
    // given
    putSsmParameter("/formkiq/" + FORMKIQ_APP_ENVIRONMENT + "/maildomain", "tryformkiq.com");
    removeSsmParameter(String.format("/formkiq/%s/siteid/%s/email", FORMKIQ_APP_ENVIRONMENT, "default"));
    ApiGatewayRequestEvent event = toRequestEvent("/request-get-sites01.json");
    setCognitoGroup(event, "default_read finance");
    // 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")));
    DynamicObject resp = new DynamicObject(fromJson(m.get("body"), Map.class));
    assertNull(resp.get("next"));
    assertNull(resp.get("previous"));
    List<DynamicObject> sites = resp.getList("sites");
    assertEquals(2, sites.size());
    assertEquals(DEFAULT_SITE_ID, sites.get(0).get("siteId"));
    assertNull(sites.get(0).get("uploadEmail"));
    assertNull(getSsmParameter(String.format("/formkiq/%s/siteid/%s/email", FORMKIQ_APP_ENVIRONMENT, sites.get(0).get("siteId"))));
    assertEquals("finance", sites.get(1).get("siteId"));
    assertNotNull(sites.get(1).get("uploadEmail"));
    String uploadEmail = sites.get(1).getString("uploadEmail");
    assertTrue(uploadEmail.endsWith("@tryformkiq.com"));
    assertTrue(Pattern.matches(EMAIL, uploadEmail.subSequence(0, uploadEmail.indexOf("@"))));
    assertNotNull(getSsmParameter(String.format("/formkiq/%s/siteid/%s/email", FORMKIQ_APP_ENVIRONMENT, sites.get(1).get("siteId"))));
    String[] strs = uploadEmail.split("@");
    assertEquals("{\"siteId\":\"finance\", \"appEnvironment\":\"" + FORMKIQ_APP_ENVIRONMENT + "\"}", getSsmParameter(String.format("/formkiq/ses/%s/%s", strs[1], strs[0])));
}
Also used : DynamicObject(com.formkiq.stacks.common.objects.DynamicObject) ApiGatewayRequestEvent(com.formkiq.lambda.apigateway.ApiGatewayRequestEvent) Map(java.util.Map) Test(org.junit.jupiter.api.Test)

Example 32 with DynamicObject

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

the class ApiDocumentsSitesRequestTest method testHandleGetSites01.

/**
 * Get /sites with SES support.
 *
 * @throws Exception an error has occurred
 */
@SuppressWarnings("unchecked")
@Test
public void testHandleGetSites01() throws Exception {
    // given
    putSsmParameter("/formkiq/" + FORMKIQ_APP_ENVIRONMENT + "/maildomain", "tryformkiq.com");
    ApiGatewayRequestEvent event = toRequestEvent("/request-get-sites01.json");
    // 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")));
    DynamicObject resp = new DynamicObject(fromJson(m.get("body"), Map.class));
    assertNull(resp.getString("next"));
    assertNull(resp.getString("previous"));
    List<DynamicObject> sites = resp.getList("sites");
    assertEquals(2, sites.size());
    assertEquals(DEFAULT_SITE_ID, sites.get(0).get("siteId"));
    assertNotNull(sites.get(0).get("uploadEmail"));
    String uploadEmail = sites.get(0).getString("uploadEmail");
    assertTrue(uploadEmail.endsWith("@tryformkiq.com"));
    assertTrue(Pattern.matches(EMAIL, uploadEmail.subSequence(0, uploadEmail.indexOf("@"))));
    assertNotNull(getSsmParameter(String.format("/formkiq/%s/siteid/%s/email", FORMKIQ_APP_ENVIRONMENT, sites.get(0).get("siteId"))));
    String[] strs = uploadEmail.split("@");
    assertEquals("tryformkiq.com", strs[1]);
    assertEquals("{\"siteId\":\"default\", \"appEnvironment\":\"" + FORMKIQ_APP_ENVIRONMENT + "\"}", getSsmParameter(String.format("/formkiq/ses/%s/%s", strs[1], strs[0])));
    assertEquals("finance", sites.get(1).get("siteId"));
    assertNotNull(sites.get(1).get("uploadEmail"));
    uploadEmail = sites.get(1).getString("uploadEmail");
    assertTrue(uploadEmail.endsWith("@tryformkiq.com"));
    assertTrue(Pattern.matches(EMAIL, uploadEmail.subSequence(0, uploadEmail.indexOf("@"))));
    assertNotNull(getSsmParameter(String.format("/formkiq/%s/siteid/%s/email", FORMKIQ_APP_ENVIRONMENT, sites.get(1).get("siteId"))));
    strs = uploadEmail.split("@");
    assertEquals("{\"siteId\":\"finance\", \"appEnvironment\":\"" + FORMKIQ_APP_ENVIRONMENT + "\"}", getSsmParameter(String.format("/formkiq/ses/%s/%s", strs[1], strs[0])));
}
Also used : DynamicObject(com.formkiq.stacks.common.objects.DynamicObject) ApiGatewayRequestEvent(com.formkiq.lambda.apigateway.ApiGatewayRequestEvent) Map(java.util.Map) Test(org.junit.jupiter.api.Test)

Example 33 with DynamicObject

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

the class WebhooksServiceImpl method findWebhooks.

@Override
public List<DynamicObject> findWebhooks(final String siteId) {
    Map<String, AttributeValue> key = queryKeys(keysGeneric(siteId, PREFIX_WEBHOOKS, null));
    String expr = GSI1_PK + " = :pk";
    QueryRequest q = QueryRequest.builder().tableName(this.documentTableName).indexName(GSI1).keyConditionExpression(expr).expressionAttributeValues(key).build();
    QueryResponse result = this.dynamoDB.query(q);
    Collection<? extends Map<String, AttributeValue>> keys = result.items().stream().map(i -> Map.of(PK, i.get(PK), SK, i.get(SK))).collect(Collectors.toList());
    List<DynamicObject> retlist = Collections.emptyList();
    if (!keys.isEmpty()) {
        Map<String, KeysAndAttributes> items = Map.of(this.documentTableName, KeysAndAttributes.builder().keys(keys).build());
        BatchGetItemResponse batch = this.dynamoDB.batchGetItem(BatchGetItemRequest.builder().requestItems(items).build());
        Map<String, List<Map<String, AttributeValue>>> responses = batch.responses();
        List<Map<String, AttributeValue>> list = responses.get(this.documentTableName);
        AttributeValueToDynamicObject transform = new AttributeValueToDynamicObject();
        retlist = list.stream().map(m -> transform.apply(m)).collect(Collectors.toList());
        retlist.forEach(ob -> updateWebhookTimeToLive(ob));
    }
    return retlist;
}
Also used : BatchGetItemRequest(software.amazon.awssdk.services.dynamodb.model.BatchGetItemRequest) KeysAndAttributes(software.amazon.awssdk.services.dynamodb.model.KeysAndAttributes) TransactWriteItem(software.amazon.awssdk.services.dynamodb.model.TransactWriteItem) Date(java.util.Date) GetItemRequest(software.amazon.awssdk.services.dynamodb.model.GetItemRequest) UpdateItemRequest(software.amazon.awssdk.services.dynamodb.model.UpdateItemRequest) SimpleDateFormat(java.text.SimpleDateFormat) HashMap(java.util.HashMap) SiteIdKeyGenerator.createDatabaseKey(com.formkiq.stacks.dynamodb.SiteIdKeyGenerator.createDatabaseKey) Map(java.util.Map) DynamoDbClient(software.amazon.awssdk.services.dynamodb.DynamoDbClient) TransactWriteItemsRequest(software.amazon.awssdk.services.dynamodb.model.TransactWriteItemsRequest) DynamicObject(com.formkiq.stacks.common.objects.DynamicObject) AttributeValueUpdate(software.amazon.awssdk.services.dynamodb.model.AttributeValueUpdate) Collection(java.util.Collection) QueryResponse(software.amazon.awssdk.services.dynamodb.model.QueryResponse) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) Put(software.amazon.awssdk.services.dynamodb.model.Put) List(java.util.List) PutItemRequest(software.amazon.awssdk.services.dynamodb.model.PutItemRequest) BatchGetItemResponse(software.amazon.awssdk.services.dynamodb.model.BatchGetItemResponse) AttributeValue(software.amazon.awssdk.services.dynamodb.model.AttributeValue) GetItemResponse(software.amazon.awssdk.services.dynamodb.model.GetItemResponse) DeleteItemRequest(software.amazon.awssdk.services.dynamodb.model.DeleteItemRequest) Collections(java.util.Collections) QueryRequest(software.amazon.awssdk.services.dynamodb.model.QueryRequest) AttributeValue(software.amazon.awssdk.services.dynamodb.model.AttributeValue) QueryRequest(software.amazon.awssdk.services.dynamodb.model.QueryRequest) KeysAndAttributes(software.amazon.awssdk.services.dynamodb.model.KeysAndAttributes) DynamicObject(com.formkiq.stacks.common.objects.DynamicObject) QueryResponse(software.amazon.awssdk.services.dynamodb.model.QueryResponse) BatchGetItemResponse(software.amazon.awssdk.services.dynamodb.model.BatchGetItemResponse) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

Example 34 with DynamicObject

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

the class ConfigServiceImplTest method testConfig02.

/**
 * Test Finding missing Config.
 */
@Test
public void testConfig02() {
    // given
    for (String siteId : Arrays.asList(null, UUID.randomUUID().toString(), DEFAULT_SITE_ID)) {
        // when
        DynamicObject config = this.service.get(siteId);
        // then
        assertEquals(0, config.size());
    }
}
Also used : DynamicObject(com.formkiq.stacks.common.objects.DynamicObject) Test(org.junit.jupiter.api.Test)

Example 35 with DynamicObject

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

the class ConfigServiceImplTest method testDelete01.

/**
 * Test Delete Config.
 */
@Test
public void testDelete01() {
    // 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 (isDefaultSiteId(siteId)) {
            assertEquals("default", config.getString("SK"));
        } else {
            assertEquals(siteId, 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));
        this.service.delete(siteId);
        assertEquals(0, this.service.get(siteId).size());
    }
}
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)

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