commit 346be8db12997c472d27f10c9676c1f8ec90200e
Author: A-Konig <alex-nebulae@seznam.cz>
Date:   Tue May 25 15:38:59 2021 +0200

    re #8934 Testing JsonParser

diff --git a/Server/ServerApp/WeatherPredictionParser/IJsonParser.cs b/Server/ServerApp/WeatherPredictionParser/IJsonParser.cs
index 1dafe16..d8bc400 100644
--- a/Server/ServerApp/WeatherPredictionParser/IJsonParser.cs
+++ b/Server/ServerApp/WeatherPredictionParser/IJsonParser.cs
@@ -3,6 +3,7 @@
 //
 
 using ServerApp.Parser.OutputInfo;
+using System;
 using System.Collections.Generic;
 
 namespace ServerApp.WeatherPredictionParser
@@ -15,7 +16,7 @@ namespace ServerApp.WeatherPredictionParser
 
         /// <summary> Current weather </summary>
         WeatherInfo current;
-        public WeatherInfo Current { get => current; set => predictions = value; }
+        public WeatherInfo Current { get => current; set => current = value; }
 
         /// <summary> Prediction for today, tommorrow and day after tommorrow </summary>
         List<WeatherInfo> predictions;
@@ -27,6 +28,16 @@ namespace ServerApp.WeatherPredictionParser
         /// </summary>
         abstract public void ParsePrediction();
 
+        /// <summary>
+        /// Get predictions from Predictions that are within specified time span
+        /// </summary>
+        /// <param name="from">DateTime from</param>
+        /// <param name="to">DateTime to</param>
+        /// <returns>List of predictions that fit specified criteria</returns>
+        abstract public List<WeatherInfo> GetPredictionForTime(DateTime from, DateTime to);
+
+
+
     }
 
 }
diff --git a/Server/ServerApp/WeatherPredictionParser/JsonParser.cs b/Server/ServerApp/WeatherPredictionParser/JsonParser.cs
index 5d296ec..fee0fb2 100644
--- a/Server/ServerApp/WeatherPredictionParser/JsonParser.cs
+++ b/Server/ServerApp/WeatherPredictionParser/JsonParser.cs
@@ -29,7 +29,6 @@ namespace ServerApp.WeatherPredictionParser
         /// <summary> Sunset time of currently parsed day </summary>
         DateTime sunsetTime;
 
-
         /// <summary>
         /// Constructor
         /// </summary>
@@ -39,6 +38,65 @@ namespace ServerApp.WeatherPredictionParser
             this.loader = loader;
         }
 
+        /// <summary>
+        /// Get predictions from Predictions that are within specified time span
+        /// From-to including
+        /// If from == DateTime.Min then all until to
+        /// If to  == DateTime.Max then all starting from from
+        /// </summary>
+        /// <param name="from">DateTime from</param>
+        /// <param name="to">DateTime to</param>
+        /// <returns>List of predictions that fit specified criteria or null if incorrect input</returns>
+        public override List<WeatherInfo> GetPredictionForTime(DateTime from, DateTime to)
+        {
+            if (Predictions == null)
+                return null;
+
+            List<WeatherInfo> res = new List<WeatherInfo>();
+
+            if (from == DateTime.MinValue)
+                from = Predictions[0].startTime;
+
+            if (to == DateTime.MaxValue) {
+                DateTime dt = Predictions[Predictions.Count - 1].startTime;
+                int hour = dt.Hour + Predictions[Predictions.Count - 1].intervalLength;
+                bool addDay = false;
+                if (hour >= 24)
+                {
+                    hour -= 24;
+                    addDay = true;
+                }
+                to = new DateTime(dt.Year, dt.Month, dt.Day, hour, dt.Minute, dt.Second);
+                if (addDay)
+                    to = to.AddDays(1);
+            }
+
+            if (from > to)
+                return null;
+
+            // for all parsed weather info
+            foreach (WeatherInfo pred in Predictions)
+            {
+                int hour = pred.startTime.Hour + pred.intervalLength;
+                bool addDay = false;
+                if (hour >= 24)
+                {
+                    hour -= 24;
+                    addDay = true;
+                }
+                DateTime endTime = new DateTime(pred.startTime.Year, pred.startTime.Month, pred.startTime.Day, hour, pred.startTime.Minute, pred.startTime.Second);
+                if (addDay)
+                    endTime = endTime.AddDays(1);
+
+                // if both end and start not outside of interval
+                if (!((pred.startTime < from && endTime <= from) || (pred.startTime > to && endTime > to)))
+                    res.Add(pred);
+            }
+
+            return res;
+        }
+
+
         /// <summary>
         /// Parse weather prediction
         /// Results is in attributes current for current weather and pred for weather prediction for today, tommorrow and day after tommorrow
