Search in sources :

Example 6 with DocumentTag

use of com.formkiq.stacks.dynamodb.DocumentTag in project formkiq-core by formkiq.

the class ApiDocumentsTagsRequestTest method testHandlePutTags05.

/**
 * PUT /documents/{documentId}/tags/{tagKey} change VALUE to VALUES request.
 *
 * @throws Exception an error has occurred
 */
@Test
public void testHandlePutTags05() 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", "nope", new Date(), userId);
        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);
        event.setBody("ewogICJ2YWx1ZXMiOiBbImFiYyIsICJ4eXoiXQp9");
        // 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());
        assertNull(tags.getResults().get(0).getValue());
        assertEquals("[abc, xyz]", tags.getResults().get(0).getValues().toString());
        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 7 with DocumentTag

use of com.formkiq.stacks.dynamodb.DocumentTag in project formkiq-core by formkiq.

the class ApiDocumentsTagsRequestTest method testHandleGetDocumentTags02.

/**
 * GET /documents/{documentId}/tags request limit 1.
 *
 * @throws Exception an error has occurred
 */
@SuppressWarnings("unchecked")
@Test
public void testHandleGetDocumentTags02() throws Exception {
    for (String siteId : Arrays.asList(null, UUID.randomUUID().toString())) {
        // given
        Date now = new Date();
        String userId = "jsmith";
        String documentId = UUID.randomUUID().toString();
        DocumentTag item0 = new DocumentTag(documentId, "category0", "person", now, userId);
        DocumentTag item1 = new DocumentTag(documentId, "category1", "thing", now, userId);
        getDocumentService().addTags(siteId, documentId, Arrays.asList(item0, item1), null);
        ApiGatewayRequestEvent event = toRequestEvent("/request-get-documents-documentid-tags02.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")));
        ApiDocumentTagsItemResponse resp = GsonUtil.getInstance().fromJson(m.get("body"), ApiDocumentTagsItemResponse.class);
        assertEquals(1, resp.getTags().size());
        assertNotNull(resp.getNext());
        assertNull(resp.getPrevious());
        assertNull(resp.getTags().get(0).getDocumentId());
        assertNotNull(resp.getTags().get(0).getInsertedDate());
        assertEquals("category0", resp.getTags().get(0).getKey());
        assertEquals("userdefined", resp.getTags().get(0).getType());
        assertEquals("jsmith", resp.getTags().get(0).getUserId());
        assertEquals("person", resp.getTags().get(0).getValue());
    }
}
Also used : DocumentTag(com.formkiq.stacks.dynamodb.DocumentTag) ApiGatewayRequestEvent(com.formkiq.lambda.apigateway.ApiGatewayRequestEvent) Date(java.util.Date) Test(org.junit.jupiter.api.Test)

Example 8 with DocumentTag

use of com.formkiq.stacks.dynamodb.DocumentTag in project formkiq-core by formkiq.

the class ApiDocumentsTagsRequestTest method testHandleGetDocumentTags03.

/**
 * GET /documents/{documentId}/tags values.
 *
 * @throws Exception an error has occurred
 */
