Esempio n. 1
0
        public void TestWords(TrollRequest troll)
        {
            int i;
            string text;

            /** check if alien text is present in the files saved on the server **/
            for (i = 0; i < 6; i++)
            {
                text = File.ReadAllText("../../Text/" + i + ".txt");
                if (string.Equals(text, troll.Text, StringComparison.CurrentCultureIgnoreCase))
                    break;
            }

            if (i == 6)
            {
                throw new WebFaultException(System.Net.HttpStatusCode.BadRequest);
            }
            /** end check **/

            Dictionary<string, int> wordCount = new Dictionary<string, int>(StringComparer.CurrentCultureIgnoreCase);
            text = troll.Text;
            char[] separators = { '.', ' ', ',', '!', ';', '-'};
            string[] wordsInText = text.Split(separators, StringSplitOptions.RemoveEmptyEntries);

            foreach (string word in wordsInText)
            {
                if (!troll.Exclude.Contains(word, StringComparer.CurrentCultureIgnoreCase))
                {
                    if (wordCount.ContainsKey(word.ToLower()))
                    {
                        wordCount[word] = wordCount[word] + 1;
                    }
                    else
                    {
                        wordCount.Add(word, 1);
                    }
                }
            }

            /** check if number of words returned with a count is correct **/
            if (wordCount.Count != troll.WordCount.Count)
            {
                throw new WebFaultException(System.Net.HttpStatusCode.BadRequest);
            }
            /** end check **/

            /** check if exclude word list is empty when text has only one unique word **/
            if (wordCount.Count == 1 && troll.Exclude.Length > 0)
            {
                throw new WebFaultException(System.Net.HttpStatusCode.BadRequest);
            }
            /** end check **/

            /** check if words to be excluded are present in the words returned with a count **/
            foreach (string excludeWord in troll.Exclude)
            {

                if (troll.WordCount.ContainsKey(excludeWord))
                {
                    throw new WebFaultException(System.Net.HttpStatusCode.BadRequest);
                }
            }
            /** end check **/

            foreach (string word in wordsInText)
            {
                if(!troll.Exclude.Contains(word, StringComparer.CurrentCultureIgnoreCase))
                {
                    /** check if word (not in exclude list) is present **/
                    if (!troll.WordCount.ContainsKey(word))
                    {
                        throw new WebFaultException(System.Net.HttpStatusCode.BadRequest);
                    }
                    /** end check **/

                    /** check if word count is correct **/
                    if (troll.WordCount[word] != wordCount[word])
                    {
                        throw new WebFaultException(System.Net.HttpStatusCode.BadRequest);
                    }
                    /** end check **/
                }
            }

            return;
        }
        public void TestTrollDetectorPost()
        {
            TrollRequest troll = new TrollRequest();
            troll.Text = "foo";
            troll.WordCount.Add("foo", 1);

            try
            {
                TrollDetectorService service = new TrollDetectorService();
                service.TestWords(troll);
            }
            catch (Exception e)
            {
                Assert.Fail();
            }

            troll = new TrollRequest();
            troll.Text = "Hodor, hodor hodor. Hodor! Hodor hodor hodor hodor hodor hodor.";
            troll.WordCount.Add("hodor", 10);

            try
            {
                TrollDetectorService service = new TrollDetectorService();
                service.TestWords(troll);
            }
            catch (Exception e)
            {
                Assert.Fail();
            }

            troll.Exclude = new string[1]{"hodor"};

            try
            {
                TrollDetectorService service = new TrollDetectorService();
                service.TestWords(troll);
            }
            catch (Exception e)
            {
                StringAssert.Equals(e.Message, "Bad Request");
            }

            troll = new TrollRequest();
            troll.Text = "The quick brown fox jumped over the lazy dog.";
            troll.Exclude = new string[2] { "the", "fox" };
            troll.WordCount.Add("the", 2);
            troll.WordCount.Add("quick", 1);
            troll.WordCount.Add("brown", 1);
            troll.WordCount.Add("jumped", 1);
            troll.WordCount.Add("over", 1);
            troll.WordCount.Add("lazy", 1);
            troll.WordCount.Add("dog", 1);

            try
            {
                TrollDetectorService service = new TrollDetectorService();
                service.TestWords(troll);
            }
            catch (Exception e)
            {
                StringAssert.Equals(e.Message, "Bad Request");
            }

            troll.WordCount.Remove("the");

            try
            {
                TrollDetectorService service = new TrollDetectorService();
                service.TestWords(troll);
            }
            catch (Exception e)
            {
                Assert.Fail();
            }

            troll = new TrollRequest();
            troll.Text = "Hello, my name is Troll alien";
            troll.Exclude = new string[3] { "my", "troll", "is" };
            troll.WordCount.Add("Hello", 1);
            troll.WordCount.Add("name", 1);
            troll.WordCount.Add("alien", 1);

            try
            {
                TrollDetectorService service = new TrollDetectorService();
                service.TestWords(troll);
                Assert.Fail();
            }
            catch (Exception e)
            {
                StringAssert.Equals(e.Message, "Bad Request");
            }

            troll = new TrollRequest();
            troll.Text = "It was the best of times, it was the worst of times, it was the age of wisdom, it was the age of foolishness, it was the epoch of belief, it was the epoch of incredulity, it was the season of Light, it was the season of Darkness, it was the spring of hope, it was the winter of despair, we had everything before us, we had nothing before us, we were all going direct to Heaven, we were all going direct the other way - in short, the period was so far like the present period, that some of its noisiest authorities insisted on its being received, for good or for evil, in the superlative degree of comparison only.";
            troll.Exclude = new string[8] { "best", "superlative", "insisted", "only", "present", "in", "good", "evil" };
            troll.WordCount.Add("It", 10);
            troll.WordCount.Add("was", 11);
            troll.WordCount.Add("the", 14);
            troll.WordCount.Add("of", 12);
            troll.WordCount.Add("times", 2);
            troll.WordCount.Add("worst", 1);
            troll.WordCount.Add("age", 2);
            troll.WordCount.Add("wisdom", 1);
            troll.WordCount.Add("foolishness", 1);
            troll.WordCount.Add("epoch", 2);
            troll.WordCount.Add("belief", 1);
            troll.WordCount.Add("incredulity", 1);
            troll.WordCount.Add("light", 1);
            troll.WordCount.Add("darkness", 1);
            troll.WordCount.Add("spring", 1);
            troll.WordCount.Add("hope", 1);
            troll.WordCount.Add("winter", 1);
            troll.WordCount.Add("despair", 1);
            troll.WordCount.Add("had", 2);
            troll.WordCount.Add("everything", 1);
            troll.WordCount.Add("before", 2);
            troll.WordCount.Add("us", 2);
            troll.WordCount.Add("were", 2);
            troll.WordCount.Add("all", 2);
            troll.WordCount.Add("going", 2);
            troll.WordCount.Add("direct", 2);
            troll.WordCount.Add("to", 1);
            troll.WordCount.Add("heaven", 1);
            troll.WordCount.Add("other", 1);
            troll.WordCount.Add("way", 1);
            troll.WordCount.Add("short", 1);
            troll.WordCount.Add("period", 2);
            troll.WordCount.Add("so", 1);
            troll.WordCount.Add("far", 1);
            troll.WordCount.Add("like", 1);
            troll.WordCount.Add("some", 1);
            troll.WordCount.Add("its", 2);
            troll.WordCount.Add("noisiest", 1);
            troll.WordCount.Add("authorities", 1);
            troll.WordCount.Add("on", 1);
            troll.WordCount.Add("being", 1);
            troll.WordCount.Add("received", 1);
            troll.WordCount.Add("degree", 1);
            troll.WordCount.Add("comparison", 1);
            troll.WordCount.Add("for", 2);
            troll.WordCount.Add("or", 1);
            troll.WordCount.Add("that", 1);
            troll.WordCount.Add("nothing", 1);
            troll.WordCount.Add("season", 2);
            troll.WordCount.Add("we", 4);

            try
            {
                TrollDetectorService service = new TrollDetectorService();
                service.TestWords(troll);
            }
            catch (Exception e)
            {
                Assert.Fail();
            }
        }