diff --git a/Server/TestProject/ParserTests/TestingParser.cs b/Server/TestProject/ParserTests/TestingParser.cs
index dfaec2b..01e1062 100644
--- a/Server/TestProject/ParserTests/TestingParser.cs
+++ b/Server/TestProject/ParserTests/TestingParser.cs
@@ -1015,9 +1015,10 @@ namespace TestProject
         [TestMethod]
         public void JsonParser()
         {
-            // TODO needs rewrite after DataDownloader has correct download method
+            /*
             TagInfo.CreateDictionaries();
 
+            // TODO make an input file
             string data = "";
 
             Mock<DataDownloader> dl = new Mock<DataDownloader>();
@@ -1038,8 +1039,159 @@ namespace TestProject
             Assert.AreEqual(new ActivityInfo("FDU", 7, new DateTime(2000, 1, 1, 17, 0, 0), 2), retVal[5]);
             Assert.AreEqual(new ActivityInfo("FDU", 7, new DateTime(2000, 1, 1, 17, 0, 0), 2), retVal[6]);
             Assert.AreEqual(new ActivityInfo("FDU", 7, new DateTime(2000, 1, 1, 17, 0, 0), 2), retVal[7]);
+            */
+        }
+
+
+        [TestMethod]
+        public void GetPredictionForTimeFilter()
+        {
+            string data = "";
+            Mock<DataDownloader> dl = new Mock<DataDownloader>("", "", "");
+            dl.Setup(m => m.DownloadWeatherPrediction()).Returns(data);
+
+            JsonParser target = new JsonParser(dl.Object);
+
+            List<WeatherInfo> pred = new List<WeatherInfo>();
+            pred.Add(new WeatherInfo(new DateTime(2000, 1, 1, 0, 0, 0), 8, 1, 2, 60_000, 3));
+
+            pred.Add(new WeatherInfo(new DateTime(2000, 1, 1, 3, 0, 0), 8, 1, 2, 60_000, 6));
+            pred.Add(new WeatherInfo(new DateTime(2000, 1, 1, 12, 0, 0), 8, 1, 2, 60_000, 6));
+            pred.Add(new WeatherInfo(new DateTime(2000, 1, 1, 18, 0, 0), 8, 1, 2, 60_000, 12));
+            pred.Add(new WeatherInfo(new DateTime(2000, 1, 2, 6, 0, 0), 8, 1, 2, 60_000, 3));
+            pred.Add(new WeatherInfo(new DateTime(2000, 1, 2, 9, 0, 0), 8, 1, 2, 60_000, 15));
+            pred.Add(new WeatherInfo(new DateTime(2000, 1, 3, 0, 0, 0), 8, 1, 2, 60_000, 3));
+
+            pred.Add(new WeatherInfo(new DateTime(2000, 1, 3, 3, 0, 0), 8, 1, 2, 60_000, 3));
+
+            target.Predictions = pred;
+
+            List<WeatherInfo> retVal = target.GetPredictionForTime(new DateTime(2000, 1, 1, 6, 0, 0), new DateTime(2000, 1, 3, 0, 0, 0));
+
+            Assert.AreEqual(6, retVal.Count);
+            Assert.AreEqual(new WeatherInfo(new DateTime(2000, 1, 1, 3, 0, 0), 8, 1, 2, 60_000, 6), retVal[0]);
+            Assert.AreEqual(new WeatherInfo(new DateTime(2000, 1, 1, 12, 0, 0), 8, 1, 2, 60_000, 6), retVal[1]);
+            Assert.AreEqual(new WeatherInfo(new DateTime(2000, 1, 1, 18, 0, 0), 8, 1, 2, 60_000, 12), retVal[2]);
+            Assert.AreEqual(new WeatherInfo(new DateTime(2000, 1, 2, 6, 0, 0), 8, 1, 2, 60_000, 3), retVal[3]);
+            Assert.AreEqual(new WeatherInfo(new DateTime(2000, 1, 2, 9, 0, 0), 8, 1, 2, 60_000, 15), retVal[4]);
+            Assert.AreEqual(new WeatherInfo(new DateTime(2000, 1, 3, 0, 0, 0), 8, 1, 2, 60_000, 3), retVal[5]);
         }
 
