public void Initialize()
        {
            this.serviceOptions = new ModeratorServiceOptions()
            {
                HostUrl = ConfigurationManager.AppSettings["HostUrl"],

                ImageServicePathV2 = ConfigurationManager.AppSettings["ImageServicePathV2"],
                ImageServiceCustomListPathV2 = ConfigurationManager.AppSettings["ImageServiceCustomListPathV2"],
                ImageCachingPath = ConfigurationManager.AppSettings["ImageCachingPath"],

                ImageServiceKey = ConfigurationManager.AppSettings["ImageServiceKey"],
                ImageServiceCustomListKey = ConfigurationManager.AppSettings["ImageServiceCustomListKey"]
            };

            IModeratorServiceV2 moderatorService = new ModeratorServiceV2(this.serviceOptions);
        }
        public void V2AddImageV2Test()
        {
            IModeratorServiceV2 moderatorService = new ModeratorServiceV2(this.serviceOptions);

            // Add Image (with labels)
            // See label details in the response documentation: https://developer.microsoftmoderator.com/docs/services/54f7932727037412a0cda396/operations/54f793272703740c70627a24
            using (Stream stream = new FileStream(TestImageContent, FileMode.Open, FileAccess.Read))
            {
                ImageModeratableContent imageContent =
                    new ImageModeratableContent(new BinaryContent(stream, "image/jpeg"));
                var addResponse = moderatorService.ImageAddAsync(imageContent, TestTags, TestLabel);
                var addResult = addResponse.Result;
                Assert.IsTrue(addResult != null, "Expected valid result, Response: {0}", JsonConvert.SerializeObject(addResult));
                Assert.IsTrue(string.IsNullOrWhiteSpace(addResult.ImageId)
                    || string.Compare(addResult.Status.Description, "Error occurred while processing request :: Failure Adding a valid image  :: Image already exists") == 0,
                    "Image Id can be null only if the Image already exists, Response: {0}", JsonConvert.SerializeObject(addResult));

                // Refresh index
                var refreshResponse = moderatorService.RefreshImageIndexAsync();
                var refreshResult = refreshResponse.Result;
                Assert.IsTrue(refreshResult.IsUpdateSuccess, "Expected update Success on refresh");
            }

            //Wait for Index to be Ready
            while (true)
            {
                var res = moderatorService.CheckImageIndexStatusAsync();
                var response = res.Result;
                if (response.IsSuccessStatusCode && response.ReasonPhrase == "The index is ready for matching.")
                {
                    break;

                }
                else { Thread.Sleep(5000); }
            }

            using (Stream stream = new FileStream(TestImageContent, FileMode.Open, FileAccess.Read))
            {
                ImageModeratableContent imageContent =
                    new ImageModeratableContent(new BinaryContent(stream, "image/jpeg"));

                // Match
                var matchResponse = moderatorService.MatchImageAsync(imageContent, true);
                var matchResult = matchResponse.Result;
                Assert.IsTrue(matchResult.IsMatch, "Expected match, Response: {0}", JsonConvert.SerializeObject(matchResult));
            }
        }
        public void V2AddTermTest()
        {
            IModeratorServiceV2 moderatorService = new ModeratorServiceV2(this.serviceOptions);

            // We are creating a term "FakeProfanity" in english (thus provide tha same english translation), then matching against it.
            TextModeratableContent textContent = new TextModeratableContent(text: "ertuythfg", englishTranslation: "FakeProfanity");
            var taskResult = moderatorService.AddTermAsync(textContent, "eng");

            var actualResult = taskResult.Result;
            Assert.IsTrue((actualResult.StatusCode != System.Net.HttpStatusCode.Created) || (actualResult.StatusCode != System.Net.HttpStatusCode.MultipleChoices), "Expected valid result for AddTerm");

            var refreshTask = moderatorService.RefreshTextIndexAsync("eng");
            var refreshResult = refreshTask.Result;
            Assert.IsTrue(refreshResult != null, "Expected valid result for RefreshIndex");

            var screenResponse = moderatorService.ScreenTextAsync(new TextModeratableContent("This is a ertuythfg!"), "eng");
            var screenResult = screenResponse.Result;

            var deleteTask = moderatorService.RemoveTermAsync(textContent, "eng");
            var deleteResult = deleteTask.Result;
            Assert.IsTrue(deleteResult.IsSuccessStatusCode, "Expected valid result for DeleteTerm");
        }
        public void V2IdentifyLanguageTest()
        {
            IModeratorServiceV2 moderatorService = new ModeratorServiceV2(this.serviceOptions);

            TextModeratableContent textContent = new TextModeratableContent("Hola este es un texto en otro idioma");
            var identifyLanguageResponse = moderatorService.IdentifyLanguageAsync(textContent);
            var actualResult = identifyLanguageResponse.Result;
            Assert.IsTrue(actualResult != null, "Expected valid result");
            Assert.AreEqual("spa", actualResult.DetectedLanguage, "Expected valid result");
        }
 public void V2UnCacheImage()
 {
     IModeratorServiceV2 moderatorService = new ModeratorServiceV2(this.serviceOptions);
     var actualResult = CacheImageContent(moderatorService);
     var moderateResult = moderatorService.UnCacheImageContent(actualResult.CacheID);
     actualResult = moderateResult.Result;
     Assert.IsTrue(actualResult == null, "Expected valid result, Response: {0}", JsonConvert.SerializeObject(actualResult));
     //Assert.IsTrue(actualResult != null, "Expected valid result");
     //Assert.AreEqual(actualResult.Status.Code, "204", "Expected status code");
 }
        public void V2ExtractTextV2AndCacheTest()
        {
            IModeratorServiceV2 moderatorService = new ModeratorServiceV2(this.serviceOptions);
            Service.Results.ExtractTextResult extractResult = ExtractTextContent(moderatorService, true);

            Assert.IsTrue(extractResult != null, "Expected valid result");
            Assert.IsTrue(extractResult.Candidates != null, "Expected valid result", "Response: {0}", JsonConvert.SerializeObject(extractResult));
            //Assert.AreEqual("THIS IS A \r\nSIMPLE TEST \r\n", extractResult.Text, "Text message was unexpected");
            Assert.AreEqual("Windows10 \r\nSatya Nadella \r\n", extractResult.Text, "Text message was unexpected, Response: {0}", JsonConvert.SerializeObject(extractResult));
        }
        public void V2CheckHashIndexStatus()
        {
            IModeratorServiceV2 moderatorService = new ModeratorServiceV2(this.serviceOptions);
            var actualResult = moderatorService.CheckImageIndexStatusAsync();
            var res = actualResult.Result;

            Assert.IsTrue(res != null, "Expected valid result, Response: {0}", JsonConvert.SerializeObject(actualResult));
            Assert.IsTrue(res.StatusCode == System.Net.HttpStatusCode.OK, "Invalid Http Status Code, Response: {0}", JsonConvert.SerializeObject(actualResult));
            Assert.AreEqual("The index is ready for matching.", res.ReasonPhrase, "Response: {0}", JsonConvert.SerializeObject(actualResult));
        }
        public void V2EvaluateImageUrlV2Test()
        {
            IModeratorServiceV2 moderatorService = new ModeratorServiceV2(this.serviceOptions);

            ImageModeratableContent imageContent = new ImageModeratableContent(TestImageUrl);
            var moderateResult = moderatorService.EvaluateImageAsync(imageContent);
            var actualResult = moderateResult.Result;
            Assert.IsTrue(actualResult != null, "Expected valid result, Response: {0}", JsonConvert.SerializeObject(actualResult));
            Assert.IsTrue(actualResult.AdvancedInfo != null, "Expected valid result, Response: {0}", JsonConvert.SerializeObject(actualResult));

            Assert.AreNotEqual(actualResult.AdultClassificationScore, "0.000", "Adult Score, Response: {0}", JsonConvert.SerializeObject(actualResult));
            Assert.AreNotEqual(actualResult.RacyClassificationScore, "0.000", "Racy Score, Response: {0}", JsonConvert.SerializeObject(actualResult));
        }
        public void V2EvaluateImageInCache()
        {
            IModeratorServiceV2 moderatorService = new ModeratorServiceV2(this.serviceOptions);
            var actualResult = EvaluateImageContent(moderatorService, true);

            var moderateResult = moderatorService.EvaluateImageInCache(actualResult.CacheID);
            actualResult = moderateResult.Result;
            Assert.IsTrue(actualResult != null, "Expected valid result, Response: {0}", JsonConvert.SerializeObject(actualResult));

            Assert.AreNotEqual(actualResult.AdultClassificationScore, "0.000", "Adult Score, Response: {0}", JsonConvert.SerializeObject(actualResult));
            Assert.AreNotEqual(actualResult.RacyClassificationScore, "0.000", "Racy Score, Response: {0}", JsonConvert.SerializeObject(actualResult));
        }
        public void V2EvaluateImageUrlTest()
        {
            IModeratorServiceV2 moderatorService = new ModeratorServiceV2(this.serviceOptions);

            ImageModeratableContent imageContent = new ImageModeratableContent(TestImageUrl);
            var moderateResult = moderatorService.EvaluateImageAsync(imageContent);
            var actualResult = moderateResult.Result;
            Assert.IsTrue(actualResult != null, "Expected valid result, Response: {0}", JsonConvert.SerializeObject(actualResult));
            Assert.IsTrue(actualResult.AdultClassificationScore > 0, "Expected higher than 0 Adult Classification Score value for test image, Response: {0}", JsonConvert.SerializeObject(actualResult));
            Assert.IsTrue(actualResult.RacyClassificationScore > 0, "Expected higher than 0 Racy Classification Score value for test image, Response: {0}", JsonConvert.SerializeObject(actualResult));
            Assert.IsTrue(!actualResult.IsImageAdultClassified, "Image should not have been classified as Adult, Response: {0}", JsonConvert.SerializeObject(actualResult));
            Assert.IsTrue(!actualResult.IsImageRacyClassified, "Image should not have been classified as Racy, Response: {0}", JsonConvert.SerializeObject(actualResult));
        }
        public void V2EvaluateImageContentTest()
        {
            using (Stream stream = new FileStream(TestImageContent, FileMode.Open, FileAccess.Read))
            {
                IModeratorServiceV2 moderatorService = new ModeratorServiceV2(this.serviceOptions);

                ImageModeratableContent imageContent = new ImageModeratableContent(new BinaryContent(stream, "image/jpeg"));
                var moderateResult = moderatorService.EvaluateImageAsync(imageContent);
                var actualResult = moderateResult.Result;
                Assert.IsTrue(actualResult != null, "Expected valid result, Response: {0}", JsonConvert.SerializeObject(actualResult));
                Assert.IsTrue(actualResult.AdultClassificationScore > 0, "Expected higher than 0 Adult Classification Score value for test image, Response: {0}", JsonConvert.SerializeObject(actualResult));
                Assert.IsTrue(actualResult.RacyClassificationScore > 0, "Expected higher than 0 Racy Classification Score value for test image, Response: {0}", JsonConvert.SerializeObject(actualResult));
                Assert.IsTrue(!actualResult.IsImageAdultClassified, "Image should not have been classified as Adult, Response: {0}", JsonConvert.SerializeObject(actualResult));
                Assert.IsTrue(!actualResult.IsImageRacyClassified, "Image should not have been classified as Racy, Response: {0}", JsonConvert.SerializeObject(actualResult));
            }
        }
        public void V2DetectFaceV2AndCacheTest()
        {
            IModeratorServiceV2 moderatorService = new ModeratorServiceV2(this.serviceOptions);
            Service.Results.DetectFaceResult extractResult = DetectFaceContent(moderatorService, true);

            Assert.IsTrue(extractResult != null, "Expected valid result, Response: {0}", JsonConvert.SerializeObject(extractResult));
            Assert.IsTrue(extractResult.Result, "Result is NULL, Response: {0}", JsonConvert.SerializeObject(extractResult));
            Assert.IsNotNull(extractResult.Faces, "Faces is NULL, Response: {0}", JsonConvert.SerializeObject(extractResult));
        }
        public void V2DetectFaceInCache()
        {
            IModeratorServiceV2 moderatorService = new ModeratorServiceV2(this.serviceOptions);
            var actualResult = DetectFaceContent(moderatorService, true);

            var moderateResult = moderatorService.DetectFaceInCache(actualResult.CacheID);
            actualResult = moderateResult.Result;
            Assert.IsTrue(actualResult != null, "Expected valid result, Response: {0}", JsonConvert.SerializeObject(actualResult));
            Assert.IsTrue(actualResult.Result, "Result is NULL, Response: {0}", JsonConvert.SerializeObject(actualResult));
            Assert.IsNotNull(actualResult.Faces, "Faces is NULL, Response: {0}", JsonConvert.SerializeObject(actualResult));
        }
 public void V2RemoveAllTermsInALanguageTest()
 {
     IModeratorServiceV2 moderatorService = new ModeratorServiceV2(this.serviceOptions);
     var deleteTask = moderatorService.RemoveAllTermsAsync("eng");
     var deleteResult = deleteTask.Result;
     Assert.IsTrue(deleteResult.IsSuccessStatusCode, "Expected valid result for DeleteTerm");
 }
        public void V2ExtractTextInCache()
        {
            IModeratorServiceV2 moderatorService = new ModeratorServiceV2(this.serviceOptions);
            var actualResult = ExtractTextContent(moderatorService, true);

            var moderateResult = moderatorService.ExtractTextInCache(actualResult.CacheID);
            actualResult = moderateResult.Result;
            Assert.IsTrue(actualResult != null, "Expected valid result, Response: {0}", JsonConvert.SerializeObject(actualResult));

            Assert.IsTrue(actualResult.Candidates != null, "Expected valid result, Response: {0}", JsonConvert.SerializeObject(actualResult));
            Assert.AreEqual("Windows10 \r\nSatya Nadella \r\n", actualResult.Text, "Text message was unexpected, Response: {0}", JsonConvert.SerializeObject(actualResult));
        }
        public void V2ScreenTextTest()
        {
            IModeratorServiceV2 moderatorService = new ModeratorServiceV2(this.serviceOptions);

            // Import the term list. This needs to only be done once before screen
            moderatorService.ImportTermListAsync("eng").Wait();

            moderatorService.RefreshTextIndexAsync("eng").Wait();

            // Run screen to match, validating match result
            string text = "The <a href=\"www.bunnies.com\">qu!ck</a> brown  <a href=\"b.suspiciousdomain.com\">f0x</a> jumps over the lzay dog www.benign.net. freaking awesome.";
            TextModeratableContent textContent = new TextModeratableContent(text);
            var screenResponse = moderatorService.ScreenTextAsync(textContent, "eng");
            var screenResult = screenResponse.Result;

            var expectedScreenTextResult = JsonConvert.DeserializeObject<ScreenTextResult>(ResultJsonForComparison.TextV2_ScreenTextTest);

            Assert.IsTrue(screenResult != null, "Expected valid result");
            Assert.AreEqual(expectedScreenTextResult.NormalizedText, screenResult.NormalizedText);
            Assert.IsTrue(expectedScreenTextResult.Terms.SequenceEqual(screenResult.Terms, new TermMatchComparer()));
            Assert.IsTrue(expectedScreenTextResult.Terms.SequenceEqual(screenResult.Terms, new TermMatchComparer()));
            Assert.IsTrue(expectedScreenTextResult.Urls.SequenceEqual(screenResult.Urls, new URLMatchComparer()));
        }
        public void V2ExtractTextTest()
        {
            IModeratorServiceV2 moderatorService = new ModeratorServiceV2(this.serviceOptions);

            using (Stream stream = new FileStream(TestImageContent, FileMode.Open, FileAccess.Read))
            {
                ImageModeratableContent imageContent =
                    new ImageModeratableContent(new BinaryContent(stream, "image/jpeg"));

                // extract
                var extractResponse = moderatorService.ExtractTextAsync(imageContent, "eng");
                var extractResult = extractResponse.Result;

                Assert.IsTrue(extractResult != null, "Expected valid result, Response: {0}", JsonConvert.SerializeObject(extractResult));
                Assert.IsTrue(extractResult.Text == "Windows10 \r\nSatya Nadella \r\n", "Extracted text is not as expected, Response: {0}", JsonConvert.SerializeObject(extractResult));

            }
        }
        public void V2GetTermListTest()
        {
            IModeratorServiceV2 moderatorService = new ModeratorServiceV2(this.serviceOptions);

            // Import the term list. This needs to only be done once before screen
            moderatorService.ImportTermListAsync("eng").Wait();
            moderatorService.RefreshTextIndexAsync("eng").Wait();

            var taskResult = moderatorService.ListTermsAsync("eng");
            var actualResult = taskResult.Result;

            Assert.IsTrue(actualResult != null, "Expected valid result for Get Term List");
            Assert.IsTrue(actualResult.Terms.Count>0, "The count of terms is 0");
        }
 public void V2CacheImage()
 {
     IModeratorServiceV2 moderatorService = new ModeratorServiceV2(this.serviceOptions);
     var actualResult = CacheImageContent(moderatorService);
     Assert.AreNotEqual(actualResult.CacheID, "");
 }