/// <summary> /// Map an API response to PointLatLng /// </summary> /// <param name="apiResponse">The API response</param> /// <returns></returns> public PointLatLng Map(IssApiResponse apiResponse) { if (apiResponse is null) { throw new ArgumentException($"{nameof(apiResponse)} cannot be null"); } if (apiResponse.iss_position is null || string.IsNullOrEmpty(apiResponse.iss_position.latitude) || string.IsNullOrEmpty(apiResponse.iss_position.longitude)) { throw new ArgumentException($"{nameof(apiResponse)} is not well formed"); } double latitude; double longitude; latitude = Convert.ToDouble(apiResponse.iss_position.latitude, CultureInfo.InvariantCulture); longitude = Convert.ToDouble(apiResponse.iss_position.longitude, CultureInfo.InvariantCulture); if (latitude > 90 || latitude < -90 || longitude > 180 || longitude < -180) { throw new ArgumentException($"Coordinates lat:{latitude} lng:{longitude} are out of bounds"); } return(new PointLatLng(latitude, longitude)); }
/// <summary> /// Retrieve the ISS current position from public web API. see http://api.open-notify.org/ /// </summary> /// <returns>Current position of the ISS</returns> public async Task <IssApiResponse> GetISSCurrentPosition() { try { var response = await client.GetAsync(apiURL); if (!response.IsSuccessStatusCode) { throw new Exception($"ISS API respond with a status error code of {response.StatusCode}"); } string stream = await response.Content.ReadAsStringAsync(); if (string.IsNullOrEmpty(stream)) { throw new Exception("Content response is Empty"); } IssApiResponse apiResult = JsonSerializer.Deserialize <IssApiResponse>(stream); return(apiResult); } catch (Exception ex) { throw new Exception("Error calling ISS API", ex); } }
public void TestNullIssApiResponse() { //Arrange IssApiResponse apiResult = null; APIMapperService apiMapperservice = new APIMapperService(); //Act Func <PointLatLng> action = () => apiMapperservice.Map(apiResult); //Assert action.Should().Throw <ArgumentException>().WithMessage("*cannot be null"); }
public void CoordinatesShouldBeValid() { //Arrange IAPIMapperService mapperService = new APIMapperService(); IssApiResponse response = new IssApiResponse(); response.iss_position = new IssPosition(); response.iss_position.latitude = "95.0"; response.iss_position.longitude = "160.0"; //Act Action mapped = () => mapperService.Map(response); //Assert mapped.Should().Throw <ArgumentException>(); }
public void TestOutOfBoundsCoordinates(string latitude, string longitude) { //Arrange IssApiResponse apiResult = new IssApiResponse() { iss_position = new IssPosition() { latitude = latitude, longitude = longitude } }; APIMapperService apiMapperservice = new APIMapperService(); //Act Func <PointLatLng> action1 = () => apiMapperservice.Map(apiResult); //Assert action1.Should().Throw <ArgumentException>().WithMessage("Coordinates*are out of bounds"); }
public void TestCoordinatesNotConvertible(string latitude, string longitude) { //Arrange IssApiResponse apiResult = new IssApiResponse() { iss_position = new IssPosition() { latitude = latitude, longitude = longitude } }; APIMapperService apiMapperservice = new APIMapperService(); //Act Func <PointLatLng> action1 = () => apiMapperservice.Map(apiResult); //Assert action1.Should().Throw <FormatException>(); }
public void TestValidCoordinates(string latitude, string longitude, double expectedLatitude, double expectedLongitude) { //Arrange IssApiResponse apiResult = new IssApiResponse() { iss_position = new IssPosition() { latitude = latitude, longitude = longitude } }; APIMapperService apiMapperservice = new APIMapperService(); //Act PointLatLng result = apiMapperservice.Map(apiResult); //Assert result.Lat.Should().Be(expectedLatitude); result.Lng.Should().Be(expectedLongitude); }
public void TestConversionIsFine() { //Arrange IAPIMapperService mapperService = new APIMapperService(); IssApiResponse response = new IssApiResponse(); response.iss_position = new IssPosition(); response.iss_position.latitude = "80.0"; response.iss_position.longitude = "160.0"; //Act PointLatLng mapped = mapperService.Map(response); //Assert mapped.Should().NotBeNull(); mapped.Lat.Should().Be(80); mapped.Lng.Should().Be(160); }
public void TestNotWellFormedIssApiResponse(bool nullIssPosition, string latitude, string longitude) { //Arrange IssApiResponse apiResult = new IssApiResponse(); if (!nullIssPosition) { apiResult.iss_position = new IssPosition() { latitude = latitude, longitude = longitude }; } APIMapperService apiMapperservice = new APIMapperService(); //Act Func <PointLatLng> action1 = () => apiMapperservice.Map(apiResult); //Assert action1.Should().Throw <ArgumentException>().WithMessage("*is not well formed"); }