Skip to content

Commit

Permalink
Add unit test tests #833 #789
Browse files Browse the repository at this point in the history
  • Loading branch information
hocinehacherouf committed Jun 20, 2022
1 parent 61036b1 commit a20d522
Show file tree
Hide file tree
Showing 5 changed files with 186 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace AzureIoTHub.Portal.Server.Tests.Unit.Pages
{
using System;
using System.Collections.Generic;
using System.Net.Http;
using Bunit;
using Client.Exceptions;
Expand Down Expand Up @@ -71,6 +72,112 @@ public void DeviceConfigurationDetailPageShouldRenderCorrectly()
cut.WaitForAssertion(() => this.mockHttpClient.VerifyNoOutstandingExpectation());
}

[Test]
public void DeviceConfigurationDetailShouldCreateConfiguration()
{
// Arrange
var modelId = Guid.NewGuid().ToString();
var configuration = new DeviceConfig
{
ConfigurationId = Guid.NewGuid().ToString(),
ModelId = Guid.NewGuid().ToString(),
Priority = 1,
Tags = new Dictionary<string, string>(),
Properties = new Dictionary<string, string>()
};

_ = this.mockHttpClient
.When(HttpMethod.Get, "/api/models")
.RespondJson(Array.Empty<DeviceModel>());

_ = this.mockHttpClient
.When(HttpMethod.Get, $"/api/models/{modelId}/properties")
.RespondJson(Array.Empty<DeviceProperty>());

_ = this.mockHttpClient
.When(HttpMethod.Get, "/api/settings/device-tags")
.RespondJson(Array.Empty<DeviceTag>());

_ = this.mockHttpClient.When(HttpMethod.Put, $"/api/device-configurations")
.RespondText(string.Empty)
.With(m =>
{
Assert.IsAssignableFrom<ObjectContent<DeviceConfig>>(m.Content);
var jsonContent = m.Content as ObjectContent<DeviceConfig>;

Assert.IsAssignableFrom<DeviceConfig>(jsonContent.Value);
Assert.AreEqual(configuration, jsonContent.Value);

return true;
});

var cut = RenderComponent<CreateDeviceConfigurationsPage>();
cut.WaitForAssertion(() => cut.Find("#saveButton"));
cut.Instance.Configuration = configuration;


// Act
cut.Find("#saveButton").Click();

// Assert
cut.WaitForAssertion(() => cut.Instance.IsLoading.Should().BeFalse());
cut.WaitForAssertion(() => this.mockHttpClient.VerifyNoOutstandingRequest());
cut.WaitForAssertion(() => this.mockHttpClient.VerifyNoOutstandingExpectation());
}

[Test]
public void DeviceConfigurationDetailShouldProcessProblemDetailsExceptionWhenIssueOccursOnCreatingConfiguration()
{
// Arrange
var modelId = Guid.NewGuid().ToString();
var configuration = new DeviceConfig
{
ConfigurationId = Guid.NewGuid().ToString(),
ModelId = Guid.NewGuid().ToString(),
Priority = 1,
Tags = new Dictionary<string, string>(),
Properties = new Dictionary<string, string>()
};

_ = this.mockHttpClient
.When(HttpMethod.Get, "/api/models")
.RespondJson(Array.Empty<DeviceModel>());

_ = this.mockHttpClient
.When(HttpMethod.Get, $"/api/models/{modelId}/properties")
.RespondJson(Array.Empty<DeviceProperty>());

_ = this.mockHttpClient
.When(HttpMethod.Get, "/api/settings/device-tags")
.RespondJson(Array.Empty<DeviceTag>());

_ = this.mockHttpClient.When(HttpMethod.Put, $"/api/device-configurations")
.Throw(new ProblemDetailsException(new ProblemDetailsWithExceptionDetails()))
.With(m =>
{
Assert.IsAssignableFrom<ObjectContent<DeviceConfig>>(m.Content);
var jsonContent = m.Content as ObjectContent<DeviceConfig>;

Assert.IsAssignableFrom<DeviceConfig>(jsonContent.Value);
Assert.AreEqual(configuration, jsonContent.Value);

return true;
});

var cut = RenderComponent<CreateDeviceConfigurationsPage>();
cut.WaitForAssertion(() => cut.Find("#saveButton"));
cut.Instance.Configuration = configuration;


// Act
cut.Find("#saveButton").Click();

// Assert
cut.WaitForAssertion(() => cut.Instance.IsLoading.Should().BeFalse());
cut.WaitForAssertion(() => this.mockHttpClient.VerifyNoOutstandingRequest());
cut.WaitForAssertion(() => this.mockHttpClient.VerifyNoOutstandingExpectation());
}

