33
44using System ;
55using System . Threading . Tasks ;
6- using Microsoft . Azure . Documents ;
7- using Microsoft . Azure . Documents . Client ;
6+ using Microsoft . Azure . Cosmos ;
87
98namespace Azure . Functions . PowerShell . Tests . E2E
109{
11- public class TestDocument
12- {
13- public string id { get ; set ; }
14- public string name { get ; set ; }
15- }
1610
1711 public static class CosmosDBHelpers
1812 {
19- private static DocumentClient _docDbClient ;
20- private static Uri inputCollectionsUri = UriFactory . CreateDocumentCollectionUri ( Constants . CosmosDB . DbName , Constants . CosmosDB . InputCollectionName ) ;
21- private static Uri outputCollectionsUri = UriFactory . CreateDocumentCollectionUri ( Constants . CosmosDB . DbName , Constants . CosmosDB . OutputCollectionName ) ;
22- private static Uri leasesCollectionsUri = UriFactory . CreateDocumentCollectionUri ( Constants . CosmosDB . DbName , Constants . CosmosDB . LeaseCollectionName ) ;
13+ private static CosmosClient _cosmosDbClient ;
14+
15+ private class Document
16+ {
17+ // lower-case because Cosmos expects a field with this name
18+ public string id { get ; set ; }
19+ }
2320
2421 static CosmosDBHelpers ( )
2522 {
2623 var builder = new System . Data . Common . DbConnectionStringBuilder ( ) ;
2724 builder . ConnectionString = Constants . CosmosDB . CosmosDBConnectionStringSetting ;
28- var serviceUri = new Uri ( builder [ "AccountEndpoint" ] . ToString ( ) ) ;
29- _docDbClient = new DocumentClient ( serviceUri , builder [ "AccountKey" ] . ToString ( ) ) ;
25+ var serviceUri = builder [ "AccountEndpoint" ] . ToString ( ) ;
26+ _cosmosDbClient = new CosmosClient ( serviceUri , builder [ "AccountKey" ] . ToString ( ) ) ;
3027 }
3128
3229 // keep
3330 public async static Task CreateDocument ( string docId )
3431 {
3532 Document documentToTest = new Document ( )
3633 {
37- Id = docId
34+ id = docId
3835 } ;
3936
40- Document insertedDoc = await _docDbClient . CreateDocumentAsync ( UriFactory . CreateDocumentCollectionUri ( Constants . CosmosDB . DbName , Constants . CosmosDB . InputCollectionName ) , documentToTest ) ;
41- }
37+ Container _inputContainer = _cosmosDbClient . GetContainer ( Constants . CosmosDB . DbName , Constants . CosmosDB . InputCollectionName ) ;
4238
43- public async static Task CreateDocument ( TestDocument testDocument )
44- {
45- Document insertedDoc = await _docDbClient . CreateDocumentAsync ( UriFactory . CreateDocumentCollectionUri ( Constants . CosmosDB . DbName , Constants . CosmosDB . InputCollectionName ) , testDocument ) ;
39+ Document insertedDoc = await _inputContainer . CreateItemAsync < Document > ( documentToTest , new PartitionKey ( documentToTest . id ) ) ;
4640 }
4741
4842 // keep
4943 public async static Task < string > ReadDocument ( string docId )
5044 {
51- var docUri = UriFactory . CreateDocumentUri ( Constants . CosmosDB . DbName , Constants . CosmosDB . OutputCollectionName , docId ) ;
5245 Document retrievedDocument = null ;
5346 await Utilities . RetryAsync ( async ( ) =>
5447 {
5548 try
5649 {
57- retrievedDocument = await _docDbClient . ReadDocumentAsync ( docUri ) ;
50+ Container container = _cosmosDbClient . GetContainer ( Constants . CosmosDB . DbName , Constants . CosmosDB . OutputCollectionName ) ;
51+
52+ retrievedDocument = await container . ReadItemAsync < Document > ( docId , new PartitionKey ( docId ) ) ;
5853 return true ;
5954 }
60- catch ( DocumentClientException ex ) when ( ex . Error . Code == "NotFound" || ex . Error . Code == "Not Found" )
55+ catch ( CosmosException ex ) when ( ex . StatusCode == System . Net . HttpStatusCode . NotFound )
6156 {
6257 return false ;
6358 }
6459 } , 120000 , 4000 ) ;
65- return retrievedDocument . Id ;
60+ return retrievedDocument . id ;
6661 }
6762
6863 // keep
6964 public async static Task DeleteTestDocuments ( string docId )
7065 {
71- var inputDocUri = UriFactory . CreateDocumentUri ( Constants . CosmosDB . DbName , Constants . CosmosDB . InputCollectionName , docId ) ;
72- await DeleteDocument ( inputDocUri ) ;
73- var outputDocUri = UriFactory . CreateDocumentUri ( Constants . CosmosDB . DbName , Constants . CosmosDB . OutputCollectionName , docId ) ;
74- await DeleteDocument ( outputDocUri ) ;
66+ await DeleteDocument ( Constants . CosmosDB . DbName , Constants . CosmosDB . InputCollectionName , docId ) ;
67+ await DeleteDocument ( Constants . CosmosDB . DbName , Constants . CosmosDB . OutputCollectionName , docId ) ;
7568 }
7669
77- private async static Task DeleteDocument ( Uri docUri )
70+ private async static Task DeleteDocument ( string dbName , string collectionName , string docId )
7871 {
7972 try
8073 {
81- await _docDbClient . DeleteDocumentAsync ( docUri ) ;
74+ Container container = _cosmosDbClient . GetContainer ( dbName , collectionName ) ;
75+ await container . DeleteItemAsync < Document > ( docId , new PartitionKey ( docId ) ) ;
8276 }
8377 catch ( Exception )
8478 {
@@ -89,41 +83,55 @@ private async static Task DeleteDocument(Uri docUri)
8983 // keep
9084 public async static Task CreateDocumentCollections ( )
9185 {
92- Database db = await _docDbClient . CreateDatabaseIfNotExistsAsync ( new Database { Id = Constants . CosmosDB . DbName } ) ;
93- Uri dbUri = UriFactory . CreateDatabaseUri ( db . Id ) ;
94-
95- await CreateCollection ( dbUri , Constants . CosmosDB . InputCollectionName ) ;
96- await CreateCollection ( dbUri , Constants . CosmosDB . OutputCollectionName ) ;
97- await CreateCollection ( dbUri , Constants . CosmosDB . LeaseCollectionName ) ;
86+ Database db = await _cosmosDbClient . CreateDatabaseIfNotExistsAsync ( Constants . CosmosDB . DbName ) ;
9887
88+ await CreateCollection ( Constants . CosmosDB . DbName , Constants . CosmosDB . InputCollectionName , "/id" ) ;
89+ await CreateCollection ( Constants . CosmosDB . DbName , Constants . CosmosDB . OutputCollectionName , "/id" ) ;
90+ // While using extensions v2-3, the leases may not have a partition key, but the new SDK requires
91+ // one to manually create a collection. This comment may be removed and this line uncommented when
92+ // extension bundles for tests are updated.
93+ //await CreateCollection(Constants.CosmosDB.DbName, Constants.CosmosDB.LeaseCollectionName, "/id");
9994 }
10095 public async static Task DeleteDocumentCollections ( )
10196 {
102- await DeleteCollection ( inputCollectionsUri ) ;
103- await DeleteCollection ( outputCollectionsUri ) ;
104- await DeleteCollection ( leasesCollectionsUri ) ;
97+ await DeleteCollection ( Constants . CosmosDB . DbName , Constants . CosmosDB . InputCollectionName ) ;
98+ await DeleteCollection ( Constants . CosmosDB . DbName , Constants . CosmosDB . OutputCollectionName ) ;
99+ await DeleteCollection ( Constants . CosmosDB . DbName , Constants . CosmosDB . LeaseCollectionName ) ;
105100 }
106101
107- private async static Task DeleteCollection ( Uri collectionUri )
102+ private async static Task DeleteCollection ( string dbName , string collectionName )
108103 {
109104 try
110105 {
111- await _docDbClient . DeleteDocumentCollectionAsync ( collectionUri ) ;
106+ Database database = _cosmosDbClient . GetDatabase ( dbName ) ;
107+ await database . GetContainer ( collectionName ) . DeleteContainerAsync ( ) ;
112108 }
113109 catch ( Exception )
114110 {
115111 //Ignore
116112 }
117113 }
118114
119- private async static Task CreateCollection ( Uri dbUri , string collectioName )
115+ private async static Task CreateCollection ( string dbName , string collectionName , string partitionKey )
120116 {
121- DocumentCollection collection = new DocumentCollection ( ) { Id = collectioName } ;
122- await _docDbClient . CreateDocumentCollectionIfNotExistsAsync ( dbUri , collection ,
123- new RequestOptions ( )
117+ Database database = _cosmosDbClient . GetDatabase ( dbName ) ;
118+ IndexingPolicy indexingPolicy = new IndexingPolicy
119+ {
120+ IndexingMode = IndexingMode . Consistent ,
121+ Automatic = true ,
122+ IncludedPaths =
124123 {
125- OfferThroughput = 400
126- } ) ;
124+ new IncludedPath
125+ {
126+ Path = "/*"
127+ }
128+ }
129+ } ;
130+ var containerProperties = new ContainerProperties ( collectionName , partitionKey )
131+ {
132+ IndexingPolicy = indexingPolicy
133+ } ;
134+ await database . CreateContainerIfNotExistsAsync ( containerProperties , 400 ) ;
127135 }
128136 }
129137}
0 commit comments