Revize 27aa66b5
Přidáno uživatelem Alex Konig před téměř 4 roky(ů)
Server/ServerApp/DataDownload/IDataDownloader.cs | ||
---|---|---|
6 | 6 |
|
7 | 7 |
namespace ServerApp.DataDownload |
8 | 8 |
{ |
9 |
interface IDataDownloader |
|
9 |
public interface IDataDownloader
|
|
10 | 10 |
{ |
11 | 11 |
/// <summary> |
12 | 12 |
/// Downloads json file - returns contents of said file |
Server/ServerApp/Program.cs | ||
---|---|---|
35 | 35 |
static void Main(string[] args) |
36 | 36 |
{ |
37 | 37 |
XmlConfigurator.Configure(); |
38 |
// SETUP FOLDERS |
|
39 |
Config config = FillConfigInfo(args); |
|
38 |
|
|
39 |
// SETUP FOLDERS |
|
40 |
Config config = FillConfigInfo(args); |
|
40 | 41 |
if (config == null) |
41 | 42 |
{ |
42 | 43 |
Console.ReadLine(); |
... | ... | |
46 | 47 |
// data download test |
47 | 48 |
DataDownloader dd = DataDownloadAndRetrievalTest(config); |
48 | 49 |
|
50 |
|
|
49 | 51 |
// xml building test |
50 | 52 |
//XMLTest(); |
51 | 53 |
|
... | ... | |
68 | 70 |
inputThread.Start(); |
69 | 71 |
|
70 | 72 |
// connection test |
71 |
ConnectionTest(controller, config); |
|
73 |
//ConnectionTest(controller, config);
|
|
72 | 74 |
|
73 | 75 |
Console.ReadLine(); |
74 | 76 |
} |
Server/ServerApp/Properties/launchSettings.json | ||
---|---|---|
2 | 2 |
"profiles": { |
3 | 3 |
"ServerApp": { |
4 | 4 |
"commandName": "Project", |
5 |
"commandLineArgs": "C:\\Users\\kalivoda\\University\\KIV-ASWI\\aswi2021tri-musketyri\\Server\\ServerApp\\server_config"
|
|
5 |
"commandLineArgs": ".\\server_config"
|
|
6 | 6 |
} |
7 | 7 |
} |
8 | 8 |
} |
Server/ServerApp/WeatherPredictionParser/JsonParser.cs | ||
---|---|---|
22 | 22 |
public class JsonParser : IJsonParser |
23 | 23 |
{ |
24 | 24 |
/// <summary> Data downloader </summary> |
25 |
DataDownloader downloader; |
|
25 |
IDataDownloader downloader;
|
|
26 | 26 |
/// <summary> Data loader </summary> |
27 | 27 |
IDataLoader loader; |
28 | 28 |
/// <summary> Currently parsed day </summary> |
... | ... | |
36 | 36 |
/// Constructor |
37 | 37 |
/// </summary> |
38 | 38 |
/// <param name="downloader"></param> |
39 |
public JsonParser(DataDownloader downloader, IDataLoader loader) |
|
39 |
public JsonParser(IDataDownloader downloader, IDataLoader loader)
|
|
40 | 40 |
{ |
41 | 41 |
this.downloader = downloader; |
42 | 42 |
this.loader = loader; |
... | ... | |
111 | 111 |
sunsetTime = new List<DateTime>(); |
112 | 112 |
|
113 | 113 |
// get file |
114 |
string file = downloader.DownloadWeatherPrediction(); |
|
115 |
string data = loader.LoadPredictionFile(file); |
|
114 |
//string file = downloader.DownloadWeatherPrediction();
|
|
115 |
//string data = loader.LoadPredictionFile(file);
|
|
116 | 116 |
|
117 |
if (data == null)
|
|
118 |
return;
|
|
117 |
// get data
|
|
118 |
string data = downloader.DownloadWeatherPrediction();
|
|
119 | 119 |
|
120 | 120 |
DateTime now = DateTime.Now; |
121 | 121 |
|
122 | 122 |
Current = new WeatherInfo(); |
123 | 123 |
Predictions = new List<WeatherInfo>(); |
124 | 124 |
|
125 |
// parse |
|
126 |
JsonDocument doc = JsonDocument.Parse(data); |
|
127 |
JsonElement root = doc.RootElement; |
|
128 |
var weatherP = root.EnumerateObject(); |
|
125 |
if (data == null || data.Length == 0) |
|
126 |
return; |
|
129 | 127 |
|
130 |
while (weatherP.MoveNext()) |
|
128 |
// parse |
|
129 |
try |
|
131 | 130 |
{ |
132 |
string name = weatherP.Current.Name; |
|
133 |
Console.WriteLine(name); |
|
131 |
JsonDocument doc = JsonDocument.Parse(data); |
|
132 |
JsonElement root = doc.RootElement; |
|
133 |
var weatherP = root.EnumerateObject(); |
|
134 | 134 |
|
135 |
switch (name)
|
|
135 |
while (weatherP.MoveNext())
|
|
136 | 136 |
{ |
137 |
// current weather |
|
138 |
case "current_condition": |
|
139 |
{ |
|
140 |
ArrayEnumerator currentWeather = weatherP.Current.Value.EnumerateArray(); |
|
141 |
Current = ParseCurrentWeather(currentWeather); |
|
142 |
|
|
143 |
break; |
|
144 |
} |
|
145 |
// weather prediction |
|
146 |
case "weather": |
|
147 |
{ |
|
148 |
ArrayEnumerator weather = weatherP.Current.Value.EnumerateArray(); |
|
149 |
ParseWeatherPredict(weather); |
|
150 |
|
|
151 |
break; |
|
152 |
} |
|
137 |
string name = weatherP.Current.Name; |
|
138 |
Console.WriteLine(name); |
|
139 |
|
|
140 |
switch (name) |
|
141 |
{ |
|
142 |
// current weather |
|
143 |
case "current_condition": |
|
144 |
{ |
|
145 |
ArrayEnumerator currentWeather = weatherP.Current.Value.EnumerateArray(); |
|
146 |
Current = ParseCurrentWeather(currentWeather); |
|
147 |
|
|
148 |
break; |
|
149 |
} |
|
150 |
// weather prediction |
|
151 |
case "weather": |
|
152 |
{ |
|
153 |
ArrayEnumerator weather = weatherP.Current.Value.EnumerateArray(); |
|
154 |
ParseWeatherPredict(weather); |
|
155 |
|
|
156 |
break; |
|
157 |
} |
|
158 |
} |
|
153 | 159 |
} |
154 |
} |
|
155 | 160 |
|
156 |
// sunrise + sunset into data |
|
157 |
EncompassSunRiseSetTimes(); |
|
161 |
// sunrise + sunset into data |
|
162 |
EncompassSunRiseSetTimes(); |
|
163 |
} catch |
|
164 |
{ |
|
165 |
Console.WriteLine("Weather prediction file in incorrect format"); |
|
166 |
} |
|
158 | 167 |
|
159 | 168 |
//TestConsoleOutput(); |
160 | 169 |
} |
Server/ServerAppFunctionalTests/ParserTests/JsonParserTesting.cs | ||
---|---|---|
270 | 270 |
}"; |
271 | 271 |
} |
272 | 272 |
|
273 |
string SetIncorrectInput() |
|
274 |
{ |
|
275 |
// TODO |
|
276 |
return "data"; |
|
277 |
} |
|
278 |
|
|
273 | 279 |
[TestMethod] |
274 | 280 |
public void JsonParserTestCurrent() |
275 | 281 |
{ |
... | ... | |
277 | 283 |
|
278 | 284 |
string data = SetBasicData(); |
279 | 285 |
|
280 |
Mock<DataDownloader> dl = new Mock<DataDownloader>("", "", "");
|
|
281 |
dl.Setup(m => m.DownloadWeatherPrediction()).Returns("test");
|
|
286 |
Mock<IDataDownloader> dl = new Mock<IDataDownloader>();
|
|
287 |
dl.Setup(m => m.DownloadWeatherPrediction()).Returns(data);
|
|
282 | 288 |
|
283 | 289 |
Mock<IDataLoader> l = new Mock<IDataLoader>(); |
284 |
l.Setup(m => m.LoadPredictionFile("test")).Returns(data); |
|
285 | 290 |
|
286 | 291 |
JsonParser target = new JsonParser(dl.Object, l.Object); |
287 | 292 |
|
... | ... | |
291 | 296 |
Assert.AreEqual(new WeatherInfo(new DateTime(2021, 5, 25, 13, 29, 0), 4, 0, 3.611, ValueToConditions.CloudCoverToConditions(100), 0), current); |
292 | 297 |
} |
293 | 298 |
|
299 |
[TestMethod] |
|
300 |
public void JsonParserNoneInput() |
|
301 |
{ |
|
302 |
TagInfo.CreateDictionaries(); |
|
303 |
|
|
304 |
string data = ""; |
|
305 |
|
|
306 |
Mock<IDataDownloader> dl = new Mock<IDataDownloader>(); |
|
307 |
dl.Setup(m => m.DownloadWeatherPrediction()).Returns(data); |
|
308 |
|
|
309 |
Mock<IDataLoader> l = new Mock<IDataLoader>(); |
|
310 |
|
|
311 |
JsonParser target = new JsonParser(dl.Object, l.Object); |
|
312 |
|
|
313 |
target.ParsePrediction(); |
|
314 |
WeatherInfo current = target.Current; |
|
315 |
List<WeatherInfo> res = target.Predictions; |
|
316 |
|
|
317 |
Assert.AreEqual(0, res.Count); |
|
318 |
Assert.AreEqual(new WeatherInfo(new DateTime(), 0, 0, 0, WeatherConditions.Dark, 0), current); |
|
319 |
} |
|
320 |
|
|
321 |
[TestMethod] |
|
322 |
public void JsonParserNullInput() |
|
323 |
{ |
|
324 |
TagInfo.CreateDictionaries(); |
|
325 |
|
|
326 |
string data = null; |
|
327 |
|
|
328 |
Mock<IDataDownloader> dl = new Mock<IDataDownloader>(); |
|
329 |
dl.Setup(m => m.DownloadWeatherPrediction()).Returns(data); |
|
330 |
|
|
331 |
Mock<IDataLoader> l = new Mock<IDataLoader>(); |
|
332 |
|
|
333 |
JsonParser target = new JsonParser(dl.Object, l.Object); |
|
334 |
|
|
335 |
target.ParsePrediction(); |
|
336 |
WeatherInfo current = target.Current; |
|
337 |
List<WeatherInfo> res = target.Predictions; |
|
338 |
|
|
339 |
Assert.AreEqual(0, res.Count); |
|
340 |
Assert.AreEqual(new WeatherInfo(new DateTime(), 0, 0, 0, WeatherConditions.Dark, 0), current); |
|
341 |
} |
|
342 |
|
|
343 |
[TestMethod] |
|
344 |
public void JsonParserIncorrectInput() |
|
345 |
{ |
|
346 |
TagInfo.CreateDictionaries(); |
|
347 |
|
|
348 |
string data = SetIncorrectInput(); |
|
349 |
|
|
350 |
Mock<IDataDownloader> dl = new Mock<IDataDownloader>(); |
|
351 |
dl.Setup(m => m.DownloadWeatherPrediction()).Returns(data); |
|
352 |
|
|
353 |
Mock<IDataLoader> l = new Mock<IDataLoader>(); |
|
354 |
|
|
355 |
JsonParser target = new JsonParser(dl.Object, l.Object); |
|
356 |
|
|
357 |
target.ParsePrediction(); |
|
358 |
WeatherInfo current = target.Current; |
|
359 |
List<WeatherInfo> res = target.Predictions; |
|
360 |
|
|
361 |
Assert.AreEqual(0, res.Count); |
|
362 |
Assert.AreEqual(new WeatherInfo(new DateTime(), 0, 0, 0, WeatherConditions.Dark, 0), current); |
|
363 |
} |
|
364 |
|
|
294 | 365 |
[TestMethod] |
295 | 366 |
public void JsonParserTestPrediction() |
296 | 367 |
{ |
297 | 368 |
string data = SetBasicData(); |
298 | 369 |
|
299 |
Mock<DataDownloader> dl = new Mock<DataDownloader>("", "", "");
|
|
300 |
dl.Setup(m => m.DownloadWeatherPrediction()).Returns("test");
|
|
370 |
Mock<IDataDownloader> dl = new Mock<IDataDownloader>();
|
|
371 |
dl.Setup(m => m.DownloadWeatherPrediction()).Returns(data);
|
|
301 | 372 |
|
302 | 373 |
Mock<IDataLoader> l = new Mock<IDataLoader>(); |
303 |
l.Setup(m => m.LoadPredictionFile("test")).Returns(data); |
|
304 | 374 |
|
305 | 375 |
JsonParser target = new JsonParser(dl.Object, l.Object); |
306 | 376 |
|
... | ... | |
321 | 391 |
{ |
322 | 392 |
string data = SetAstronomyData(); |
323 | 393 |
|
324 |
Mock<DataDownloader> dl = new Mock<DataDownloader>("", "", "");
|
|
325 |
dl.Setup(m => m.DownloadWeatherPrediction()).Returns("test");
|
|
394 |
Mock<IDataDownloader> dl = new Mock<IDataDownloader>();
|
|
395 |
dl.Setup(m => m.DownloadWeatherPrediction()).Returns(data);
|
|
326 | 396 |
|
327 | 397 |
Mock<IDataLoader> l = new Mock<IDataLoader>(); |
328 |
l.Setup(m => m.LoadPredictionFile("test")).Returns(data); |
|
329 | 398 |
|
330 | 399 |
JsonParser target = new JsonParser(dl.Object, l.Object); |
331 | 400 |
|
... | ... | |
351 | 420 |
public void GetPredictionForTimeFilter() |
352 | 421 |
{ |
353 | 422 |
string data = ""; |
354 |
Mock<DataDownloader> dl = new Mock<DataDownloader>("", "", ""); |
|
355 |
dl.Setup(m => m.DownloadWeatherPrediction()).Returns("test"); |
|
423 |
|
|
424 |
Mock<IDataDownloader> dl = new Mock<IDataDownloader>(); |
|
425 |
dl.Setup(m => m.DownloadWeatherPrediction()).Returns(data); |
|
356 | 426 |
|
357 | 427 |
Mock<IDataLoader> l = new Mock<IDataLoader>(); |
358 |
l.Setup(m => m.LoadPredictionFile("test")).Returns(data); |
|
359 | 428 |
|
360 | 429 |
JsonParser target = new JsonParser(dl.Object, l.Object); |
361 | 430 |
|
... | ... | |
388 | 457 |
public void GetPredictionForTimeInvalidInput() |
389 | 458 |
{ |
390 | 459 |
string data = ""; |
391 |
Mock<DataDownloader> dl = new Mock<DataDownloader>("", "", ""); |
|
392 |
dl.Setup(m => m.DownloadWeatherPrediction()).Returns("test"); |
|
460 |
|
|
461 |
Mock<IDataDownloader> dl = new Mock<IDataDownloader>(); |
|
462 |
dl.Setup(m => m.DownloadWeatherPrediction()).Returns(data); |
|
393 | 463 |
|
394 | 464 |
Mock<IDataLoader> l = new Mock<IDataLoader>(); |
395 |
l.Setup(m => m.LoadPredictionFile("test")).Returns(data); |
|
396 | 465 |
|
397 | 466 |
JsonParser target = new JsonParser(dl.Object, l.Object); |
398 | 467 |
|
... | ... | |
418 | 487 |
public void GetPredictionForTimeAllTo() |
419 | 488 |
{ |
420 | 489 |
string data = ""; |
421 |
Mock<DataDownloader> dl = new Mock<DataDownloader>("", "", ""); |
|
422 |
dl.Setup(m => m.DownloadWeatherPrediction()).Returns("test"); |
|
490 |
|
|
491 |
Mock<IDataDownloader> dl = new Mock<IDataDownloader>(); |
|
492 |
dl.Setup(m => m.DownloadWeatherPrediction()).Returns(data); |
|
423 | 493 |
|
424 | 494 |
Mock<IDataLoader> l = new Mock<IDataLoader>(); |
425 |
l.Setup(m => m.LoadPredictionFile("test")).Returns(data); |
|
426 | 495 |
|
427 | 496 |
JsonParser target = new JsonParser(dl.Object, l.Object); |
428 | 497 |
|
... | ... | |
453 | 522 |
public void GetPredictionForTimeAllFrom() |
454 | 523 |
{ |
455 | 524 |
string data = ""; |
456 |
Mock<DataDownloader> dl = new Mock<DataDownloader>("", "", ""); |
|
457 |
dl.Setup(m => m.DownloadWeatherPrediction()).Returns("test"); |
|
525 |
|
|
526 |
Mock<IDataDownloader> dl = new Mock<IDataDownloader>(); |
|
527 |
dl.Setup(m => m.DownloadWeatherPrediction()).Returns(data); |
|
458 | 528 |
|
459 | 529 |
Mock<IDataLoader> l = new Mock<IDataLoader>(); |
460 |
l.Setup(m => m.LoadPredictionFile("test")).Returns(data); |
|
461 | 530 |
|
462 | 531 |
JsonParser target = new JsonParser(dl.Object, l.Object); |
463 | 532 |
|
... | ... | |
485 | 554 |
|
486 | 555 |
// TODO make an input file |
487 | 556 |
string data = ""; |
488 |
Mock<DataDownloader> dl = new Mock<DataDownloader>("", "", ""); |
|
489 |
dl.Setup(m => m.DownloadWeatherPrediction()).Returns("test"); |
|
557 |
|
|
558 |
Mock<IDataDownloader> dl = new Mock<IDataDownloader>(); |
|
559 |
dl.Setup(m => m.DownloadWeatherPrediction()).Returns(data); |
|
490 | 560 |
|
491 | 561 |
Mock<IDataLoader> l = new Mock<IDataLoader>(); |
492 |
l.Setup(m => m.LoadPredictionFile("test")).Returns(data); |
|
493 | 562 |
|
494 | 563 |
JsonParser target = new JsonParser(dl.Object, l.Object); |
495 | 564 |
|
Server/ServerAppFunctionalTests/ParserTests/WeatherParserTesting.cs | ||
---|---|---|
197 | 197 |
Assert.AreEqual(new WeatherInfo(new DateTime(2000, 2, 1, 11, 0, 0), 5, 0, 2, 60_000, 2), retVal[2]); |
198 | 198 |
Assert.AreEqual(new WeatherInfo(new DateTime(2000, 2, 1, 13, 0, 0), 10, 25, 2, 10_000, 2), retVal[3]); |
199 | 199 |
Assert.AreEqual(new WeatherInfo(new DateTime(2000, 2, 1, 15, 0, 0), 15, 40, 2, 10_000, 2), retVal[4]); |
200 |
|
|
201 |
retVal = (List<WeatherInfo>)obj.Invoke("ProcessOneWeatherFileAsIntervals", path, 3, new DateTime(2000, 1, 1, 8, 0, 0), new DateTime(2001, 1, 1, 11, 0, 0)); |
|
202 |
|
|
203 |
Assert.AreEqual(5, retVal.Count); |
|
204 |
Assert.AreEqual(new DateTime(2000, 1, 1, 7, 0, 0), retVal[0].startTime); |
|
205 |
Assert.AreEqual(new DateTime(2000, 1, 1, 10, 0, 0), retVal[1].startTime); |
|
206 |
Assert.AreEqual(new DateTime(2000, 2, 1, 10, 0, 0), retVal[2].startTime); |
|
207 |
Assert.AreEqual(new DateTime(2000, 2, 1, 13, 0, 0), retVal[3].startTime); |
|
208 |
Assert.AreEqual(new DateTime(2000, 2, 1, 16, 0, 0), retVal[4].startTime); |
|
200 | 209 |
} |
201 | 210 |
|
202 | 211 |
|
Také k dispozici: Unified diff
re #9037 Fixing json parser tests to work w/ IDataDownloader