Skip to content

Commit

Permalink
CON-4835
Browse files Browse the repository at this point in the history
  • Loading branch information
VasanthaKasirajan3008 committed May 29, 2022
1 parent 1277499 commit 66d9074
Showing 1 changed file with 91 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
using System;
using System.Configuration;
using System.Net;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Moq;
using Moq.Protected;
using NUnit.Framework;
using SFA.DAS.EmployerAccounts.Configuration;
using SFA.DAS.EmployerAccounts.Interfaces;
using SFA.DAS.EmployerAccounts.Services;

namespace SFA.DAS.EmployerAccounts.UnitTests.Services.ProviderRegistration
{
class WhenIGetInvitation
{
private IProviderRegistrationApiClient _sut;
private ProviderRegistrationClientApiConfiguration _configuration;
private string _correlationId;
private string _testData;
private string _apiBaseUrl;
private string _identifierUri;
Mock<HttpMessageHandler> _mockHttpMessageHandler;

[SetUp]
public void Arrange()
{
ConfigurationManager.AppSettings["EnvironmentName"] = "LOCAL";
_apiBaseUrl = $"http://{Guid.NewGuid().ToString()}/";
_identifierUri = Guid.NewGuid().ToString();
_correlationId = Guid.NewGuid().ToString();
_testData = "Employer details";

_configuration = new ProviderRegistrationClientApiConfiguration
{
BaseUrl = _apiBaseUrl,
IdentifierUri = _identifierUri
};

_mockHttpMessageHandler = new Mock<HttpMessageHandler>();

_mockHttpMessageHandler
.Protected()
.Setup<Task<HttpResponseMessage>>("SendAsync",
ItExpr.IsAny<HttpRequestMessage>(),
ItExpr.IsAny<CancellationToken>())
.ReturnsAsync(new HttpResponseMessage
{
Content = new StringContent(_testData),
StatusCode = HttpStatusCode.OK
}
).Verifiable("");

var httpClient = new HttpClient(_mockHttpMessageHandler.Object);

_sut = new ProviderRegistrationApiClient(httpClient, _configuration);
}

[Test]
public async Task Verify_ProviderRegistrationApiToGetInvitationIsCalled()
{
//act
await _sut.GetInvitations(_correlationId);

//assert
_mockHttpMessageHandler
.Protected()
.Verify("SendAsync", Times.Once(),
ItExpr.Is<HttpRequestMessage>(r => r.Method == HttpMethod.Get
&& r.RequestUri.AbsoluteUri == $"{_configuration.BaseUrl}api/invitations/{_correlationId}"),
ItExpr.IsAny<CancellationToken>());
}

[Test]
public async Task Verify_ProviderRegistrationApiToUnsubscribeIsCalled()
{
//act
await _sut.Unsubscribe(_correlationId);

//assert
_mockHttpMessageHandler
.Protected()
.Verify("SendAsync", Times.Once(),
ItExpr.Is<HttpRequestMessage>(r => r.Method == HttpMethod.Get
&& r.RequestUri.AbsoluteUri == $"{_configuration.BaseUrl}api/unsubscribe/{_correlationId}"),
ItExpr.IsAny<CancellationToken>());
}

}
}

0 comments on commit 66d9074

Please sign in to comment.