66import com .azure .cosmos .CosmosAsyncClient ;
77import com .azure .cosmos .CosmosAsyncDatabase ;
88import com .azure .cosmos .CosmosClientBuilder ;
9+ import com .azure .cosmos .CosmosException ;
910import com .azure .cosmos .encryption .models .CosmosEncryptionAlgorithm ;
1011import com .azure .cosmos .encryption .models .CosmosEncryptionType ;
12+ import com .azure .cosmos .encryption .models .SqlQuerySpecWithEncryption ;
1113import com .azure .cosmos .models .ClientEncryptionIncludedPath ;
1214import com .azure .cosmos .models .ClientEncryptionPolicy ;
1315import com .azure .cosmos .models .CosmosContainerProperties ;
1921import com .azure .cosmos .models .PartitionKey ;
2022import com .azure .cosmos .models .SqlParameter ;
2123import com .azure .cosmos .models .SqlQuerySpec ;
22- import com .azure .cosmos .encryption .models .SqlQuerySpecWithEncryption ;
2324import com .azure .cosmos .rx .TestSuiteBase ;
2425import com .azure .cosmos .util .CosmosPagedFlux ;
2526import com .fasterxml .jackson .annotation .JsonProperty ;
3132import org .testng .annotations .Factory ;
3233import org .testng .annotations .Test ;
3334
35+ import java .io .IOException ;
3436import java .util .ArrayList ;
3537import java .util .HashMap ;
3638import java .util .List ;
@@ -65,19 +67,19 @@ public void before_CosmosItemTest() {
6567 cosmosEncryptionAsyncDatabase =
6668 cosmosEncryptionAsyncClient .getCosmosEncryptionAsyncDatabase (cosmosAsyncDatabase );
6769
68- ClientEncryptionPolicy clientEncryptionPolicy = new ClientEncryptionPolicy (getPaths ());
69- String containerId = UUID .randomUUID ().toString ();
70- CosmosContainerProperties properties = new CosmosContainerProperties (containerId , "/mypk" );
71- properties .setClientEncryptionPolicy (clientEncryptionPolicy );
72- cosmosEncryptionAsyncDatabase .getCosmosAsyncDatabase ().createContainer (properties ).block ();
73- cosmosEncryptionAsyncContainer = cosmosEncryptionAsyncDatabase .getCosmosEncryptionAsyncContainer (containerId );
7470 metadata1 = new EncryptionKeyWrapMetadata ("key1" , "tempmetadata1" );
7571 metadata2 = new EncryptionKeyWrapMetadata ("key2" , "tempmetadata2" );
7672 cosmosEncryptionAsyncDatabase .createClientEncryptionKey ("key1" ,
7773 CosmosEncryptionAlgorithm .AEAES_256_CBC_HMAC_SHA_256 , metadata1 ).block ();
7874 cosmosEncryptionAsyncDatabase .createClientEncryptionKey ("key2" ,
7975 CosmosEncryptionAlgorithm .AEAES_256_CBC_HMAC_SHA_256 , metadata2 ).block ();
80- cosmosEncryptionAsyncDatabase .rewrapClientEncryptionKey ("key2" , metadata2 ).block ();
76+
77+ ClientEncryptionPolicy clientEncryptionPolicy = new ClientEncryptionPolicy (getPaths ());
78+ String containerId = UUID .randomUUID ().toString ();
79+ CosmosContainerProperties properties = new CosmosContainerProperties (containerId , "/mypk" );
80+ properties .setClientEncryptionPolicy (clientEncryptionPolicy );
81+ cosmosEncryptionAsyncDatabase .getCosmosAsyncDatabase ().createContainer (properties ).block ();
82+ cosmosEncryptionAsyncContainer = cosmosEncryptionAsyncDatabase .getCosmosEncryptionAsyncContainer (containerId );
8183 }
8284
8385 @ AfterClass (groups = {"encryption" }, timeOut = SHUTDOWN_TIMEOUT , alwaysRun = true )
@@ -87,7 +89,7 @@ public void afterClass() {
8789 }
8890
8991 @ Test (groups = {"encryption" }, timeOut = TIMEOUT )
90- public void createItemEncrypt_readItemDecrypt () {
92+ public void createItemEncrypt_readItemDecrypt () throws MicrosoftDataEncryptionException , IOException {
9193 Pojo properties = getItem (UUID .randomUUID ().toString ());
9294 CosmosItemResponse <Pojo > itemResponse = cosmosEncryptionAsyncContainer .createItem (properties ,
9395 new PartitionKey (properties .mypk ), new CosmosItemRequestOptions ()).block ();
@@ -98,10 +100,35 @@ public void createItemEncrypt_readItemDecrypt() {
98100 Pojo readItem = cosmosEncryptionAsyncContainer .readItem (properties .id , new PartitionKey (properties .mypk ),
99101 new CosmosItemRequestOptions (), Pojo .class ).block ().getItem ();
100102 validateResponse (properties , readItem );
103+
104+ //Check for max length support of 8000
105+ properties = getItem (UUID .randomUUID ().toString ());
106+ String longString = "" ;
107+ for (int i = 0 ; i < 8000 ; i ++) {
108+ longString += "a" ;
109+ }
110+ properties .sensitiveString = longString ;
111+ itemResponse = cosmosEncryptionAsyncContainer .createItem (properties ,
112+ new PartitionKey (properties .mypk ), new CosmosItemRequestOptions ()).block ();
113+ assertThat (itemResponse .getRequestCharge ()).isGreaterThan (0 );
114+ responseItem = itemResponse .getItem ();
115+ validateResponse (properties , responseItem );
116+
117+ //Check for exception for length greater that 8000
118+ longString += "a" ;
119+ properties .sensitiveString = longString ;
120+ try {
121+ cosmosEncryptionAsyncContainer .createItem (properties ,
122+ new PartitionKey (properties .mypk ), new CosmosItemRequestOptions ()).block ();
123+ fail ("Item create should fail as length of encryption field is greater than 8000" );
124+ } catch (CosmosException ex ) {
125+ assertThat (ex .getMessage ()).contains ("Unable to convert JSON to byte[]" );
126+ assertThat (ex .getCause () instanceof MicrosoftDataEncryptionException ).isTrue ();
127+ }
101128 }
102129
103130 @ Test (groups = {"encryption" }, timeOut = TIMEOUT )
104- public void upsertItem_readItem () throws Exception {
131+ public void upsertItem_readItem () {
105132 Pojo properties = getItem (UUID .randomUUID ().toString ());
106133 CosmosItemResponse <Pojo > itemResponse = cosmosEncryptionAsyncContainer .upsertItem (properties ,
107134 new PartitionKey (properties .mypk ), new CosmosItemRequestOptions ()).block ();
@@ -299,7 +326,6 @@ private void validateResponse(Pojo originalItem, Pojo result) {
299326 assertThat (result .sensitiveChildPojo2DArray [0 ][0 ].sensitiveIntArray ).isEqualTo (originalItem .sensitiveChildPojo2DArray [0 ][0 ].sensitiveIntArray );
300327 assertThat (result .sensitiveChildPojo2DArray [0 ][0 ].sensitiveStringArray ).isEqualTo (originalItem .sensitiveChildPojo2DArray [0 ][0 ].sensitiveStringArray );
301328 assertThat (result .sensitiveChildPojo2DArray [0 ][0 ].sensitiveString3DArray ).isEqualTo (originalItem .sensitiveChildPojo2DArray [0 ][0 ].sensitiveString3DArray );
302-
303329 }
304330
305331 public static Pojo getItem (String documentId ) {
@@ -316,10 +342,14 @@ public static Pojo getItem(String documentId) {
316342
317343 Pojo nestedPojo = new Pojo ();
318344 nestedPojo .id = "nestedPojo" ;
345+ nestedPojo .mypk = "nestedPojo" ;
319346 nestedPojo .sensitiveString = "nestedPojo" ;
320347 nestedPojo .sensitiveDouble = 10.123 ;
321348 nestedPojo .sensitiveInt = 123 ;
322349 nestedPojo .sensitiveLong = 1234 ;
350+ nestedPojo .sensitiveStringArray = new String []{"str1" , "str1" };
351+ nestedPojo .sensitiveString3DArray = new String [][][]{{{"str1" , "str2" }, {"str3" , "str4" }}, {{"str5" , "str6" }, {
352+ "str7" , "str8" }}};
323353 nestedPojo .sensitiveBoolean = true ;
324354
325355 pojo .sensitiveNestedPojo = nestedPojo ;
@@ -336,7 +366,9 @@ public static Pojo getItem(String documentId) {
336366 childPojo1 .sensitiveInt = 123 ;
337367 childPojo1 .sensitiveLong = 1234 ;
338368 childPojo1 .sensitiveBoolean = true ;
339-
369+ childPojo1 .sensitiveStringArray = new String []{"str1" , "str1" };
370+ childPojo1 .sensitiveString3DArray = new String [][][]{{{"str1" , "str2" }, {"str3" , "str4" }}, {{"str5" , "str6" }, {
371+ "str7" , "str8" }}};
340372 Pojo childPojo2 = new Pojo ();
341373 childPojo2 .id = "childPojo2" ;
342374 childPojo2 .sensitiveString = "child2TestingString" ;
@@ -475,7 +507,6 @@ public static List<ClientEncryptionIncludedPath> getPaths() {
475507 includedPath8 .setEncryptionType (CosmosEncryptionType .DETERMINISTIC );
476508 includedPath8 .setEncryptionAlgorithm (CosmosEncryptionAlgorithm .AEAES_256_CBC_HMAC_SHA_256 );
477509
478-
479510 ClientEncryptionIncludedPath includedPath9 = new ClientEncryptionIncludedPath ();
480511 includedPath9 .setClientEncryptionKeyId ("key1" );
481512 includedPath9 .setPath ("/sensitiveIntArray" );
0 commit comments