Search in sources :

Example 1 with SearchTagCriteria

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

the class ApiDocumentsSearchRequestTest method testHandleSearchRequest09.

/**
 * Valid POST search by eq tagValue with > 10 Document.
 *
 * @throws Exception an error has occurred
 */
@SuppressWarnings("unchecked")
@Test
public void testHandleSearchRequest09() throws Exception {
    for (String siteId : Arrays.asList(null, UUID.randomUUID().toString())) {
        // given
        final int count = 100;
        final String tagKey = "category";
        final String tagvalue = "person";
        final String username = "jsmith";
        List<String> documentIds = new ArrayList<>();
        for (int i = 0; i < count; i++) {
            String documentId = UUID.randomUUID().toString();
            Date now = new Date();
            documentIds.add(documentId);
            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));
        }
        ApiGatewayRequestEvent event = toRequestEvent("/request-post-search01.json");
        addParameter(event, "siteId", siteId);
        event.setIsBase64Encoded(Boolean.FALSE);
        QueryRequest q = new QueryRequest().query(new SearchQuery().tag(new SearchTagCriteria().key(tagKey).eq(tagvalue)).documentsIds(documentIds));
        event.setBody(GsonUtil.getInstance().toJson(q));
        // 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(count, documents.size());
        // given not search by documentIds should be limited to 10
        q = new QueryRequest().query(new SearchQuery().tag(new SearchTagCriteria().key(tagKey).eq(tagvalue)).documentsIds(null));
        event.setBody(GsonUtil.getInstance().toJson(q));
        // when
        response = handleRequest(event);
        // then
        final int ten = 10;
        m = fromJson(response, Map.class);
        assertEquals(mapsize, m.size());
        assertEquals("200.0", String.valueOf(m.get("statusCode")));
        assertEquals(getHeaders(), "\"headers\":" + GsonUtil.getInstance().toJson(m.get("headers")));
        resp = new DynamicObject(fromJson(m.get("body"), Map.class));
        documents = resp.getList("documents");
        assertEquals(ten, documents.size());
    }
}
Also used : SearchQuery(com.formkiq.stacks.dynamodb.SearchQuery) DocumentSearchQuery(com.formkiq.stacks.client.models.DocumentSearchQuery) DocumentTag(com.formkiq.stacks.dynamodb.DocumentTag) ArrayList(java.util.ArrayList) SearchTagCriteria(com.formkiq.stacks.dynamodb.SearchTagCriteria) Date(java.util.Date) DynamicObject(com.formkiq.stacks.common.objects.DynamicObject) ApiGatewayRequestEvent(com.formkiq.lambda.apigateway.ApiGatewayRequestEvent) Map(java.util.Map) DocumentItemDynamoDb(com.formkiq.stacks.dynamodb.DocumentItemDynamoDb) Test(org.junit.jupiter.api.Test)

Example 2 with SearchTagCriteria

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

the class ApiDocumentsSearchRequestTest method testHandleSearchRequest08.

/**
 * Valid POST search by eq tagValue and TOO many DocumentId.
 *
 * @throws Exception an error has occurred
 */
@Test
public void testHandleSearchRequest08() throws Exception {
    final int count = 101;
    for (String siteId : Arrays.asList(null, UUID.randomUUID().toString())) {
        // given
        ApiGatewayRequestEvent event = toRequestEvent("/request-post-search01.json");
        addParameter(event, "siteId", siteId);
        event.setIsBase64Encoded(Boolean.FALSE);
        QueryRequest q = new QueryRequest().query(new SearchQuery().tag(new SearchTagCriteria().key("test")).documentsIds(new ArrayList<>()));
        List<String> ids = new ArrayList<>();
        for (int i = 0; i < count; i++) {
            ids.add(UUID.randomUUID().toString());
        }
        q.query().documentsIds(ids);
        event.setIsBase64Encoded(Boolean.FALSE);
        event.setBody(GsonUtil.getInstance().toJson(q));
        // when
        String response = handleRequest(event);
        // then
        String expected = "{" + getHeaders() + ",\"body\":" + "\"{\\\"message\\\":\\\"Maximum number of DocumentIds is 100\\\"}\"," + "\"statusCode\":400}";
        assertEquals(expected, response);
    }
}
Also used : SearchQuery(com.formkiq.stacks.dynamodb.SearchQuery) DocumentSearchQuery(com.formkiq.stacks.client.models.DocumentSearchQuery) ArrayList(java.util.ArrayList) SearchTagCriteria(com.formkiq.stacks.dynamodb.SearchTagCriteria) ApiGatewayRequestEvent(com.formkiq.lambda.apigateway.ApiGatewayRequestEvent) Test(org.junit.jupiter.api.Test)

Aggregations

ApiGatewayRequestEvent (com.formkiq.lambda.apigateway.ApiGatewayRequestEvent)2 DocumentSearchQuery (com.formkiq.stacks.client.models.DocumentSearchQuery)2 SearchQuery (com.formkiq.stacks.dynamodb.SearchQuery)2 SearchTagCriteria (com.formkiq.stacks.dynamodb.SearchTagCriteria)2 ArrayList (java.util.ArrayList)2 Test (org.junit.jupiter.api.Test)2 DynamicObject (com.formkiq.stacks.common.objects.DynamicObject)1 DocumentItemDynamoDb (com.formkiq.stacks.dynamodb.DocumentItemDynamoDb)1 DocumentTag (com.formkiq.stacks.dynamodb.DocumentTag)1 Date (java.util.Date)1 Map (java.util.Map)1