public void ProfileOptions_Constructor2_Equal()
        {
            var content = new Content();

            var contentItems = new List<ContentItem>
            {
                new ContentItem
                {
                    Content = "This is a sample tweet",
                    Charset = "utf-8",
                    ContentType = "text/plain",
                    Created = 1454450300,
                    Forward = false,
                    Id = "245160944223793152",
                    ContentLanguage = ContentLanguage.En,
                    Reply = false,
                    SourceId = "twitter",
                    Updated = 1454450300,
                    UserId = "bob"
                }
            };

            content.ContentItems = contentItems;

            var profileOptions = new ProfileOptions(content);

            Assert.Equal("application/json", profileOptions.ContentType);
            Assert.Equal(1, content.ContentItems.Count());
            Assert.Equal(ContentLanguage.En, profileOptions.ContentLanguage);
            Assert.Equal(AcceptLanguage.En, profileOptions.AcceptLanguage);
            Assert.Equal(false, profileOptions.IncludeRaw);
            Assert.Null(profileOptions.Text);
        }
        public void BuildRequestMessage_AcceptLanguageHeader_Equal(AcceptLanguage acceptLanguage)
        {
            var options = new ProfileOptions("Hello World") {AcceptLanguage = acceptLanguage};
            var requestBuilder = new ProfileRequestBuilder();
            var message = requestBuilder.BuildRequestMessage(ServiceUrl, options, false);

            Assert.Equal($"{acceptLanguage}".ToLower(), message.Headers.AcceptLanguage.First().Value);
        }
        public async Task BuildRequestMessage_ProfileWithText_Equal(string content)
        {
            var options = new ProfileOptions(content);
            var requestBuilder = new ProfileRequestBuilder();
            var message = requestBuilder.BuildRequestMessage(ServiceUrl, options, false);
            var actual = await message.Content.ReadAsStringAsync().ConfigureAwait(false);

            Assert.Equal(content, actual);
        }
        public void BuildRequestMessage_WithNullArguments_ThrowsArgumentNullException(string serviceUrl,
            ProfileOptions options)
        {
            var requestBuilder = new ProfileRequestBuilder();

            var exception =
                Record.Exception(() => requestBuilder.BuildRequestMessage(serviceUrl, options, false));
            Assert.NotNull(exception);
            Assert.IsType<ArgumentNullException>(exception);
        }
        public async Task GetProfileAsync_WithOptions_IsNotNull()
        {
            var content = File.ReadAllText("SampleContent1.txt");

            var service = new PersonalityInsightsService(Settings.Username, Settings.Password);
            var options = new ProfileOptions(content) {IncludeRaw = true};
            var profile = await service.GetProfileAsync(options).ConfigureAwait(false);

            Assert.NotNull(profile);
        }
        public void BuildRequestMessage_WithEmptyContentItem_ThrowsArgumentOutOfRangeException()
        {
            IContent content = new Content();
            var options = new ProfileOptions(content);
            var requestBuilder = new ProfileRequestBuilder();

            var exception =
                Record.Exception(() => requestBuilder.BuildRequestMessage(ServiceUrl, options, false));
            Assert.NotNull(exception);
            Assert.IsType<ArgumentOutOfRangeException>(exception);
        }
        public void ProfileOptions_Constructor1_Equal()
        {
            var profileOptions = new ProfileOptions("Hello World");

            Assert.Equal("text/plain", profileOptions.ContentType);
            Assert.Equal("Hello World", profileOptions.Text);
            Assert.Equal(ContentLanguage.En, profileOptions.ContentLanguage);
            Assert.Equal(AcceptLanguage.En, profileOptions.AcceptLanguage);
            Assert.Equal(false, profileOptions.IncludeRaw);
            Assert.Null(profileOptions.Content);
        }
        /// <summary>
        ///     Gets a PersonalityProfile in Csv format based on the string content to analyze.
        /// </summary>
        /// <param name="content">The content to analyze.</param>
        /// <exception cref="ArgumentNullException"></exception>
        /// <exception cref="WatsonException"></exception>
        /// <returns></returns>
        public async Task<string> GetProfileAsCsvAsync(string content)
        {
            //If no content is provided, we can't proceed
            if (string.IsNullOrWhiteSpace(content))
                throw new ArgumentNullException(nameof(content));

            var options = new ProfileOptions(content);

            using (var request = RequestBuilder.BuildRequestMessage("v2/profile", options, true))
            {
                var profile = await SendRequestAsync(request).ConfigureAwait(false);
                return profile;
            }
        }
        public async Task GetProfileAsync_ValidOptions_Equal()
        {
            var mockHttpMessageHandler = new MockHttpMessageHandler();
            var mockResponse = new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content = new StringContent(MockPersonalityInsightsResponses.MockResponse)
            };

            mockHttpMessageHandler.AddResponseMessage(ServiceUrl, mockResponse);

            var httpCLient = new HttpClient(mockHttpMessageHandler);
            var options = new ProfileOptions("bob");

            var service = new PersonalityInsightsService("username", "password", httpCLient, new WatsonSettings());
            var profile = await service.GetProfileAsync(options).ConfigureAwait(false);

            Assert.NotNull(profile);
        }
        public async Task GetProfileAsync_WithContentItems_IsNotNull()
        {
            var content1 = File.ReadAllText("SampleContent1.txt");
            var content2 = File.ReadAllText("SampleContent2.txt");
            var service = new PersonalityInsightsService(Settings.Username, Settings.Password);

            var contentItems = new List<ContentItem>
            {
                new ContentItem(content1),
                new ContentItem(content2)
            };

            var content = new Content(contentItems);
            var options = new ProfileOptions(content);
            var profile = await service.GetProfileAsync(options).ConfigureAwait(false);

            Assert.NotNull(profile);
        }
        public async Task GetProfileAsCsvAsync_IsNotNull()
        {
            var content = File.ReadAllText("SampleContent1.txt");

            var service = new PersonalityInsightsService(Settings.Username, Settings.Password);
            var options = new ProfileOptions(content)
            {
                IncludeRaw = true,
                IncludeCsvHeaders = true,
                AcceptLanguage = AcceptLanguage.Es
            };
            var profile = await service.GetProfileAsCsvAsync(options).ConfigureAwait(false);

            Assert.False(string.IsNullOrWhiteSpace(profile));
        }
        public void BuildRequestMessage_ContentLanguageHeader_Equal(ContentLanguage contentLanguage)
        {
            var options = new ProfileOptions("Hello World") {ContentLanguage = contentLanguage};
            var requestBuilder = new ProfileRequestBuilder();
            var message = requestBuilder.BuildRequestMessage(ServiceUrl, options, false);

            Assert.Equal($"{contentLanguage}".ToLower(), message.Content.Headers.ContentLanguage.FirstOrDefault());
        }
 public void BuildRequestMessage_Uri_Equal(string serviceUrl, ProfileOptions options)
 {
     var requestBuilder = new ProfileRequestBuilder();
     var message = requestBuilder.BuildRequestMessage(ServiceUrl, options, false);
     Assert.Equal(serviceUrl, message.RequestUri.ToString());
 }
        public async Task BuildRequestMessage_ProfileWithContentItems_Equal()
        {
            var content = new Content();

            var contentItems = new List<ContentItem>
            {
                new ContentItem
                {
                    Content = "This is a sample tweet",
                    Charset = "utf-8",
                    ContentType = "text/plain",
                    Created = 1420070400,
                    Forward = false,
                    Id = "245160944223793152",
                    ContentLanguage = ContentLanguage.En,
                    Reply = false,
                    SourceId = "twitter",
                    Updated = 1420070400,
                    UserId = "bob"
                }
            };

            content.ContentItems = contentItems;

            var expected =
                "{\"ContentItems\":[{\"Charset\":\"utf-8\",\"Content\":\"This is a sample tweet\",\"ContentType\":\"text/plain\",\"Created\":1420070400,\"Forward\":false,\"Id\":\"245160944223793152\",\"ContentLanguage\":\"en\",\"ParentId\":null,\"Reply\":false,\"SourceId\":\"twitter\",\"Updated\":1420070400,\"UserId\":\"bob\"}]}";

            var options = new ProfileOptions(content);
            var requestBuilder = new ProfileRequestBuilder();
            var message = requestBuilder.BuildRequestMessage(ServiceUrl, options, false);
            var actual = await message.Content.ReadAsStringAsync().ConfigureAwait(false);

            Assert.Equal(expected, actual);
        }
        public async Task GetProfileAsCsvAsync_ValidOptionsAndIncludeRawAndHeaders_Equal()
        {
            var mockHttpMessageHandler = new MockHttpMessageHandler();
            var mockResponse = new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content = new StringContent(MockPersonalityInsightsResponses.MockStringResponseWithHeaders)
            };

            mockHttpMessageHandler.AddResponseMessage($"{ServiceUrl}?include_raw=true&headers=true",
                mockResponse);

            var httpCLient = new HttpClient(mockHttpMessageHandler);
            var options = new ProfileOptions("bob") {IncludeRaw = true, IncludeCsvHeaders = true};

            var service = new PersonalityInsightsService("username", "password", httpCLient, new WatsonSettings());
            var profile = await service.GetProfileAsCsvAsync(options).ConfigureAwait(false);

            Assert.NotNull(profile);
            Assert.Equal(MockPersonalityInsightsResponses.MockStringResponseWithHeaders, profile);
        }
        public async Task GetProfileAsJsonAsync_ValidOptionsWithFormatting_Equal()
        {
            var mockHttpMessageHandler = new MockHttpMessageHandler();
            var mockResponse = new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content = new StringContent(MockPersonalityInsightsResponses.MockResponse)
            };

            mockHttpMessageHandler.AddResponseMessage(ServiceUrl, mockResponse);

            var httpCLient = new HttpClient(mockHttpMessageHandler);
            var options = new ProfileOptions("bob");

            var service = new PersonalityInsightsService("username", "password", httpCLient, new WatsonSettings());
            var profile = await service.GetProfileAsJsonAsync(options, Formatting.Indented).ConfigureAwait(false);

            var mockResponseObj = JsonConvert.DeserializeObject(profile);
            var mockResponseJson = JsonConvert.SerializeObject(mockResponseObj, Formatting.Indented);

            Assert.Equal(mockResponseJson, profile);
        }
        public void BuildRequestMessage_AcceptHeaders_Equal(string expected, bool asCsv)
        {
            var options = new ProfileOptions("Hello World");
            var requestBuilder = new ProfileRequestBuilder();
            var message = requestBuilder.BuildRequestMessage(ServiceUrl, options, asCsv);

            Assert.Equal(expected, message.Headers.Accept.First().MediaType);
        }
        /// <summary>
        ///     Gets a PersonalityProfile in its raw json format based on the string content to analyze.
        /// </summary>
        /// <param name="content">The content to analyze.</param>
        /// <param name="formatting">Indicates how the output is formatted.</param>
        /// <exception cref="ArgumentNullException"></exception>
        /// <exception cref="WatsonException"></exception>
        /// <returns></returns>
        public async Task<string> GetProfileAsJsonAsync(string content, Formatting formatting = Formatting.None)
        {
            //If no content is provided, we can't proceed
            if (string.IsNullOrWhiteSpace(content))
                throw new ArgumentNullException(nameof(content));

            var options = new ProfileOptions(content);

            using (var request = RequestBuilder.BuildRequestMessage("v2/profile", options, false))
            {
                var rawProfile = await SendRequestAsync(request).ConfigureAwait(false);

                //As the raw string content isn't properly formatted as Json
                //Deserialize it and serialize it with formatting settings.
                var profile = JsonConvert.DeserializeObject(rawProfile);
                return JsonConvert.SerializeObject(profile, formatting);
            }
        }