Skip to content

Commit

Permalink
CON-1640-adding tests to test polly timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
shomavg committed Apr 21, 2020
1 parent 98e08d6 commit e7207ec
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using FluentAssertions;
using Moq;
using Newtonsoft.Json;
using NUnit.Framework;
using Polly;
using Polly.Registry;
using SFA.DAS.EmployerAccounts.Interfaces;
using SFA.DAS.EmployerAccounts.Models.Reservations;
using SFA.DAS.EmployerAccounts.Services;

namespace SFA.DAS.EmployerAccounts.UnitTests.Services.Reservations
{
class WhenIGetReservationWithTimeoutData
{
private Mock<IReservationsApiClient> _mockReservationsApiClient;
private IReservationsService _sut;
private Mock<ReservationsService> _mockreservationsService;
private string _testData;
private Mock<IAsyncPolicy> _mockPolicy;

long _accountId;

[SetUp]
public void Arrange()
{
_accountId = 123;
_testData = JsonConvert.SerializeObject(new List<Reservation> { new Reservation { AccountId = _accountId } });

_mockReservationsApiClient = new Mock<IReservationsApiClient>();
_mockReservationsApiClient
.Setup(m => m.Get(_accountId))
.ReturnsAsync(_testData);
_mockPolicy = new Mock<IAsyncPolicy>();
var mockRegistryPolicy = new PolicyRegistry();
mockRegistryPolicy.Add(Constants.DefaultServiceTimeout, _mockPolicy.Object);
_mockreservationsService = new Mock<ReservationsService>(_mockReservationsApiClient.Object);
_sut = new ReservationsServiceWithTimeout(_mockreservationsService.Object, mockRegistryPolicy);
}

[Test]
public async Task ThenTheReservationsAreReturnedFromTheService()
{
//arrange
IEnumerable<Reservation> reservations = new List<Reservation> {
new Reservation {AccountId = _accountId}
};
_mockreservationsService.Setup(rs => rs.Get(_accountId))
.Returns(It.IsAny<Task<IEnumerable<Reservation>>>());
//act
await _sut.Get(_accountId);

// assert
_mockPolicy.Verify(p => p.ExecuteAsync(It.IsAny<Func<Task<IEnumerable<Reservation>>>>()));
}

[Test]
public async Task ThenTheReservationsServiceReturnsATimeout()
{
//act
await _sut.Get(_accountId);

// assert
_mockPolicy.Verify(p => p.ExecuteAsync(It.IsAny<Func<Task<IEnumerable<Reservation>>>>()));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public ReservationsService(IReservationsApiClient client)

public async Task<IEnumerable<Reservation>> Get(long accountId)
{
string reservations = await _client.Get(accountId);
var reservations = await _client.Get(accountId);
return JsonConvert.DeserializeObject<IEnumerable<Reservation>>(reservations);
}
}
Expand Down

0 comments on commit e7207ec

Please sign in to comment.