Skip to content

Commit

Permalink
finish first draft
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuamshana committed Aug 31, 2019
1 parent fc1a447 commit 18cd6b4
Show file tree
Hide file tree
Showing 3 changed files with 264 additions and 30 deletions.
36 changes: 24 additions & 12 deletions lib/configuration.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,46 @@ 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<dynamic, dynamic> parseApiUrl(Map<String, dynamic> data) {
Map<dynamic, dynamic> 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;
}
}

Map<String, String> 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) {
Expand Down
116 changes: 98 additions & 18 deletions lib/controller/domain.dart
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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};
Expand All @@ -71,9 +115,22 @@ class DomainController implements DomainI {
}

@override
Future search(String name, Map<String, Object> options) {
// TODO: implement search
return null;
Future search(String name, Map<String, Object> 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
Expand All @@ -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"});
}
}
}
142 changes: 142 additions & 0 deletions test/bfast_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
// }
});

});

Expand Down

0 comments on commit 18cd6b4

Please sign in to comment.