Skip to content

Commit

Permalink
Replace deprecated packages in E2E (#1022)
Browse files Browse the repository at this point in the history
* Replace Microsoft.Azure.EventHubs with latest stable Azure.Messaging.EventHubs
* Replace Microsoft.Azure.DocumentDB.Core with latest stable Microsoft.Azure.Cosmos
  • Loading branch information
andystaples authored Oct 18, 2023
1 parent a37e3e1 commit f7d1387
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Azure.DocumentDB.Core" Version="2.18.0" />
<PackageReference Include="Microsoft.Azure.EventHubs" Version="4.3.2" />
<PackageReference Include="Azure.Messaging.EventHubs" Version="5.9.3" />
<PackageReference Include="Microsoft.Azure.Cosmos" Version="3.35.4" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="7.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.4.1" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.2" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ public static class Queue {
public static class CosmosDB {
public static string CosmosDBConnectionStringSetting = Environment.GetEnvironmentVariable("AzureWebJobsCosmosDBConnectionString");
public static string DbName = "ItemDb";
public static string InputCollectionName = "ItemCollectionIn";
public static string OutputCollectionName = "ItemCollectionOut";
public static string InputCollectionName = "PartitionedItemCollectionIn";
public static string OutputCollectionName = "PartitionedItemCollectionOut";
public static string LeaseCollectionName = "leases";
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,82 +3,76 @@

using System;
using System.Threading.Tasks;
using Microsoft.Azure.Documents;
using Microsoft.Azure.Documents.Client;
using Microsoft.Azure.Cosmos;

namespace Azure.Functions.PowerShell.Tests.E2E
{
public class TestDocument
{
public string id { get; set; }
public string name { get; set; }
}

public static class CosmosDBHelpers
{
private static DocumentClient _docDbClient;
private static Uri inputCollectionsUri = UriFactory.CreateDocumentCollectionUri(Constants.CosmosDB.DbName, Constants.CosmosDB.InputCollectionName);
private static Uri outputCollectionsUri = UriFactory.CreateDocumentCollectionUri(Constants.CosmosDB.DbName, Constants.CosmosDB.OutputCollectionName);
private static Uri leasesCollectionsUri = UriFactory.CreateDocumentCollectionUri(Constants.CosmosDB.DbName, Constants.CosmosDB.LeaseCollectionName);
private static CosmosClient _cosmosDbClient;

private class Document
{
// lower-case because Cosmos expects a field with this name
public string id { get; set; }
}

static CosmosDBHelpers()
{
var builder = new System.Data.Common.DbConnectionStringBuilder();
builder.ConnectionString = Constants.CosmosDB.CosmosDBConnectionStringSetting;
var serviceUri = new Uri(builder["AccountEndpoint"].ToString());
_docDbClient = new DocumentClient(serviceUri, builder["AccountKey"].ToString());
var serviceUri = builder["AccountEndpoint"].ToString();
_cosmosDbClient = new CosmosClient(serviceUri, builder["AccountKey"].ToString());
}

// keep
public async static Task CreateDocument(string docId)
{
Document documentToTest = new Document()
{
Id = docId
id = docId
};

Document insertedDoc = await _docDbClient.CreateDocumentAsync(UriFactory.CreateDocumentCollectionUri(Constants.CosmosDB.DbName, Constants.CosmosDB.InputCollectionName), documentToTest);
}
Container _inputContainer = _cosmosDbClient.GetContainer(Constants.CosmosDB.DbName, Constants.CosmosDB.InputCollectionName);

public async static Task CreateDocument(TestDocument testDocument)
{
Document insertedDoc = await _docDbClient.CreateDocumentAsync(UriFactory.CreateDocumentCollectionUri(Constants.CosmosDB.DbName, Constants.CosmosDB.InputCollectionName), testDocument);
Document insertedDoc = await _inputContainer.CreateItemAsync<Document>(documentToTest, new PartitionKey(documentToTest.id));
}

// keep
public async static Task<string> ReadDocument(string docId)
{
var docUri = UriFactory.CreateDocumentUri(Constants.CosmosDB.DbName, Constants.CosmosDB.OutputCollectionName, docId);
Document retrievedDocument = null;
await Utilities.RetryAsync(async () =>
{
try
{
retrievedDocument = await _docDbClient.ReadDocumentAsync(docUri);
Container container = _cosmosDbClient.GetContainer(Constants.CosmosDB.DbName, Constants.CosmosDB.OutputCollectionName);

retrievedDocument = await container.ReadItemAsync<Document>(docId, new PartitionKey(docId));
return true;
}
catch (DocumentClientException ex) when (ex.Error.Code == "NotFound" || ex.Error.Code == "Not Found")
catch (CosmosException ex) when (ex.StatusCode == System.Net.HttpStatusCode.NotFound)
{
return false;
}
}, 120000, 4000);
return retrievedDocument.Id;
return retrievedDocument.id;
}

// keep
public async static Task DeleteTestDocuments(string docId)
{
var inputDocUri = UriFactory.CreateDocumentUri(Constants.CosmosDB.DbName, Constants.CosmosDB.InputCollectionName, docId);
await DeleteDocument(inputDocUri);
var outputDocUri = UriFactory.CreateDocumentUri(Constants.CosmosDB.DbName, Constants.CosmosDB.OutputCollectionName, docId);
await DeleteDocument(outputDocUri);
await DeleteDocument(Constants.CosmosDB.DbName, Constants.CosmosDB.InputCollectionName, docId);
await DeleteDocument(Constants.CosmosDB.DbName, Constants.CosmosDB.OutputCollectionName, docId);
}

private async static Task DeleteDocument(Uri docUri)
private async static Task DeleteDocument(string dbName, string collectionName, string docId)
{
try
{
await _docDbClient.DeleteDocumentAsync(docUri);
Container container = _cosmosDbClient.GetContainer(dbName, collectionName);
await container.DeleteItemAsync<Document>(docId, new PartitionKey(docId));
}
catch (Exception)
{
Expand All @@ -89,41 +83,55 @@ private async static Task DeleteDocument(Uri docUri)
// keep
public async static Task CreateDocumentCollections()
{
Database db = await _docDbClient.CreateDatabaseIfNotExistsAsync(new Database { Id = Constants.CosmosDB.DbName });
Uri dbUri = UriFactory.CreateDatabaseUri(db.Id);

await CreateCollection(dbUri, Constants.CosmosDB.InputCollectionName);
await CreateCollection(dbUri, Constants.CosmosDB.OutputCollectionName);
await CreateCollection(dbUri, Constants.CosmosDB.LeaseCollectionName);
Database db = await _cosmosDbClient.CreateDatabaseIfNotExistsAsync(Constants.CosmosDB.DbName);

await CreateCollection(Constants.CosmosDB.DbName, Constants.CosmosDB.InputCollectionName, "/id");
await CreateCollection(Constants.CosmosDB.DbName, Constants.CosmosDB.OutputCollectionName, "/id");
// While using extensions v2-3, the leases may not have a partition key, but the new SDK requires
// one to manually create a collection. This comment may be removed and this line uncommented when
// extension bundles for tests are updated.
//await CreateCollection(Constants.CosmosDB.DbName, Constants.CosmosDB.LeaseCollectionName, "/id");
}
public async static Task DeleteDocumentCollections()
{
await DeleteCollection(inputCollectionsUri);
await DeleteCollection(outputCollectionsUri);
await DeleteCollection(leasesCollectionsUri);
await DeleteCollection(Constants.CosmosDB.DbName, Constants.CosmosDB.InputCollectionName);
await DeleteCollection(Constants.CosmosDB.DbName, Constants.CosmosDB.OutputCollectionName);
await DeleteCollection(Constants.CosmosDB.DbName, Constants.CosmosDB.LeaseCollectionName);
}

private async static Task DeleteCollection(Uri collectionUri)
private async static Task DeleteCollection(string dbName, string collectionName)
{
try
{
await _docDbClient.DeleteDocumentCollectionAsync(collectionUri);
Database database = _cosmosDbClient.GetDatabase(dbName);
await database.GetContainer(collectionName).DeleteContainerAsync();
}
catch (Exception)
{
//Ignore
}
}

private async static Task CreateCollection(Uri dbUri, string collectioName)
private async static Task CreateCollection(string dbName, string collectionName, string partitionKey)
{
DocumentCollection collection = new DocumentCollection() { Id = collectioName };
await _docDbClient.CreateDocumentCollectionIfNotExistsAsync(dbUri, collection,
new RequestOptions()
Database database = _cosmosDbClient.GetDatabase(dbName);
IndexingPolicy indexingPolicy = new IndexingPolicy
{
IndexingMode = IndexingMode.Consistent,
Automatic = true,
IncludedPaths =
{
OfferThroughput = 400
});
new IncludedPath
{
Path = "/*"
}
}
};
var containerProperties = new ContainerProperties(collectionName, partitionKey)
{
IndexingPolicy = indexingPolicy
};
await database.CreateContainerIfNotExistsAsync(containerProperties, 400);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

using Microsoft.Azure.EventHubs;
using Azure.Messaging.EventHubs;
using Azure.Messaging.EventHubs.Producer;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Collections.Generic;
Expand Down Expand Up @@ -29,13 +30,11 @@ public static async Task SendJSONMessagesAsync(string eventId, string eventHubNa
events.Add(evt);
}

EventHubsConnectionStringBuilder builder = new EventHubsConnectionStringBuilder(Constants.EventHubs.EventHubsConnectionStringSetting);
builder.EntityPath = eventHubName;
EventHubClient eventHubClient = EventHubClient.CreateFromConnectionString(builder.ToString());
EventHubProducerClient eventHubClient = new EventHubProducerClient(Constants.EventHubs.EventHubsConnectionStringSetting, eventHubName);
await eventHubClient.SendAsync(events);
}

public static async Task SendMessagesAsync(string eventId, string evenHubName)
public static async Task SendMessagesAsync(string eventId, string eventHubName)
{
// write 3 events
List<EventData> events = new List<EventData>();
Expand All @@ -48,9 +47,7 @@ public static async Task SendMessagesAsync(string eventId, string evenHubName)
events.Add(evt);
}

EventHubsConnectionStringBuilder builder = new EventHubsConnectionStringBuilder(Constants.EventHubs.EventHubsConnectionStringSetting);
builder.EntityPath = evenHubName;
EventHubClient eventHubClient = EventHubClient.CreateFromConnectionString(builder.ToString());
EventHubProducerClient eventHubClient = new EventHubProducerClient(Constants.EventHubs.EventHubsConnectionStringSetting, eventHubName);
await eventHubClient.SendAsync(events);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"leaseCollectionName": "leases",
"connectionStringSetting": "AzureWebJobsCosmosDBConnectionString",
"databaseName": "ItemDb",
"collectionName": "ItemCollectionIn",
"collectionName": "PartitionedItemCollectionIn",
"createLeaseCollectionIfNotExists": true
},
{
Expand All @@ -17,7 +17,7 @@
"leaseCollectionName": "leases",
"connectionStringSetting": "AzureWebJobsCosmosDBConnectionString",
"databaseName": "ItemDb",
"collectionName": "ItemCollectionOut"
"collectionName": "PartitionedItemCollectionOut"
}
]
}

0 comments on commit f7d1387

Please sign in to comment.