public void AddImageTest() { IModeratorService moderatorService = new ModeratorService(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); 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, Response: {0}", JsonConvert.SerializeObject(refreshResult)); } 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); var matchResult = matchResponse.Result; Assert.IsTrue(matchResult.IsMatch, "Expected match, Response: {0}", JsonConvert.SerializeObject(matchResult)); } }
public void IdentifyLanguageTest() { IModeratorService moderatorService = new ModeratorService(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 AddTermTest() { IModeratorService moderatorService = new ModeratorService(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: "FakeProfanity", 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 FakeProfanity!"), "eng"); var screenResult = screenResponse.Result; Assert.IsTrue(screenResult.MatchDetails != null, "Expected valid terms"); var deleteTask = moderatorService.RemoveTermAsync(textContent, "eng"); var deleteResult = deleteTask.Result; Assert.IsTrue(deleteResult.IsSuccessStatusCode, "Expected valid result for DeleteTerm"); }
public void EvaluateImageContentTest() { using (Stream stream = new FileStream(TestImageContent, FileMode.Open, FileAccess.Read)) { IModeratorService moderatorService = new ModeratorService(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.AdvancedInfo != null, "Expected valid result, Response: {0}", JsonConvert.SerializeObject(actualResult)); var score = actualResult.AdvancedInfo.First( x => string.Equals(x.Key, "score", StringComparison.OrdinalIgnoreCase)); double scoreValue = double.Parse(score.Value); Assert.IsTrue(scoreValue > 0, "Expected higher than 0 score value for test image, Response: {0}", JsonConvert.SerializeObject(actualResult)); } }
public void Initialize() { this.serviceOptions = new ModeratorServiceOptions() { HostUrl = ConfigurationManager.AppSettings["HostUrl"], ImageServicePath = ConfigurationManager.AppSettings["ImageServicePath"], ImageServiceCustomListPath = ConfigurationManager.AppSettings["ImageServiceCustomListPath"], ImageServiceKey = ConfigurationManager.AppSettings["ImageServiceKey"], ImageServiceCustomListKey = ConfigurationManager.AppSettings["ImageServiceCustomListKey"] }; IModeratorService moderatorService = new ModeratorService(this.serviceOptions); }
public void ExtractTextTest() { IModeratorService moderatorService = new ModeratorService(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.AdvancedInfo != null, "Expected valid result, Response: {0}", JsonConvert.SerializeObject(extractResult)); var text = extractResult.AdvancedInfo.First( x => string.Equals(x.Key, "Text", StringComparison.OrdinalIgnoreCase)); Assert.AreEqual("Windows10 \r\nSatya Nadella \r\n", text.Value, "Text message was unexpected, Response: {0}", JsonConvert.SerializeObject(extractResult)); } }
public void EvaluateImageUrlTest() { IModeratorService moderatorService = new ModeratorService(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, "AdvancedInfo is NULL, Response: {0}", JsonConvert.SerializeObject(actualResult)); var score = actualResult.AdvancedInfo.First(x => string.Equals(x.Key, "score", StringComparison.OrdinalIgnoreCase)); Assert.AreNotEqual("0.000", score.Value, "score value, Response: {0}", JsonConvert.SerializeObject(actualResult)); }
public void ScreenTextTest() { IModeratorService moderatorService = new ModeratorService(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 = "My evil freaking text!"; TextModeratableContent textContent = new TextModeratableContent(text); var screenResponse = moderatorService.ScreenTextAsync(textContent, "eng"); var screenResult = screenResponse.Result; Assert.IsTrue(screenResult != null, "Expected valid result"); Assert.IsTrue(screenResult.MatchDetails != null, "Expected valid Match Details"); Assert.IsTrue(screenResult.MatchDetails.MatchFlags != null, "Expected valid Match Flags"); var matchFlag = screenResult.MatchDetails.MatchFlags.FirstOrDefault(); Assert.IsTrue(matchFlag != null, "Expected to see a match flag!"); Assert.AreEqual("freaking", matchFlag.Source, "Expected term to match"); }