+        [TestMethod]
+        public void GetPredictionForTimeInvalidInput()
+        {
+            string data = "";
+            Mock<DataDownloader> dl = new Mock<DataDownloader>("", "", "");
+            dl.Setup(m => m.DownloadWeatherPrediction()).Returns(data);
+
+            JsonParser target = new JsonParser(dl.Object);
+
+            List<WeatherInfo> pred = new List<WeatherInfo>();
+            pred.Add(new WeatherInfo(new DateTime(2000, 1, 1, 0, 0, 0), 8, 1, 2, 60_000, 3));
+
+            pred.Add(new WeatherInfo(new DateTime(2000, 1, 1, 3, 0, 0), 8, 1, 2, 60_000, 6));
+            pred.Add(new WeatherInfo(new DateTime(2000, 1, 1, 12, 0, 0), 8, 1, 2, 60_000, 6));
+            pred.Add(new WeatherInfo(new DateTime(2000, 1, 1, 18, 0, 0), 8, 1, 2, 60_000, 12));
+            pred.Add(new WeatherInfo(new DateTime(2000, 1, 2, 6, 0, 0), 8, 1, 2, 60_000, 3));
+            pred.Add(new WeatherInfo(new DateTime(2000, 1, 2, 9, 0, 0), 8, 1, 2, 60_000, 15));
+            pred.Add(new WeatherInfo(new DateTime(2000, 1, 3, 0, 0, 0), 8, 1, 2, 60_000, 3));
+
+            pred.Add(new WeatherInfo(new DateTime(2000, 1, 3, 3, 0, 0), 8, 1, 2, 60_000, 3));
+
+            target.Predictions = pred;
+
+            List<WeatherInfo> retVal = target.GetPredictionForTime(new DateTime(2000, 1, 3, 0, 0, 0), new DateTime(2000, 1, 1, 6, 0, 0));
+            Assert.AreEqual(null, retVal);
+        }
+
+        [TestMethod]
+        public void GetPredictionForTimeAllTo()
+        {
+            string data = "";
+            Mock<DataDownloader> dl = new Mock<DataDownloader>("", "", "");
+            dl.Setup(m => m.DownloadWeatherPrediction()).Returns(data);
+
+            JsonParser target = new JsonParser(dl.Object);
+
+            List<WeatherInfo> pred = new List<WeatherInfo>();
+            pred.Add(new WeatherInfo(new DateTime(2000, 1, 1, 0, 0, 0), 8, 1, 2, 60_000, 3));
+            pred.Add(new WeatherInfo(new DateTime(2000, 1, 1, 3, 0, 0), 8, 1, 2, 60_000, 6));
+            pred.Add(new WeatherInfo(new DateTime(2000, 1, 1, 12, 0, 0), 8, 1, 2, 60_000, 6));
+            pred.Add(new WeatherInfo(new DateTime(2000, 1, 1, 18, 0, 0), 8, 1, 2, 60_000, 12));
+            pred.Add(new WeatherInfo(new DateTime(2000, 1, 2, 6, 0, 0), 8, 1, 2, 60_000, 3));
+            
+            pred.Add(new WeatherInfo(new DateTime(2000, 1, 2, 9, 0, 0), 8, 1, 2, 60_000, 15));
+            pred.Add(new WeatherInfo(new DateTime(2000, 1, 3, 0, 0, 0), 8, 1, 2, 60_000, 3));
+            pred.Add(new WeatherInfo(new DateTime(2000, 1, 3, 3, 0, 0), 8, 1, 2, 60_000, 3));
+
+            target.Predictions = pred;
+
+            List<WeatherInfo> retVal = target.GetPredictionForTime(DateTime.MinValue, new DateTime(2000, 1, 2, 6, 0, 0));
+
+            Assert.AreEqual(5, retVal.Count);
+            Assert.AreEqual(new WeatherInfo(new DateTime(2000, 1, 1, 0, 0, 0), 8, 1, 2, 60_000, 3), retVal[0]);
+            Assert.AreEqual(new WeatherInfo(new DateTime(2000, 1, 1, 3, 0, 0), 8, 1, 2, 60_000, 6), retVal[1]);
+            Assert.AreEqual(new WeatherInfo(new DateTime(2000, 1, 1, 12, 0, 0), 8, 1, 2, 60_000, 6), retVal[2]);
+            Assert.AreEqual(new WeatherInfo(new DateTime(2000, 1, 1, 18, 0, 0), 8, 1, 2, 60_000, 12), retVal[3]);
+            Assert.AreEqual(new WeatherInfo(new DateTime(2000, 1, 2, 6, 0, 0), 8, 1, 2, 60_000, 3), retVal[4]);
+        }
+
+        [TestMethod]
+        public void GetPredictionForTimeAllFrom()
+        {
+            string data = "";
+            Mock<DataDownloader> dl = new Mock<DataDownloader>("", "", "");
+            dl.Setup(m => m.DownloadWeatherPrediction()).Returns(data);
+
+            JsonParser target = new JsonParser(dl.Object);
+
+            List<WeatherInfo> pred = new List<WeatherInfo>();
+            pred.Add(new WeatherInfo(new DateTime(2000, 1, 1, 0, 0, 0), 8, 1, 2, 60_000, 3));
+            pred.Add(new WeatherInfo(new DateTime(2000, 1, 1, 3, 0, 0), 8, 1, 2, 60_000, 3));
+            pred.Add(new WeatherInfo(new DateTime(2000, 1, 1, 6, 0, 0), 8, 1, 2, 60_000, 3));
+            pred.Add(new WeatherInfo(new DateTime(2000, 1, 1, 9, 0, 0), 8, 1, 2, 60_000, 3));
+            pred.Add(new WeatherInfo(new DateTime(2000, 1, 1, 12, 0, 0), 8, 1, 2, 60_000, 6));
+            pred.Add(new WeatherInfo(new DateTime(2000, 1, 1, 18, 0, 0), 8, 1, 2, 60_000, 12));
+            pred.Add(new WeatherInfo(new DateTime(2000, 1, 2, 6, 0, 0), 8, 1, 2, 60_000, 3));
+
+            target.Predictions = pred;
+
+            List<WeatherInfo> retVal = target.GetPredictionForTime(new DateTime(2000, 1, 1, 12, 0, 0), DateTime.MaxValue);
+
+            Assert.AreEqual(3, retVal.Count);
+            Assert.AreEqual(new WeatherInfo(new DateTime(2000, 1, 1, 12, 0, 0), 8, 1, 2, 60_000, 6), retVal[0]);
+            Assert.AreEqual(new WeatherInfo(new DateTime(2000, 1, 1, 18, 0, 0), 8, 1, 2, 60_000, 12), retVal[1]);
+            Assert.AreEqual(new WeatherInfo(new DateTime(2000, 1, 2, 6, 0, 0), 8, 1, 2, 60_000, 3), retVal[2]);
+        }
+
+        public void GetPredictionForTimeAll()
+        {
+
+            // TODO make an input file
+            string data = "";
+            Mock<DataDownloader> dl = new Mock<DataDownloader>("", "", "");
+            dl.Setup(m => m.DownloadWeatherPrediction()).Returns(data);
+
+            JsonParser target = new JsonParser(dl.Object);
+
+            List<WeatherInfo> pred = new List<WeatherInfo>();
+            pred.Add(new WeatherInfo(new DateTime(2000, 1, 1, 0, 0, 0), 8, 1, 2, 60_000, 3));
+            pred.Add(new WeatherInfo(new DateTime(2000, 1, 1, 3, 0, 0), 8, 1, 2, 60_000, 6));
+            pred.Add(new WeatherInfo(new DateTime(2000, 1, 1, 12, 0, 0), 8, 1, 2, 60_000, 6));
+            pred.Add(new WeatherInfo(new DateTime(2000, 1, 1, 18, 0, 0), 8, 1, 2, 60_000, 12));
+            pred.Add(new WeatherInfo(new DateTime(2000, 1, 2, 6, 0, 0), 8, 1, 2, 60_000, 3));
+
+            target.Predictions = pred;
+
+            List<WeatherInfo> retVal = target.GetPredictionForTime(DateTime.MinValue, DateTime.MaxValue);
+
+            Assert.AreEqual(5, retVal.Count);
+            Assert.AreEqual(new WeatherInfo(new DateTime(2000, 1, 1, 0, 0, 0), 8, 1, 2, 60_000, 3), retVal[0]);
+            Assert.AreEqual(new WeatherInfo(new DateTime(2000, 1, 1, 3, 0, 0), 8, 1, 2, 60_000, 6), retVal[1]);
+            Assert.AreEqual(new WeatherInfo(new DateTime(2000, 1, 1, 12, 0, 0), 8, 1, 2, 60_000, 6), retVal[2]);
+            Assert.AreEqual(new WeatherInfo(new DateTime(2000, 1, 1, 18, 0, 0), 8, 1, 2, 60_000, 12), retVal[3]);
+            Assert.AreEqual(new WeatherInfo(new DateTime(2000, 1, 2, 6, 0, 0), 8, 1, 2, 60_000, 3), retVal[4]);
+        }
         #endregion
     }
 }