@SuppressWarnings("unchecked")
@Test
public void testHandleGetDocumentTags03() throws Exception {
    for (String siteId : Arrays.asList(null, UUID.randomUUID().toString())) {
        // given
        Date now = new Date();
        String userId = "jsmith";
        String documentId = UUID.randomUUID().toString();
        DocumentTag item0 = new DocumentTag(documentId, "category0", null, now, userId);
        item0.setValues(Arrays.asList("abc", "xyz"));
        DocumentTag item1 = new DocumentTag(documentId, "category1", null, now, userId);
        item1.setValues(Arrays.asList("bbb", "ccc"));
        getDocumentService().addTags(siteId, documentId, Arrays.asList(item0, item1), null);
        ApiGatewayRequestEvent event = toRequestEvent("/request-get-documents-documentid-tags02.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")));
        ApiDocumentTagsItemResponse resp = GsonUtil.getInstance().fromJson(m.get("body"), ApiDocumentTagsItemResponse.class);
        assertEquals(1, resp.getTags().size());
        assertNotNull(resp.getNext());
        assertNull(resp.getPrevious());
        assertNull(resp.getTags().get(0).getDocumentId());
        assertNotNull(resp.getTags().get(0).getInsertedDate());
        assertEquals("category0", resp.getTags().get(0).getKey());
        assertEquals("userdefined", resp.getTags().get(0).getType());
        assertEquals("jsmith", resp.getTags().get(0).getUserId());
        assertNull(resp.getTags().get(0).getValue());
        assertEquals("[abc, xyz]", resp.getTags().get(0).getValues().toString());
    }
}
Also used : DocumentTag(com.formkiq.stacks.dynamodb.DocumentTag) ApiGatewayRequestEvent(com.formkiq.lambda.apigateway.ApiGatewayRequestEvent) Date(java.util.Date) Test(org.junit.jupiter.api.Test)

Example 9 with DocumentTag

use of com.formkiq.stacks.dynamodb.DocumentTag in project formkiq-core by formkiq.

the class ApiDocumentsTagsRequestTest method testHandlePostDocumentTags04.

/**
 * POST /documents/{documentId}/tags tags request. Add Tag Key ONLY no value
 *
 * @throws Exception an error has occurred
 */
@Test
public void testHandlePostDocumentTags04() throws Exception {
    for (String siteId : Arrays.asList(null, UUID.randomUUID().toString())) {
        // given
        final String documentId = "test" + UUID.randomUUID().toString() + ".pdf";
        final String tagname = "category";
        final String tagvalue = "";
        ApiGatewayRequestEvent event = toRequestEvent("/request-post-documents-documentid-tags03.json");
        addParameter(event, "siteId", siteId);
        setPathParameter(event, "documentId", documentId);
        String expected = "{" + getHeaders() + ",\"body\":\"" + "{\\\"message\\\":\\\"Created Tag 'category'.\\\"}\",\"statusCode\":201}";
        // 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(tagname, tags.getResults().get(0).getKey());
        assertEquals(tagvalue, tags.getResults().get(0).getValue());
        assertTrue(getLogger().containsString("response: " + expected));
    }
}
Also used : DocumentTag(com.formkiq.stacks.dynamodb.DocumentTag) ApiGatewayRequestEvent(com.formkiq.lambda.apigateway.ApiGatewayRequestEvent) Test(org.junit.jupiter.api.Test)

Example 10 with DocumentTag

use of com.formkiq.stacks.dynamodb.DocumentTag in project formkiq-core by formkiq.

the class ApiWebhooksRequestTest method testPostWebhooks01.

/**
 * POST /webhooks.
 * Test TTL.
 *
 * @throws Exception an error has occurred
 */
