Skip to content

Commit 4f61c25

Browse files
authored
Replace deprecated packages in E2E (#1020)
* Replace Microsoft.Azure.EventHubs with latest stable Azure.Messaging.EventHubs * Replace Microsoft.Azure.DocumentDB.Core with latest stable Microsoft.Azure.Cosmos
1 parent a9b4914 commit 4f61c25

File tree

5 files changed

+65
-60
lines changed

5 files changed

+65
-60
lines changed

test/E2E/Azure.Functions.PowerShellWorker.E2E/Azure.Functions.PowerShellWorker.E2E/Azure.Functions.PowerShellWorker.E2E.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10-
<PackageReference Include="Microsoft.Azure.DocumentDB.Core" Version="2.18.0" />
11-
<PackageReference Include="Microsoft.Azure.EventHubs" Version="4.3.2" />
10+
<PackageReference Include="Azure.Messaging.EventHubs" Version="5.9.3" />
11+
<PackageReference Include="Microsoft.Azure.Cosmos" Version="3.35.4" />
1212
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="7.0.0" />
1313
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.4.1" />
1414
<PackageReference Include="Newtonsoft.Json" Version="13.0.2" />

test/E2E/Azure.Functions.PowerShellWorker.E2E/Azure.Functions.PowerShellWorker.E2E/Constants.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ public static class Queue {
2323
public static class CosmosDB {
2424
public static string CosmosDBConnectionStringSetting = Environment.GetEnvironmentVariable("AzureWebJobsCosmosDBConnectionString");
2525
public static string DbName = "ItemDb";
26-
public static string InputCollectionName = "ItemCollectionIn";
27-
public static string OutputCollectionName = "ItemCollectionOut";
26+
public static string InputCollectionName = "PartitionedItemCollectionIn";
27+
public static string OutputCollectionName = "PartitionedItemCollectionOut";
2828
public static string LeaseCollectionName = "leases";
2929
}
3030

test/E2E/Azure.Functions.PowerShellWorker.E2E/Azure.Functions.PowerShellWorker.E2E/Helpers/CosmosDBHelpers.cs

Lines changed: 54 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -3,82 +3,76 @@
33

44
using System;
55
using System.Threading.Tasks;
6-
using Microsoft.Azure.Documents;
7-
using Microsoft.Azure.Documents.Client;
6+
using Microsoft.Azure.Cosmos;
87

98
namespace 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
}

test/E2E/Azure.Functions.PowerShellWorker.E2E/Azure.Functions.PowerShellWorker.E2E/Helpers/EventHubsHelpers.cs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the MIT License. See License.txt in the project root for license information.
33

4-
using Microsoft.Azure.EventHubs;
4+
using Azure.Messaging.EventHubs;
5+
using Azure.Messaging.EventHubs.Producer;
56
using Newtonsoft.Json;
67
using Newtonsoft.Json.Linq;
78
using System.Collections.Generic;
@@ -29,13 +30,11 @@ public static async Task SendJSONMessagesAsync(string eventId, string eventHubNa
2930
events.Add(evt);
3031
}
3132

32-
EventHubsConnectionStringBuilder builder = new EventHubsConnectionStringBuilder(Constants.EventHubs.EventHubsConnectionStringSetting);
33-
builder.EntityPath = eventHubName;
34-
EventHubClient eventHubClient = EventHubClient.CreateFromConnectionString(builder.ToString());
33+
EventHubProducerClient eventHubClient = new EventHubProducerClient(Constants.EventHubs.EventHubsConnectionStringSetting, eventHubName);
3534
await eventHubClient.SendAsync(events);
3635
}
3736

38-
public static async Task SendMessagesAsync(string eventId, string evenHubName)
37+
public static async Task SendMessagesAsync(string eventId, string eventHubName)
3938
{
4039
// write 3 events
4140
List<EventData> events = new List<EventData>();
@@ -48,9 +47,7 @@ public static async Task SendMessagesAsync(string eventId, string evenHubName)
4847
events.Add(evt);
4948
}
5049

51-
EventHubsConnectionStringBuilder builder = new EventHubsConnectionStringBuilder(Constants.EventHubs.EventHubsConnectionStringSetting);
52-
builder.EntityPath = evenHubName;
53-
EventHubClient eventHubClient = EventHubClient.CreateFromConnectionString(builder.ToString());
50+
EventHubProducerClient eventHubClient = new EventHubProducerClient(Constants.EventHubs.EventHubsConnectionStringSetting, eventHubName);
5451
await eventHubClient.SendAsync(events);
5552
}
5653
}

test/E2E/TestFunctionApp/CosmosDBTriggerAndOutput/function.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"leaseCollectionName": "leases",
88
"connectionStringSetting": "AzureWebJobsCosmosDBConnectionString",
99
"databaseName": "ItemDb",
10-
"collectionName": "ItemCollectionIn",
10+
"collectionName": "PartitionedItemCollectionIn",
1111
"createLeaseCollectionIfNotExists": true
1212
},
1313
{
@@ -17,7 +17,7 @@
1717
"leaseCollectionName": "leases",
1818
"connectionStringSetting": "AzureWebJobsCosmosDBConnectionString",
1919
"databaseName": "ItemDb",
20-
"collectionName": "ItemCollectionOut"
20+
"collectionName": "PartitionedItemCollectionOut"
2121
}
2222
]
2323
}

0 commit comments

Comments
 (0)