From 64c09c00b3ab302e06431b2f7172c999e04887ff Mon Sep 17 00:00:00 2001 From: "Marcel Kapfer (mmk2410)" Date: Sun, 17 Jul 2016 23:39:58 +0200 Subject: [PATCH 1/4] Added JSON with course data to .gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index bda2954..012d4be 100644 --- a/.gitignore +++ b/.gitignore @@ -26,3 +26,4 @@ doc/api/ /index.html /json/ /test.js +data.json From a1de036338482aaa5de3fd8dd87b07eeaa5cf7c6 Mon Sep 17 00:00:00 2001 From: "Marcel Kapfer (mmk2410)" Date: Sun, 17 Jul 2016 23:40:44 +0200 Subject: [PATCH 2/4] Removed obsolete file --- lib/common/utils.dart | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 lib/common/utils.dart diff --git a/lib/common/utils.dart b/lib/common/utils.dart deleted file mode 100644 index e69de29..0000000 From fc5c786b00d70e032b5c92f6ed6e82669594e852 Mon Sep 17 00:00:00 2001 From: "Marcel Kapfer (mmk2410)" Date: Sun, 17 Jul 2016 23:41:24 +0200 Subject: [PATCH 3/4] Implemented read json courses --- bin/titama.dart | 7 ++++++- lib/common/messages.dart | 11 +++++++++++ lib/server/titamaapi.dart | 30 ++++-------------------------- lib/server/titamaio.dart | 27 ++++++++++++++++++++++++++- 4 files changed, 47 insertions(+), 28 deletions(-) diff --git a/bin/titama.dart b/bin/titama.dart index 3785ded..56deb0d 100644 --- a/bin/titama.dart +++ b/bin/titama.dart @@ -6,6 +6,8 @@ import 'package:logging/logging.dart'; import 'package:rpc/rpc.dart'; import '../lib/server/titamaapi.dart'; +import '../lib/server/titamaio.dart'; +import '../lib/common/messages.dart'; final ApiServer _apiServer = new ApiServer(prettyPrint: true); @@ -14,7 +16,10 @@ main() async { ..level = Level.INFO ..onRecord.listen(print); - _apiServer.addApi(new TitamaApi()); + // read saved data + List courses = await new TitamaIo().readJson(); + + _apiServer.addApi(new TitamaApi(courses)); HttpServer server = await HttpServer.bind(InternetAddress.ANY_IP_V4, 8080); server.listen(_apiServer.httpRequestHandler); diff --git a/lib/common/messages.dart b/lib/common/messages.dart index 3a20ca9..0ba62cf 100644 --- a/lib/common/messages.dart +++ b/lib/common/messages.dart @@ -43,4 +43,15 @@ class Course { map["id"] = id; return map; } + + Course.fromJson(Map courseData) { + title = courseData["title"]; + time = courseData["time"]; + day = courseData["day"]; + kind = courseData["kind"]; + place = courseData["place"]; + prof = courseData["prof"]; + turnin = courseData["turnin"]; + id = courseData["id"]; + } } diff --git a/lib/server/titamaapi.dart b/lib/server/titamaapi.dart index 5dac42a..b2f522e 100644 --- a/lib/server/titamaapi.dart +++ b/lib/server/titamaapi.dart @@ -8,32 +8,11 @@ import './titamaio.dart'; @ApiClass(version: 'v1') class TitamaApi { - final List _courses = new List(); + List _courses = new List(); - TitamaApi() { - // example course, to remove once the database connection is implemented - Course course = new Course(); - course - ..title = "UlmAPI" - ..time = "18:00" - ..day = "Monday" - ..id = 0 - ..kind = "Lab" - ..place = "O27/343" - ..prof = "" - ..turnin = ""; - _courses.add(course); - Course course2 = new Course(); - course2 - ..title = "CCC Ulm" - ..time = "20:00" - ..day = "Monday" - ..id = 1 - ..kind = "Meeting" - ..place = "Cafe Einstein" - ..prof = "" - ..turnin = ""; - _courses.add(course2); + TitamaApi(List courses) { + // assign saved courses to list + _courses = courses; } @ApiMethod(path: 'courses') @@ -84,4 +63,3 @@ class TitamaApi { return course; } } - diff --git a/lib/server/titamaio.dart b/lib/server/titamaio.dart index 0dec2e3..5169b4c 100644 --- a/lib/server/titamaio.dart +++ b/lib/server/titamaio.dart @@ -2,6 +2,7 @@ library titama.io; import 'dart:io'; import 'dart:convert'; +import 'dart:async'; import '../common/messages.dart'; @@ -13,7 +14,7 @@ class TitamaIo { final _filename = "./data.json"; /** - * Write courses as JSON in a file.k + * Write courses as JSON in a file. * @param List _course List of courses to save. */ writeJson(List _courses) async { @@ -21,4 +22,28 @@ class TitamaIo { await new File(_filename).writeAsString(_json); } + /** + * Read a saved json file. + * @return Future> future with list of courses. + */ + Future> readJson() async { + List courses = new List(); + + File data = new File(_filename); + + if (await data.exists()) { + + String _content = await data.readAsString(); + Map parsed = JSON.decode(_content); + + + for (int i = 0; i < parsed.length; i++) { + courses.add(new Course.fromJson(parsed[i])); + } + + } + + return courses; + } + } From 5ade16fc3dc47678f03404aaf31e9f592e2fac55 Mon Sep 17 00:00:00 2001 From: "Marcel Kapfer (mmk2410)" Date: Mon, 18 Jul 2016 20:44:09 +0200 Subject: [PATCH 4/4] Added tests for JSON serialization --- pubspec.lock | 116 ++++++++++++++++++++++++++++++++++++++- pubspec.yaml | 1 + test/TitamaIo_test.dart | 101 ++++++++++++++++++++++++++++++++++ test/TitamaIo_test.dart~ | 101 ++++++++++++++++++++++++++++++++++ test/packages | 1 + 5 files changed, 319 insertions(+), 1 deletion(-) create mode 100644 test/TitamaIo_test.dart create mode 100644 test/TitamaIo_test.dart~ create mode 120000 test/packages diff --git a/pubspec.lock b/pubspec.lock index d3b1732..f91a456 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -7,6 +7,12 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "0.1.3+1" + analyzer: + description: + name: analyzer + url: "https://pub.dartlang.org" + source: hosted + version: "0.27.2" args: description: name: args @@ -19,6 +25,18 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "1.11.0" + barback: + description: + name: barback + url: "https://pub.dartlang.org" + source: hosted + version: "0.15.2+8" + boolean_selector: + description: + name: boolean_selector + url: "https://pub.dartlang.org" + source: hosted + version: "1.0.2" charcode: description: name: charcode @@ -43,6 +61,12 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "1.1.1" + csslib: + description: + name: csslib + url: "https://pub.dartlang.org" + source: hosted + version: "0.13.2" discoveryapis_generator: description: name: discoveryapis_generator @@ -55,6 +79,12 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "0.2.0+13" + glob: + description: + name: glob + url: "https://pub.dartlang.org" + source: hosted + version: "1.1.3" googleapis: description: name: googleapis @@ -67,12 +97,24 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "0.27.1" + html: + description: + name: html + url: "https://pub.dartlang.org" + source: hosted + version: "0.12.2+2" http: description: name: http url: "https://pub.dartlang.org" source: hosted version: "0.11.3+7" + http_multi_server: + description: + name: http_multi_server + url: "https://pub.dartlang.org" + source: hosted + version: "2.0.2" http_parser: description: name: http_parser @@ -97,12 +139,36 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "0.9.3" + package_config: + description: + name: package_config + url: "https://pub.dartlang.org" + source: hosted + version: "0.1.5" path: description: name: path url: "https://pub.dartlang.org" source: hosted version: "1.3.9" + plugin: + description: + name: plugin + url: "https://pub.dartlang.org" + source: hosted + version: "0.1.0" + pool: + description: + name: pool + url: "https://pub.dartlang.org" + source: hosted + version: "1.2.4" + pub_semver: + description: + name: pub_semver + url: "https://pub.dartlang.org" + source: hosted + version: "1.3.0" quiver: description: name: quiver @@ -115,6 +181,36 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "0.5.6+1" + shelf: + description: + name: shelf + url: "https://pub.dartlang.org" + source: hosted + version: "0.6.5+2" + shelf_static: + description: + name: shelf_static + url: "https://pub.dartlang.org" + source: hosted + version: "0.2.4" + shelf_web_socket: + description: + name: shelf_web_socket + url: "https://pub.dartlang.org" + source: hosted + version: "0.2.1" + source_map_stack_trace: + description: + name: source_map_stack_trace + url: "https://pub.dartlang.org" + source: hosted + version: "1.0.4" + source_maps: + description: + name: source_maps + url: "https://pub.dartlang.org" + source: hosted + version: "0.10.1+1" source_span: description: name: source_span @@ -139,6 +235,12 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "0.1.5" + test: + description: + name: test + url: "https://pub.dartlang.org" + source: hosted + version: "0.12.15+1" typed_data: description: name: typed_data @@ -157,10 +259,22 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "0.9.0+3" + watcher: + description: + name: watcher + url: "https://pub.dartlang.org" + source: hosted + version: "0.9.7+2" + web_socket_channel: + description: + name: web_socket_channel + url: "https://pub.dartlang.org" + source: hosted + version: "1.0.4" yaml: description: name: yaml url: "https://pub.dartlang.org" source: hosted version: "2.1.9" -sdk: ">=1.16.0 <2.0.0" +sdk: ">=1.16.0 <1.20.0" diff --git a/pubspec.yaml b/pubspec.yaml index d2db44f..3d63fe0 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -7,3 +7,4 @@ environment: dependencies: rpc: "^0.5.6+1" logging: "^0.11.3" + test: "^0.12.15+1" diff --git a/test/TitamaIo_test.dart b/test/TitamaIo_test.dart new file mode 100644 index 0000000..310197a --- /dev/null +++ b/test/TitamaIo_test.dart @@ -0,0 +1,101 @@ +import 'dart:io'; + +import 'package:test/test.dart'; + +import '../lib/common/messages.dart'; +import '../lib/server/titamaio.dart'; + +String jsonContent = '[{"title":"UlmApi","time":"18:00","day":"Monday","kind":"Lab","place":"Weinhof9","prof":"","turnin":"","id":0},{"title":"CCC Ulm","time":"20:00","day":"Monday","kind":"Meeting","place":"Cafe Einstein","prof":"","turnin":"","id":1}]'; + +void main() { + + group("JSON Serialization", () { + + test("writeJson() writes a course list as a JSON object to a file.", () async { + + List _courses = new List(); + + Course course = new Course(); + + course + ..title = "UlmApi" + ..time = "18:00" + ..day = "Monday" + ..id = 0 + ..kind = "Lab" + ..place = "Weinhof9" + ..prof = "" + ..turnin = ""; + + _courses.add(course); + + course = new Course(); + + course + ..title = "CCC Ulm" + ..time = "20:00" + ..day = "Monday" + ..id = 1 + ..kind = "Meeting" + ..place = "Cafe Einstein" + ..prof = "" + ..turnin = ""; + + _courses.add(course); + + TitamaIo io = new TitamaIo(); + + await io.writeJson(_courses); + + String content = await new File('data.json').readAsString(); + + expect(content, equals(jsonContent)); + + }); + + test("readJson() reads the JSON storage file and parses it", () async { + + final filename = "data.json"; + + List _courses = new List(); + + Course course = new Course(); + + course + ..title = "UlmApi" + ..time = "18:00" + ..day = "Monday" + ..id = 0 + ..kind = "Lab" + ..place = "Weinhof9" + ..prof = "" + ..turnin = ""; + + _courses.add(course); + + course = new Course(); + + course + ..title = "CCC Ulm" + ..time = "20:00" + ..day = "Monday" + ..id = 1 + ..kind = "Meeting" + ..place = "Cafe Einstein" + ..prof = "" + ..turnin = ""; + + _courses.add(course); + + await new File(filename).writeAsString(jsonContent); + + TitamaIo io = new TitamaIo(); + + List courses = await io.readJson(); + + expect(courses, equals(_courses)); + }); + + }); + +} diff --git a/test/TitamaIo_test.dart~ b/test/TitamaIo_test.dart~ new file mode 100644 index 0000000..310197a --- /dev/null +++ b/test/TitamaIo_test.dart~ @@ -0,0 +1,101 @@ +import 'dart:io'; + +import 'package:test/test.dart'; + +import '../lib/common/messages.dart'; +import '../lib/server/titamaio.dart'; + +String jsonContent = '[{"title":"UlmApi","time":"18:00","day":"Monday","kind":"Lab","place":"Weinhof9","prof":"","turnin":"","id":0},{"title":"CCC Ulm","time":"20:00","day":"Monday","kind":"Meeting","place":"Cafe Einstein","prof":"","turnin":"","id":1}]'; + +void main() { + + group("JSON Serialization", () { + + test("writeJson() writes a course list as a JSON object to a file.", () async { + + List _courses = new List(); + + Course course = new Course(); + + course + ..title = "UlmApi" + ..time = "18:00" + ..day = "Monday" + ..id = 0 + ..kind = "Lab" + ..place = "Weinhof9" + ..prof = "" + ..turnin = ""; + + _courses.add(course); + + course = new Course(); + + course + ..title = "CCC Ulm" + ..time = "20:00" + ..day = "Monday" + ..id = 1 + ..kind = "Meeting" + ..place = "Cafe Einstein" + ..prof = "" + ..turnin = ""; + + _courses.add(course); + + TitamaIo io = new TitamaIo(); + + await io.writeJson(_courses); + + String content = await new File('data.json').readAsString(); + + expect(content, equals(jsonContent)); + + }); + + test("readJson() reads the JSON storage file and parses it", () async { + + final filename = "data.json"; + + List _courses = new List(); + + Course course = new Course(); + + course + ..title = "UlmApi" + ..time = "18:00" + ..day = "Monday" + ..id = 0 + ..kind = "Lab" + ..place = "Weinhof9" + ..prof = "" + ..turnin = ""; + + _courses.add(course); + + course = new Course(); + + course + ..title = "CCC Ulm" + ..time = "20:00" + ..day = "Monday" + ..id = 1 + ..kind = "Meeting" + ..place = "Cafe Einstein" + ..prof = "" + ..turnin = ""; + + _courses.add(course); + + await new File(filename).writeAsString(jsonContent); + + TitamaIo io = new TitamaIo(); + + List courses = await io.readJson(); + + expect(courses, equals(_courses)); + }); + + }); + +} diff --git a/test/packages b/test/packages new file mode 120000 index 0000000..a16c405 --- /dev/null +++ b/test/packages @@ -0,0 +1 @@ +../packages \ No newline at end of file