Skip to content

Commit

Permalink
Merge branch 'master' into RemoveUsernameDependencyOnVideoAPI
Browse files Browse the repository at this point in the history
  • Loading branch information
muralikaturi committed Jan 19, 2024
2 parents 66630ab + cea087d commit 8aa3348
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ public async Task Should_return_forbidden_request()
}

[Test]
public async Task Should_return_exception()
public async Task Should_forward_error_when_video_api_returns_error()
{
var apiException = new VideoApiException<ProblemDetails>("Internal Server Error",
(int) HttpStatusCode.InternalServerError,
Expand All @@ -184,5 +184,22 @@ public async Task Should_return_exception()
var typedResult = result.Value;
typedResult.Should().BeNull();
}

[Test]
public async Task Should_forward_error_when_bookings_api_returns_error()
{
var apiException = new BookingsApiException<ProblemDetails>("Internal Server Error",
(int)HttpStatusCode.InternalServerError,
"Stacktrace goes here", null, default, null);

_mocker.Mock<IBookingsApiClient>()
.Setup(x => x.GetConfirmedHearingsByUsernameForTodayAsync(It.IsAny<string>()))
.ThrowsAsync(apiException);

var result = await _controller.GetConferencesForHostAsync();

var typedResult = (ObjectResult)result.Result;
typedResult.StatusCode.Should().Be((int)HttpStatusCode.InternalServerError);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public void Setup()
_sut = _mocker.Create<ConferencesController>();
_sut.ControllerContext = context;
}

[Test]
public async Task Should_return_ok_with_list_of_conferences_for_quickLink_user()
{
Expand Down Expand Up @@ -181,7 +181,7 @@ public async Task Should_return_forbidden_request()
}

[Test]
public async Task Should_return_exception()
public async Task Should_forward_error_when_video_api_returns_error()
{
var apiException = new VideoApiException<ProblemDetails>("Internal Server Error", (int)HttpStatusCode.InternalServerError,
"Stacktrace goes here", null, default, null);
Expand All @@ -193,5 +193,22 @@ public async Task Should_return_exception()
var typedResult = result.Value;
typedResult.Should().BeNull();
}

[Test]
public async Task Should_forward_error_when_bookings_api_returns_error()
{
var apiException = new BookingsApiException<ProblemDetails>("Internal Server Error",
(int)HttpStatusCode.InternalServerError,
"Stacktrace goes here", null, default, null);

_mocker.Mock<IBookingsApiClient>()
.Setup(x => x.GetConfirmedHearingsByUsernameForTodayAsync(It.IsAny<string>()))
.ThrowsAsync(apiException);

var result = await _sut.GetConferencesForIndividual();

var typedResult = (ObjectResult)result.Result;
typedResult.StatusCode.Should().Be((int)HttpStatusCode.InternalServerError);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,23 @@ public async Task Should_forward_error_when_video_api_returns_error()
var typedResult = (ObjectResult)result.Result;
typedResult.StatusCode.Should().Be((int)HttpStatusCode.InternalServerError);
}

[Test]
public async Task Should_forward_error_when_bookings_api_returns_error()
{
var apiException = new BookingsApiException<ProblemDetails>("Internal Server Error",
(int)HttpStatusCode.InternalServerError,
"Stacktrace goes here", null, default, null);

_mocker.Mock<IBookingsApiClient>()
.Setup(x => x.GetHearingsForTodayByVenueAsync(It.IsAny<List<string>>()))
.ThrowsAsync(apiException);

var result = await _controller.GetConferencesForStaffMemberAsync(new List<string>());

var typedResult = (ObjectResult)result.Result;
typedResult.StatusCode.Should().Be((int)HttpStatusCode.InternalServerError);
}

[Test]
public async Task Should_return_ok_with_list_of_conferences()
Expand Down Expand Up @@ -103,9 +120,13 @@ public async Task Should_return_ok_with_no_conferences()
{
var hearingVenueNamesQuery = new List<string>();
var conferences = new List<Conference>();
var bookingException = new BookingsApiException("User does not have any hearings", (int)HttpStatusCode.NotFound, "Error", null, null);
_mocker.Mock<IVideoApiClient>()
.Setup(x => x.GetConferencesForHostByHearingRefIdAsync(It.IsAny<GetConferencesByHearingIdsRequest>()))
.ReturnsAsync(conferences);
_mocker.Mock<IBookingsApiClient>()
.Setup(x => x.GetHearingsForTodayByVenueAsync(It.IsAny<List<string>>()))
.ThrowsAsync(bookingException);

var result = await _controller.GetConferencesForStaffMemberAsync(hearingVenueNamesQuery);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,23 @@ public async Task Should_forward_error_when_video_api_returns_error()
var typedResult = (ObjectResult) result.Result;
typedResult.StatusCode.Should().Be((int) HttpStatusCode.InternalServerError);
}

[Test]
public async Task Should_forward_error_when_bookings_api_returns_error()
{
var apiException = new BookingsApiException<ProblemDetails>("Internal Server Error",
(int)HttpStatusCode.InternalServerError,
"Stacktrace goes here", null, default, null);

_mocker.Mock<IBookingsApiClient>()
.Setup(x => x.GetHearingsForTodayByVenueAsync(It.IsAny<List<string>>()))
.ThrowsAsync(apiException);

var result = await _controller.GetConferencesForVhOfficerAsync(new VhoConferenceFilterQuery());

var typedResult = (ObjectResult)result.Result;
typedResult.StatusCode.Should().Be((int)HttpStatusCode.InternalServerError);
}


[Test]
Expand Down Expand Up @@ -351,6 +368,27 @@ public async Task Should_return_ok_with_list_of_conferences_when_querying_by_cso
conferencesForUser.Count.Should().Be(1);
conferencesForUser[0].HearingRefId.Should().Be(unallocatedHearing.HearingId);
}

