@@ -2080,4 +2080,96 @@ protected static void truncateCollection(DocumentCollection collection) {
20802080 }
20812081 }
20822082
2083+ /**
2084+ * Deletes a document using AsyncDocumentClient.
2085+ * @param client the AsyncDocumentClient
2086+ * @param documentLink the document link
2087+ * @param partitionKey the partition key
2088+ * @param etag the etag (can be null)
2089+ */
2090+ public static void deleteDocument (AsyncDocumentClient client , String documentLink , PartitionKey partitionKey , String etag ) {
2091+ RequestOptions options = new RequestOptions ();
2092+ options .setPartitionKey (partitionKey );
2093+ if (etag != null ) {
2094+ options .setIfMatchETag (etag );
2095+ }
2096+ try {
2097+ client .deleteDocument (documentLink , options ).block ();
2098+ } catch (Exception e ) {
2099+ // Ignore ResourceNotFoundException if document was already deleted, or other errors during cleanup
2100+ }
2101+ }
2102+
2103+ /**
2104+ * Bulk inserts documents using AsyncDocumentClient.
2105+ * @param client the AsyncDocumentClient
2106+ * @param collectionLink the collection link
2107+ * @param documents the documents to insert
2108+ * @param <T> the document type
2109+ */
2110+ public static <T extends Resource > void bulkInsert (AsyncDocumentClient client , String collectionLink , List <T > documents ) {
2111+ bulkInsert (client , collectionLink , documents , DEFAULT_BULK_INSERT_CONCURRENCY_LEVEL );
2112+ }
2113+
2114+ /**
2115+ * Bulk inserts documents using AsyncDocumentClient with concurrency level.
2116+ * @param client the AsyncDocumentClient
2117+ * @param collectionLink the collection link
2118+ * @param documents the documents to insert
2119+ * @param concurrencyLevel the concurrency level
2120+ * @param <T> the document type
2121+ */
2122+ public static <T extends Resource > void bulkInsert (AsyncDocumentClient client , String collectionLink , List <T > documents , int concurrencyLevel ) {
2123+ Flux .fromIterable (documents )
2124+ .flatMap (doc -> client .createDocument (collectionLink , doc , new RequestOptions (), false ), concurrencyLevel )
2125+ .collectList ()
2126+ .block ();
2127+ }
2128+
2129+ /**
2130+ * Truncates a DocumentCollection using AsyncDocumentClient.
2131+ * @param client the AsyncDocumentClient
2132+ * @param collection the DocumentCollection
2133+ */
2134+ protected static void truncateCollection (AsyncDocumentClient client , DocumentCollection collection ) {
2135+ logger .info ("Truncating DocumentCollection {} ..." , collection .getId ());
2136+
2137+ String collectionLink = collection .getSelfLink ();
2138+ if (collectionLink == null ) {
2139+ collectionLink = TestUtils .getCollectionNameLink (
2140+ collection .getAltLink ().split ("/" )[1 ], // dbs/{dbId}/colls/{collId} -> dbId
2141+ collection .getId ());
2142+ }
2143+
2144+ CosmosQueryRequestOptions options = new CosmosQueryRequestOptions ();
2145+ options .setMaxDegreeOfParallelism (-1 );
2146+
2147+ QueryFeedOperationState dummyState = TestUtils .createDummyQueryFeedOperationState (
2148+ ResourceType .Document ,
2149+ OperationType .Query ,
2150+ options ,
2151+ client );
2152+
2153+ try {
2154+ String query = "SELECT * FROM c" ;
2155+ final String finalCollectionLink = collectionLink ;
2156+ List <Document > docs = client .queryDocuments (finalCollectionLink , query , dummyState , Document .class )
2157+ .flatMap (page -> Flux .fromIterable (page .getResults ()))
2158+ .collectList ()
2159+ .block ();
2160+
2161+ if (docs != null ) {
2162+ for (Document doc : docs ) {
2163+ try {
2164+ client .deleteDocument (doc .getSelfLink (), new RequestOptions ()).block ();
2165+ } catch (Exception e ) {
2166+ // Ignore ResourceNotFoundException if document was already deleted, or other errors during cleanup
2167+ }
2168+ }
2169+ }
2170+ } finally {
2171+ safeClose (dummyState );
2172+ }
2173+ }
2174+
20832175}
0 commit comments