public async Task <GeolocationData> AddAsync(RemoteGeolocationData remoteGeolocationData) { if (await _geolocationDataRepository.GetByIpAsync(remoteGeolocationData.IpAddress) != null) { throw new EntityDuplicateException($"Geolocation data for IP {remoteGeolocationData.IpAddress} already exists."); } var geolocationData = _geolocationDataConverter.Convert(remoteGeolocationData); await _geolocationDataRepository.AddAsync(geolocationData); return(geolocationData); }
public async Task Post_GivenValidAndNonDuplicatedIpAddress_ShouldReturnCreatedAndGeolocationData() { //Arrange string ipAddress = "80.80.80.81"; var remoteGeolocationData = new RemoteGeolocationData { IpAddress = ipAddress, City = "Warszawa", ContinentCode = "EU", CountryCode = "PL", Latitude = 52.234, Longitude = 21.12, CountryName = "Poland", IpAddressType = "ipv4" }; _geolocationDataServiceMock.Setup(x => x.GetByIpAddressAsync(It.IsAny <string>())).ReturnsAsync(remoteGeolocationData); var client = _factory.WithWebHostBuilder(builder => { builder.ConfigureTestServices(services => { services.AddSingleton(_geolocationDataServiceMock.Object); }); }).CreateClient(); //Act var stringContent = new StringContent(JsonConvert.SerializeObject(new IpAddress { Value = ipAddress }), Encoding.UTF8, "application/json"); var result = await client.PostAsync($"/api/geolocation", stringContent); //Assert var geolocationData = await result.Content.ReadAsAsync <GeolocationData>(); result.StatusCode.Should().Be(HttpStatusCode.Created); result.Content.Headers.ContentType.MediaType.Should().Be("application/json"); geolocationData.Should().NotBeNull(); geolocationData.Should().BeEquivalentTo(remoteGeolocationData); }
public GeolocationData Convert(RemoteGeolocationData remoteGeolocationData) { return(_mapper.Map <GeolocationData>(remoteGeolocationData)); }