Search in sources :

Example 16 with DocumentTag

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

the class ApiDocumentsSearchRequestTest method testHandleSearchRequest03.

/**
 * Valid POST search by eq tagValue.
 *
 * @throws Exception an error has occurred
 */
@SuppressWarnings("unchecked")
@Test
public void testHandleSearchRequest03() throws Exception {
    for (String op : Arrays.asList("eq", "eqOr")) {
        for (String siteId : Arrays.asList(null, UUID.randomUUID().toString())) {
            // given
            String documentId = UUID.randomUUID().toString();
            final String tagKey = "category";
            final String tagvalue = "person";
            final String username = "jsmith";
            final Date now = new Date();
            ApiGatewayRequestEvent event = toRequestEvent("/request-post-search01.json");
            addParameter(event, "siteId", siteId);
            DocumentSearch s = new DocumentSearch().query(new DocumentSearchQuery().tag(new DocumentSearchTag().key("category").eq("person")).documentIds(Arrays.asList(documentId)));
            event.setBody(GsonUtil.getInstance().toJson(s));
            event.setIsBase64Encoded(Boolean.FALSE);
            if ("eqOr".equals(op)) {
                s.query().tag().eq(null).eqOr(Arrays.asList("person"));
            }
            DocumentTag item = new DocumentTag(documentId, tagKey, tagvalue, now, username);
            item.setUserId(UUID.randomUUID().toString());
            getDocumentService().saveDocument(siteId, new DocumentItemDynamoDb(documentId, now, username), Arrays.asList(item));
            // when
            String response = handleRequest(event);
            // then
            Map<String, String> m = 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")));
            DynamicObject resp = new DynamicObject(fromJson(m.get("body"), Map.class));
            List<DynamicObject> documents = resp.getList("documents");
            assertEquals(1, documents.size());
            assertEquals(documentId, documents.get(0).get("documentId"));
            assertEquals(username, documents.get(0).get("userId"));
            assertNotNull(documents.get(0).get("insertedDate"));
            Map<String, Object> matchedTag = (Map<String, Object>) documents.get(0).get("matchedTag");
            assertEquals(MATCH_COUNT, matchedTag.size());
            assertEquals("USERDEFINED", matchedTag.get("type"));
            assertEquals("category", matchedTag.get("key"));
            assertEquals("person", matchedTag.get("value"));
            assertNull(resp.get("next"));
            assertNull(resp.get("previous"));
        }
    }
}
Also used : DocumentTag(com.formkiq.stacks.dynamodb.DocumentTag) DocumentSearchTag(com.formkiq.stacks.client.models.DocumentSearchTag) DocumentSearch(com.formkiq.stacks.client.models.DocumentSearch) Date(java.util.Date) DocumentSearchQuery(com.formkiq.stacks.client.models.DocumentSearchQuery) DynamicObject(com.formkiq.stacks.common.objects.DynamicObject) ApiGatewayRequestEvent(com.formkiq.lambda.apigateway.ApiGatewayRequestEvent) DynamicObject(com.formkiq.stacks.common.objects.DynamicObject) Map(java.util.Map) DocumentItemDynamoDb(com.formkiq.stacks.dynamodb.DocumentItemDynamoDb) Test(org.junit.jupiter.api.Test)

Example 17 with DocumentTag

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

the class ApiDocumentsTagsRequestTest method testHandleDeleteTagValue02.

/**
 * DELETE /documents/{documentId}/tags/{tagKey}/{tagValue} request with Tag Values.
 *
 * @throws Exception an error has occurred
 */
@SuppressWarnings("unchecked")
@Test
public void testHandleDeleteTagValue02() 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", "xyz");
        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("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(1, tags.getResults().size());
        assertEquals("abc", tags.getResults().get(0).getValue());
        assertNull(tags.getResults().get(0).getValues());
    }
}
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 18 with DocumentTag

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

the class ApiDocumentsTagsRequestTest method testHandleGetTags04.

/**
 * GET /documents/{documentId}/tags/{tagKey} request. Tag and values found.
 *
 * @throws Exception an error has occurred
 */
@SuppressWarnings("unchecked")
@Test
public void testHandleGetTags04() 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", null, new Date(), "jsmith");
        tag.setValues(Arrays.asList("abc", "xyz"));
        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());
        assertNull(resp.getValue());
        assertEquals("[abc, xyz]", resp.getValues().toString());
    }
}
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 19 with DocumentTag

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

the class ApiDocumentsTagsRequestTest method testHandleDeleteTagDocument03.

/**
 * DELETE /documents/{documentId}/tags/{tagKey} request with Tag Values.
 *
 * @throws Exception an error has occurred
 */
@SuppressWarnings("unchecked")
@Test
public void testHandleDeleteTagDocument03() throws Exception {
    for (String siteId : Arrays.asList(null, UUID.randomUUID().toString())) {
        // given
        Date now = new Date();
        String documentId = UUID.randomUUID().toString();
        String tagKey = "category";
        String userId = "jsmith";
        ApiGatewayRequestEvent event = toRequestEvent("/request-delete-documents-documentid-tags01.json");
        addParameter(event, "siteId", siteId);
        setPathParameter(event, "documentId", documentId);
        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("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 'category' 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());
        String expected = "response: {" + getHeaders() + ",\"body\":\"{\\\"message\\\":\\\"Removed 'category' from document '" + documentId + "'.\\\"}\"," + "\"statusCode\":200}";
        assertTrue(getLogger().containsString(expected));
    }
}
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 20 with DocumentTag

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

the class ApiDocumentsTagsRequestTest method testHandlePostDocumentTags05.

/**
 * POST /documents/{documentId}/tags request.
 *
 * @throws Exception an error has occurred
 */
@Test
public void testHandlePostDocumentTags05() throws Exception {
    for (String siteId : Arrays.asList(null, UUID.randomUUID().toString())) {
        // given
        String documentId = UUID.randomUUID().toString();
        final String tagname = "category";
        final String tagvalue = "somevalue";
        ApiGatewayRequestEvent event = toRequestEvent("/request-post-documents-documentid-tags04.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());
        assertEquals("AROAZB6IP7U6SDBIQTEUX:formkiq-docstack-unittest-api-ApiGatewayInvokeRole-IKJY8XKB0IUK", 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)

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