[Test]
public async Task Should_return_ok_with_no_conferences()
{
var conferences = new List<ConferenceForAdminResponse>();
var bookingException = new BookingsApiException("User does not have any hearings", (int)HttpStatusCode.NotFound, "Error", null, null);
_mocker.Mock<IVideoApiClient>()
.Setup(x => x.GetConferencesForAdminByHearingRefIdAsync(It.IsAny<GetConferencesByHearingIdsRequest>()))
.ReturnsAsync(conferences);
_mocker.Mock<IBookingsApiClient>()
.Setup(x => x.GetHearingsForTodayByVenueAsync(It.IsAny<List<string>>()))
.ThrowsAsync(bookingException);

var result = await _controller.GetConferencesForVhOfficerAsync(new VhoConferenceFilterQuery());

var typedResult = (OkObjectResult)result.Result;
typedResult.Should().NotBeNull();

var conferencesForUser = (List<ConferenceForVhOfficerResponse>)typedResult.Value;
conferencesForUser.Should().BeEmpty();
}

private ConferencesController SetupControllerWithClaims(ClaimsPrincipal claimsPrincipal)
{
Expand Down
53 changes: 31 additions & 22 deletions VideoWeb/VideoWeb/Controllers/ConferencesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,18 +80,11 @@ public async Task<ActionResult<List<ConferenceForHostResponse>>> GetConferencesF
}
catch (BookingsApiException e)
{
if (e.StatusCode == (int)HttpStatusCode.NotFound)
{
_logger.LogWarning("No hearings found for user");
return Ok(new List<ConferenceForHostResponse>());
}

throw;
return HandleBookingsApiExceptionForGetHearings<ConferenceForHostResponse>(e);
}
catch (VideoApiException e)
{
_logger.LogError(e, "Unable to get conferences for user");
return StatusCode(e.StatusCode, e.Response);
return HandleVideoApiExceptionForGetConferences(e);
}
}

Expand Down Expand Up @@ -119,10 +112,13 @@ public async Task<ActionResult<List<ConferenceForHostResponse>>> GetConferencesF
.ToList();
return Ok(response);
}
catch (BookingsApiException e)
{
return HandleBookingsApiExceptionForGetHearings<ConferenceForHostResponse>(e);
}
catch (VideoApiException e)
{
_logger.LogError(e, "Unable to get conferences for staff member");
return StatusCode(e.StatusCode, e.Response);
return HandleVideoApiExceptionForGetConferences(e);
}
}

Expand Down Expand Up @@ -166,18 +162,11 @@ public async Task<ActionResult<List<ConferenceForIndividualResponse>>> GetConfer
}
catch (BookingsApiException e)
{
if (e.StatusCode == (int)HttpStatusCode.NotFound)
{
_logger.LogWarning("No hearings found for user");
return Ok(new List<ConferenceForIndividualResponse>());
}

throw;
return HandleBookingsApiExceptionForGetHearings<ConferenceForIndividualResponse>(e);
}
catch (VideoApiException e)
{
_logger.LogError(e, "Unable to get conferences for user");
return StatusCode(e.StatusCode, e.Response);
return HandleVideoApiExceptionForGetConferences(e);
}
}

Expand Down Expand Up @@ -222,10 +211,13 @@ public async Task<ActionResult<List<ConferenceForVhOfficerResponse>>> GetConfere
responses.Sort(new SortConferenceForVhoOfficerHelper());
return Ok(responses);
}
catch (BookingsApiException e)
{
return HandleBookingsApiExceptionForGetHearings<ConferenceForVhOfficerResponse>(e);
}
catch (VideoApiException e)
{
_logger.LogError(e, "Unable to get conferences for vh officer");
return StatusCode(e.StatusCode, e.Response);
return HandleVideoApiExceptionForGetConferences(e);
}
}

Expand Down Expand Up @@ -377,5 +369,22 @@ public async Task<ActionResult<ConferenceResponse>> GetConferenceByIdAsync(Guid

return Ok(response);
}

private ActionResult HandleBookingsApiExceptionForGetHearings<T>(BookingsApiException e) where T : class
{
if (e.StatusCode == (int)HttpStatusCode.NotFound)
{
_logger.LogWarning("No hearings found for user");
return Ok(new List<T>());
}

return StatusCode(e.StatusCode, e.Response);
}

private ActionResult HandleVideoApiExceptionForGetConferences(VideoApiException e)
{
_logger.LogError(e, "Unable to get conferences for user");
return StatusCode(e.StatusCode, e.Response);
}
}
}

0 comments on commit 8aa3348

Please sign in to comment.