Search in sources :

Example 1 with ApiGatewayRequestEvent

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

the class ApiDocumentsTagsRequestTest method testHandlePostDocumentTags03.

/**
 * POST /documents/{documentId}/tags tags request. Add Tag non Base 64 and add 'webnotify=true'
 * parameter
 *
 * @throws Exception an error has occurred
 */
@Test
@Timeout(value = TEST_TIMEOUT, unit = TimeUnit.MILLISECONDS)
public void testHandlePostDocumentTags03() throws Exception {
    for (String siteId : Arrays.asList(null, UUID.randomUUID().toString())) {
        // given
        final long sleep = 500L;
        final String documentId = "test" + UUID.randomUUID().toString() + ".pdf";
        final String tagname = "category";
        final String tagvalue = "job";
        ApiGatewayRequestEvent event = toRequestEvent("/request-post-documents-documentid-tags02.json");
        addParameter(event, "siteId", siteId);
        addParameter(event, "webnotify", "true");
        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));
        ReceiveMessageResponse msgs = getAwsServices().sqsService().receiveMessages(getSqsWebsocketQueueUrl());
        while (msgs.messages().isEmpty()) {
            msgs = getAwsServices().sqsService().receiveMessages(getSqsWebsocketQueueUrl());
            Thread.sleep(sleep);
        }
        assertEquals(1, msgs.messages().size());
        if (siteId != null) {
            assertEquals("{\"siteId\":\"" + siteId + "\",\"documentId\":\"" + documentId + "\",\"message\":\"{\\\"key\\\": \\\"category\\\",\\\"value\\\": \\\"job\\\"}\"}", msgs.messages().get(0).body());
        } else {
            assertEquals("{\"documentId\":\"" + documentId + "\",\"message\":\"{\\\"key\\\": \\\"category\\\",\\\"value\\\": \\\"job\\\"}\"}", msgs.messages().get(0).body());
        }
    }
}
Also used : DocumentTag(com.formkiq.stacks.dynamodb.DocumentTag) ApiGatewayRequestEvent(com.formkiq.lambda.apigateway.ApiGatewayRequestEvent) ReceiveMessageResponse(software.amazon.awssdk.services.sqs.model.ReceiveMessageResponse) Test(org.junit.jupiter.api.Test) Timeout(org.junit.jupiter.api.Timeout)

Example 2 with ApiGatewayRequestEvent

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

the class ApiDocumentsTagsRequestTest method testHandlePostDocumentTags07.

/**
 * POST /documents/{documentId}/tags "values" request. Add Tag Base 64
 *
 * @throws Exception an error has occurred
 */
@Test
public void testHandlePostDocumentTags07() throws Exception {
    for (String siteId : Arrays.asList(null, UUID.randomUUID().toString())) {
        // given
        final String documentId = UUID.randomUUID().toString();
        final String tagname = "category";
        final String expected = "{" + getHeaders() + ",\"body\":\"" + "{\\\"message\\\":\\\"Created Tag 'category'.\\\"}\",\"statusCode\":201}";
        ApiGatewayRequestEvent event = toRequestEvent("/request-post-documents-documentid-tags01.json");
        addParameter(event, "siteId", siteId);
        setPathParameter(event, "documentId", documentId);
        event.setBody("eyJrZXkiOiAiY2F0ZWdvcnkiLCJ2YWx1ZXMiOiBbImpvYiIsIndob2tub3dzIl19");
        // 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());
        assertNull(tags.getResults().get(0).getValue());
        assertEquals("[job, whoknows]", tags.getResults().get(0).getValues().toString());
        assertEquals("testadminuser@formkiq.com", tags.getResults().get(0).getUserId());
        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 3 with ApiGatewayRequestEvent

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

the class ApiDocumentsTagsRequestTest method testHandlePutTags01.

/**
 * PUT /documents/{documentId}/tags/{tagKey} VALUE request.
 *
 * @throws Exception an error has occurred
 */
@Test
public void testHandlePutTags01() 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}";
        getDocumentService().saveDocument(siteId, new DocumentItemDynamoDb(documentId, new Date(), userId), Arrays.asList(new DocumentTag(null, "category", "nope", new Date(), userId)));
        ApiGatewayRequestEvent event = toRequestEvent("/request-put-documents-documentid-tags01.json");
        addParameter(event, "siteId", siteId);
        setPathParameter(event, "documentId", documentId);
        // when
        String response = handleRequest(event);
        // then
        assertEquals(expected, response);
    }
}
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 4 with ApiGatewayRequestEvent

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

