@@ -61,12 +61,11 @@ public void validateBadPreferredRegions1() {
6161
6262 @ Test (groups = "unit" )
6363 public void validateBadPreferredRegions2 () {
64- try {
65- CosmosAsyncClient client = new CosmosClientBuilder ()
64+ try (CosmosAsyncClient client = new CosmosClientBuilder ()
6665 .key (TestConfigurations .MASTER_KEY )
6766 .endpoint (hostName )
6867 .preferredRegions (Arrays .asList (" " ))
69- .buildAsyncClient ();
68+ .buildAsyncClient ()) {
7069 client .close ();
7170 } catch (Exception e ) {
7271 assertThat (e ).isInstanceOf (RuntimeException .class );
@@ -92,9 +91,11 @@ public void validateApiTypePresent() {
9291 ImplementationBridgeHelpers .CosmosClientBuilderHelper .getCosmosClientBuilderAccessor ();
9392 accessor .setCosmosClientApiType (cosmosClientBuilder , apiType );
9493
95- RxDocumentClientImpl documentClient =
96- (RxDocumentClientImpl ) ReflectionUtils .getAsyncDocumentClient (cosmosClientBuilder .buildAsyncClient ());
97- assertThat (ReflectionUtils .getApiType (documentClient )).isEqualTo (apiType );
94+ try (CosmosAsyncClient cosmosClient = cosmosClientBuilder .buildAsyncClient ()) {
95+ RxDocumentClientImpl documentClient =
96+ (RxDocumentClientImpl ) ReflectionUtils .getAsyncDocumentClient (cosmosClient );
97+ assertThat (ReflectionUtils .getApiType (documentClient )).isEqualTo (apiType );
98+ }
9899 }
99100
100101 @ Test (groups = "emulator" , dataProvider = "regionScopedSessionContainerConfigs" )
@@ -111,23 +112,24 @@ public void validateSessionTokenCapturingForAccountDefaultConsistency(boolean sh
111112 .key (TestConfigurations .MASTER_KEY )
112113 .userAgentSuffix ("custom-direct-client" );
113114
114- CosmosAsyncClient client = cosmosClientBuilder .buildAsyncClient ();
115- RxDocumentClientImpl documentClient =
116- (RxDocumentClientImpl ) ReflectionUtils .getAsyncDocumentClient (client );
115+ try ( CosmosAsyncClient client = cosmosClientBuilder .buildAsyncClient ()) {
116+ RxDocumentClientImpl documentClient =
117+ (RxDocumentClientImpl ) ReflectionUtils .getAsyncDocumentClient (client );
117118
118- if (documentClient .getDefaultConsistencyLevelOfAccount () != ConsistencyLevel .SESSION ) {
119- throw new SkipException ("This test is only applicable when default account-level consistency is Session." );
120- }
119+ if (documentClient .getDefaultConsistencyLevelOfAccount () != ConsistencyLevel .SESSION ) {
120+ throw new SkipException ("This test is only applicable when default account-level consistency is Session." );
121+ }
121122
122- ISessionContainer sessionContainer = documentClient .getSession ();
123+ ISessionContainer sessionContainer = documentClient .getSession ();
123124
124- if (System .getProperty ("COSMOS.SESSION_CAPTURING_TYPE" ) != null && System .getProperty ("COSMOS.SESSION_CAPTURING_TYPE" ).equals ("REGION_SCOPED" )) {
125- assertThat (sessionContainer instanceof RegionScopedSessionContainer ).isTrue ();
126- } else {
127- assertThat (sessionContainer instanceof SessionContainer ).isTrue ();
128- }
125+ if (System .getProperty ("COSMOS.SESSION_CAPTURING_TYPE" ) != null && System .getProperty ("COSMOS.SESSION_CAPTURING_TYPE" ).equals ("REGION_SCOPED" )) {
126+ assertThat (sessionContainer instanceof RegionScopedSessionContainer ).isTrue ();
127+ } else {
128+ assertThat (sessionContainer instanceof SessionContainer ).isTrue ();
129+ }
129130
130- assertThat (sessionContainer .getDisableSessionCapturing ()).isEqualTo (false );
131+ assertThat (sessionContainer .getDisableSessionCapturing ()).isEqualTo (false );
132+ }
131133 } finally {
132134 System .clearProperty ("COSMOS.SESSION_CAPTURING_TYPE" );
133135 }
@@ -144,126 +146,133 @@ public void validateSessionTokenCapturingForAccountDefaultConsistencyWithEnvVari
144146 .key (TestConfigurations .MASTER_KEY )
145147 .userAgentSuffix ("custom-direct-client" );
146148
147- CosmosAsyncClient client = cosmosClientBuilder .buildAsyncClient ();
148- RxDocumentClientImpl documentClient =
149- (RxDocumentClientImpl ) ReflectionUtils .getAsyncDocumentClient (client );
149+ try ( CosmosAsyncClient client = cosmosClientBuilder .buildAsyncClient ()) {
150+ RxDocumentClientImpl documentClient =
151+ (RxDocumentClientImpl ) ReflectionUtils .getAsyncDocumentClient (client );
150152
151- if (documentClient .getDefaultConsistencyLevelOfAccount () != ConsistencyLevel .SESSION ) {
152- throw new SkipException ("This test is only applicable when default account-level consistency is Session." );
153- }
153+ if (documentClient .getDefaultConsistencyLevelOfAccount () != ConsistencyLevel .SESSION ) {
154+ throw new SkipException ("This test is only applicable when default account-level consistency is Session." );
155+ }
154156
155- ISessionContainer sessionContainer = documentClient .getSession ();
157+ ISessionContainer sessionContainer = documentClient .getSession ();
156158
157- if (System .getenv ("COSMOS.SESSION_CAPTURING_TYPE" ) != null && System .getenv ("COSMOS.SESSION_CAPTURING_TYPE" ).equals ("REGION_SCOPED" )) {
158- assertThat (sessionContainer instanceof RegionScopedSessionContainer ).isTrue ();
159- } else {
160- assertThat (sessionContainer instanceof SessionContainer ).isTrue ();
161- }
159+ if (System .getenv ("COSMOS.SESSION_CAPTURING_TYPE" ) != null && System .getenv ("COSMOS.SESSION_CAPTURING_TYPE" ).equals ("REGION_SCOPED" )) {
160+ assertThat (sessionContainer instanceof RegionScopedSessionContainer ).isTrue ();
161+ } else {
162+ assertThat (sessionContainer instanceof SessionContainer ).isTrue ();
163+ }
162164
163- assertThat (sessionContainer .getDisableSessionCapturing ()).isEqualTo (false );
165+ assertThat (sessionContainer .getDisableSessionCapturing ()).isEqualTo (false );
166+ }
164167 } finally {
165168 System .clearProperty ("COSMOS.SESSION_CAPTURING_TYPE" );
166169 }
167170 }
168171
169172 @ Test (groups = "emulator" )
170173 public void validateContainerCreationInterceptor () {
171- CosmosClient clientWithoutInterceptor = new CosmosClientBuilder ()
174+ try ( CosmosClient clientWithoutInterceptor = new CosmosClientBuilder ()
172175 .endpoint (TestConfigurations .HOST )
173176 .key (TestConfigurations .MASTER_KEY )
174177 .userAgentSuffix ("noInterceptor" )
175- .buildClient ();
176-
177- ConcurrentMap <CacheKey , List <?>> queryCache = new ConcurrentHashMap <>();
178-
179- CosmosClient clientWithInterceptor = new CosmosClientBuilder ()
180- .endpoint (TestConfigurations .HOST )
181- .key (TestConfigurations .MASTER_KEY )
182- .userAgentSuffix ("withInterceptor" )
183- .containerCreationInterceptor (originalContainer -> new CacheAndValidateQueriesContainer (originalContainer , queryCache ))
184- .buildClient ();
185-
186- CosmosAsyncClient asyncClientWithInterceptor = new CosmosClientBuilder ()
187- .endpoint (TestConfigurations .HOST )
188- .key (TestConfigurations .MASTER_KEY )
189- .userAgentSuffix ("withInterceptor" )
190- .containerCreationInterceptor (originalContainer -> new CacheAndValidateQueriesContainer (originalContainer , queryCache ))
191- .buildAsyncClient ();
192-
193- CosmosContainer normalContainer = clientWithoutInterceptor
194- .getDatabase ("TestDB" )
195- .getContainer ("TestContainer" );
196- assertThat (normalContainer ).isNotNull ();
197- assertThat (normalContainer .getClass ()).isEqualTo (CosmosContainer .class );
198- assertThat (normalContainer .asyncContainer .getClass ()).isEqualTo (CosmosAsyncContainer .class );
199-
200- CosmosContainer customSyncContainer = clientWithInterceptor
201- .getDatabase ("TestDB" )
202- .getContainer ("TestContainer" );
203- assertThat (customSyncContainer ).isNotNull ();
204- assertThat (customSyncContainer .getClass ()).isEqualTo (CosmosContainer .class );
205- assertThat (customSyncContainer .asyncContainer .getClass ()).isEqualTo (CacheAndValidateQueriesContainer .class );
206-
207- CosmosAsyncContainer customAsyncContainer = asyncClientWithInterceptor
208- .getDatabase ("TestDB" )
209- .getContainer ("TestContainer" );
210- assertThat (customAsyncContainer ).isNotNull ();
211- assertThat (customAsyncContainer .getClass ()).isEqualTo (CacheAndValidateQueriesContainer .class );
212-
213- try {
214- customSyncContainer .queryItems ("SELECT * from c" , null , ObjectNode .class );
215- fail ("Unparameterized query should throw" );
216- } catch (IllegalStateException expectedError ) {}
217-
218- try {
219- customAsyncContainer .queryItems ("SELECT * from c" , null , ObjectNode .class );
220- fail ("Unparameterized query should throw" );
221- } catch (IllegalStateException expectedError ) {}
222-
223- try {
224- customAsyncContainer .queryItems ("SELECT * from c" , ObjectNode .class );
225- fail ("Unparameterized query should throw" );
226- } catch (IllegalStateException expectedError ) {}
227-
228- SqlQuerySpec querySpec = new SqlQuerySpec ().setQueryText ("SELECT * from c" );
229- assertThat (queryCache ).size ().isEqualTo (0 );
230-
231- try {
232- List <ObjectNode > items = customSyncContainer
233- .queryItems (querySpec , null , ObjectNode .class )
234- .stream ().collect (Collectors .toList ());
235- fail ("Not yet cached - the query above should always throw" );
236- } catch (CosmosException cosmosException ) {
237- // Container does not exist - when not cached should fail
238- assertThat (cosmosException .getStatusCode ()).isEqualTo (404 );
239- assertThat (cosmosException .getSubStatusCode ()).isEqualTo (1003 );
240- }
241-
242- queryCache .putIfAbsent (new CacheKey (ObjectNode .class .getCanonicalName (), querySpec ), new ArrayList <>());
243- assertThat (queryCache ).size ().isEqualTo (1 );
244-
245- // Validate that CacheKey equality check works
246- queryCache .putIfAbsent (new CacheKey (ObjectNode .class .getCanonicalName (), querySpec ), new ArrayList <>());
247- assertThat (queryCache ).size ().isEqualTo (1 );
248-
249- // Validate that form cache the results can be served
250- List <ObjectNode > items = customSyncContainer
251- .queryItems (querySpec , null , ObjectNode .class )
252- .stream ().collect (Collectors .toList ());
178+ .buildClient ()) {
253179
254- querySpec = new SqlQuerySpec ().setQueryText ("SELECT * from c" );
255- CosmosPagedFlux <ObjectNode > cachedPagedFlux = customAsyncContainer
256- .queryItems (querySpec , null , ObjectNode .class );
257- assertThat (cachedPagedFlux .getClass ().getName ()).startsWith ("com.azure.cosmos.util.CosmosPagedFluxStaticListImpl" );
180+ ConcurrentMap <CacheKey , List <?>> queryCache = new ConcurrentHashMap <>();
258181
259- // Validate that uncached query form async Container also fails with 404 due to non-existing Container
260- querySpec = new SqlQuerySpec ().setQueryText ("SELECT * from r" );
261- try {
262- CosmosPagedFlux <ObjectNode > uncachedPagedFlux = customAsyncContainer
263- .queryItems (querySpec , null , ObjectNode .class );
264- } catch (CosmosException cosmosException ) {
265- assertThat (cosmosException .getStatusCode ()).isEqualTo (404 );
266- assertThat (cosmosException .getSubStatusCode ()).isEqualTo (1003 );
182+ try (CosmosClient clientWithInterceptor = new CosmosClientBuilder ()
183+ .endpoint (TestConfigurations .HOST )
184+ .key (TestConfigurations .MASTER_KEY )
185+ .userAgentSuffix ("withInterceptor" )
186+ .containerCreationInterceptor (originalContainer -> new CacheAndValidateQueriesContainer (originalContainer , queryCache ))
187+ .buildClient ()) {
188+
189+ try (CosmosAsyncClient asyncClientWithInterceptor = new CosmosClientBuilder ()
190+ .endpoint (TestConfigurations .HOST )
191+ .key (TestConfigurations .MASTER_KEY )
192+ .userAgentSuffix ("withInterceptor" )
193+ .containerCreationInterceptor (originalContainer -> new CacheAndValidateQueriesContainer (originalContainer , queryCache ))
194+ .buildAsyncClient ()) {
195+
196+ CosmosContainer normalContainer = clientWithoutInterceptor
197+ .getDatabase ("TestDB" )
198+ .getContainer ("TestContainer" );
199+ assertThat (normalContainer ).isNotNull ();
200+ assertThat (normalContainer .getClass ()).isEqualTo (CosmosContainer .class );
201+ assertThat (normalContainer .asyncContainer .getClass ()).isEqualTo (CosmosAsyncContainer .class );
202+
203+ CosmosContainer customSyncContainer = clientWithInterceptor
204+ .getDatabase ("TestDB" )
205+ .getContainer ("TestContainer" );
206+ assertThat (customSyncContainer ).isNotNull ();
207+ assertThat (customSyncContainer .getClass ()).isEqualTo (CosmosContainer .class );
208+ assertThat (customSyncContainer .asyncContainer .getClass ()).isEqualTo (CacheAndValidateQueriesContainer .class );
209+
210+ CosmosAsyncContainer customAsyncContainer = asyncClientWithInterceptor
211+ .getDatabase ("TestDB" )
212+ .getContainer ("TestContainer" );
213+ assertThat (customAsyncContainer ).isNotNull ();
214+ assertThat (customAsyncContainer .getClass ()).isEqualTo (CacheAndValidateQueriesContainer .class );
215+
216+ try {
217+ customSyncContainer .queryItems ("SELECT * from c" , null , ObjectNode .class );
218+ fail ("Unparameterized query should throw" );
219+ } catch (IllegalStateException expectedError ) {
220+ }
221+
222+ try {
223+ customAsyncContainer .queryItems ("SELECT * from c" , null , ObjectNode .class );
224+ fail ("Unparameterized query should throw" );
225+ } catch (IllegalStateException expectedError ) {
226+ }
227+
228+ try {
229+ customAsyncContainer .queryItems ("SELECT * from c" , ObjectNode .class );
230+ fail ("Unparameterized query should throw" );
231+ } catch (IllegalStateException expectedError ) {
232+ }
233+
234+ SqlQuerySpec querySpec = new SqlQuerySpec ().setQueryText ("SELECT * from c" );
235+ assertThat (queryCache ).size ().isEqualTo (0 );
236+
237+ try {
238+ List <ObjectNode > items = customSyncContainer
239+ .queryItems (querySpec , null , ObjectNode .class )
240+ .stream ().collect (Collectors .toList ());
241+ fail ("Not yet cached - the query above should always throw" );
242+ } catch (CosmosException cosmosException ) {
243+ // Container does not exist - when not cached should fail
244+ assertThat (cosmosException .getStatusCode ()).isEqualTo (404 );
245+ assertThat (cosmosException .getSubStatusCode ()).isEqualTo (1003 );
246+ }
247+
248+ queryCache .putIfAbsent (new CacheKey (ObjectNode .class .getCanonicalName (), querySpec ), new ArrayList <>());
249+ assertThat (queryCache ).size ().isEqualTo (1 );
250+
251+ // Validate that CacheKey equality check works
252+ queryCache .putIfAbsent (new CacheKey (ObjectNode .class .getCanonicalName (), querySpec ), new ArrayList <>());
253+ assertThat (queryCache ).size ().isEqualTo (1 );
254+
255+ // Validate that form cache the results can be served
256+ List <ObjectNode > items = customSyncContainer
257+ .queryItems (querySpec , null , ObjectNode .class )
258+ .stream ().collect (Collectors .toList ());
259+
260+ querySpec = new SqlQuerySpec ().setQueryText ("SELECT * from c" );
261+ CosmosPagedFlux <ObjectNode > cachedPagedFlux = customAsyncContainer
262+ .queryItems (querySpec , null , ObjectNode .class );
263+ assertThat (cachedPagedFlux .getClass ().getName ()).startsWith ("com.azure.cosmos.util.CosmosPagedFluxStaticListImpl" );
264+
265+ // Validate that uncached query form async Container also fails with 404 due to non-existing Container
266+ querySpec = new SqlQuerySpec ().setQueryText ("SELECT * from r" );
267+ try {
268+ CosmosPagedFlux <ObjectNode > uncachedPagedFlux = customAsyncContainer
269+ .queryItems (querySpec , null , ObjectNode .class );
270+ } catch (CosmosException cosmosException ) {
271+ assertThat (cosmosException .getStatusCode ()).isEqualTo (404 );
272+ assertThat (cosmosException .getSubStatusCode ()).isEqualTo (1003 );
273+ }
274+ }
275+ }
267276 }
268277 }
269278
0 commit comments