Search in sources :

Example 61 with ApiGatewayRequestEvent

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

the class ApiDocumentsSearchRequestTest method testHandleSearchRequest05.

/**
 * InValid POST search body.
 *
 * @throws Exception an error has occurred
 */
@Test
public void testHandleSearchRequest05() throws Exception {
    for (String siteId : Arrays.asList(null, UUID.randomUUID().toString())) {
        // given
        final String expected = "{" + getHeaders() + ",\"body\":\"" + "{\\\"message\\\":\\\"Invalid JSON body.\\\"}\",\"statusCode\":400}";
        ApiGatewayRequestEvent event = toRequestEvent("/request-post-search02.json");
        addParameter(event, "siteId", siteId);
        // when
        String response = handleRequest(event);
        // then
        assertEquals(expected, response);
    }
}
Also used : ApiGatewayRequestEvent(com.formkiq.lambda.apigateway.ApiGatewayRequestEvent) Test(org.junit.jupiter.api.Test)

Example 62 with ApiGatewayRequestEvent

use of com.formkiq.lambda.apigateway.ApiGatewayRequestEvent 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 63 with ApiGatewayRequestEvent

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

the class ApiDocumentsSearchRequestTest method testHandleSearchRequest06.

/**
 * Valid POST search no results.
 *
 * @throws Exception an error has occurred
 */
@SuppressWarnings("unchecked")
@Test
public void testHandleSearchRequest06() throws Exception {
    for (String siteId : Arrays.asList(null, UUID.randomUUID().toString())) {
        // given
        ApiGatewayRequestEvent event = toRequestEvent("/request-post-search01.json");
        addParameter(event, "siteId", siteId);
        // 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(0, documents.size());
    }
}
Also used : DynamicObject(com.formkiq.stacks.common.objects.DynamicObject) ApiGatewayRequestEvent(com.formkiq.lambda.apigateway.ApiGatewayRequestEvent) Map(java.util.Map) Test(org.junit.jupiter.api.Test)

Example 64 with ApiGatewayRequestEvent

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

the class ApiDocumentsSitesRequestTest method testHandleGetSites03.

/**
 * Get /sites with 'read' permissions SES support.
 *
 * @throws Exception an error has occurred
 */
@SuppressWarnings("unchecked")
@Test
public void testHandleGetSites03() throws Exception {
    // given
    putSsmParameter("/formkiq/" + FORMKIQ_APP_ENVIRONMENT + "/maildomain", "tryformkiq.com");
    removeSsmParameter(String.format("/formkiq/%s/siteid/%s/email", FORMKIQ_APP_ENVIRONMENT, "default"));
    ApiGatewayRequestEvent event = toRequestEvent("/request-get-sites01.json");
    setCognitoGroup(event, "default_read finance");
    // 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")));
    DynamicObject resp = new DynamicObject(fromJson(m.get("body"), Map.class));
    assertNull(resp.get("next"));
    assertNull(resp.get("previous"));
    List<DynamicObject> sites = resp.getList("sites");
    assertEquals(2, sites.size());
    assertEquals(DEFAULT_SITE_ID, sites.get(0).get("siteId"));
    assertNull(sites.get(0).get("uploadEmail"));
    assertNull(getSsmParameter(String.format("/formkiq/%s/siteid/%s/email", FORMKIQ_APP_ENVIRONMENT, sites.get(0).get("siteId"))));
    assertEquals("finance", sites.get(1).get("siteId"));
    assertNotNull(sites.get(1).get("uploadEmail"));
    String uploadEmail = sites.get(1).getString("uploadEmail");
    assertTrue(uploadEmail.endsWith("@tryformkiq.com"));
    assertTrue(Pattern.matches(EMAIL, uploadEmail.subSequence(0, uploadEmail.indexOf("@"))));
    assertNotNull(getSsmParameter(String.format("/formkiq/%s/siteid/%s/email", FORMKIQ_APP_ENVIRONMENT, sites.get(1).get("siteId"))));
    String[] strs = uploadEmail.split("@");
    assertEquals("{\"siteId\":\"finance\", \"appEnvironment\":\"" + FORMKIQ_APP_ENVIRONMENT + "\"}", getSsmParameter(String.format("/formkiq/ses/%s/%s", strs[1], strs[0])));
}
Also used : DynamicObject(com.formkiq.stacks.common.objects.DynamicObject) ApiGatewayRequestEvent(com.formkiq.lambda.apigateway.ApiGatewayRequestEvent) Map(java.util.Map) Test(org.junit.jupiter.api.Test)

Example 65 with ApiGatewayRequestEvent

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

the class ApiDocumentsSitesRequestTest method testHandleGetSites01.

/**
 * Get /sites with SES support.
 *
 * @throws Exception an error has occurred
 */
@SuppressWarnings("unchecked")
@Test
public void testHandleGetSites01() throws Exception {
    // given
    putSsmParameter("/formkiq/" + FORMKIQ_APP_ENVIRONMENT + "/maildomain", "tryformkiq.com");
    ApiGatewayRequestEvent event = toRequestEvent("/request-get-sites01.json");
    // 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")));
    DynamicObject resp = new DynamicObject(fromJson(m.get("body"), Map.class));
    assertNull(resp.getString("next"));
    assertNull(resp.getString("previous"));
    List<DynamicObject> sites = resp.getList("sites");
    assertEquals(2, sites.size());
    assertEquals(DEFAULT_SITE_ID, sites.get(0).get("siteId"));
    assertNotNull(sites.get(0).get("uploadEmail"));
    String uploadEmail = sites.get(0).getString("uploadEmail");
    assertTrue(uploadEmail.endsWith("@tryformkiq.com"));
    assertTrue(Pattern.matches(EMAIL, uploadEmail.subSequence(0, uploadEmail.indexOf("@"))));
    assertNotNull(getSsmParameter(String.format("/formkiq/%s/siteid/%s/email", FORMKIQ_APP_ENVIRONMENT, sites.get(0).get("siteId"))));
    String[] strs = uploadEmail.split("@");
    assertEquals("tryformkiq.com", strs[1]);
    assertEquals("{\"siteId\":\"default\", \"appEnvironment\":\"" + FORMKIQ_APP_ENVIRONMENT + "\"}", getSsmParameter(String.format("/formkiq/ses/%s/%s", strs[1], strs[0])));
    assertEquals("finance", sites.get(1).get("siteId"));
    assertNotNull(sites.get(1).get("uploadEmail"));
    uploadEmail = sites.get(1).getString("uploadEmail");
    assertTrue(uploadEmail.endsWith("@tryformkiq.com"));
    assertTrue(Pattern.matches(EMAIL, uploadEmail.subSequence(0, uploadEmail.indexOf("@"))));
    assertNotNull(getSsmParameter(String.format("/formkiq/%s/siteid/%s/email", FORMKIQ_APP_ENVIRONMENT, sites.get(1).get("siteId"))));
    strs = uploadEmail.split("@");
    assertEquals("{\"siteId\":\"finance\", \"appEnvironment\":\"" + FORMKIQ_APP_ENVIRONMENT + "\"}", getSsmParameter(String.format("/formkiq/ses/%s/%s", strs[1], strs[0])));
}
Also used : DynamicObject(com.formkiq.stacks.common.objects.DynamicObject) ApiGatewayRequestEvent(com.formkiq.lambda.apigateway.ApiGatewayRequestEvent) Map(java.util.Map) 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