From 153a06d19988062b5451d1f0cb01b59373ba1c19 Mon Sep 17 00:00:00 2001 From: nchandra Date: Sun, 28 Jan 2024 22:59:52 +0000 Subject: [PATCH] ISSUES-603 Mask applied for logging n removed for API exec --- .../ZeroCodeAssertionsProcessor.java | 4 + .../ZeroCodeAssertionsProcessorImpl.java | 74 +++++++++++++++---- .../ZeroCodeMultiStepsScenarioRunnerImpl.java | 23 ++++-- .../zerocode/core/utils/TokenUtils.java | 2 +- .../zerocode/core/utils/TokenUtilsTest.java | 10 +-- 5 files changed, 83 insertions(+), 30 deletions(-) diff --git a/core/src/main/java/org/jsmart/zerocode/core/engine/preprocessor/ZeroCodeAssertionsProcessor.java b/core/src/main/java/org/jsmart/zerocode/core/engine/preprocessor/ZeroCodeAssertionsProcessor.java index 0d5ac829c..c74a8d9b8 100644 --- a/core/src/main/java/org/jsmart/zerocode/core/engine/preprocessor/ZeroCodeAssertionsProcessor.java +++ b/core/src/main/java/org/jsmart/zerocode/core/engine/preprocessor/ZeroCodeAssertionsProcessor.java @@ -22,4 +22,8 @@ public interface ZeroCodeAssertionsProcessor { Step resolveJsonContent(Step thisStep, ScenarioExecutionState scenarioExecutionState); + String fieldMasksRemoved(String resolvedRequestJson); + + String fieldMasksApplied(String resolvedRequestJson); + } diff --git a/core/src/main/java/org/jsmart/zerocode/core/engine/preprocessor/ZeroCodeAssertionsProcessorImpl.java b/core/src/main/java/org/jsmart/zerocode/core/engine/preprocessor/ZeroCodeAssertionsProcessorImpl.java index cafb18f35..e083918fc 100644 --- a/core/src/main/java/org/jsmart/zerocode/core/engine/preprocessor/ZeroCodeAssertionsProcessorImpl.java +++ b/core/src/main/java/org/jsmart/zerocode/core/engine/preprocessor/ZeroCodeAssertionsProcessorImpl.java @@ -8,6 +8,28 @@ import com.google.inject.Inject; import com.google.inject.name.Named; import com.jayway.jsonpath.JsonPath; +import net.minidev.json.JSONArray; +import org.apache.commons.lang.text.StrSubstitutor; +import org.jsmart.zerocode.core.domain.Step; +import org.jsmart.zerocode.core.engine.assertion.FieldAssertionMatcher; +import org.jsmart.zerocode.core.engine.assertion.JsonAsserter; +import org.jsmart.zerocode.core.engine.assertion.array.ArrayIsEmptyAsserterImpl; +import org.jsmart.zerocode.core.engine.assertion.array.ArraySizeAsserterImpl; +import org.jsmart.zerocode.core.engine.assertion.field.FieldContainsStringAsserter; +import org.jsmart.zerocode.core.engine.assertion.field.FieldContainsStringIgnoreCaseAsserter; +import org.jsmart.zerocode.core.engine.assertion.field.FieldHasDateAfterValueAsserter; +import org.jsmart.zerocode.core.engine.assertion.field.FieldHasDateBeforeValueAsserter; +import org.jsmart.zerocode.core.engine.assertion.field.FieldHasEqualNumberValueAsserter; +import org.jsmart.zerocode.core.engine.assertion.field.FieldHasExactValueAsserter; +import org.jsmart.zerocode.core.engine.assertion.field.FieldHasGreaterThanValueAsserter; +import org.jsmart.zerocode.core.engine.assertion.field.FieldHasInEqualNumberValueAsserter; +import org.jsmart.zerocode.core.engine.assertion.field.FieldHasLesserThanValueAsserter; +import org.jsmart.zerocode.core.engine.assertion.field.FieldIsNotNullAsserter; +import org.jsmart.zerocode.core.engine.assertion.field.FieldIsNullAsserter; +import org.jsmart.zerocode.core.engine.assertion.field.FieldIsOneOfValueAsserter; +import org.jsmart.zerocode.core.engine.assertion.field.FieldMatchesCustomAsserter; +import org.jsmart.zerocode.core.engine.assertion.field.FieldMatchesRegexPatternAsserter; + import java.io.IOException; import java.math.BigDecimal; import java.time.LocalDateTime; @@ -19,37 +41,47 @@ import java.util.List; import java.util.Map; import java.util.Properties; -import net.minidev.json.JSONArray; -import org.apache.commons.lang.text.StrSubstitutor; -import org.jsmart.zerocode.core.domain.Step; -import org.jsmart.zerocode.core.engine.assertion.FieldAssertionMatcher; -import org.jsmart.zerocode.core.engine.assertion.JsonAsserter; -import org.jsmart.zerocode.core.engine.assertion.array.ArrayIsEmptyAsserterImpl; -import org.jsmart.zerocode.core.engine.assertion.array.ArraySizeAsserterImpl; -import org.jsmart.zerocode.core.engine.assertion.field.*; -import org.jsmart.zerocode.core.utils.SmartUtils; import static java.lang.Integer.valueOf; import static java.lang.String.format; import static org.apache.commons.lang.StringEscapeUtils.escapeJava; import static org.apache.commons.lang.StringUtils.substringBetween; -import static org.jsmart.zerocode.core.engine.tokens.ZeroCodeAssertionTokens.*; +import static org.jsmart.zerocode.core.engine.tokens.ZeroCodeAssertionTokens.ASSERT_LOCAL_DATETIME_AFTER; +import static org.jsmart.zerocode.core.engine.tokens.ZeroCodeAssertionTokens.ASSERT_LOCAL_DATETIME_BEFORE; +import static org.jsmart.zerocode.core.engine.tokens.ZeroCodeAssertionTokens.ASSERT_PATH_SIZE; +import static org.jsmart.zerocode.core.engine.tokens.ZeroCodeAssertionTokens.ASSERT_VALUE_CONTAINS_STRING; +import static org.jsmart.zerocode.core.engine.tokens.ZeroCodeAssertionTokens.ASSERT_VALUE_CONTAINS_STRING_IGNORE_CASE; +import static org.jsmart.zerocode.core.engine.tokens.ZeroCodeAssertionTokens.ASSERT_VALUE_CUSTOM_ASSERT; +import static org.jsmart.zerocode.core.engine.tokens.ZeroCodeAssertionTokens.ASSERT_VALUE_EMPTY_ARRAY; +import static org.jsmart.zerocode.core.engine.tokens.ZeroCodeAssertionTokens.ASSERT_VALUE_EQUAL_TO_NUMBER; +import static org.jsmart.zerocode.core.engine.tokens.ZeroCodeAssertionTokens.ASSERT_VALUE_GREATER_THAN; +import static org.jsmart.zerocode.core.engine.tokens.ZeroCodeAssertionTokens.ASSERT_VALUE_IS_NOT_NULL; +import static org.jsmart.zerocode.core.engine.tokens.ZeroCodeAssertionTokens.ASSERT_VALUE_IS_NULL; +import static org.jsmart.zerocode.core.engine.tokens.ZeroCodeAssertionTokens.ASSERT_VALUE_IS_ONE_OF; +import static org.jsmart.zerocode.core.engine.tokens.ZeroCodeAssertionTokens.ASSERT_VALUE_LESSER_THAN; +import static org.jsmart.zerocode.core.engine.tokens.ZeroCodeAssertionTokens.ASSERT_VALUE_MATCHES_STRING; +import static org.jsmart.zerocode.core.engine.tokens.ZeroCodeAssertionTokens.ASSERT_VALUE_NOT_EQUAL_TO_NUMBER; +import static org.jsmart.zerocode.core.engine.tokens.ZeroCodeAssertionTokens.ASSERT_VALUE_NOT_NULL; +import static org.jsmart.zerocode.core.engine.tokens.ZeroCodeAssertionTokens.ASSERT_VALUE_NULL; +import static org.jsmart.zerocode.core.engine.tokens.ZeroCodeAssertionTokens.ASSERT_VALUE_ONE_OF; +import static org.jsmart.zerocode.core.engine.tokens.ZeroCodeAssertionTokens.RAW_BODY; import static org.jsmart.zerocode.core.engine.tokens.ZeroCodeValueTokens.$VALUE; import static org.jsmart.zerocode.core.engine.tokens.ZeroCodeValueTokens.JSON_CONTENT; -import static org.jsmart.zerocode.core.engine.tokens.ZeroCodeValueTokens.JSON_PAYLOAD_FILE; -import static org.jsmart.zerocode.core.engine.tokens.ZeroCodeValueTokens.YAML_PAYLOAD_FILE; import static org.jsmart.zerocode.core.utils.FieldTypeConversionUtils.deepTypeCast; import static org.jsmart.zerocode.core.utils.FieldTypeConversionUtils.fieldTypes; import static org.jsmart.zerocode.core.utils.PropertiesProviderUtils.loadAbsoluteProperties; +import static org.jsmart.zerocode.core.utils.SmartUtils.checkDigNeeded; +import static org.jsmart.zerocode.core.utils.SmartUtils.getJsonFilePhToken; import static org.jsmart.zerocode.core.utils.SmartUtils.isValidAbsolutePath; -import static org.jsmart.zerocode.core.utils.SmartUtils.readJsonAsString; -import static org.jsmart.zerocode.core.utils.SmartUtils.readYamlAsString; -import static org.jsmart.zerocode.core.utils.SmartUtils.checkDigNeeded;; -import static org.jsmart.zerocode.core.utils.SmartUtils.getJsonFilePhToken;; +import static org.jsmart.zerocode.core.utils.TokenUtils.getMasksRemoved; +import static org.jsmart.zerocode.core.utils.TokenUtils.getMasksReplaced; import static org.jsmart.zerocode.core.utils.TokenUtils.getTestCaseTokens; import static org.jsmart.zerocode.core.utils.TokenUtils.populateParamMap; import static org.slf4j.LoggerFactory.getLogger; +; +; + public class ZeroCodeAssertionsProcessorImpl implements ZeroCodeAssertionsProcessor { private static final org.slf4j.Logger LOGGER = getLogger(ZeroCodeAssertionsProcessorImpl.class); @@ -380,6 +412,16 @@ public Step resolveJsonContent(Step thisStep, ScenarioExecutionState scenarioExe } } + @Override + public String fieldMasksRemoved(String resolvedRequestJson) { + return getMasksRemoved(resolvedRequestJson); + } + + @Override + public String fieldMasksApplied(String resolvedRequestJson) { + return getMasksReplaced(resolvedRequestJson); + } + private void loadAnnotatedHostProperties() { try { if(isValidAbsolutePath(hostFileName)){ diff --git a/core/src/main/java/org/jsmart/zerocode/core/runner/ZeroCodeMultiStepsScenarioRunnerImpl.java b/core/src/main/java/org/jsmart/zerocode/core/runner/ZeroCodeMultiStepsScenarioRunnerImpl.java index fd9190e76..7808a5fd0 100644 --- a/core/src/main/java/org/jsmart/zerocode/core/runner/ZeroCodeMultiStepsScenarioRunnerImpl.java +++ b/core/src/main/java/org/jsmart/zerocode/core/runner/ZeroCodeMultiStepsScenarioRunnerImpl.java @@ -414,6 +414,13 @@ private String executeApi(String logPrefixRelationshipId, // -------------------------------- url = zeroCodeAssertionsProcessor.resolveStringJson(url, scenarioExecutionState.getResolvedScenarioState()); + // ------------------------------------------------ + // 1) Removed the MASKED wrapper for API execution (For logging) + // 2) Replace the MASKED field with masked content (For API executions) + // ------------------------------------------------ + String resolvedRequestJsonMaskRemoved = zeroCodeAssertionsProcessor.fieldMasksRemoved(resolvedRequestJson); + String resolvedRequestJsonMaskApplied = zeroCodeAssertionsProcessor.fieldMasksApplied(resolvedRequestJson); + final LocalDateTime requestTimeStamp = LocalDateTime.now(); String executionResult; @@ -428,9 +435,9 @@ private String executeApi(String logPrefixRelationshipId, .url(url) .method(operationName) .id(stepId) - .request(prettyPrintJson(resolvedRequestJson)); + .request(prettyPrintJson(resolvedRequestJsonMaskApplied)); - executionResult = apiExecutor.executeHttpApi(url, operationName, resolvedRequestJson); + executionResult = apiExecutor.executeHttpApi(url, operationName, resolvedRequestJsonMaskRemoved); break; case JAVA_CALL: @@ -441,10 +448,10 @@ private String executeApi(String logPrefixRelationshipId, .id(stepId) .url(url) .method(operationName) - .request(prettyPrintJson(resolvedRequestJson)); + .request(prettyPrintJson(resolvedRequestJsonMaskApplied)); url = apiTypeUtils.getQualifiedJavaApi(url); - executionResult = apiExecutor.executeJavaOperation(url, operationName, resolvedRequestJson); + executionResult = apiExecutor.executeJavaOperation(url, operationName, resolvedRequestJsonMaskRemoved); break; case KAFKA_CALL: @@ -459,10 +466,10 @@ private String executeApi(String logPrefixRelationshipId, .url(url) .method(operationName.toUpperCase()) .id(stepId) - .request(prettyPrintJson(resolvedRequestJson)); + .request(prettyPrintJson(resolvedRequestJsonMaskApplied)); String topicName = url.substring(KAFKA_TOPIC.length()); - executionResult = apiExecutor.executeKafkaService(kafkaServers, topicName, operationName, resolvedRequestJson, scenarioExecutionState); + executionResult = apiExecutor.executeKafkaService(kafkaServers, topicName, operationName, resolvedRequestJsonMaskRemoved, scenarioExecutionState); break; case NONE: @@ -473,14 +480,14 @@ private String executeApi(String logPrefixRelationshipId, .id(stepId) .url(url) .method(operationName) - .request(prettyPrintJson(resolvedRequestJson)); + .request(prettyPrintJson(resolvedRequestJsonMaskApplied)); executionResult = prettyPrintJson(resolvedRequestJson); break; default: throw new RuntimeException("Oops! API Type Undecided. If it is intentional, " + - "then keep the value as empty to receive the request in the response"); + "then keep the value as empty to receive the request as response"); } return executionResult; diff --git a/core/src/main/java/org/jsmart/zerocode/core/utils/TokenUtils.java b/core/src/main/java/org/jsmart/zerocode/core/utils/TokenUtils.java index e9e4707c6..367c1ff42 100644 --- a/core/src/main/java/org/jsmart/zerocode/core/utils/TokenUtils.java +++ b/core/src/main/java/org/jsmart/zerocode/core/utils/TokenUtils.java @@ -146,7 +146,7 @@ public static List getTestCaseTokens(String aString) { return keyTokens; } - public static String getMaskedTokensReplaced(String aString) { + public static String getMasksReplaced(String aString) { String regex = "\\$\\{MASKED:([^\\}]*)\\}"; Matcher maskMatcher = Pattern.compile(regex).matcher(aString); while(maskMatcher.find()) { diff --git a/core/src/test/java/org/jsmart/zerocode/core/utils/TokenUtilsTest.java b/core/src/test/java/org/jsmart/zerocode/core/utils/TokenUtilsTest.java index 6c01ac6c8..a4dedeadd 100644 --- a/core/src/test/java/org/jsmart/zerocode/core/utils/TokenUtilsTest.java +++ b/core/src/test/java/org/jsmart/zerocode/core/utils/TokenUtilsTest.java @@ -10,7 +10,7 @@ import static org.hamcrest.CoreMatchers.containsString; import static org.hamcrest.CoreMatchers.is; import static org.jsmart.zerocode.core.utils.TokenUtils.absolutePathOf; -import static org.jsmart.zerocode.core.utils.TokenUtils.getMaskedTokensReplaced; +import static org.jsmart.zerocode.core.utils.TokenUtils.getMasksReplaced; import static org.jsmart.zerocode.core.utils.TokenUtils.getMasksRemoved; import static org.jsmart.zerocode.core.utils.TokenUtils.resolveKnownTokens; import static org.junit.Assert.*; @@ -175,22 +175,22 @@ public void testAbsolutePathOf() { @Test public void testGetMaskedTokensReplaced_multipleOccurrences(){ - assertEquals("This is a ***masked*** message with ***masked*** tokens.", getMaskedTokensReplaced("This is a ${MASKED:secret} message with ${MASKED:masked} tokens.")); + assertEquals("This is a ***masked*** message with ***masked*** tokens.", getMasksReplaced("This is a ${MASKED:secret} message with ${MASKED:masked} tokens.")); } @Test public void testGetMaskedTokensReplaced_noOccurrences(){ - assertEquals("This string has no masked tokens.", getMaskedTokensReplaced("This string has no masked tokens.")); + assertEquals("This string has no masked tokens.", getMasksReplaced("This string has no masked tokens.")); } @Test public void testGetMaskedTokensReplaced_emptyString(){ - assertEquals("", getMaskedTokensReplaced("")); + assertEquals("", getMasksReplaced("")); } @Test public void testGetMaskedTokensReplaced_specialCharacters(){ - assertEquals("***masked*** and ***masked***", getMaskedTokensReplaced("${MASKED:abc@123} and ${MASKED:!@#$%^}")); + assertEquals("***masked*** and ***masked***", getMasksReplaced("${MASKED:abc@123} and ${MASKED:!@#$%^}")); } @Test