public async void GetAcademiesByTrustId_OkResult_WithItems_ReturnsCorrectResult() { //Arrange var returnedEntities = new ResultSet <GetAcademiesD365Model> { Items = new List <GetAcademiesD365Model> { new GetAcademiesD365Model(), new GetAcademiesD365Model() } }; var json = JsonConvert.SerializeObject(returnedEntities); HttpResponseMessage httpResponse = new HttpResponseMessage { StatusCode = HttpStatusCode.OK, Content = new StringContent(json, Encoding.UTF8, "application/json") }; var mockClient = new Mock <IAuthenticatedHttpClient>(); mockClient.Setup(m => m.GetAsync(It.IsAny <string>())).ReturnsAsync(httpResponse); var mockedLogger = new Mock <ILogger <AcademiesRepository> >(); var mockUrlBuilder = new Mock <IOdataUrlBuilder <GetAcademiesD365Model> >(); mockUrlBuilder.Setup(m => m.GetPropertyAnnotation(nameof(GetAcademiesD365Model.ParentTrustId))) .Returns("_parentaccountid_value"); mockUrlBuilder.Setup(m => m.BuildFilterUrl("accounts", It.IsAny <List <string> >())) .Returns("buildFilterUrl"); var academiesRepository = new AcademiesRepository(mockClient.Object, mockUrlBuilder.Object, mockedLogger.Object); var academyId = Guid.Parse("a16e9020-9123-4420-8055-851d1b672fb1"); //Execute var result = await academiesRepository.GetAcademiesByTrustId(academyId); //Assert var expectedFilters = new List <string> { "(_parentaccountid_value eq a16e9020-9123-4420-8055-851d1b672fb1)" }; mockUrlBuilder.Verify(m => m.BuildFilterUrl("accounts", expectedFilters), Times.Once); mockClient.Verify(m => m.GetAsync("buildFilterUrl"), Times.Once); Assert.True(result.IsValid); Assert.NotNull(result.Result); Assert.Equal(2, result.Result.Count); }
public async void GetAcademiesByTrustId_FailedHttpClientCall_ReturnsFailedResult() { //Arrange HttpResponseMessage httpResponse = new HttpResponseMessage { StatusCode = HttpStatusCode.BadRequest, Content = new StringContent("Error - Bad Request", Encoding.UTF8, "application/json") }; var mockClient = new Mock <IAuthenticatedHttpClient>(); mockClient.Setup(m => m.GetAsync(It.IsAny <string>())).ReturnsAsync(httpResponse); var mockedLogger = new Mock <ILogger <AcademiesRepository> >(); var mockUrlBuilder = new Mock <IOdataUrlBuilder <GetAcademiesD365Model> >(); mockUrlBuilder.Setup(m => m.GetPropertyAnnotation(nameof(GetAcademiesD365Model.ParentTrustId))) .Returns("mockedTrustIdField"); mockUrlBuilder.Setup(m => m.BuildFilterUrl("accounts", It.IsAny <List <string> >())) .Returns("buildFilterUrl"); var academiesRepository = new AcademiesRepository(mockClient.Object, mockUrlBuilder.Object, mockedLogger.Object); var academyId = Guid.Parse("a16e9020-9123-4420-8055-851d1b672fb1"); //Execute var result = await academiesRepository.GetAcademiesByTrustId(academyId); //Assert var expectedFilters = new List <string> { "(mockedTrustIdField eq a16e9020-9123-4420-8055-851d1b672fb1)" }; mockUrlBuilder.Verify(m => m.BuildFilterUrl("accounts", expectedFilters), Times.Once); mockClient.Verify(m => m.GetAsync("buildFilterUrl"), Times.Once); Assert.False(result.IsValid); Assert.Null(result.Result); Assert.Equal(HttpStatusCode.BadRequest, result.Error.StatusCode); Assert.Equal("Error - Bad Request", result.Error.ErrorMessage); }