Skip to content

Commit 8b42170

Browse files
committed
Clean-up
1 parent 77fce5a commit 8b42170

File tree

5 files changed

+116
-11
lines changed

5 files changed

+116
-11
lines changed

src/main/java/com/mastercard/developer/encryption/FieldLevelEncryption.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,8 @@ private static void decryptPayloadPath(DocumentContext payloadContext, String js
201201
return;
202202
}
203203

204-
if ((config.encryptedKeyFieldName == null || config.ivFieldName == null) && params == null) {
205-
throw new IllegalStateException("Encryption params have to be passed in argument or field names have to be set in config!");
204+
if (!config.useHttpPayloads() && params == null) {
205+
throw new IllegalStateException("Encryption params have to be set when not stored in HTTP payloads!");
206206
}
207207

208208
if (params == null) {

src/main/java/com/mastercard/developer/encryption/FieldLevelEncryptionConfig.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,13 @@ public boolean useHttpHeaders() {
149149
return encryptedKeyHeaderName != null && ivHeaderName != null;
150150
}
151151

152+
/**
153+
* If the encryption parameters must be written to/read from HTTP payloads.
154+
*/
155+
public boolean useHttpPayloads() {
156+
return encryptedKeyFieldName != null && ivFieldName != null;
157+
}
158+
152159
public String getOaepPaddingDigestAlgorithmHeaderName() {
153160
return oaepPaddingDigestAlgorithmHeaderName;
154161
}

src/main/java/com/mastercard/developer/json/JsonEngine.java

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,6 @@ public boolean isNullOrEmptyJson(Object jsonElement) {
100100

101101
/**
102102
* Get JSON path to the parent of the object at the given JSON path.
103-
* Examples:
104-
* - "$['obj1']['obj2']" will return "$['obj1']"
105-
* - "$.obj1.obj2" will return "$['obj1']"
106-
* - "obj1.obj2" will return "$['obj1']"
107103
*/
108104
public static String getParentJsonPath(String jsonPathString) {
109105
JsonPath jsonPath = JsonPath.compile(jsonPathString);
@@ -117,10 +113,6 @@ public static String getParentJsonPath(String jsonPathString) {
117113

118114
/**
119115
* Get object key at the given JSON path.
120-
* Examples:
121-
* - "$['obj1']['obj2']" will return "obj2"
122-
* - "$.obj1.obj2" will return "obj2"
123-
* - "obj1.obj2" will return "obj2"
124116
*/
125117
public static String getJsonElementKey(String jsonPathString) {
126118
JsonPath jsonPath = JsonPath.compile(jsonPathString);

src/test/java/com/mastercard/developer/encryption/FieldLevelEncryptionWithDefaultJsonEngineTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1200,7 +1200,7 @@ public void testDecryptPayload_ShouldThrowIllegalStateException_WhenEncryptionPa
12001200

12011201
// THEN
12021202
expectedException.expect(IllegalStateException.class);
1203-
expectedException.expectMessage("Encryption params have to be passed in argument or field names have to be set in config!");
1203+
expectedException.expectMessage("Encryption params have to be set when not stored in HTTP payloads!");
12041204

12051205
// WHEN
12061206
FieldLevelEncryption.decryptPayload(encryptedPayload, config, null);
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package com.mastercard.developer.json;
2+
3+
import org.junit.Assert;
4+
import org.junit.Rule;
5+
import org.junit.Test;
6+
import org.junit.rules.ExpectedException;
7+
8+
public class JsonEngineTest {
9+
10+
@Rule
11+
public ExpectedException expectedException = ExpectedException.none();
12+
13+
@Test
14+
public void testGetParentJsonPath_Nominal() {
15+
16+
// GIVEN
17+
String jsonPath1 = "$['obj1']['obj2']";
18+
String jsonPath2 = "obj1.obj2";
19+
String jsonPath3 = "$.obj1.obj2";
20+
21+
// WHEN
22+
String parentJsonPath1 = JsonEngine.getParentJsonPath(jsonPath1);
23+
String parentJsonPath2 = JsonEngine.getParentJsonPath(jsonPath2);
24+
String parentJsonPath3 = JsonEngine.getParentJsonPath(jsonPath3);
25+
26+
// THEN
27+
Assert.assertEquals("$['obj1']", parentJsonPath1);
28+
Assert.assertEquals("$['obj1']", parentJsonPath2);
29+
Assert.assertEquals("$['obj1']", parentJsonPath3);
30+
}
31+
32+
@Test
33+
public void testGetJsonElementKey_Nominal() {
34+
35+
// GIVEN
36+
String jsonPath1 = "$['obj1']['obj2']";
37+
String jsonPath2 = "obj1.obj2";
38+
String jsonPath3 = "$.obj1.obj2";
39+
40+
// WHEN
41+
String jsonElementKey1 = JsonEngine.getJsonElementKey(jsonPath1);
42+
String jsonElementKey2 = JsonEngine.getJsonElementKey(jsonPath2);
43+
String jsonElementKey3 = JsonEngine.getJsonElementKey(jsonPath3);
44+
45+
// THEN
46+
Assert.assertEquals("obj2", jsonElementKey1);
47+
Assert.assertEquals("obj2", jsonElementKey2);
48+
Assert.assertEquals("obj2", jsonElementKey3);
49+
}
50+
51+
@Test
52+
public void testGetParentJsonPath_ShouldThrowIllegalArgumentException_WhenJsonPathNullOrEmpty() {
53+
54+
// GIVEN
55+
String jsonPath = "";
56+
57+
// THEN
58+
expectedException.expect(IllegalArgumentException.class);
59+
expectedException.expectMessage("json can not be null or empty");
60+
61+
// WHEN
62+
JsonEngine.getParentJsonPath(jsonPath);
63+
}
64+
65+
@Test
66+
public void testGetJsonElementKey_ShouldThrowIllegalArgumentException_WhenJsonPathNullOrEmpty() {
67+
68+
// GIVEN
69+
String jsonPath = "";
70+
71+
// THEN
72+
expectedException.expect(IllegalArgumentException.class);
73+
expectedException.expectMessage("json can not be null or empty");
74+
75+
// WHEN
76+
JsonEngine.getJsonElementKey(jsonPath);
77+
}
78+
79+
@Test
80+
public void testGetParentJsonPath_ShouldThrowIllegalStateException_WhenNoParent() {
81+
82+
// GIVEN
83+
String jsonPath = "$";
84+
85+
// THEN
86+
expectedException.expect(IllegalStateException.class);
87+
expectedException.expectMessage("Unable to find parent for '$'");
88+
89+
// WHEN
90+
JsonEngine.getParentJsonPath(jsonPath);
91+
}
92+
93+
@Test
94+
public void testGetJsonElementKey_ShouldThrowIllegalStateException_WhenNoKey() {
95+
96+
// GIVEN
97+
String jsonPath = "$";
98+
99+
// THEN
100+
expectedException.expect(IllegalStateException.class);
101+
expectedException.expectMessage("Unable to find object key for '$'");
102+
103+
// WHEN
104+
JsonEngine.getJsonElementKey(jsonPath);
105+
}
106+
}

0 commit comments

Comments
 (0)