Skip to content

Commit

Permalink
ConfigService cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
mfriesen committed Dec 30, 2024
1 parent 5d5a10d commit ddb61b5
Show file tree
Hide file tree
Showing 25 changed files with 382 additions and 529 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@

import java.util.Collection;
import java.util.List;
import java.util.Map;

import com.formkiq.module.actions.Action;
import com.formkiq.validation.ValidationError;
Expand All @@ -42,19 +41,22 @@ public interface ActionsValidator {
*
* @param siteId {@link String}
* @param action {@link Action}
* @param configs {@link Map}
* @param chatGptApiKey {@link String}
* @param notificationsEmail {@link String}
* @return {@link Collection} {@link ValidationError}
*/
Collection<ValidationError> validation(String siteId, Action action, Map<String, Object> configs);
Collection<ValidationError> validation(String siteId, Action action, String chatGptApiKey,
String notificationsEmail);

/**
* Validates {@link List} {@link Action}.
*
* @param siteId {@link String}
* @param action {@link Action}
* @param configs {@link Map}
* @param chatGptApiKey {@link String}
* @param notificationsEmail {@link String}
* @return {@link List} {@link Collection} {@link ValidationError}
*/
List<Collection<ValidationError>> validation(String siteId, List<Action> action,
Map<String, Object> configs);
String chatGptApiKey, String notificationsEmail);
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
import java.util.Collections;
import java.util.List;
import java.util.Map;
import com.formkiq.aws.dynamodb.DynamicObject;

