public async Task GetGasStationsAsync_ShoultReturnResponseWithBusinessError_InvalidCoordinates()
        {
            _gasStationRepository.Setup(_ => _.GetGasStationsByLocationAsync(It.IsAny <GetLocationDto>()));
            _geolocationExternalService.Setup(_ => _.GetGeolocationAsync(It.IsAny <string>()));

            var requestMessage = new GetGasStationRequestMessage
            {
                Latitude  = "1000",
                Longitude = "-500"
            };

            var response = await GasStationService.GetGasStationsAsync(requestMessage);

            response.Should().NotBeNull();
            response.HasError.Should().BeTrue();
            response.Messages.Should().HaveCount(2);
            response.Messages.Should().Contain(message => message.Type.Equals(MessageType.BusinessError));
            response.Messages.Should().Contain(message => message.Property.Equals("latitude"));
            _gasStationRepository.Verify(_ => _.GetGasStationsByLocationAsync(It.IsAny <GetLocationDto>()), Times.Never);
            _geolocationExternalService.Verify(_ => _.GetGeolocationAsync(It.IsAny <string>()), Times.Never);
        }
        public async Task GetGasStationsAsync_ShoultReturnResponseWithBusinessError_InvalidAddress()
        {
            _gasStationRepository.Setup(_ => _.GetGasStationsByLocationAsync(It.IsAny <GetLocationDto>()));
            _geolocationExternalService.Setup(_ => _.GetGeolocationAsync(It.IsAny <string>()))
            .ReturnsAsync(Response <LocationDto> .Create().WithBusinessError("Any error"))
            .Verifiable();

            var requestMessage = new GetGasStationRequestMessage
            {
                Address = "Any address"
            };

            var response = await GasStationService.GetGasStationsAsync(requestMessage);

            response.Should().NotBeNull();
            response.HasError.Should().BeTrue();
            response.Messages.Should().HaveCount(1);
            response.Messages.Should().Contain(message => message.Type.Equals(MessageType.BusinessError));
            _gasStationRepository.Verify(_ => _.GetGasStationsByLocationAsync(It.IsAny <GetLocationDto>()), Times.Never);
            _geolocationExternalService.Verify();
        }
        public async Task GetGasStationsAsync_ShouldReturnSuccess()
        {
            _gasStationRepository.Setup(_ => _.GetGasStationsByLocationAsync(It.IsAny <GetLocationDto>()))
            .ReturnsAsync(Response <List <Model.GasStation> > .Create(new List <Model.GasStation> {
                GasStationFake()
            }))
            .Verifiable();
            _geolocationExternalService.Setup(_ => _.GetGeolocationAsync(It.IsAny <string>()))
            .ReturnsAsync(Response <LocationDto> .Create(new LocationDto()))
            .Verifiable();

            var requestMessage = new GetGasStationRequestMessage
            {
                Address = "Any address"
            };

            var response = await GasStationService.GetGasStationsAsync(requestMessage);

            response.Should().NotBeNull();
            response.HasError.Should().BeFalse();
            response.Messages.Should().BeEmpty();
            _gasStationRepository.Verify();
            _geolocationExternalService.Verify();
        }
Пример #4
0
        public async Task <Response <GetGasStationResponseMessage> > GetGasStationsAsync(GetGasStationRequestMessage requestMessage)
        {
            var response = Response <GetGasStationResponseMessage> .Create();

            if (requestMessage is null)
            {
                return(response.WithBusinessError("Search data was not reported or invalid"));
            }

            var isCoordinateSearch = double.TryParse(requestMessage.Latitude, out var latitude) & double.TryParse(requestMessage.Longitude, out var longitude);

            if (string.IsNullOrEmpty(requestMessage.Address) && !isCoordinateSearch)
            {
                return(response.WithBusinessError(nameof(requestMessage.Address), "It is necessary to inform the address to be searched or latitude and longitude coordinates"));
            }

            var geolocation = isCoordinateSearch
                ? GetLocationDto.Create(latitude, longitude)
                : await GetGeocodingAsync(requestMessage.Address);

            if (geolocation.HasError)
            {
                return(response.WithMessages(geolocation.Messages));
            }

            var gasStationsResponse = await GasStationRepository.GetGasStationsByLocationAsync(geolocation.Data.Value);

            if (gasStationsResponse.HasError)
            {
                return(response.WithMessages(gasStationsResponse.Messages));
            }

            return(response.SetValue(gasStationsResponse.Data.Value.ToGetGasStationResponseMessage(geolocation.Data.Value)));
        }