33import com .google .gson .Gson ;
44import com .google .gson .JsonObject ;
55import org .junit .Test ;
6+ import org .junit .jupiter .api .Assertions ;
67
78import static com .mastercard .developer .test .TestUtils .*;
89import static org .junit .Assert .assertNotNull ;
10+ import static org .junit .jupiter .api .Assertions .assertNull ;
911
1012public class JweEncryptionWithDefaultJsonEngineTest {
1113
@@ -31,6 +33,24 @@ public void testEncryptPayload_ShouldEncryptRootArrays() throws Exception {
3133 assertDecryptedJweEquals ("[{},{}]" , encryptedPayload , config );
3234 }
3335
36+ @ Test
37+ public void testEncryptPayload_ShouldCreateEncryptedValue_WhenOutPathParentDoesNotExistInPayload () throws Exception {
38+
39+ // GIVEN
40+ String payload = "{\" data\" : {}}" ;
41+ JweConfig config = getTestJweConfigBuilder ()
42+ .withEncryptionPath ("$" , "$.encryptedDataParent" )
43+ .build ();
44+
45+ // WHEN
46+ String encryptedPayload = JweEncryption .encryptPayload (payload , config );
47+
48+ // THEN
49+ JsonObject encryptedPayloadObject = new Gson ().fromJson (encryptedPayload , JsonObject .class );
50+ assertNull (encryptedPayloadObject .get ("data" ));
51+ Assertions .assertNotNull (encryptedPayloadObject .get ("encryptedDataParent" ).getAsJsonObject ().get ("encryptedData" ));
52+ }
53+
3454 @ Test
3555 public void testDecryptPayload_ShouldDecryptRootArrays () throws Exception {
3656
@@ -48,4 +68,24 @@ public void testDecryptPayload_ShouldDecryptRootArrays() throws Exception {
4868 // THEN
4969 assertPayloadEquals ("[{},{}]" , payload );
5070 }
71+
72+ @ Test
73+ public void testDecryptPayload_ShouldSupportPayloadWithEncryptedValueParent () throws Exception {
74+
75+ // GIVEN
76+ String encryptedPayload = "{\n " +
77+ " \" encryptedDataParent\" : {\n " +
78+ " \" encryptedData\" : \" eyJraWQiOiI3NjFiMDAzYzFlYWRlM2E1NDkwZTUwMDBkMzc4ODdiYWE1ZTZlYzBlMjI2YzA3NzA2ZTU5OTQ1MWZjMDMyYTc5IiwiY3R5IjoiYXBwbGljYXRpb24vanNvbiIsImVuYyI6IkEyNTZHQ00iLCJhbGciOiJSU0EtT0FFUC0yNTYifQ.XVy1AR51sUvwT-AtcsogQDo_klFi1EMYW8Wz7qM0e1dA3jNX5nTa38JhRcVuyVK15OenTYfg7aaH_fLjPZI1Mukd0BBnTuonh8T9CX5tbAAYx_KGPxc7a7ekBO-xXEA762eRvIIQJDZgQ_C3U39kc-XoaxC-ZYx8Va_aPBsXI1uozAfj3j5XVDnSmGAVWc2N4STTlCKbL4EO6YXASl_PrAOIVVSUrhpYvNS7GnjrP9x49tlRmTS0Dx-_MhkIAJM6H25YAuUmO-LW3gikReOUgGeY9_JtOioDs2J4ncKqugPFKr8kYF1cKnMwFv0TS9p5qR0kiF20bxRMvhbazf_Q5Q.V2Uz5-YRNq9ZIJjhRsKYIw.jB1s8rczGEj2OjU.qs4zVUf2tHML02Rglq5ncw\" \n " +
79+ " }\n " +
80+ "}" ;
81+ JweConfig config = getTestJweConfigBuilder ()
82+ .withDecryptionPath ("$.encryptedDataParent.encryptedData" , "$" )
83+ .build ();
84+
85+ // WHEN
86+ String payload = JweEncryption .decryptPayload (encryptedPayload , config );
87+
88+ // THEN
89+ assertPayloadEquals ("{\" data\" : {}}" , payload );
90+ }
5191}
0 commit comments