Search in sources :

Example 6 with ApiGatewayRequestEvent

use of com.formkiq.lambda.apigateway.ApiGatewayRequestEvent 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 ApiGatewayRequestEvent

use of com.formkiq.lambda.apigateway.ApiGatewayRequestEvent 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 ApiGatewayRequestEvent

use of com.formkiq.lambda.apigateway.ApiGatewayRequestEvent 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 ApiGatewayRequestEvent

use of com.formkiq.lambda.apigateway.ApiGatewayRequestEvent 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 ApiGatewayRequestEvent

use of com.formkiq.lambda.apigateway.ApiGatewayRequestEvent in project formkiq-core by formkiq.

the class ApiDocumentsUploadRequestTest method testHandleGetDocumentsUpload06.

/**
 * Content-Length > Max Content Length.
 *
 * @throws Exception an error has occurred
 */
@SuppressWarnings("unchecked")
@Test
public void testHandleGetDocumentsUpload06() throws Exception {
    String maxContentLengthBytes = "2783034";
    getAwsServices().configService().save(null, new DynamicObject(Map.of(ConfigService.MAX_DOCUMENT_SIZE_BYTES, maxContentLengthBytes)));
    for (String siteId : Arrays.asList(null, UUID.randomUUID().toString())) {
        if (siteId != null) {
            getAwsServices().configService().save(siteId, new DynamicObject(Map.of(ConfigService.MAX_DOCUMENT_SIZE_BYTES, maxContentLengthBytes)));
        }
        // given
        ApiGatewayRequestEvent event = toRequestEvent("/request-get-documents-upload-documentid.json");
        addParameter(event, "siteId", siteId);
        addParameter(event, "contentLength", "2783035");
        // 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("400.0", String.valueOf(m.get("statusCode")));
        assertEquals("{\"message\":\"'contentLength' cannot exceed 2783034 bytes\"}", m.get("body"));
    }
}
Also used : DynamicObject(com.formkiq.stacks.common.objects.DynamicObject) ApiGatewayRequestEvent(com.formkiq.lambda.apigateway.ApiGatewayRequestEvent) Test(org.junit.jupiter.api.Test)

Aggregations

ApiGatewayRequestEvent (com.formkiq.lambda.apigateway.ApiGatewayRequestEvent)131 Test (org.junit.jupiter.api.Test)121 Date (java.util.Date)49 DynamicObject (com.formkiq.stacks.common.objects.DynamicObject)46 Map (java.util.Map)45 DocumentTag (com.formkiq.stacks.dynamodb.DocumentTag)34 DocumentItemDynamoDb (com.formkiq.stacks.dynamodb.DocumentItemDynamoDb)28 HashMap (java.util.HashMap)13 List (java.util.List)11 S3Client (software.amazon.awssdk.services.s3.S3Client)10 ZonedDateTime (java.time.ZonedDateTime)9 ApiMessageResponse (com.formkiq.lambda.apigateway.ApiMessageResponse)8 LambdaLogger (com.amazonaws.services.lambda.runtime.LambdaLogger)7 ApiAuthorizer (com.formkiq.lambda.apigateway.ApiAuthorizer)7 ApiGatewayRequestEventUtil (com.formkiq.lambda.apigateway.ApiGatewayRequestEventUtil)7 ApiGatewayRequestHandler (com.formkiq.lambda.apigateway.ApiGatewayRequestHandler)7 ApiRequestHandlerResponse (com.formkiq.lambda.apigateway.ApiRequestHandlerResponse)7 SC_OK (com.formkiq.lambda.apigateway.ApiResponseStatus.SC_OK)7 AwsServiceCache (com.formkiq.lambda.apigateway.AwsServiceCache)7 LocalDate (java.time.LocalDate)6