Search in sources :

Example 11 with DynamicObject

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

the class StagingS3Create method createContentTypeMap.

/**
 * Generate {@link Map} of DocumentId / Content Type.
 * @param doc {@link DynamicDocumentItem}
 * @return {@link Map}
 */
private Map<String, String> createContentTypeMap(final DynamicDocumentItem doc) {
    Map<String, String> map = new HashMap<>();
    if (doc.hasString("documentId") && doc.getContentType() != null) {
        map.put(doc.getString("documentId"), doc.getContentType());
    }
    List<DynamicObject> documents = doc.getList("documents");
    for (DynamicObject document : documents) {
        if (document.hasString("contentType") && document.hasString("documentId")) {
            map.put(document.getString("documentId"), document.getString("contentType"));
        }
    }
    return map;
}
Also used : DynamicObject(com.formkiq.stacks.common.objects.DynamicObject) HashMap(java.util.HashMap)

Example 12 with DynamicObject

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

the class ApiWebhooksTagsRequestTest method testGetWebhooks02.

/**
 * Get /webhooks/{webhookId}/tags empty.
 *
 * @throws Exception an error has occurred
 */
@SuppressWarnings("unchecked")
@Test
public void testGetWebhooks02() throws Exception {
    // given
    String webhookId = getAwsServices().webhookService().saveWebhook(null, "testwebhook", "joe", null, "true");
    ApiGatewayRequestEvent event = toRequestEvent("/request-post-webhooks-webhookid-tags01.json");
    setPathParameter(event, "webhookId", webhookId);
    // 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("201.0", String.valueOf(m.get("statusCode")));
    assertEquals(getHeaders(), "\"headers\":" + GsonUtil.getInstance().toJson(m.get("headers")));
    assertEquals("{\"message\":\"Created Tag 'category'.\"}", m.get("body"));
    // given
    event = toRequestEvent("/request-get-webhooks-webhookid-tags01.json");
    setPathParameter(event, "webhookId", webhookId);
    // when
    response = handleRequest(event);
    // then
    m = GsonUtil.getInstance().fromJson(response, Map.class);
    assertEquals(mapsize, m.size());
    assertEquals("200.0", String.valueOf(m.get("statusCode")));
    assertEquals(getHeaders(), "\"headers\":" + GsonUtil.getInstance().toJson(m.get("headers")));
    Map<String, Object> body = fromJson(m.get("body"), Map.class);
    DynamicObject obj = new DynamicObject(body);
    List<DynamicObject> list = obj.getList("tags");
    assertEquals(1, list.size());
    assertEquals(webhookId, list.get(0).getString("webhookId"));
    assertEquals("USERDEFINED", list.get(0).getString("type"));
    assertEquals("8a73dfef-26d3-43d8-87aa-b3ec358e43ba@formkiq.com", list.get(0).getString("userId"));
    assertEquals("category", list.get(0).getString("key"));
    assertEquals("job", list.get(0).getString("value"));
    assertNotNull(list.get(0).getString("insertedDate"));
}
Also used : DynamicObject(com.formkiq.stacks.common.objects.DynamicObject) ApiGatewayRequestEvent(com.formkiq.lambda.apigateway.ApiGatewayRequestEvent) DynamicObject(com.formkiq.stacks.common.objects.DynamicObject) Map(java.util.Map) Test(org.junit.jupiter.api.Test)

Example 13 with DynamicObject

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

the class ConfigServiceImpl method get.

@Override
public DynamicObject get(final String siteId) {
    Collection<Map<String, AttributeValue>> keys = new ArrayList<>();
    if (isDefaultSiteId(siteId)) {
        keys.add(keysGeneric(null, PREFIX_CONFIG, DEFAULT_SITE_ID));
    } else {
        keys.add(keysGeneric(null, PREFIX_CONFIG, siteId));
        keys.add(keysGeneric(null, PREFIX_CONFIG, DEFAULT_SITE_ID));
    }
    Map<String, KeysAndAttributes> items = Map.of(this.documentTableName, KeysAndAttributes.builder().keys(keys).build());
    BatchGetItemResponse response = this.dynamoDB.batchGetItem(BatchGetItemRequest.builder().requestItems(items).build());
    AttributeValueToDynamicObject transform = new AttributeValueToDynamicObject();
    Optional<Map<String, AttributeValue>> map = Optional.empty();
    List<Map<String, AttributeValue>> list = response.responses().get(this.documentTableName);
    if (!list.isEmpty()) {
        map = list.stream().filter(s -> s.get(SK).s().equals(siteId)).findFirst();
        if (map.isEmpty()) {
            map = list.stream().filter(s -> s.get(SK).s().equals(DEFAULT_SITE_ID)).findFirst();
        }
    }
    return !map.isEmpty() ? transform.apply(map.get()) : new DynamicObject(Map.of());
}
Also used : DynamicObject(com.formkiq.stacks.common.objects.DynamicObject) BatchGetItemResponse(software.amazon.awssdk.services.dynamodb.model.BatchGetItemResponse) ArrayList(java.util.ArrayList) KeysAndAttributes(software.amazon.awssdk.services.dynamodb.model.KeysAndAttributes) Map(java.util.Map)

Example 14 with DynamicObject

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

the class AttributeValueToDynamicObject method apply.

@Override
public DynamicObject apply(final Map<String, AttributeValue> map) {
    DynamicObject o = new DynamicObject(new HashMap<>());
    for (Map.Entry<String, AttributeValue> e : map.entrySet()) {
        String s = e.getValue().s();
        String n = e.getValue().n();
        String v = s == null && n != null ? n : s;
        o.put(e.getKey(), v);
    }
    return o;
}
Also used : AttributeValue(software.amazon.awssdk.services.dynamodb.model.AttributeValue) DynamicObject(com.formkiq.stacks.common.objects.DynamicObject) Map(java.util.Map) HashMap(java.util.HashMap)

Example 15 with DynamicObject

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

the class ApiDocumentsPatchRequestTest method assert200Response.

/**
 * Asserts 200 response.
 *
 * @param siteId {@link String}
 * @param response {@link String}.
 * @throws IOException IOException
 */
@SuppressWarnings("unchecked")
private void assert200Response(final String siteId, final String response) throws IOException {
    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));
    assertNotNull(resp.get("documentId"));
    if (siteId != null) {
        assertEquals(siteId, resp.get("siteId"));
    }
    assertNull(resp.get("next"));
    assertNull(resp.get("previous"));
    String key = siteId != null ? siteId + "/" + resp.get("documentId") + ".fkb64" : resp.get("documentId") + ".fkb64";
    assertTrue(getLogger().containsString("s3 putObject " + key + " into bucket " + STAGE_BUCKET_NAME));
    assertNotNull(UUID.fromString(resp.getString("documentId")));
}
Also used : DynamicObject(com.formkiq.stacks.common.objects.DynamicObject) HashMap(java.util.HashMap) Map(java.util.Map)

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