From 18cd6b45cc51977f3a63c1eb7b148e6ad0cee248 Mon Sep 17 00:00:00 2001 From: Joshua Date: Sat, 31 Aug 2019 12:58:48 +0300 Subject: [PATCH] finish first draft --- lib/configuration.dart | 36 ++++++---- lib/controller/domain.dart | 116 +++++++++++++++++++++++++----- test/bfast_test.dart | 142 +++++++++++++++++++++++++++++++++++++ 3 files changed, 264 insertions(+), 30 deletions(-) diff --git a/lib/configuration.dart b/lib/configuration.dart index f934958..b666581 100644 --- a/lib/configuration.dart +++ b/lib/configuration.dart @@ -6,15 +6,22 @@ class Config { static String apiKey; static http.Client client; - - getApiUrl(String domain) { - return '${Config.serverUrl}/ide/api/$domain'; + getApiUrl(String domain, {Map params}) { + if(params!=null){ + var paramsString = '?'; + params.forEach((key, value){ + paramsString = paramsString + '$key=$value&'; + }); + return '${Config.serverUrl}/ide/api/$domain$paramsString'; + }else{ + return '${Config.serverUrl}/ide/api/$domain'; + } } - Map parseApiUrl(Map data) { + Map parseApiUrl(String data) { if (data != null) { - var stringData = jsonEncode(data); - stringData = stringData.replaceAll(new RegExp(r'http://ide:3000'), '${Config.serverUrl}/ide/api'); + var stringData = data.replaceAll( + new RegExp(r'http://ide:3000'), '${Config.serverUrl}/ide/api'); return jsonDecode(stringData); } else { return null; @@ -22,18 +29,23 @@ class Config { } Map getHeaders() { - return { - 'Content-Type': 'application/json', - 'X-Api-Key': Config.apiKey - }; + return {'Content-Type': 'application/json', 'X-Api-Key': Config.apiKey}; } String getFaasApi() { return '${Config.serverUrl}/ide/faas'; } - String getSearchApi(String domain, String queryName) { - return '${this.getApiUrl(domain)}/search/$queryName'; + String getSearchApi(String domain, String queryName, {Map params}) { + if(params!=null){ + var paramsString = '?'; + params.forEach((key, value){ + paramsString = paramsString + '$key=$value&'; + }); + return '${this.getApiUrl(domain)}/search/$queryName$paramsString'; + }else{ + return '${this.getApiUrl(domain)}/search/$queryName'; + } } String getFunctionApi(String name) { diff --git a/lib/controller/domain.dart b/lib/controller/domain.dart index 9c9e560..2a1a3e1 100644 --- a/lib/controller/domain.dart +++ b/lib/controller/domain.dart @@ -1,6 +1,7 @@ +import 'dart:convert'; + import 'package:bfast/configuration.dart'; import 'package:bfast/core/domain.dart'; -import 'dart:convert'; class DomainController implements DomainI { String _domainName; @@ -31,27 +32,71 @@ class DomainController implements DomainI { } else { throw Exception({"message": '${results.reasonPhrase}'}); } - }else{ + } else { throw Exception({"message": "Please provide object id or link"}); } } @override - Future many({Map options}) { - // TODO: implement many - return null; + Future many({Map options}) async { + var headers = this._config.getHeaders(); + var results = await Config.client.get( + this._config.getApiUrl(this._domainName, + params: options != null ? options : Map()), + headers: headers); + if (results.statusCode.toString().startsWith('2')) { + var resultObj = this._config.parseApiUrl(results.body); + return { + this._domainName: resultObj['_embedded'][this._domainName], + 'links': resultObj['_links'], + 'page': resultObj['page'] + }; + } else { + throw Exception(results.body); + } } @override - Future navigate(String link) { - // TODO: implement navigate - return null; + Future navigate(String link) async { + var headers = this._config.getHeaders(); + var results = await Config.client.get(link, headers: headers); + if (results.statusCode.toString().startsWith('2')) { + var resultObj = this._config.parseApiUrl(results.body); + return { + this._domainName: resultObj['_embedded'][this._domainName], + 'links': resultObj['_links'] != null ? resultObj['_links'] : {}, + 'page': resultObj['page'] != null ? resultObj['page'] : {} + }; + } else { + throw Exception(results.body); + } } @override - Future one({String link, String id}) { - // TODO: implement one - return null; + Future one({String link, String id}) async { + var headers = this._config.getHeaders(); + if (link != null) { + var results = await Config.client.get(link, headers: headers); + if (results.statusCode.toString().startsWith('2')) { + var resultObj = this._config.parseApiUrl(results.body); + return {this._domainName: resultObj}; + } else { + throw Exception({"message": '${results.reasonPhrase}'}); + } + } else if (id != null) { + var results = await Config.client.get( + '${this._config.getApiUrl(this._domainName)}/$id', + headers: headers); + if (results.statusCode.toString().startsWith('2')) { + var resultObj = this._config.parseApiUrl(results.body); + return {this._domainName: resultObj}; + } else { + throw Exception({"message": '${results.reasonPhrase}'}); + } + } else { + throw Exception( + {"message": "Please provide ${this._domainName} objectId or link"}); + } } @override @@ -61,7 +106,6 @@ class DomainController implements DomainI { this._config.getApiUrl(this._domainName), headers: headers, body: jsonEncode(this._model)); -// print(results.statusCode.toString().startsWith('2')); this._model = Map(); if (results.statusCode.toString().startsWith('2')) { return {"message": 'Object created', this._domainName: results.body}; @@ -71,9 +115,22 @@ class DomainController implements DomainI { } @override - Future search(String name, Map options) { - // TODO: implement search - return null; + Future search(String name, Map options) async { + var headers = this._config.getHeaders(); + var results = await Config.client.get( + this._config.getSearchApi(this._domainName, name, params: options), + headers: headers); + if (results.statusCode.toString().startsWith('2')) { + var resultObj = this._config.parseApiUrl(results.body); + // print(resultObj); + return { + this._domainName: resultObj['_embedded']!=null?resultObj['_embedded'][this._domainName]:resultObj, + 'links': resultObj['_links'] != null ? resultObj['_links'] : {}, + 'page': resultObj['page'] != null ? resultObj['page'] : {} + }; + } else { + throw Exception(results.body); + } } @override @@ -89,8 +146,31 @@ class DomainController implements DomainI { } @override - Future update({String link, String id}) { - // TODO: implement update - return null; + Future update({String link, String id}) async { + var headers = this._config.getHeaders(); + if (link != null) { + var results = await Config.client + .patch(link, headers: headers, body: jsonEncode(this._model)); + this._model = Map(); + if (results.statusCode.toString().startsWith('2')) { + return {"message": "${this._domainName} object update"}; + } else { + throw Exception({"message": '${results.reasonPhrase}'}); + } + } else if (id != null) { + var results = await Config.client.patch( + '${this._config.getApiUrl(this._domainName)}/$id', + headers: headers, + body: jsonEncode(this._model)); + this._model = Map(); + if (results.statusCode.toString().startsWith('2')) { + return {"message": "${this._domainName} object update"}; + } else { + throw Exception({"message": '${results.reasonPhrase}'}); + } + } else { + throw Exception( + {"message": "Please provide ${this._domainName} objectId or link"}); + } } } diff --git a/test/bfast_test.dart b/test/bfast_test.dart index 8740cb0..266c9b5 100644 --- a/test/bfast_test.dart +++ b/test/bfast_test.dart @@ -44,8 +44,150 @@ void main() { expect(r['message'], 'Object deleted'); }); + test("should throw exception when delete a domain", () async { + final client = MockClient(); + final bfast = new BFast(); + bfast.int(serverUrl: client.mockAPi, httpClient: client, apiKey: ''); + + when(client.delete( + '${client.mockAPi}/ide/api/tests/5d6912d19470450007f48717', + headers: anyNamed('headers'))) + .thenAnswer( + (_) async => http.Response('{"message": "Object deleted"}', 404)); + + try { + await bfast.domain('tests').delete(id: '5d6912d19470450007f48717'); + } catch (e) { + expect(e, isException); + } + }); + + test('should get many objects of a domain', () async { + final client = MockClient(); + final bfast = new BFast(); + bfast.int(serverUrl: client.mockAPi, httpClient: client, apiKey: ''); + + when(client.get(argThat(startsWith('${client.mockAPi}/ide/api/tests')), + headers: anyNamed('headers'))) + .thenAnswer((_) async => http.Response( + '' + '{' + '"_embedded": {' + '"tests": []},' + '"page": {},' + '"_links": {}}', + 200)); + + //try { + var r = await bfast.domain('tests').many(); + // print(r); + expect(r['tests'], isList); + // } catch (e) { + // print(e) + // expect(e, isException); + // } + }); + + test('should get one objects of a domain', () async { + final client = MockClient(); + final bfast = new BFast(); + bfast.int(serverUrl: client.mockAPi, httpClient: client, apiKey: ''); + + when(client.get( + argThat(startsWith( + '${client.mockAPi}/ide/api/tests/5d690ba79470450007f4870d')), + headers: anyNamed('headers'))) + .thenAnswer((_) async => http.Response( + '{' + '"name": "Joshua",' + '"_links": {} }', + 200)); + + //try { + var r = await bfast.domain('tests').one(id: '5d690ba79470450007f4870d'); + // print(r); + expect(r['tests']['name'], "Joshua"); + // } catch (e) { + // print(e) + // expect(e, isException); + // } + }); + + test('should update one objects of a domain', () async { + final client = MockClient(); + final bfast = new BFast(); + bfast.int(serverUrl: client.mockAPi, httpClient: client, apiKey: ''); + + when( + client.patch( + argThat(startsWith( + '${client.mockAPi}/ide/api/tests/5d690ba79470450007f4870d')), + headers: anyNamed('headers'), + body: jsonEncode({"name": "Ethan"}))).thenAnswer( + (_) async => http.Response('{"message":"tests object update"}', 200)); + + //try { + var r = await bfast + .domain('tests') + .set("name", "Ethan") + .update(id: '5d690ba79470450007f4870d'); + // print(r); + expect(r['message'], "tests object update"); + // } catch (e) { + // print(e) + // expect(e, isException); + // } + }); + test('should navigate to next objects of a domain', () async { + final client = MockClient(); + final bfast = new BFast(); + bfast.int(serverUrl: client.mockAPi, httpClient: client, apiKey: ''); + + when(client.get(argThat(startsWith('${client.mockAPi}/ide/api/tests?')), + headers: anyNamed('headers'))) + .thenAnswer((_) async => http.Response( + '{' + '"_embedded": {' + '"tests": []},' + '"page": {},' + '"_links": {}}', + 200)); + + //try { + var r = await bfast.domain('tests').navigate('${client.mockAPi}/ide/api/tests?page=1&size=1'); + // print(r); + expect(r['tests'], isList); + // } catch (e) { + // print(e) + // expect(e, isException); + // } + }); + test('should search a domain by using predifined query', () async { + final client = MockClient(); + final bfast = new BFast(); + bfast.int(serverUrl: client.mockAPi, httpClient: client, apiKey: ''); + + when(client.get(argThat(startsWith('${client.mockAPi}/ide/api/tests/search/')), + headers: anyNamed('headers'))) + .thenAnswer((_) async => http.Response( + '{' + '"_embedded": {' + '"tests": []},' + '"page": {},' + '"_links": {}}', + 200)); + + //try { + var r = await bfast.domain('tests').search('findAllBySupplierContainingIgnoreCase', {"supplier":""}); + // print(r); + expect(r['tests'], isList); + // } catch (e) { + // print(e) + // expect(e, isException); + // } + }); });