Search in sources :

Example 6 with DynamicObject

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

the class ApiWebhooksRequestTest method testPostWebhooks05.

/**
 * POST /webhooks with Config WEBHOOK_TIME_TO_LIVE.
 *
 * @throws Exception an error has occurred
 */
@SuppressWarnings("unchecked")
@Test
public void testPostWebhooks05() throws Exception {
    // given
    putSsmParameter("/formkiq/" + FORMKIQ_APP_ENVIRONMENT + "/api/DocumentsPublicHttpUrl", "http://localhost:8080");
    ApiGatewayRequestEvent eventPost = toRequestEvent("/request-post-webhooks01.json");
    eventPost.setBody("{\"name\":\"john smith\"}");
    ApiGatewayRequestEvent event = toRequestEvent("/request-get-webhooks01.json");
    String siteId = null;
    String ttl = "87400";
    getAwsServices().configService().save(siteId, new DynamicObject(Map.of(WEBHOOK_TIME_TO_LIVE, ttl)));
    // when
    String responsePost = handleRequest(eventPost);
    final String response = handleRequest(event);
    // then
    Map<String, String> m = GsonUtil.getInstance().fromJson(responsePost, Map.class);
    assertEquals("200.0", String.valueOf(m.get("statusCode")));
    Map<String, Object> result = GsonUtil.getInstance().fromJson(m.get("body"), Map.class);
    final String id = verifyPostWebhooks05(result);
    m = GsonUtil.getInstance().fromJson(response, Map.class);
    assertEquals("200.0", String.valueOf(m.get("statusCode")));
    result = GsonUtil.getInstance().fromJson(m.get("body"), Map.class);
    List<Map<String, Object>> list = (List<Map<String, Object>>) result.get("webhooks");
    assertEquals(1, list.size());
    verifyPostWebhooks05(list.get(0));
    WebhooksService webhookService = getAwsServices().webhookService();
    DynamicObject obj = webhookService.findWebhook(null, id);
    long epoch = Long.parseLong(obj.getString("TimeToLive"));
    ZonedDateTime ld = Instant.ofEpochMilli(epoch * TO_MILLIS).atZone(ZoneOffset.UTC);
    ZonedDateTime now = ZonedDateTime.now(ZoneOffset.UTC).plusDays(1);
    assertEquals(now.getDayOfMonth(), ld.getDayOfMonth());
    // given
    ttl = "-87400";
    getAwsServices().configService().save(siteId, new DynamicObject(Map.of(WEBHOOK_TIME_TO_LIVE, ttl)));
    // when
    responsePost = handleRequest(eventPost);
    // then
    m = GsonUtil.getInstance().fromJson(responsePost, Map.class);
    assertEquals("200.0", String.valueOf(m.get("statusCode")));
    result = GsonUtil.getInstance().fromJson(m.get("body"), Map.class);
    assertEquals("false", result.get("enabled"));
    assertNotNull(result.get("ttl"));
}
Also used : DynamicObject(com.formkiq.stacks.common.objects.DynamicObject) ZonedDateTime(java.time.ZonedDateTime) ApiGatewayRequestEvent(com.formkiq.lambda.apigateway.ApiGatewayRequestEvent) DynamicObject(com.formkiq.stacks.common.objects.DynamicObject) List(java.util.List) Map(java.util.Map) WebhooksService(com.formkiq.stacks.dynamodb.WebhooksService) Test(org.junit.jupiter.api.Test)

Example 7 with DynamicObject

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

the class ApiWebhooksRequestTest method testPostWebhooks02.

/**
 * POST /webhooks with TAGS.
 * Test TTL.
 *
 * @throws Exception an error has occurred
 */
@SuppressWarnings("unchecked")
@Test
public void testPostWebhooks02() throws Exception {
    // given
    putSsmParameter("/formkiq/" + FORMKIQ_APP_ENVIRONMENT + "/api/DocumentsPublicHttpUrl", "http://localhost:8080");
    for (String siteId : Arrays.asList(null, UUID.randomUUID().toString())) {
        ApiGatewayRequestEvent event = toRequestEvent("/request-post-webhooks01.json");
        addParameter(event, "siteId", siteId);
        event.setBody("{\"name\":\"joe smith\",\"tags\":" + "[{\"key\":\"category\",\"value\":\"person\"},{\"key\":\"day\",\"value\":\"today\"}]}");
        // when
        String response = handleRequest(event);
        // then
        Map<String, String> m = GsonUtil.getInstance().fromJson(response, Map.class);
        assertEquals("200.0", String.valueOf(m.get("statusCode")));
        Map<String, Object> result = GsonUtil.getInstance().fromJson(m.get("body"), Map.class);
        if (siteId != null) {
            assertEquals(siteId, result.get("siteId"));
        } else {
            assertEquals("default", result.get("siteId"));
        }
        assertNotNull(result.get("id"));
        final String id = result.get("id").toString();
        assertNotNull(result.get("insertedDate"));
        assertEquals("joe smith", result.get("name"));
        assertEquals("test@formkiq.com", result.get("userId"));
        verifyUrl(siteId, id, result);
        WebhooksService webhookService = getAwsServices().webhookService();
        DynamicObject obj = webhookService.findWebhook(siteId, id);
        assertEquals("joe smith", obj.getString("path"));
        assertEquals("test@formkiq.com", obj.getString("userId"));
        PaginationResults<DynamicObject> tags = webhookService.findTags(siteId, id, null);
        assertEquals(2, tags.getResults().size());
        DynamicObject tag = tags.getResults().get(0);
        assertEquals(id, tag.getString("documentId"));
        assertEquals("USERDEFINED", tag.getString("type"));
        assertEquals("test@formkiq.com", tag.getString("userId"));
        assertEquals("category", tag.getString("tagKey"));
        assertEquals("person", tag.getString("tagValue"));
        assertNotNull(tag.getString("inserteddate"));
        tag = tags.getResults().get(1);
        assertEquals(id, tag.getString("documentId"));
        assertEquals("USERDEFINED", tag.getString("type"));
        assertEquals("test@formkiq.com", tag.getString("userId"));
        assertEquals("day", tag.getString("tagKey"));
        assertEquals("today", tag.getString("tagValue"));
        assertNotNull(tag.getString("inserteddate"));
    }
}
Also used : DynamicObject(com.formkiq.stacks.common.objects.DynamicObject) ApiGatewayRequestEvent(com.formkiq.lambda.apigateway.ApiGatewayRequestEvent) DynamicObject(com.formkiq.stacks.common.objects.DynamicObject) WebhooksService(com.formkiq.stacks.dynamodb.WebhooksService) Test(org.junit.jupiter.api.Test)