@SuppressWarnings("unchecked")
@Test
public void testPostWebhooks01() throws Exception {
    // given
    putSsmParameter("/formkiq/" + FORMKIQ_APP_ENVIRONMENT + "/api/DocumentsPublicHttpUrl", "http://localhost:8080");
    ApiGatewayRequestEvent event = toRequestEvent("/request-post-webhooks01.json");
    String ttl = "90000";
    event.setBody("{\"name\":\"john smith\",\"ttl\":\"" + ttl + "\"}");
    // 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);
    assertEquals("default", result.get("siteId"));
    assertNotNull(result.get("id"));
    final String id = result.get("id").toString();
    assertNotNull(result.get("insertedDate"));
    assertEquals("john smith", result.get("name"));
    assertEquals("test@formkiq.com", result.get("userId"));
    assertEquals("http://localhost:8080/public/webhooks/" + id, result.get("url"));
    assertNotNull(result.get("ttl"));
    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).plusHours(1);
    assertEquals(now.getDayOfMonth(), ld.getDayOfMonth());
    // given
    DocumentTag tag = new DocumentTag(id, "category", "person", new Date(), "joe");
    // when
    webhookService.addTags(null, id, Arrays.asList(tag), null);
    // then
    assertNull(webhookService.findTag(null, id, "category").getString("TimeToLive"));
    // update ttl/name
    // given
    ttl = "180000";
    event = toRequestEvent("/request-patch-webhooks-webhookid01.json");
    event.setPathParameters(Map.of("webhookId", id));
    event.setBody("{\"name\":\"john smith2\",\"ttl\":\"" + ttl + "\"}");
    // when
    response = handleRequest(event);
    // then
    m = GsonUtil.getInstance().fromJson(response, Map.class);
    assertEquals("200.0", String.valueOf(m.get("statusCode")));
    result = GsonUtil.getInstance().fromJson(m.get("body"), Map.class);
    assertEquals("'" + id + "' object updated", result.get("message"));
    obj = webhookService.findWebhook(null, id);
    assertEquals("john smith2", obj.get("path"));
    epoch = Long.parseLong(obj.getString("TimeToLive"));
    ld = Instant.ofEpochMilli(epoch * TO_MILLIS).atZone(ZoneOffset.UTC);
    now = ZonedDateTime.now(ZoneOffset.UTC).plusDays(2).plusHours(2);
    assertEquals(now.getDayOfMonth(), ld.getDayOfMonth());
    DynamicObject dtag = webhookService.findTag(null, id, "category");
    assertNotNull(dtag.getString("TimeToLive"));
    epoch = Long.parseLong(dtag.getString("TimeToLive"));
    ld = Instant.ofEpochMilli(epoch * TO_MILLIS).atZone(ZoneOffset.UTC);
    now = ZonedDateTime.now(ZoneOffset.UTC).plusDays(2);
    assertEquals(now.getDayOfMonth(), ld.getDayOfMonth());
}
Also used : DocumentTag(com.formkiq.stacks.dynamodb.DocumentTag) DynamicObject(com.formkiq.stacks.common.objects.DynamicObject) ZonedDateTime(java.time.ZonedDateTime) ApiGatewayRequestEvent(com.formkiq.lambda.apigateway.ApiGatewayRequestEvent) DynamicObject(com.formkiq.stacks.common.objects.DynamicObject) WebhooksService(com.formkiq.stacks.dynamodb.WebhooksService) Map(java.util.Map) Date(java.util.Date) Test(org.junit.jupiter.api.Test)

Aggregations

DocumentTag (com.formkiq.stacks.dynamodb.DocumentTag)46 Date (java.util.Date)34 Test (org.junit.jupiter.api.Test)34 ApiGatewayRequestEvent (com.formkiq.lambda.apigateway.ApiGatewayRequestEvent)31 DocumentItemDynamoDb (com.formkiq.stacks.dynamodb.DocumentItemDynamoDb)13 ApiMessageResponse (com.formkiq.lambda.apigateway.ApiMessageResponse)11 DynamicObject (com.formkiq.stacks.common.objects.DynamicObject)10 DocumentItem (com.formkiq.stacks.dynamodb.DocumentItem)10 ApiRequestHandlerResponse (com.formkiq.lambda.apigateway.ApiRequestHandlerResponse)9 DynamicDocumentItem (com.formkiq.stacks.dynamodb.DynamicDocumentItem)9 BadException (com.formkiq.lambda.apigateway.exception.BadException)7 Map (java.util.Map)7 ApiResponse (com.formkiq.lambda.apigateway.ApiResponse)5 NotFoundException (com.formkiq.lambda.apigateway.exception.NotFoundException)5 DynamicDocumentTag (com.formkiq.stacks.dynamodb.DynamicDocumentTag)5 Timeout (org.junit.jupiter.api.Timeout)5 DocumentService (com.formkiq.stacks.dynamodb.DocumentService)4 S3Client (software.amazon.awssdk.services.s3.S3Client)4 LambdaLogger (com.amazonaws.services.lambda.runtime.LambdaLogger)3 DocumentSearchQuery (com.formkiq.stacks.client.models.DocumentSearchQuery)3