import com.formkiq.aws.dynamodb.DynamoDbService;
import com.formkiq.aws.dynamodb.model.MappingRecord;
import com.formkiq.aws.dynamodb.objects.Strings;
Expand All @@ -53,10 +53,6 @@
*/
public class ActionsValidatorImpl implements ActionsValidator {

/** ChatGpt Api Key. */
private static final String CHATGPT_API_KEY = "ChatGptApiKey";
/** Notification Email. */
private static final String NOTIFICATION_EMAIL = "NotificationEmail";
/** {@link DynamoDbService}. */
private final DynamoDbService db;

Expand All @@ -81,14 +77,13 @@ private boolean hasValue(final Map<String, String> parameters, final String key)
/**
* Validate Document Tagging.
*
* @param configs {@link DynamicObject}
* @param chatGptApiKey {@link String}
* @param parameters {@link Map}
* @param errors {@link Collections} {@link ValidationError}
*/
private void validateDocumentTagging(final Map<String, Object> configs,
private void validateDocumentTagging(final String chatGptApiKey,
final Map<String, String> parameters, final Collection<ValidationError> errors) {

String chatGptApiKey = (String) configs.get(CHATGPT_API_KEY);
if (!parameters.containsKey("tags")) {
errors.add(new ValidationErrorImpl().key("parameters.tags")
.error("action 'tags' parameter is required"));
Expand All @@ -108,15 +103,13 @@ private void validateDocumentTagging(final Map<String, Object> configs,
/**
* Validate Notification Email.
*
* @param configs {@link Map}
* @param notificationEmail {@link String}
* @param action {@link Action}
* @param errors {@link Collection} {@link ValidationError}
*/
private void validateNotificationEmail(final Map<String, Object> configs, final Action action,
private void validateNotificationEmail(final String notificationEmail, final Action action,
final Collection<ValidationError> errors) {

String notificationEmail = (String) configs.get(NOTIFICATION_EMAIL);

if (isEmpty(notificationEmail)) {
errors.add(new ValidationErrorImpl().key("parameters.notificationEmail")
.error("notificationEmail is not configured"));
Expand Down Expand Up @@ -191,7 +184,7 @@ private void validateEventBridge(final Action action, final Collection<Validatio

@Override
public Collection<ValidationError> validation(final String siteId, final Action action,
final Map<String, Object> configs) {
final String chatGptApiKey, final String notificationsEmail) {
Collection<ValidationError> errors = new ArrayList<>();

if (action == null) {
Expand All @@ -210,7 +203,7 @@ public Collection<ValidationError> validation(final String siteId, final Action

} else {

validateActionParameters(siteId, action, configs, errors);
validateActionParameters(siteId, action, chatGptApiKey, notificationsEmail, errors);
}
}

Expand All @@ -219,23 +212,25 @@ public Collection<ValidationError> validation(final String siteId, final Action

@Override
public List<Collection<ValidationError>> validation(final String siteId,
final List<Action> actions, final Map<String, Object> configs) {
final List<Action> actions, final String chatGptApiKey, final String notificationsEmail) {

List<Collection<ValidationError>> errors = new ArrayList<>();
actions.forEach(a -> errors.add(validation(siteId, a, configs)));
actions.forEach(a -> errors.add(validation(siteId, a, chatGptApiKey, notificationsEmail)));
return errors;
}

private void validateActionParameters(final String siteId, final Action action,
final Map<String, Object> configs, final Collection<ValidationError> errors) {
final String chatGptApiKey, final String notificationsEmail,
final Collection<ValidationError> errors) {

Map<String, String> parameters = getParameters(action);
if (ActionType.WEBHOOK.equals(action.type()) && !parameters.containsKey("url")) {
errors.add(new ValidationErrorImpl().key("parameters.url")
.error("action 'url' parameter is required"));
} else if (ActionType.DOCUMENTTAGGING.equals(action.type())) {
validateDocumentTagging(configs, parameters, errors);
validateDocumentTagging(chatGptApiKey, parameters, errors);
} else if (ActionType.NOTIFICATION.equals(action.type())) {
validateNotificationEmail(configs, action, errors);
validateNotificationEmail(notificationsEmail, action, errors);
} else if (ActionType.QUEUE.equals(action.type())) {
validateQueue(siteId, action, errors);
} else if (ActionType.IDP.equals(action.type())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,13 @@
import static com.formkiq.testutils.aws.DynamoDbExtension.DOCUMENTS_TABLE;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import java.util.Arrays;

import java.util.Collection;
import java.util.List;
import java.util.Map;

import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import com.formkiq.aws.dynamodb.DynamicObject;
import com.formkiq.aws.dynamodb.DynamoDbConnectionBuilder;
import com.formkiq.aws.dynamodb.DynamoDbService;
import com.formkiq.aws.dynamodb.DynamoDbServiceImpl;
Expand All @@ -46,8 +45,6 @@
@ExtendWith(DynamoDbExtension.class)
class ActionsValidatorTest {

/** {@link DynamoDbService}. */
private static DynamoDbService db;
/** {@link ActionsValidator}. */
private static ActionsValidator validator;

Expand All @@ -59,19 +56,16 @@ class ActionsValidatorTest {
@BeforeAll
public static void beforeAll() throws Exception {
DynamoDbConnectionBuilder dynamoDbConnection = DynamoDbTestServices.getDynamoDbConnection();
db = new DynamoDbServiceImpl(dynamoDbConnection, DOCUMENTS_TABLE);
DynamoDbService db = new DynamoDbServiceImpl(dynamoDbConnection, DOCUMENTS_TABLE);
validator = new ActionsValidatorImpl(db);
}

@Test
void testValidation01() {
// given
String siteId = null;
Action action = null;
DynamicObject obj = new DynamicObject(Map.of());

// when
Collection<ValidationError> errors = validator.validation(siteId, action, obj);
Collection<ValidationError> errors = validator.validation(null, (Action) null, null, null);

// then
assertEquals(1, errors.size());
Expand All @@ -83,12 +77,10 @@ void testValidation01() {
@Test
void testValidation02() {
// given
String siteId = null;
Action action = new Action();
DynamicObject obj = new DynamicObject(Map.of());

// when
Collection<ValidationError> errors = validator.validation(siteId, action, obj);
Collection<ValidationError> errors = validator.validation(null, action, null, null);

// then
assertEquals(1, errors.size());
Expand All @@ -100,12 +92,10 @@ void testValidation02() {
@Test
void testValidation03() {
// given
String siteId = null;
Action action = new Action().type(ActionType.WEBHOOK).userId("joe");
DynamicObject obj = new DynamicObject(Map.of());

// when
Collection<ValidationError> errors = validator.validation(siteId, action, obj);
Collection<ValidationError> errors = validator.validation(null, action, null, null);

// then
assertEquals(1, errors.size());
Expand All @@ -117,13 +107,11 @@ void testValidation03() {
@Test
void testValidation04() {
// given
String siteId = null;
Action action = new Action();
DynamicObject obj = new DynamicObject(Map.of());
List<Action> actions = Arrays.asList(action);
List<Action> actions = List.of(action);

// when
List<Collection<ValidationError>> errorList = validator.validation(siteId, actions, obj);
List<Collection<ValidationError>> errorList = validator.validation(null, actions, null, null);

// then
assertEquals(1, errorList.size());
Expand All @@ -137,12 +125,10 @@ void testValidation04() {
@Test
void testValidation05() {
// given
String siteId = null;
Action action = new Action().type(ActionType.OCR).userId("joe");
DynamicObject obj = new DynamicObject(Map.of());

// when
Collection<ValidationError> errorList = validator.validation(siteId, action, obj);
Collection<ValidationError> errorList = validator.validation(null, action, null, null);

// then
assertEquals(0, errorList.size());
Expand All @@ -151,12 +137,10 @@ void testValidation05() {
@Test
void testValidation06() {
// given
String siteId = null;
Action action = new Action().type(ActionType.QUEUE).userId("joe");
DynamicObject obj = new DynamicObject(Map.of());

// when
Collection<ValidationError> errors = validator.validation(siteId, action, obj);
Collection<ValidationError> errors = validator.validation(null, action, null, null);

// then
assertEquals(1, errors.size());
Expand All @@ -169,12 +153,10 @@ void testValidation06() {
@Test
void testValidation07() {
// given
String siteId = null;
Action action = new Action().type(ActionType.QUEUE).queueId("Testqueue").userId("joe");
DynamicObject obj = new DynamicObject(Map.of());

// when
Collection<ValidationError> errors = validator.validation(siteId, action, obj);
Collection<ValidationError> errors = validator.validation(null, action, null, null);

// then
assertEquals(1, errors.size());
Expand All @@ -190,12 +172,10 @@ void testValidation07() {
@Test
void testValidation08() {
// given
String siteId = null;
Action action = new Action().type(ActionType.EVENTBRIDGE).userId("joe");
DynamicObject obj = new DynamicObject(Map.of());

// when
Collection<ValidationError> errors = validator.validation(siteId, action, obj);
Collection<ValidationError> errors = validator.validation(null, action, null, null);

// then
assertEquals(1, errors.size());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,6 @@
*/
package com.formkiq.stacks.dynamodb.config;

import com.formkiq.aws.dynamodb.DynamicObject;

import java.util.Map;

/** Config Service. */
public interface ConfigService {

Expand Down Expand Up @@ -65,9 +61,9 @@ public interface ConfigService {
* Get Config.
*
* @param siteId Optional Grouping siteId
* @return {@link DynamicObject}
* @return {@link SiteConfiguration}
*/
Map<String, Object> get(String siteId);
SiteConfiguration get(String siteId);

/**
* Save Config.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,17 +69,68 @@ public void delete(final String siteId) {
}

@Override
public Map<String, Object> get(final String siteId) {
public SiteConfiguration get(final String siteId) {

List<Map<String, AttributeValue>> keys = new ArrayList<>();

String site = !isDefaultSiteId(siteId) ? siteId : DEFAULT_SITE_ID;
keys.add(keysGeneric(null, PREFIX_CONFIG, site));

BatchGetConfig config = new BatchGetConfig();
List<Map<String, AttributeValue>> list = this.db.getBatch(config, keys);
BatchGetConfig batchConfig = new BatchGetConfig();
List<Map<String, AttributeValue>> list = this.db.getBatch(batchConfig, keys);

return !list.isEmpty() ? new AttributeValueToMap().apply(list.get(0)) : Map.of();
Map<String, Object> map =
!list.isEmpty() ? new AttributeValueToMap().apply(list.get(0)) : Map.of();
return transform(map);
}

private SiteConfiguration transform(final Map<String, Object> map) {

SiteConfiguration config = new SiteConfiguration();
config.setChatGptApiKey(getString(map.get(CHATGPT_API_KEY)));
config.setGoogle(transformGoogle(map));
config.setMaxWebhooks(getString(map.get(MAX_WEBHOOKS)));
config.setMaxDocuments(getString(map.get(MAX_DOCUMENTS)));
config.setDocumentTimeToLive(getString(map.get(DOCUMENT_TIME_TO_LIVE)));
config.setDocusign(transformDocusign(map));
config.setMaxContentLengthBytes(getString(map.get(MAX_DOCUMENT_SIZE_BYTES)));
config.setNotificationEmail(getString(map.get(NOTIFICATION_EMAIL)));
config.setOcr(transformOcr(map));
config.setWebhookTimeToLive(getString(map.get(WEBHOOK_TIME_TO_LIVE)));
return config;
}

private SiteConfigurationOcr transformOcr(final Map<String, Object> map) {
SiteConfigurationOcr ocr = new SiteConfigurationOcr();
ocr.setMaxTransactions(
map.containsKey("maxTransactions") ? ((Double) map.get("maxTransactions")).longValue()
: -1);
ocr.setMaxPagesPerTransaction(map.containsKey("maxPagesPerTransaction")
? ((Double) map.get("maxPagesPerTransaction")).longValue()
: -1);
return ocr;
}

private SiteConfigurationDocusign transformDocusign(final Map<String, Object> map) {
SiteConfigurationDocusign docusign = new SiteConfigurationDocusign();
docusign.setUserId(getString(map.get(KEY_DOCUSIGN_USER_ID)));
docusign.setRsaPrivateKey(getString(map.get(KEY_DOCUSIGN_RSA_PRIVATE_KEY)));
docusign.setHmacSignature(getString(map.get(KEY_DOCUSIGN_HMAC_SIGNATURE)));
docusign.setIntegrationKey(getString(map.get(KEY_DOCUSIGN_INTEGRATION_KEY)));

return docusign;
}

private SiteConfigurationGoogle transformGoogle(final Map<String, Object> map) {
SiteConfigurationGoogle google = new SiteConfigurationGoogle();
google.setWorkloadIdentityAudience(getString(map.get("googleWorkloadIdentityAudience")));
google.setWorkloadIdentityServiceAccount(
getString(map.get("googleWorkloadIdentityServiceAccount")));
return google;
}

private String getString(final Object o) {
return o != null ? o.toString() : "";
}

@Override
Expand Down
Loading

0 comments on commit ddb61b5

Please sign in to comment.