public async Task RetrieveRoadStatusAsync_WhenInvalidRoadGiven_InvalidRoadErrorReturnedInConsole() { //Arrange var mockApiService = new Mock <IApiService>(); mockApiService.Setup(x => x.GetAsync(It.IsAny <string>(), It.IsAny <Dictionary <string, string> >())).ReturnsAsync(new HttpResponseMessage { Content = new StringContent(_singleRoadApiResponse), StatusCode = HttpStatusCode.NotFound }); var roadStatusService = new RoadStatusService(mockApiService.Object, _applicationConfig); var expectedOutput = "Error: 'A23456' is not a valid road or list of roads. Please check the full list of supported road identifiers and try again.\r\n"; var consoleOutput = new StringWriter(); Console.SetOut(consoleOutput); //Act await roadStatusService.RetreiveRoadStatus("A23456"); //Assert Assert.That(consoleOutput.ToString(), Is.EqualTo(expectedOutput)); mockApiService.Verify(x => x.GetAsync("https://mockTflApiUrl/A23456", _queryParams), Times.Once); }
public RoadStatusServiceTests() { mockedConfiguration = new Mock <IConfiguration>(); mockHttpMessageHandler = new MockHttpMessageHandler(); httpClient = new HttpClient(mockHttpMessageHandler); sut = new RoadStatusService(mockedConfiguration.Object, httpClient); }
public async Task RetrieveRoadStatusAsync_WhenMultipleRoadsGiven_MultipleRoadsReturnedInConsole() { //Arrange var mockApiService = new Mock <IApiService>(); mockApiService.Setup(x => x.GetAsync(It.IsAny <string>(), It.IsAny <Dictionary <string, string> >())).ReturnsAsync(new HttpResponseMessage { Content = new StringContent(_multipleRoadApiResponse), StatusCode = HttpStatusCode.OK }); var roadStatusService = new RoadStatusService(mockApiService.Object, _applicationConfig); var expectedOutput = "The status of the A2 is as follows:\n\tRoad Status is: Serious\n\tRoad Status Description is: Serious Delays.\r\nThe status of the A20 is as follows:\n\tRoad Status is: Serious\n\tRoad Status Description is: Serious Delays.\r\n"; var consoleOutput = new StringWriter(); Console.SetOut(consoleOutput); //Act await roadStatusService.RetreiveRoadStatus("A2,A20"); //Assert Assert.That(consoleOutput.ToString(), Is.EqualTo(expectedOutput)); mockApiService.Verify(x => x.GetAsync("https://mockTflApiUrl/A2,A20", _queryParams), Times.Once); }
public RoadStatusTestsDefinition() { IConfigurationRoot configuration = Program.GetConfigurationSettings(Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location.Substring(0, System.Reflection.Assembly.GetEntryAssembly().Location.IndexOf("bin\\")))); if (string.IsNullOrEmpty(configuration["apiEndpointBase"]) || string.IsNullOrEmpty(configuration["appId"]) || string.IsNullOrEmpty(configuration["appKey"])) { throw new Exception($"Configuration settings are invalid. Please configure appsettings.<environment>.json in project folder TFLTest_AvaisMohammad appropriately"); } roadStatusService = new RoadStatusService(configuration, new System.Net.Http.HttpClient()); }
public async Task GetRoadStatusFromApiAsync_WhenInvokedMultipleRoadsOrNoRoads_ReturnsMultipleRoadsResponse(string roadString) { //Arrange var mockApiService = new Mock <IApiService>(); mockApiService.Setup(x => x.GetAsync(It.IsAny <string>(), It.IsAny <Dictionary <string, string> >())).ReturnsAsync(new HttpResponseMessage { Content = new StringContent(_multipleRoadApiResponse), StatusCode = HttpStatusCode.OK }); var roadStatusService = new RoadStatusService(mockApiService.Object, _applicationConfig); //Act var result = await roadStatusService.GetRoadStatusFromApiAsync(roadString); //Assert mockApiService.Verify(x => x.GetAsync($"https://mockTflApiUrl/{roadString}", _queryParams), Times.Once); Assert.IsInstanceOf <IEnumerable <RoadStatusDto> >(result); Assert.AreEqual(2, result.Count()); var firstRoad = result.First(); Assert.Multiple(() => { Assert.That(firstRoad.Id, Is.EqualTo("a2")); Assert.That(firstRoad.DisplayName, Is.EqualTo("A2")); Assert.That(firstRoad.Group, Is.EqualTo(null)); Assert.That(firstRoad.StatusSeverity, Is.EqualTo("Serious")); Assert.That(firstRoad.StatusSeverityDescription, Is.EqualTo("Serious Delays")); Assert.That(firstRoad.Bounds, Is.EqualTo("[[-0.0857,51.44091],[0.17118,51.49438]]")); Assert.That(firstRoad.Envelope, Is.EqualTo("[[-0.0857,51.44091],[-0.0857,51.49438],[0.17118,51.49438],[0.17118,51.44091],[-0.0857,51.44091]]")); Assert.That(firstRoad.StatusAggregationStartDate, Is.EqualTo(null)); Assert.That(firstRoad.StatusAggregationEndDate, Is.EqualTo(null)); Assert.That(firstRoad.Url, Is.EqualTo("/Road/a2")); }); var lastRoad = result.Last(); Assert.Multiple(() => { Assert.That(lastRoad.Id, Is.EqualTo("a20")); Assert.That(lastRoad.DisplayName, Is.EqualTo("A20")); Assert.That(lastRoad.Group, Is.EqualTo(null)); Assert.That(lastRoad.StatusSeverity, Is.EqualTo("Serious")); Assert.That(lastRoad.StatusSeverityDescription, Is.EqualTo("Serious Delays")); Assert.That(lastRoad.Bounds, Is.EqualTo("[[-0.11925,51.40825],[0.14918,51.48643]]")); Assert.That(lastRoad.Envelope, Is.EqualTo("[[-0.11925,51.40825],[-0.11925,51.48643],[0.14918,51.48643],[0.14918,51.40825],[-0.11925,51.40825]]")); Assert.That(lastRoad.StatusAggregationStartDate, Is.EqualTo(null)); Assert.That(lastRoad.StatusAggregationEndDate, Is.EqualTo(null)); Assert.That(lastRoad.Url, Is.EqualTo("/Road/a20")); }); }
public void FetchRoadStatusTest() { var roadStatusService = new RoadStatusService(_dataProcess.Object, _helperClass.Object); var mockRoadDetails = new Mock <RoadDetails>(); var mockRoadStatusInfo = new Mock <RoadStatusInfo>(); _helperClass.Setup(h => h.JsonConverter <RoadStatusInfo>(It.IsAny <string>())).Returns(mockRoadStatusInfo.Object); _dataProcess.Setup(d => d.GetRoadStatusCall(mockRoadDetails.Object)).ReturnsAsync("test"); var check = roadStatusService.FetchRoadStatus(mockRoadDetails.Object); _dataProcess.Verify(d => d.GetRoadStatusCall(mockRoadDetails.Object)); _helperClass.Verify(h => h.JsonConverter <RoadStatusInfo>(It.IsAny <string>())); }
public void GetRoadStatusFromApiAsync_WhenInternalServerError_ThrowsException() { //Arrange var mockApiService = new Mock <IApiService>(); mockApiService.Setup(x => x.GetAsync(It.IsAny <string>(), It.IsAny <Dictionary <string, string> >())).ReturnsAsync(new HttpResponseMessage { Content = new StringContent(""), StatusCode = HttpStatusCode.InternalServerError }); var roadStatusService = new RoadStatusService(mockApiService.Object, _applicationConfig); //Act //Assert Exception ex = Assert.ThrowsAsync <Exception>(() => roadStatusService.GetRoadStatusFromApiAsync("A2")); Assert.That(ex.Message, Is.EqualTo("Error: API request failed. Reason: Internal Server Error. ErrorCode: 500.")); mockApiService.Verify(x => x.GetAsync("https://mockTflApiUrl/A2", _queryParams), Times.Once); }
public void GetRoadStatusFromApiAsync_WhenErrorTooManyRequests_ThrowsArgumentException() { //Arrange var mockApiService = new Mock <IApiService>(); mockApiService.Setup(x => x.GetAsync(It.IsAny <string>(), It.IsAny <Dictionary <string, string> >())).ReturnsAsync(new HttpResponseMessage { Content = new StringContent(""), StatusCode = HttpStatusCode.TooManyRequests }); var roadStatusService = new RoadStatusService(mockApiService.Object, _applicationConfig); //Act //Assert ArgumentException ex = Assert.ThrowsAsync <ArgumentException>(() => roadStatusService.GetRoadStatusFromApiAsync("A2")); Assert.That(ex.Message, Is.EqualTo("Error: Too many API requests and/or App Key supplied is invalid and may have expired and/or the API URL is invalid. Please check the status of your App Key and the Road API URL on the TfL API Developer Portal.")); mockApiService.Verify(x => x.GetAsync("https://mockTflApiUrl/A2", _queryParams), Times.Once); }
public void GetRoadStatusFromApiAsync_WhenErrorNotFound_ThrowsArgumentException() { //Arrange var mockApiService = new Mock <IApiService>(); mockApiService.Setup(x => x.GetAsync(It.IsAny <string>(), It.IsAny <Dictionary <string, string> >())).ReturnsAsync(new HttpResponseMessage { Content = new StringContent(""), StatusCode = HttpStatusCode.NotFound }); var roadStatusService = new RoadStatusService(mockApiService.Object, _applicationConfig); //Act //Assert ArgumentException ex = Assert.ThrowsAsync <ArgumentException>(() => roadStatusService.GetRoadStatusFromApiAsync("A2345")); Assert.That(ex.Message, Is.EqualTo("Error: 'A2345' is not a valid road or list of roads. Please check the full list of supported road identifiers and try again.")); mockApiService.Verify(x => x.GetAsync("https://mockTflApiUrl/A2345", _queryParams), Times.Once); }
public void FetchRoadStatus_InvalidRoad_Test() { var roadStatusService = new RoadStatusService(_dataProcess.Object, _helperClass.Object); var mockRoadDetails = new Mock <RoadDetails>(); var fakeRoadStatusInfo = new RoadStatusInfo { httpStatusCode = "404", message = "A233 is not a valid road" }; _helperClass.Setup(h => h.JsonConverter <RoadStatusInfo>(It.IsAny <string>())).Returns(fakeRoadStatusInfo); _dataProcess.Setup(d => d.GetRoadStatusCall(mockRoadDetails.Object)).ReturnsAsync("test"); var check = roadStatusService.FetchRoadStatus(mockRoadDetails.Object); _dataProcess.Verify(d => d.GetRoadStatusCall(mockRoadDetails.Object)); _helperClass.Verify(h => h.JsonConverter <RoadStatusInfo>(It.IsAny <string>())); Assert.AreEqual(check.Result.httpStatusCode, Constants.statusCode); }
public void FetchRoadStatus_ValidRoad_Test() { var roadStatusService = new RoadStatusService(_dataProcess.Object, _helperClass.Object); var mockRoadDetails = new Mock <RoadDetails>(); var fakeRoadStatusInfo = new RoadStatusInfo { displayName = "The status of the A2 is as follows", statusSeverity = "Road Status is Good", statusSeverityDescription = "Road Status Description is No Exceptional Delays" }; _helperClass.Setup(h => h.JsonConverter <RoadStatusInfo>(It.IsAny <string>())).Returns(fakeRoadStatusInfo); _dataProcess.Setup(d => d.GetRoadStatusCall(mockRoadDetails.Object)).ReturnsAsync("test"); var check = roadStatusService.FetchRoadStatus(mockRoadDetails.Object); _dataProcess.Verify(d => d.GetRoadStatusCall(mockRoadDetails.Object)); _helperClass.Verify(h => h.JsonConverter <RoadStatusInfo>(It.IsAny <string>())); Assert.AreEqual(check.Result.httpStatusCode, null); }
public RoadServiceTests() { this._mockRestApiCallService = new Mock <IRestApiCallService>(); this._dummytflApiSettings = new TflApiSettings() { BaseUrl = "https://api.tfl.gov.uk/", Authentication = new Authentication() { UsernameIdentifier = "api_id", Username = "******", PasswordIdentifier = "api_key", Password = "******" }, Resources = new List <Resource>() { new Resource { Name = "road", Value = "road/{roadId}" } } }; this._validResponseForA2 = new RestApiResponse() { Content = JsonConvert.SerializeObject( new RoadStatusLookupSuccessResponse[1] { new RoadStatusLookupSuccessResponse() { Type = "Tfl.Api.Presentation.Entities.RoadCorridor, Tfl.Api.Presentation.Entities", Id = "a2", DisplayName = "A2", Bounds = "[[-0.0857,51.44091],[0.17118,51.49438]]", Envelope = "[[-0.0857,51.44091],[-0.0857,51.49438],[0.17118,51.49438],[0.17118,51.44091],[-0.0857,51.44091]]", StatusSeverity = "Good", StatusSeverityDescription = "No Exceptional Delays", Url = "/Road/a2" } } ), StatusCode = System.Net.HttpStatusCode.OK }; this._notFoundResponseForA233 = new RestApiResponse() { Content = JsonConvert.SerializeObject( new RoadStatusLookupErrorResponse() { Type = "Tfl.Api.Presentation.Entities.RoadCorridor, Tfl.Api.Presentation.Entities", ExceptionType = "EntityNotFoundException", HttpStatusCode = 404, HttpStatus = "NotFound", RelativeUri = "/Road/A233", Message = "The following road id is not recognised: A233", TimestampUtc = DateTime.Parse("2017-11-21T14:37:39.7206118Z") } ), StatusCode = System.Net.HttpStatusCode.NotFound }; var options = Options.Create(_dummytflApiSettings); this._sut = new RoadStatusService(_mockRestApiCallService.Object, options); }