public JsonResult Analyse()
        {
            if (Request.Files.Count == 0)
                return null;

            var file = Request.Files[0];

            if (file == null)
                return null;

            if (file.ContentLength == 0)
                return null;

            ITextStatisticBuilder builder = new TextStatisticsBuilder();
            var stats = builder.Build(file.InputStream);

            LightWeightObject lightWeight = stats.ToLightWeight();

            var user = Session["user"] as User;
            if (user != null)
            {
                AddLightObjToDb(lightWeight, user, file.FileName);
            }

            var words = lightWeight.WordFrequencyDictionary
            .Take(CountToSerialize)
            .ToDictionary(pair => pair.Key, pair => pair.Value);

            var serializer = new JsonSerializerForJit();
            string serrialized = serializer.Serialize(words);

            JsonResult result = Json(serrialized, JsonRequestBehavior.AllowGet);

            return result;
        }
 public void Build_TextFile_StatisticsIsNotNull(string fName)
 {
     TestPrecondition.EnsureFileExist(fName);
     Stream stream = File.OpenRead(fName);
     ITextStatisticBuilder builder = new TextStatisticsBuilder();
     var actual = builder.Build(stream);
     Assert.IsNotNull(actual);
 }
 public void Build_TextFile_OriginalStreamCanRead(string fName)
 {
     TestPrecondition.EnsureFileExist(fName);
     Stream stream = File.OpenRead(fName);
     ITextStatisticBuilder builder = new TextStatisticsBuilder();
     var actual = builder.Build(stream);
     Assert.IsTrue(stream.CanRead);
 }
        public void ToText_StreamIsTooLarge_ObjectIsTooLargeException()
        {
            IStreamConverter c = new StreamConverter();
            string str = ConfigurationManager
                                    .AppSettings["maxAllowedInputBytes"];

            int max;
            TestPrecondition.EnsureCanParse(str, out max);
            ITextStatisticBuilder builder = new TextStatisticsBuilder();
            builder.Build(new MemoryStream(new byte[max + 1]));
            Assert.Fail("Shouldn't reach this code");
        }
        public void Serialize_Json_For_Jit(string fName)
        {
            TestPrecondition.EnsureFileExist(fName);
            const int count = 100;

            //for now file exist in the bin directory
            Stream stream = File.OpenRead(fName);

            ITextStatisticBuilder builder = new TextStatisticsBuilder();
            var stats = builder.Build(stream);

            var iterator = stats.WordFrequencyDictionary.Take(count);
            var dictionary = iterator.ToDictionary(pair => pair.Key, pair => pair.Value);

            var serrializer = new JsonSerrializer<IDictionary<string, int>>();
            string json = serrializer.SerializeForJit(dictionary);
        }
        public void Serialize_Dictionary_Take_nRecord(string fName)
        {
            TestPrecondition.EnsureFileExist(fName);
            const int expectedCount = 100;

            Stream stream = File.OpenRead(fName);

            ITextStatisticBuilder builder = new TextStatisticsBuilder();
            var stats = builder.Build(stream);

            var iterator = stats.WordFrequencyDictionary.Take(expectedCount);
            var dictionary = iterator.ToDictionary(pair => pair.Key, pair => pair.Value);

            var serrializer = new JsonSerrializer<IDictionary<string, int>>();
            string json = serrializer.Serialize(dictionary);

            int actual = json.Split(':').Count() - 1;

            Assert.AreEqual(expectedCount, actual);
        }
 public void Build_StreamIsNull_ArgumentNullException()
 {
     ITextStatisticBuilder builder = new TextStatisticsBuilder();
     builder.Build(null);
     Assert.Fail("Shouldn't reach this code");
 }
 public void Build_StreamIsEmpty_ArgumentException()
 {
     ITextStatisticBuilder builder = new TextStatisticsBuilder();
     builder.Build(new MemoryStream());
     Assert.Fail("Shouldn't reach this code");
 }