From 180fba5f949c6365e65281929069b71c3b8912cf Mon Sep 17 00:00:00 2001 From: Vivek Kumar Date: Sat, 30 Aug 2025 00:03:54 +0530 Subject: [PATCH 1/2] Refactored method to reduce cognitive complexity --- .../encryption/EncryptionConfigBuilder.java | 58 ++++++++++--------- 1 file changed, 32 insertions(+), 26 deletions(-) diff --git a/src/main/java/com/mastercard/developer/encryption/EncryptionConfigBuilder.java b/src/main/java/com/mastercard/developer/encryption/EncryptionConfigBuilder.java index 8350483..98b28d3 100644 --- a/src/main/java/com/mastercard/developer/encryption/EncryptionConfigBuilder.java +++ b/src/main/java/com/mastercard/developer/encryption/EncryptionConfigBuilder.java @@ -54,34 +54,40 @@ static byte[] sha256digestBytes(byte[] bytes) throws NoSuchAlgorithmException { } void checkJsonPathParameterValues() { - for (Map.Entry entry : decryptionPaths.entrySet()) { - if(entry.getKey().contains("[*]") || entry.getValue().contains("[*]")){ - if(!(entry.getKey().contains("[*]") && entry.getValue().contains("[*]"))){ - throw new IllegalArgumentException("JSON paths for decryption with wildcard must both contain a wildcard!"); - } - if((entry.getKey().split("[*]", -1).length-1 > 1 || entry.getValue().split("[*]", -1).length-1 > 1)){ - throw new IllegalArgumentException("JSON paths for decryption with can only contain one wildcard!"); - } - } else { - if (!JsonPath.isPathDefinite(entry.getKey()) || !JsonPath.isPathDefinite(entry.getValue())) { - throw new IllegalArgumentException("JSON paths for decryption must point to a single item!"); - } - } + decryptionPaths.forEach((key, value) -> validatePaths(key, value, "decryption")); + encryptionPaths.forEach((key, value) -> validatePaths(key, value, "encryption")); + } + + private void validatePaths(String key, String value, String action) { + boolean keyHasWildcard = key.contains("[*]"); + boolean valueHasWildcard = value.contains("[*]"); + if (keyHasWildcard || valueHasWildcard) { + validateBothOrNoneHasWildcard(keyHasWildcard, valueHasWildcard, action); + validateSingleWildcardOnly(key, value, action); + } else { + validateDefinitePaths(key, value, action); } + } - for (Map.Entry entry : encryptionPaths.entrySet()) { - if(entry.getKey().contains("[*]") || entry.getValue().contains("[*]")){ - if(!(entry.getKey().contains("[*]") && entry.getValue().contains("[*]"))){ - throw new IllegalArgumentException("JSON paths for encryption with wildcard must both contain a wildcard!"); - } - if((entry.getKey().split("[*]", -1).length-1 > 1 || entry.getValue().split("[*]", -1).length-1 > 1)){ - throw new IllegalArgumentException("JSON paths for encryption with can only contain one wildcard!"); - } - } else { - if (!JsonPath.isPathDefinite(entry.getKey()) || !JsonPath.isPathDefinite(entry.getValue())) { - throw new IllegalArgumentException("JSON paths for encryption must point to a single item!"); - } - } + private void validateBothOrNoneHasWildcard(boolean keyHasWildcard, boolean valueHasWildcard, String action) { + if (!(keyHasWildcard && valueHasWildcard)) { + throw new IllegalArgumentException("JSON paths for " + action + " with wildcard must both contain a wildcard!"); } } + + private void validateSingleWildcardOnly(String key, String value, String action) { + if (countWildcards(key) > 1 || countWildcards(value) > 1) { + throw new IllegalArgumentException("JSON paths for " + action + " can only contain one wildcard!"); + } + } + + private void validateDefinitePaths(String key, String value, String action) { + if (!JsonPath.isPathDefinite(key) || !JsonPath.isPathDefinite(value)) { + throw new IllegalArgumentException("JSON paths for " + action + " must point to a single item!"); + } + } + + private int countWildcards(String path) { + return path.split("\\[\\*]", -1).length - 1; + } } From 2146894177adeeb36d9f12ed27a8944e588bc7fd Mon Sep 17 00:00:00 2001 From: VivekKumar97 <74033840+VivekKumar97@users.noreply.github.com> Date: Tue, 9 Sep 2025 21:33:53 +0530 Subject: [PATCH 2/2] added error message according to expecations --- .../developer/encryption/EncryptionConfigBuilder.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/mastercard/developer/encryption/EncryptionConfigBuilder.java b/src/main/java/com/mastercard/developer/encryption/EncryptionConfigBuilder.java index 98b28d3..42c6005 100644 --- a/src/main/java/com/mastercard/developer/encryption/EncryptionConfigBuilder.java +++ b/src/main/java/com/mastercard/developer/encryption/EncryptionConfigBuilder.java @@ -77,7 +77,7 @@ private void validateBothOrNoneHasWildcard(boolean keyHasWildcard, boolean value private void validateSingleWildcardOnly(String key, String value, String action) { if (countWildcards(key) > 1 || countWildcards(value) > 1) { - throw new IllegalArgumentException("JSON paths for " + action + " can only contain one wildcard!"); + throw new IllegalArgumentException("JSON paths for " + action + " with can only contain one wildcard!"); } }