Search in sources :

Example 81 with ApiGatewayRequestEvent

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

the class ApiDocumentsTagsRequestTest method testHandlePostDocumentTags02.

/**
 * POST /documents/{documentId}/tags tags request. Add Tag Base 64
 *
 * @throws Exception an error has occurred
 */
@Test
public void testHandlePostDocumentTags02() throws Exception {
    for (String siteId : Arrays.asList(null, UUID.randomUUID().toString())) {
        // given
        final String documentId = UUID.randomUUID().toString();
        final String tagname = "category";
        final String tagvalue = "job";
        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);
        // 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());
        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 82 with ApiGatewayRequestEvent

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

the class ApiDocumentsTagsRequestTest method testHandleDeleteTagValue01.

/**
 * DELETE /documents/{documentId}/tags/{tagKey}/{tagValue} request with Tag Value.
 *
 * @throws Exception an error has occurred
 */
@SuppressWarnings("unchecked")
@Test
public void testHandleDeleteTagValue01() throws Exception {
    for (String siteId : Arrays.asList(null, UUID.randomUUID().toString())) {
        // given
        final Date now = new Date();
        String documentId = UUID.randomUUID().toString();
        String tagKey = "category";
        final String tagValue = "person";
        final String userId = "jsmith";
        ApiGatewayRequestEvent event = toRequestEvent("/request-delete-documents-documentid-tag-value01.json");
        addParameter(event, "siteId", siteId);
        setPathParameter(event, "documentId", documentId);
        setPathParameter(event, "tagKey", tagKey);
        setPathParameter(event, "tagValue", tagValue);
        DocumentTag tag = new DocumentTag(documentId, tagKey, tagValue, now, userId);
        tag.setInsertedDate(new Date());
        getDocumentService().addTags(siteId, documentId, Arrays.asList(tag), null);
        assertEquals(1, getDocumentService().findDocumentTags(siteId, documentId, null, MAX_RESULTS).getResults().size());
        // 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")));
        ApiMessageResponse resp = GsonUtil.getInstance().fromJson(m.get("body"), ApiMessageResponse.class);
        assertEquals("Removed Tag from document '" + documentId + "'.", resp.getMessage());
        assertNull(resp.getNext());
        assertNull(resp.getPrevious());
        PaginationResults<DocumentTag> tags = getDocumentService().findDocumentTags(siteId, documentId, null, MAX_RESULTS);
        assertEquals(0, tags.getResults().size());
    }
}
Also used : DocumentTag(com.formkiq.stacks.dynamodb.DocumentTag) ApiMessageResponse(com.formkiq.lambda.apigateway.ApiMessageResponse) ApiGatewayRequestEvent(com.formkiq.lambda.apigateway.ApiGatewayRequestEvent) Date(java.util.Date) Test(org.junit.jupiter.api.Test)

Example 83 with ApiGatewayRequestEvent

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

the class ApiDocumentsTagsRequestTest method testHandleDeleteTagValue03.

/**
 * DELETE /documents/{documentId}/tags/{tagKey}/{tagValue} wrong Tag Value.
 *
 * @throws Exception an error has occurred
 */
@SuppressWarnings("unchecked")
@Test
public void testHandleDeleteTagValue03() throws Exception {
    for (String siteId : Arrays.asList(null, UUID.randomUUID().toString())) {
        // given
        final Date now = new Date();
        String documentId = UUID.randomUUID().toString();
        String tagKey = "category";
        final String userId = "jsmith";
        ApiGatewayRequestEvent event = toRequestEvent("/request-delete-documents-documentid-tag-value01.json");
        addParameter(event, "siteId", siteId);
        setPathParameter(event, "documentId", documentId);
        setPathParameter(event, "tagKey", tagKey);
        setPathParameter(event, "tagValue", "xyz123");
        DocumentTag tag = new DocumentTag(documentId, tagKey, null, now, userId);
        tag.setValues(Arrays.asList("abc", "xyz"));
        tag.setInsertedDate(new Date());
        getDocumentService().addTags(siteId, documentId, Arrays.asList(tag), null);
        assertEquals(1, getDocumentService().findDocumentTags(siteId, documentId, null, MAX_RESULTS).getResults().size());
        // 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("404.0", String.valueOf(m.get("statusCode")));
        assertEquals(getHeaders(), "\"headers\":" + GsonUtil.getInstance().toJson(m.get("headers")));
        ApiMessageResponse resp = GsonUtil.getInstance().fromJson(m.get("body"), ApiMessageResponse.class);
        assertEquals("Tag/Value combination not found.", resp.getMessage());
        assertNull(resp.getNext());
        assertNull(resp.getPrevious());
        PaginationResults<DocumentTag> tags = getDocumentService().findDocumentTags(siteId, documentId, null, MAX_RESULTS);
        assertEquals(1, tags.getResults().size());
        assertNull(tags.getResults().get(0).getValue());
        assertEquals("[abc, xyz]", tags.getResults().get(0).getValues().toString());
    }
}
Also used : DocumentTag(com.formkiq.stacks.dynamodb.DocumentTag) ApiMessageResponse(com.formkiq.lambda.apigateway.ApiMessageResponse) ApiGatewayRequestEvent(com.formkiq.lambda.apigateway.ApiGatewayRequestEvent) Date(java.util.Date) Test(org.junit.jupiter.api.Test)

Example 84 with ApiGatewayRequestEvent

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

the class ApiDocumentsUploadRequestTest method testHandleGetDocumentsUpload03.

/**
 * Valid PUT generate upload document signed url for existing document.
 *
 * @throws Exception an error has occurred
 */
@Test
public void testHandleGetDocumentsUpload03() throws Exception {
    for (String siteId : Arrays.asList(null, UUID.randomUUID().toString())) {
        // given
        Date date = new Date();
        String documentId = UUID.randomUUID().toString();
        DocumentItemDynamoDb item = new DocumentItemDynamoDb(documentId, date, "jsmith");
        ApiGatewayRequestEvent event = toRequestEvent("/request-get-documents-upload-documentid01.json");
        addParameter(event, "siteId", siteId);
        setPathParameter(event, "documentId", documentId);
        // when
        getDocumentService().saveDocument(siteId, item, new ArrayList<>());
        String response = handleRequest(event);
        // then
        ApiUrlResponse resp = expectResponse(response);
        assertTrue(getLogger().containsString("generated presign url: " + this.localstack.getEndpointOverride(Service.S3).toString() + "/testbucket/"));
        assertTrue(getLogger().containsString("for document " + resp.getDocumentId()));
        assertEquals(documentId, resp.getDocumentId());
    }
}
Also used : ApiGatewayRequestEvent(com.formkiq.lambda.apigateway.ApiGatewayRequestEvent) Date(java.util.Date) DocumentItemDynamoDb(com.formkiq.stacks.dynamodb.DocumentItemDynamoDb) Test(org.junit.jupiter.api.Test)

Example 85 with ApiGatewayRequestEvent

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

the class ApiDocumentsUploadRequestTest method testHandleGetDocumentsUpload04.

/**
 * Valid PUT generate upload document signed url for NEW document.
 *
 * @throws Exception an error has occurred
 */
@Test
public void testHandleGetDocumentsUpload04() throws Exception {
    for (String siteId : Arrays.asList(null, UUID.randomUUID().toString())) {
        // given
        ApiGatewayRequestEvent event = toRequestEvent("/request-get-documents-upload-documentid02.json");
        addParameter(event, "siteId", siteId);
        // when
        String response = handleRequest(event);
        // then
        ApiUrlResponse resp = expectResponse(response);
        DocumentItem item = getDocumentService().findDocument(siteId, resp.getDocumentId());
        assertEquals("AROAZB6IP7U6SDBIQTEUX:formkiq-docstack-unittest-api-ApiGatewayInvokeRole-IKJY8XKB0IUK", item.getUserId());
    }
}
Also used : DocumentItem(com.formkiq.stacks.dynamodb.DocumentItem) 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