From 946f9d4acf62d7406b3738b08204b5dbd899b1c1 Mon Sep 17 00:00:00 2001 From: Cecilia Avila <44245136+ceciliaavila@users.noreply.github.com> Date: Mon, 18 Sep 2023 12:00:31 -0300 Subject: [PATCH] replace TestFramework package usage (#6690) Co-authored-by: JhontSouth --- .../Microsoft.Bot.Connector.Tests/BaseTest.cs | 1 - .../Microsoft.Bot.Connector.Tests.csproj | 3 +- .../MockContext.cs | 103 ++++++++++++++++++ .../ResourceGroupCleaner.cs | 61 +++++++++++ 4 files changed, 165 insertions(+), 3 deletions(-) create mode 100644 tests/Microsoft.Bot.Connector.Tests/MockContext.cs create mode 100644 tests/Microsoft.Bot.Connector.Tests/ResourceGroupCleaner.cs diff --git a/tests/Microsoft.Bot.Connector.Tests/BaseTest.cs b/tests/Microsoft.Bot.Connector.Tests/BaseTest.cs index 0a4c6dcb9b..2fc885adc5 100644 --- a/tests/Microsoft.Bot.Connector.Tests/BaseTest.cs +++ b/tests/Microsoft.Bot.Connector.Tests/BaseTest.cs @@ -11,7 +11,6 @@ using Microsoft.Bot.Connector.Authentication; using Microsoft.Bot.Schema; using Microsoft.Rest; -using Microsoft.Rest.ClientRuntime.Azure.TestFramework; using Moq; namespace Microsoft.Bot.Connector.Tests diff --git a/tests/Microsoft.Bot.Connector.Tests/Microsoft.Bot.Connector.Tests.csproj b/tests/Microsoft.Bot.Connector.Tests/Microsoft.Bot.Connector.Tests.csproj index 68f54caefb..c076ed7830 100644 --- a/tests/Microsoft.Bot.Connector.Tests/Microsoft.Bot.Connector.Tests.csproj +++ b/tests/Microsoft.Bot.Connector.Tests/Microsoft.Bot.Connector.Tests.csproj @@ -12,10 +12,9 @@ - + - diff --git a/tests/Microsoft.Bot.Connector.Tests/MockContext.cs b/tests/Microsoft.Bot.Connector.Tests/MockContext.cs new file mode 100644 index 0000000000..15c4ea509c --- /dev/null +++ b/tests/Microsoft.Bot.Connector.Tests/MockContext.cs @@ -0,0 +1,103 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +using System; +using System.Collections.Generic; +using Microsoft.Azure.Test.HttpRecorder; +using Microsoft.Azure.Test.HttpRecorder.ProcessRecordings; + +namespace Microsoft.Bot.Connector.Tests +{ + /// + /// A coordinator for tracking and undoing WAML operations. Usage pattern is + /// using(MockContext.Create()) + /// { + /// maml stuff + /// } + /// You can also manually call the Dispose() or UndoAll() methods to undo all 'undoable' operations since the + /// UndoContext was created. + /// Call: MockContext.Commit() to remove all undo information. + /// + public class MockContext : IDisposable + { + //prevent multiple dispose events + private bool disposed = false; + private List undoHandlers = new List(); + + internal bool OptimizeTestRecordingFile { get; set; } = false; + + /// + /// Initialize a new MockContext. + /// + /// The class name to identify the mock server. + /// Returns a new MockContext. + /// The name method used for the test. + public static MockContext Start( + string className, + [System.Runtime.CompilerServices.CallerMemberName] + string methodName = "testframework_failed") + { + var context = new MockContext(); + if (HttpMockServer.FileSystemUtilsObject == null) + { + HttpMockServer.FileSystemUtilsObject = new FileSystemUtils(); + } + + HttpMockServer.Initialize(className, methodName); + if (HttpMockServer.Mode != HttpRecorderMode.Playback) + { + context.disposed = false; + } + + return context; + } + + /// + /// Dispose the object. + /// + public void Dispose() + { + this.Dispose(true); + } + + /// + /// Stop recording and Discard all undo information. + /// + public void Stop() + { + if (HttpMockServer.Mode != HttpRecorderMode.Playback) + { + foreach (var undoHandler in undoHandlers) + { + undoHandler.DeleteResourceGroups().ConfigureAwait(false).GetAwaiter().GetResult(); + } + } + + string recordedFilePath = HttpMockServer.Flush(); + + if (HttpMockServer.Mode == HttpRecorderMode.Record) + { + // this check should be removed once we make the optimizatoin default + if (OptimizeTestRecordingFile) + { + ProcessRecordedFiles procRecFile = new ProcessRecordedFiles(recordedFilePath); + procRecFile.CompactLroPolling(); + procRecFile.SerializeCompactData(); + } + } + } + + /// + /// Dispose only if we have not previously been disposed. + /// + /// true if we should dispose, otherwise false. + protected virtual void Dispose(bool disposing) + { + if (disposing && !this.disposed) + { + this.Stop(); + this.disposed = true; + } + } + } +} diff --git a/tests/Microsoft.Bot.Connector.Tests/ResourceGroupCleaner.cs b/tests/Microsoft.Bot.Connector.Tests/ResourceGroupCleaner.cs new file mode 100644 index 0000000000..6939b08671 --- /dev/null +++ b/tests/Microsoft.Bot.Connector.Tests/ResourceGroupCleaner.cs @@ -0,0 +1,61 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Globalization; +using System.Net.Http; +using System.Text.RegularExpressions; +using System.Threading; +using System.Threading.Tasks; +using Microsoft.Rest; + +namespace Microsoft.Bot.Connector.Tests +{ + public class ResourceGroupCleaner : DelegatingHandler + { + private Regex _resourceGroupPattern = new Regex(@"/subscriptions/[^/]+/resourcegroups/([^?]+)\?api-version"); + private HashSet _resourceGroupsCreated = new HashSet(); + private TokenCredentials _tokenCredentials; + + public ResourceGroupCleaner(TokenCredentials tokenCredentials) + { + _tokenCredentials = tokenCredentials; + } + + public async Task DeleteResourceGroups() + { + HttpClient httpClient = new HttpClient(); + foreach (var resourceGroupUri in _resourceGroupsCreated) + { + HttpRequestMessage httpRequest = new HttpRequestMessage(); + httpRequest.Method = new HttpMethod("DELETE"); + httpRequest.RequestUri = new Uri(resourceGroupUri); + + _tokenCredentials.ProcessHttpRequestAsync(httpRequest, CancellationToken.None).ConfigureAwait(false).GetAwaiter().GetResult(); + + HttpResponseMessage httpResponse = await httpClient.SendAsync(httpRequest).ConfigureAwait(false); + string groupName = _resourceGroupPattern.Match(resourceGroupUri).Groups[1].Value; + string message = string.Format( + CultureInfo.InvariantCulture, + "Started deletion of resource group '{0}'. Server responded with status code {1}.", + groupName, + httpResponse.StatusCode); + Console.WriteLine(message); + Debug.WriteLine(message); + } + } + + protected override Task SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) + { + if (_resourceGroupPattern.IsMatch(request.RequestUri.AbsoluteUri) && + request.Method == HttpMethod.Put) + { + _resourceGroupsCreated.Add(request.RequestUri.AbsoluteUri); + } + + return base.SendAsync(request, cancellationToken); + } + } +}