Example 8 with DynamicObject

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

the class DocumentsRequestTest method testPost06.

/**
 * Post /documents, MAX DocumentGreater than allowed.
 *
 * @throws Exception Exception
 */
@Test(timeout = TEST_TIMEOUT)
public void testPost06() throws Exception {
    // given
    getConfigService().save(SITEID1, new DynamicObject(Map.of(MAX_DOCUMENTS, "1")));
    AddDocument post = new AddDocument();
    post.content("dummy data", StandardCharsets.UTF_8);
    post.contentType("application/pdf");
    AddDocumentRequest req = new AddDocumentRequest().document(post).siteId(SITEID1);
    FormKiqClientV1 c = getFormKiqClients().get(0);
    HttpResponse<String> response = c.addDocumentAsHttpResponse(req);
    assertEquals(STATUS_CREATED, response.statusCode());
    for (FormKiqClientV1 client : getFormKiqClients()) {
        // when
        response = client.addDocumentAsHttpResponse(req);
        // then
        assertEquals(STATUS_BAD_REQUEST, response.statusCode());
        assertEquals("{\"message\":\"Max Number of Documents reached\"}", response.body());
        assertRequestCorsHeaders(response.headers());
    }
}
Also used : DynamicObject(com.formkiq.stacks.common.objects.DynamicObject) AddDocument(com.formkiq.stacks.client.models.AddDocument) AddDocumentRequest(com.formkiq.stacks.client.requests.AddDocumentRequest) FormKiqClientV1(com.formkiq.stacks.client.FormKiqClientV1) Test(org.junit.Test)

Example 9 with DynamicObject

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

the class DocumentsUploadRequestTest method testGet04.

/**
 * Get Request Upload Document Url, Content Length Greater than allowed.
 *
 * @throws Exception Exception
 */
@Test(timeout = TEST_TIMEOUT)
public void testGet04() throws Exception {
    // given
    final int contentLength = 5;
    getConfigService().save(SITEID0, new DynamicObject(Map.of(MAX_DOCUMENT_SIZE_BYTES, "" + contentLength)));
    GetDocumentUploadRequest request = new GetDocumentUploadRequest().siteId(SITEID0).contentLength(contentLength);
    for (FormKiqClientV1 client : getFormKiqClients()) {
        // when
        HttpResponse<String> response = client.getDocumentUploadAsHttpResponse(request);
        // then
        assertEquals(STATUS_OK, response.statusCode());
        assertRequestCorsHeaders(response.headers());
    }
}
Also used : DynamicObject(com.formkiq.stacks.common.objects.DynamicObject) GetDocumentUploadRequest(com.formkiq.stacks.client.requests.GetDocumentUploadRequest) FormKiqClientV1(com.formkiq.stacks.client.FormKiqClientV1) Test(org.junit.Test)

Example 10 with DynamicObject

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

the class DocumentsUploadRequestTest method testGet02.

/**
 * Get Request Upload Document Url, Content Length missing.
 *
 * @throws Exception Exception
 */
@Test(timeout = TEST_TIMEOUT)
public void testGet02() throws Exception {
    // given
    getConfigService().save(SITEID0, new DynamicObject(Map.of(MAX_DOCUMENT_SIZE_BYTES, "5")));
    for (FormKiqClientV1 client : getFormKiqClients()) {
        GetDocumentUploadRequest request = new GetDocumentUploadRequest().siteId(SITEID0);
        // when
        HttpResponse<String> response = client.getDocumentUploadAsHttpResponse(request);
        // then
        assertEquals(STATUS_BAD_REQUEST, response.statusCode());
        assertRequestCorsHeaders(response.headers());
        assertEquals("{\"message\":\"'contentLength' is required\"}", response.body());
    }
}
Also used : DynamicObject(com.formkiq.stacks.common.objects.DynamicObject) GetDocumentUploadRequest(com.formkiq.stacks.client.requests.GetDocumentUploadRequest) FormKiqClientV1(com.formkiq.stacks.client.FormKiqClientV1) Test(org.junit.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