the class ApiDocumentsTagsRequestTest method testHandlePostDocumentTags08.

/**
 * POST /documents/{documentId}/tags multiple "tags" request.
 *
 * @throws Exception an error has occurred
 */
@Test
public void testHandlePostDocumentTags08() throws Exception {
    for (String siteId : Arrays.asList(null, UUID.randomUUID().toString())) {
        // given
        final String documentId = UUID.randomUUID().toString();
        final String expected = "{" + getHeaders() + ",\"body\":\"" + "{\\\"message\\\":\\\"Created Tags.\\\"}\",\"statusCode\":201}";
        ApiGatewayRequestEvent event = toRequestEvent("/request-post-documents-documentid-tags01.json");
        addParameter(event, "siteId", siteId);
        setPathParameter(event, "documentId", documentId);
        event.setBody("ewogICJ0YWdzIjogWwogICAgewogICAgICAia2V5IjogIm1pbmUiCiAg" + "ICB9LAogICAgewogICAgICAia2V5IjogInBsYXllcklkIiwKICAgICAgInZhbHVlI" + "jogIjEiCiAgICB9LAogICAgewogICAgICAia2V5IjogImNhc2VJZCIsCiAgICAgICJ" + "2YWx1ZXMiOiBbCiAgICAgICAgIjEyMyIsCiAgICAgICAgIjk5OSIKICAgICAgXQogIC" + "AgfQogIF0KfQ==");
        // when
        String response = handleRequest(event);
        // then
        assertEquals(expected, response);
        final int count = 3;
        PaginationResults<DocumentTag> tags = getDocumentService().findDocumentTags(siteId, documentId, null, MAX_RESULTS);
        assertEquals(count, tags.getResults().size());
        assertEquals("caseId", tags.getResults().get(0).getKey());
        assertNull(tags.getResults().get(0).getValue());
        assertEquals("[123, 999]", tags.getResults().get(0).getValues().toString());
        assertEquals("testadminuser@formkiq.com", tags.getResults().get(0).getUserId());
        assertEquals("mine", tags.getResults().get(1).getKey());
        assertEquals("", tags.getResults().get(1).getValue());
        assertNull(tags.getResults().get(1).getValues());
        assertEquals("testadminuser@formkiq.com", tags.getResults().get(1).getUserId());
        assertEquals("playerId", tags.getResults().get(2).getKey());
        assertEquals("1", tags.getResults().get(2).getValue());
        assertNull(tags.getResults().get(2).getValues());
        assertEquals("testadminuser@formkiq.com", tags.getResults().get(2).getUserId());
        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 5 with ApiGatewayRequestEvent

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

the class ApiDocumentsTagsRequestTest method testHandleGetTags02.

/**
 * GET /documents/{documentId}/tags/{tagKey} request. Tag and value found.
 *
 * @throws Exception an error has occurred
 */
@SuppressWarnings("unchecked")
@Test
public void testHandleGetTags02() throws Exception {
    for (String siteId : Arrays.asList(null, UUID.randomUUID().toString())) {
        // given
        String documentId = UUID.randomUUID().toString();
        DocumentItemDynamoDb item = new DocumentItemDynamoDb(documentId, new Date(), "jsmith");
        DocumentTag tag = new DocumentTag(documentId, "category", "person", new Date(), "jsmith");
        getDocumentService().saveDocument(siteId, item, Arrays.asList(tag));
        ApiGatewayRequestEvent event = toRequestEvent("/request-get-documents-documentid-tags01.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")));
        ApiDocumentTagItemResponse resp = GsonUtil.getInstance().fromJson(m.get("body"), ApiDocumentTagItemResponse.class);
        assertEquals("category", resp.getKey());
        assertEquals("person", resp.getValue());
    }
}
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)

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