Skip to content

Commit

Permalink
Test:MainWindowViewModel.ConnectionBtnPopupCommand when connections s…
Browse files Browse the repository at this point in the history
…tring dialogue ok'd with a connectionstring

This commit adds a test.

This commit contains no functional change.
  • Loading branch information
LosManos committed Dec 5, 2023
1 parent a1dfa95 commit 1556b7e
Showing 1 changed file with 72 additions and 7 deletions.
79 changes: 72 additions & 7 deletions Test/PurpleExplorerTest/ViewModels/MainWindowViewModelTest.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections.ObjectModel;
using FluentAssertions;
using Microsoft.Azure.ServiceBus.Management;
using Moq;
using PurpleExplorer.Helpers;
using PurpleExplorer.Models;
Expand Down Expand Up @@ -102,29 +103,85 @@ public void ConnectionBtnPopupCommand_sets_connectionstring_and_nothing_else_Whe

// Assert.
sut.ConnectionString.Should().NotBe(myOriginalConnectionString);
Assert.True(AreEqual((myNewConnectionString.ConnectionString, myNewConnectionString.UseManagedIdentity),
sut.ConnectionString));
Assert.True(AreSameExceptName(myNewConnectionString, sut.ConnectionString));

// Verify no call is made to any topic or queue to verify nothing was done
// when the user chose Cancel. A bit crude, but I found no better way.
topicHelper.VerifyAll();
queueHelper.VerifyAll();
}

[Fact]
public void ConnectionBtnPopupCommand_sets_ConnectedServiceBuses_When_ok_with_connectionstring_value()
{
var myOriginalConnectionString = new ServiceBusConnectionString();
var myNewConnectionString = new ServiceBusConnectionString
{
Name = string.Empty,
ConnectionString = "MyNewConnectionString",
UseManagedIdentity = true,
};
var myNameSpaceInfo = new NamespaceInfo
{
Name = "MyNameSpaceInfoName",
CreatedTime = new DateTime(1906, 12, 09),
};

var sut = CreateSut(out _, out var appState, out var modalWindowService, out var topicHelper, out var queueHelper)
.With(s => s.ConnectionString = myOriginalConnectionString);

appState.Setup(p => p.SavedConnectionStrings)
.Returns(new ObservableCollection<ServiceBusConnectionString>());

modalWindowService.Setup_ShowModalWindow(
new ConnectionStringWindowViewModel(appState.Object)
.With(c=> c.Cancel = false)
.With(c => c.ConnectionString = myNewConnectionString.ConnectionString)
.With(c => c.UseManagedIdentity = myNewConnectionString.UseManagedIdentity)
);

topicHelper.Setup(m => m.GetNamespaceInfo(It_Is_SameExceptName(myNewConnectionString)))
.ReturnsAsync(myNameSpaceInfo);

topicHelper.Setup(m => m.GetTopicsAndSubscriptions(It_Is_SameExceptName(myNewConnectionString)))
.ReturnsAsync(new List<ServiceBusTopic>());

queueHelper.Setup(m => m.GetQueues(It_Is_SameExceptName(myNewConnectionString)))
.ReturnsAsync(new List<ServiceBusQueue>());

// Act
sut.ConnectionBtnPopupCommand();

// Assert.
sut.ConnectionString.Should().NotBe(myOriginalConnectionString);
Assert.True(AreSameExceptName(myNewConnectionString, sut.ConnectionString));

sut.ConnectedServiceBuses.Single().Name.Should().Be(myNameSpaceInfo.Name);
sut.ConnectedServiceBuses.Single().CreatedTime.Should().Be(myNameSpaceInfo.CreatedTime);

AreSameExceptName(myNewConnectionString, sut.ConnectedServiceBuses.Single().ConnectionString);

// Without rewriting a lot we cannot test `ServiceBusQueue` nor `ServiceBusTopic`
// as they cannot be constructed outside Microsoft's own framework due to
// constructor visibility.
// So we just check there is something. Something empty.
sut.ConnectedServiceBuses.Single().Queues.Should().BeEmpty();
sut.ConnectedServiceBuses.Single().Topics.Should().BeEmpty();
}

#endregion

/// <summary>Returns true if the expected and actual contains the same values.*
/// False otherwise.
/// *)Note that Name is not compared as this is an implementation for a special case.
/// </summary>
/// <param name="expected"></param>
/// <param name="actual"></param>
/// <returns></returns>
static bool AreEqual((string connectionString, bool useManagedIdentity) expected,
static bool AreSameExceptName(
ServiceBusConnectionString expected,
ServiceBusConnectionString actual)
{
return expected.connectionString == actual.ConnectionString &&
expected.useManagedIdentity == actual.UseManagedIdentity;
return expected.ConnectionString == actual.ConnectionString &&
expected.UseManagedIdentity == actual.UseManagedIdentity;
}

private static MainWindowViewModel CreateSut(
Expand Down Expand Up @@ -152,4 +209,12 @@ out Mock<IQueueHelper> queueHelperMock
queueHelperMock = queueHelper;
return sut;
}

private static ServiceBusConnectionString It_Is_SameExceptName(ServiceBusConnectionString myNewConnectionString)
{
// // We do not compare name as for this case it is not interesting.
return It.Is<ServiceBusConnectionString>(x =>
AreSameExceptName(x, myNewConnectionString)
);
}
}

0 comments on commit 1556b7e

Please sign in to comment.