[Test]
public void DeviceConfigurationDetailPageShouldProcessProblemDetailsExceptionWhenIssueOccursOnLoadingModels()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,25 @@ public void DeviceConfigurationDetailPageShouldRenderCorrectly()
cut.WaitForAssertion(() => this.mockHttpClient.VerifyNoOutstandingExpectation());
}

[Test]
public void DeviceConfigurationDetailPageShouldProcessProblemDetailsExceptionWhenIssueOccursOnGettingConfiguration()
{
// Arrange
var configurationId = Guid.NewGuid().ToString();

_ = this.mockHttpClient
.When(HttpMethod.Get, $"/api/device-configurations/{configurationId}")
.Throw(new ProblemDetailsException(new ProblemDetailsWithExceptionDetails()));

// Act
var cut = RenderComponent<DeviceConfigurationDetailPage>(ComponentParameter.CreateParameter("ConfigId", configurationId));

// Assert
cut.WaitForAssertion(() => cut.Instance.IsLoading.Should().BeFalse());
cut.WaitForAssertion(() => this.mockHttpClient.VerifyNoOutstandingRequest());
cut.WaitForAssertion(() => this.mockHttpClient.VerifyNoOutstandingExpectation());
}

[Test]
public void DeviceConfigurationDetailPageShouldRenderCardCorrectly()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,31 @@ public void ClickOnSaveShouldProcessProblemDetailsExceptionWhenIssueOccursOnCrea
cut.WaitForAssertion(() => this.mockRepository.VerifyAll());
}

[Test]
public void ClickOnSaveShouldNotCreateConcentratorWhenModelIsNotValid()
{
// Arrange

var mockDialogReference = new DialogReference(Guid.NewGuid(), this.mockDialogService.Object);

_ = this.mockDialogService.Setup(c => c.Show<ProcessingDialog>(It.IsAny<string>(), It.IsAny<DialogParameters>()))
.Returns(mockDialogReference);
_ = this.mockDialogService.Setup(c => c.Close(It.Is<DialogReference>(x => x == mockDialogReference)));

_ = this.mockSnackbarService.Setup(c => c.Add(It.IsAny<string>(), Severity.Error, null)).Returns((Snackbar)null);

var cut = RenderComponent<CreateConcentratorPage>();
cut.WaitForAssertion(() => cut.Find("#saveButton"));

// Act
cut.Find("#saveButton").Click();

// Assert
this.mockHttpClient.VerifyNoOutstandingRequest();
this.mockHttpClient.VerifyNoOutstandingExpectation();
cut.WaitForAssertion(() => this.mockRepository.VerifyAll());
}

public void Dispose()
{
Dispose(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,40 @@ public async Task DeleteConcentratorPageShouldProcessProblemDetailsExceptionWhen
cut.WaitForAssertion(() => this.mockHttpClient.VerifyNoOutstandingExpectation());
}

[Test]
public async Task OnClickOnCancelShouldCancelDialog()
{
// Arrange
var deviceId = Guid.NewGuid().ToString();

_ = this.mockHttpClient
.When(HttpMethod.Delete, $"/api/lorawan/concentrators/{deviceId}")
.Throw(new ProblemDetailsException(new ProblemDetailsWithExceptionDetails()));

var cut = RenderComponent<MudDialogProvider>();

var parameters = new DialogParameters
{
{
"deviceId", deviceId
}
};

IDialogReference dialogReference = null;

await cut.InvokeAsync(() => dialogReference = this.dialogService?.Show<DeleteConcentratorPage>(string.Empty, parameters));
cut.WaitForAssertion(() => cut.Find("#cancel-delete-concentrator"));

// Act
cut.Find("#cancel-delete-concentrator").Click();
var result = await dialogReference.Result;

// Assert
_ = result.Cancelled.Should().BeTrue();
cut.WaitForAssertion(() => this.mockHttpClient.VerifyNoOutstandingRequest());
cut.WaitForAssertion(() => this.mockHttpClient.VerifyNoOutstandingExpectation());
}

public void Dispose()
{
Dispose(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<p><i>Warning : this cannot be undone.</i></p>
</DialogContent>
<DialogActions>
<MudButton OnClick="Cancel">Cancel</MudButton>
<MudButton id="cancel-delete-concentrator" OnClick="Cancel">Cancel</MudButton>
<MudButton id="delete-concentrator" Color="Color.Primary" OnClick="DeleteDevice">Delete</MudButton>
</DialogActions>
</MudDialog>
Expand Down

0 comments on commit a20d522

Please sign in to comment.