/// <summary> /// See <see cref="INfieldAddressesService.AddAsync"/> /// </summary> public Task<Address> AddAsync(string surveyId, string samplingPointId, Address address) { CheckSurveyIdAndSamplingPointId(surveyId, samplingPointId); return Client.PostAsJsonAsync(AdressesApi(surveyId, samplingPointId, null).AbsoluteUri, address) .ContinueWith(task => task.Result.Content.ReadAsStringAsync().Result) .ContinueWith(task => JsonConvert.DeserializeObjectAsync<Address>(task.Result).Result) .FlattenExceptions(); }
public void TestAddAsync_ServerAcceptsAddress_ReturnsAddress() { var address = new Address { Details = "Language X" }; var mockedNfieldConnection = new Mock<INfieldConnectionClient>(); var mockedHttpClient = CreateHttpClientMock(mockedNfieldConnection); var content = new StringContent(JsonConvert.SerializeObject(address)); mockedHttpClient .Setup(client => client.PostAsJsonAsync(ServiceAddress + "Surveys/" + SurveyId + "/SamplingPoints/" + SamplingPointId + "/Addresses", address)) .Returns(CreateTask(HttpStatusCode.OK, content)); var target = new NfieldAddressesService(); target.InitializeNfieldConnection(mockedNfieldConnection.Object); var actual = target.AddAsync(SurveyId, SamplingPointId, address).Result; Assert.Equal(address.Details, actual.Details); }
public void TestQueryAsync_ServerReturnsQuery_ReturnsListWithAddresses() { var expectedAddresses = new Address[] { new Address{AddressId = "id1", Details = "X"}, new Address{AddressId = "id2", Details = "Y"} }; var mockedNfieldConnection = new Mock<INfieldConnectionClient>(); var mockedHttpClient = CreateHttpClientMock(mockedNfieldConnection); mockedHttpClient .Setup(client => client.GetAsync(ServiceAddress + "Surveys/" + SurveyId + "/SamplingPoints/" + SamplingPointId + "/Addresses")) .Returns(CreateTask(HttpStatusCode.OK, new StringContent(JsonConvert.SerializeObject(expectedAddresses)))); var target = new NfieldAddressesService(); target.InitializeNfieldConnection(mockedNfieldConnection.Object); var actualAddresses = target.QueryAsync(SurveyId, SamplingPointId).Result; Assert.Equal(expectedAddresses[0].AddressId, actualAddresses.ToArray()[0].AddressId); Assert.Equal(expectedAddresses[1].AddressId, actualAddresses.ToArray()[1].AddressId); Assert.Equal(2, actualAddresses.Count()); }