/// <summary>
        /// See <see cref="INfieldTranslationsService.AddAsync"/>
        /// </summary>
        public Task<Translation> AddAsync(string surveyId, int languageId, Translation translation)
        {
            CheckSurveyId(surveyId);

            if (translation == null)
            {
                throw new ArgumentNullException("translation");
            }

            return Client.PostAsJsonAsync(TranslationsApi(surveyId, languageId, null).AbsoluteUri, translation)
                         .ContinueWith(task => task.Result.Content.ReadAsStringAsync().Result)
                         .ContinueWith(task => JsonConvert.DeserializeObjectAsync<Translation>(task.Result).Result)
                         .FlattenExceptions();
        }
        public void TestAddAsync_ServerAcceptsTranslation_ReturnsTranslation()
        {
            var translation = new Translation { Name = "X", Text = "X Translated" };
            var mockedNfieldConnection = new Mock<INfieldConnectionClient>();
            var mockedHttpClient = CreateHttpClientMock(mockedNfieldConnection);
            var content = new StringContent(JsonConvert.SerializeObject(translation));
            mockedHttpClient
                .Setup(client => client.PostAsJsonAsync(ServiceAddress + "Surveys/" + SurveyId + "/Languages/10/Translations", translation))
                .Returns(CreateTask(HttpStatusCode.OK, content));

            var target = new NfieldTranslationsService();
            target.InitializeNfieldConnection(mockedNfieldConnection.Object);

            var actual = target.AddAsync(SurveyId, LanguageId, translation).Result;

            Assert.Equal(translation.Name, actual.Name);
        }
        public void TestDefaultTextsAsync_ServerReturnsQuery_ReturnsListWithTranslations()
        {
            var expectedTranslations = new Translation[]
            { new Translation{ Name = "X", Text = "Translation X" },
              new Translation{ Name = "Y", Text = "Translation Y" }
            };
            var mockedNfieldConnection = new Mock<INfieldConnectionClient>();
            var mockedHttpClient = CreateHttpClientMock(mockedNfieldConnection);
            mockedHttpClient
                .Setup(client => client.GetAsync(ServiceAddress + "DefaultTexts"))
                .Returns(CreateTask(HttpStatusCode.OK, new StringContent(JsonConvert.SerializeObject(expectedTranslations))));

            var target = new NfieldTranslationsService();
            target.InitializeNfieldConnection(mockedNfieldConnection.Object);

            var actualTranslations = target.DefaultTextsAsync.Result;

            Assert.Equal(expectedTranslations[0].Name, actualTranslations.ToArray()[0].Name);
            Assert.Equal(expectedTranslations[1].Name, actualTranslations.ToArray()[1].Name);
            Assert.Equal(2, actualTranslations.Count());
        }
        public void TestUpdateAsync_TranslationExists_DoesNotThrow()
        {
            var translation = new Translation
            {
                Name = "X",
                Text = "Translation X"
            };
            var mockedNfieldConnection = new Mock<INfieldConnectionClient>();
            var mockedHttpClient = CreateHttpClientMock(mockedNfieldConnection);
            mockedHttpClient
                .Setup(client => client.PutAsJsonAsync(
                    ServiceAddress + "Surveys/" + SurveyId + "/Languages/10/Translations", It.IsAny<Translation>()))
                .Returns(CreateTask(HttpStatusCode.OK));

            var target = new NfieldTranslationsService();
            target.InitializeNfieldConnection(mockedNfieldConnection.Object);

            Assert.DoesNotThrow(() => target.UpdateAsync(SurveyId, LanguageId, translation).Wait());
        }
        /// <summary>
        /// See <see cref="INfieldTranslationsService.UpdateAsync"/>
        /// </summary>
        public Task UpdateAsync(string surveyId, int languageId, Translation translation)
        {
            CheckSurveyId(surveyId);

            if (translation == null)
            {
                throw new ArgumentNullException("translation");
            }

            return Client.PutAsJsonAsync(TranslationsApi(surveyId, languageId, null).AbsoluteUri,
                translation).FlattenExceptions();
        }
        /// <summary>
        /// See <see cref="INfieldTranslationsService.RemoveAsync"/>
        /// </summary>
        public Task RemoveAsync(string surveyId, int languageId, Translation translation)
        {
            CheckSurveyId(surveyId);

            if (translation == null)
            {
                throw new ArgumentNullException("translation");
            }

            return
                Client.DeleteAsync(TranslationsApi(surveyId, languageId, translation.Name).AbsoluteUri)
                      .FlattenExceptions();
        }
 /// <summary>
 /// A synchronous version of <see cref="INfieldTranslationsService.UpdateAsync"/>
 /// </summary>
 public static void Update(this INfieldTranslationsService languagesService, string surveyId, int languageId, Translation translation)
 {
     languagesService.UpdateAsync(surveyId, languageId, translation).Wait();
 }
 /// <summary>
 /// A synchronous version of <see cref="INfieldLanguagesService.AddAsync"/>
 /// </summary>
 public static Translation Add(this INfieldTranslationsService translationsService, string surveyId, int languageId, Translation translation)
 {
     return translationsService.AddAsync(surveyId, languageId, translation).Result;
 }