Search in sources :

Example 26 with DynamicDocumentItem

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

the class ApiRequestHandlerTest method testHandleGetRequest06.

/**
 * Get Document Request, Document with sub docs found.
 *
 * @throws Exception an error has occurred
 */
@SuppressWarnings("unchecked")
@Test
public void testHandleGetRequest06() throws Exception {
    // given
    Date date = new Date();
    String documentId0 = "1a1d1938-451e-4e20-bf95-e0e7a749505a";
    String documentId1 = UUID.randomUUID().toString();
    String userId = "jsmith";
    DocumentItem item = new DocumentItemDynamoDb(documentId0, date, userId);
    DocumentItem citem = new DocumentItemDynamoDb(documentId1, date, userId);
    DynamicDocumentItem doc = new DocumentItemToDynamicDocumentItem().apply(item);
    doc.put("documents", Arrays.asList(new DocumentItemToDynamicDocumentItem().apply(citem)));
    getDocumentService().saveDocumentItemWithTag(null, doc);
    ApiGatewayRequestEvent event = toRequestEvent("/request-get-documents-documentid02.json");
    // 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));
    assertEquals(documentId0, resp.get("documentId"));
    assertEquals(userId, resp.get("userId"));
    assertNotNull(resp.get("insertedDate"));
    assertEquals(DEFAULT_SITE_ID, resp.get("siteId"));
    assertNull(resp.get("next"));
    assertNull(resp.get("previous"));
    List<Map<String, Object>> children = (List<Map<String, Object>>) resp.get("documents");
    assertEquals(1, children.size());
    assertEquals(documentId1, children.get(0).get("documentId"));
    assertNotNull(userId, children.get(0).get("userId"));
    assertNotNull(children.get(0).get("belongsToDocumentId"));
    assertNotNull(children.get(0).get("insertedDate"));
    assertNull(children.get(0).get("siteId"));
}
Also used : DocumentItemToDynamicDocumentItem(com.formkiq.stacks.dynamodb.DocumentItemToDynamicDocumentItem) Date(java.util.Date) DocumentItemToDynamicDocumentItem(com.formkiq.stacks.dynamodb.DocumentItemToDynamicDocumentItem) DynamicDocumentItem(com.formkiq.stacks.dynamodb.DynamicDocumentItem) DynamicObject(com.formkiq.stacks.common.objects.DynamicObject) DocumentItemToDynamicDocumentItem(com.formkiq.stacks.dynamodb.DocumentItemToDynamicDocumentItem) DynamicDocumentItem(com.formkiq.stacks.dynamodb.DynamicDocumentItem) DocumentItem(com.formkiq.stacks.dynamodb.DocumentItem) ApiGatewayRequestEvent(com.formkiq.lambda.apigateway.ApiGatewayRequestEvent) ArrayList(java.util.ArrayList) List(java.util.List) DynamicObject(com.formkiq.stacks.common.objects.DynamicObject) Map(java.util.Map) DocumentItemDynamoDb(com.formkiq.stacks.dynamodb.DocumentItemDynamoDb) Test(org.junit.jupiter.api.Test)

Example 27 with DynamicDocumentItem

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

the class SearchRequestHandler method post.

@Override
public ApiRequestHandlerResponse post(final LambdaLogger logger, final ApiGatewayRequestEvent event, final ApiAuthorizer authorizer, final AwsServiceCache awsservice) throws Exception {
    DynamoDbCacheService cacheService = awsservice.documentCacheService();
    ApiPagination pagination = getPagination(cacheService, event);
    int limit = pagination != null ? pagination.getLimit() : getLimit(logger, event);
    PaginationMapToken ptoken = pagination != null ? pagination.getStartkey() : null;
    QueryRequest q = fromBodyToObject(logger, event, QueryRequest.class);
    if (q == null || q.query() == null || q.query().tag() == null) {
        throw new BadException("Invalid JSON body.");
    }
    Collection<String> documentIds = q.query().documentIds();
    if (documentIds != null) {
        if (documentIds.size() > MAX_DOCUMENT_IDS) {
            throw new BadException("Maximum number of DocumentIds is " + MAX_DOCUMENT_IDS);
        }
        if (!getQueryParameterMap(event).containsKey("limit")) {
            limit = documentIds.size();
        }
    }
    String siteId = authorizer.getSiteId();
    PaginationResults<DynamicDocumentItem> results = awsservice.documentSearchService().search(siteId, q.query(), ptoken, limit);
    ApiPagination current = createPagination(cacheService, event, pagination, results.getToken(), limit);
    List<DynamicDocumentItem> documents = subList(results.getResults(), limit);
    Map<String, Object> map = new HashMap<>();
    map.put("documents", documents);
    map.put("previous", current.getPrevious());
    map.put("next", current.hasNext() ? current.getNext() : null);
    ApiMapResponse resp = new ApiMapResponse(map);
    return new ApiRequestHandlerResponse(SC_OK, resp);
}
Also used : ApiPagination(com.formkiq.lambda.apigateway.ApiPagination) QueryRequest(com.formkiq.stacks.api.QueryRequest) HashMap(java.util.HashMap) BadException(com.formkiq.lambda.apigateway.exception.BadException) DynamicDocumentItem(com.formkiq.stacks.dynamodb.DynamicDocumentItem) DynamoDbCacheService(com.formkiq.stacks.dynamodb.DynamoDbCacheService) ApiRequestHandlerResponse(com.formkiq.lambda.apigateway.ApiRequestHandlerResponse) ApiMapResponse(com.formkiq.lambda.apigateway.ApiMapResponse) PaginationMapToken(com.formkiq.stacks.dynamodb.PaginationMapToken)

Aggregations

DynamicDocumentItem (com.formkiq.stacks.dynamodb.DynamicDocumentItem)27 Date (java.util.Date)18 Test (org.junit.jupiter.api.Test)15 DocumentItem (com.formkiq.stacks.dynamodb.DocumentItem)14 Timeout (org.junit.jupiter.api.Timeout)9 DocumentTag (com.formkiq.stacks.dynamodb.DocumentTag)7 DynamicDocumentTag (com.formkiq.stacks.dynamodb.DynamicDocumentTag)6 S3Client (software.amazon.awssdk.services.s3.S3Client)6 DocumentItemToDynamicDocumentItem (com.formkiq.stacks.dynamodb.DocumentItemToDynamicDocumentItem)5 DynamicObject (com.formkiq.stacks.common.objects.DynamicObject)4 DynamoDbClient (software.amazon.awssdk.services.dynamodb.DynamoDbClient)4 AttributeValue (software.amazon.awssdk.services.dynamodb.model.AttributeValue)4 ApiGatewayRequestEvent (com.formkiq.lambda.apigateway.ApiGatewayRequestEvent)3 ApiMapResponse (com.formkiq.lambda.apigateway.ApiMapResponse)3 ApiPagination (com.formkiq.lambda.apigateway.ApiPagination)3 ApiRequestHandlerResponse (com.formkiq.lambda.apigateway.ApiRequestHandlerResponse)3 SimpleDateFormat (java.text.SimpleDateFormat)3 HashMap (java.util.HashMap)3 List (java.util.List)3 Map (java.util.Map)3