Example #1
0
        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"));
            });
        }
Example #2
0
        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);
        }
Example #3
0
        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);
        }
Example #4
0
        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);
        }