GUI -- Created the RestService in the Remote module.
- implemented get() function (with unit tests). Change-Id: I65edb5422ed3719d8bdc262e398673e0a1db3f0b
Showing
4 changed files
with
167 additions
and
0 deletions
1 | +/* | ||
2 | + * Copyright 2015 Open Networking Laboratory | ||
3 | + * | ||
4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | ||
5 | + * you may not use this file except in compliance with the License. | ||
6 | + * You may obtain a copy of the License at | ||
7 | + * | ||
8 | + * http://www.apache.org/licenses/LICENSE-2.0 | ||
9 | + * | ||
10 | + * Unless required by applicable law or agreed to in writing, software | ||
11 | + * distributed under the License is distributed on an "AS IS" BASIS, | ||
12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
13 | + * See the License for the specific language governing permissions and | ||
14 | + * limitations under the License. | ||
15 | + */ | ||
16 | + | ||
17 | +/* | ||
18 | + ONOS GUI -- Remote Communications Module | ||
19 | + | ||
20 | + @author Bri Prebilic Cole | ||
21 | + @author Simon Hunt | ||
22 | + */ | ||
23 | +(function () { | ||
24 | + 'use strict'; | ||
25 | + | ||
26 | + angular.module('onosRemote', ['onosUtil']); | ||
27 | + | ||
28 | +}()); |
1 | +/* | ||
2 | + * Copyright 2015 Open Networking Laboratory | ||
3 | + * | ||
4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | ||
5 | + * you may not use this file except in compliance with the License. | ||
6 | + * You may obtain a copy of the License at | ||
7 | + * | ||
8 | + * http://www.apache.org/licenses/LICENSE-2.0 | ||
9 | + * | ||
10 | + * Unless required by applicable law or agreed to in writing, software | ||
11 | + * distributed under the License is distributed on an "AS IS" BASIS, | ||
12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
13 | + * See the License for the specific language governing permissions and | ||
14 | + * limitations under the License. | ||
15 | + */ | ||
16 | + | ||
17 | +/* | ||
18 | + ONOS GUI -- Remote Communications Module -- REST Service | ||
19 | + | ||
20 | + @author Bri Prebilic Cole | ||
21 | + @author Simon Hunt | ||
22 | + */ | ||
23 | +(function () { | ||
24 | + 'use strict'; | ||
25 | + | ||
26 | + var $log; | ||
27 | + | ||
28 | + angular.module('onosRemote') | ||
29 | + .factory('RestService', ['$log', '$http', function (_$log_, $http) { | ||
30 | + $log = _$log_; | ||
31 | + | ||
32 | + function get(url, callback) { | ||
33 | + $http.get(url).then(function (response) { | ||
34 | + // success | ||
35 | + callback(response.data); | ||
36 | + }, function (response) { | ||
37 | + // error | ||
38 | + $log.warn('Failed to retrieve JSON data: ' + url, | ||
39 | + response.status, response.data); | ||
40 | + }); | ||
41 | + } | ||
42 | + | ||
43 | + return { | ||
44 | + get: get | ||
45 | + }; | ||
46 | + }]); | ||
47 | + | ||
48 | +}()); |
1 | +/* | ||
2 | + * Copyright 2015 Open Networking Laboratory | ||
3 | + * | ||
4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | ||
5 | + * you may not use this file except in compliance with the License. | ||
6 | + * You may obtain a copy of the License at | ||
7 | + * | ||
8 | + * http://www.apache.org/licenses/LICENSE-2.0 | ||
9 | + * | ||
10 | + * Unless required by applicable law or agreed to in writing, software | ||
11 | + * distributed under the License is distributed on an "AS IS" BASIS, | ||
12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
13 | + * See the License for the specific language governing permissions and | ||
14 | + * limitations under the License. | ||
15 | + */ | ||
16 | + | ||
17 | +/* | ||
18 | + ONOS GUI -- Remote -- REST Service - Unit Tests | ||
19 | + | ||
20 | + @author Bri Prebilic Cole | ||
21 | + @author Simon Hunt | ||
22 | + */ | ||
23 | +describe('factory: fw/remote/rest.js', function() { | ||
24 | + var $log, $httpBackend, fs, rs, promise; | ||
25 | + | ||
26 | + beforeEach(module('onosUtil', 'onosRemote')); | ||
27 | + | ||
28 | + beforeEach(inject(function (_$log_, _$httpBackend_, FnService, RestService) { | ||
29 | + $log = _$log_; | ||
30 | + $httpBackend = _$httpBackend_; | ||
31 | + fs = FnService; | ||
32 | + rs = RestService; | ||
33 | + })); | ||
34 | + | ||
35 | + it('should define RestService', function () { | ||
36 | + expect(rs).toBeDefined(); | ||
37 | + }); | ||
38 | + | ||
39 | + it('should define api functions', function () { | ||
40 | + expect(fs.areFunctions(rs, [ | ||
41 | + 'get' | ||
42 | + ])).toBeTruthy(); | ||
43 | + }); | ||
44 | + | ||
45 | + var mockData = { | ||
46 | + id: 1, | ||
47 | + prop: 'abc' | ||
48 | + }; | ||
49 | + | ||
50 | + it('should fetch remote data', function () { | ||
51 | + var called = 0, | ||
52 | + capture = null; | ||
53 | + $httpBackend.expectGET('/bar').respond(mockData); | ||
54 | + spyOn($log, 'warn'); | ||
55 | + | ||
56 | + rs.get('/bar', function (data) { | ||
57 | + called++; | ||
58 | + capture = data; | ||
59 | + }); | ||
60 | + | ||
61 | + expect(called).toEqual(0); | ||
62 | + expect(capture).toBeNull(); | ||
63 | + $httpBackend.flush(); | ||
64 | + expect(called).toEqual(1); | ||
65 | + expect(capture).toEqual(mockData); | ||
66 | + expect($log.warn).not.toHaveBeenCalled(); | ||
67 | + }); | ||
68 | + | ||
69 | + it('should fail to fetch remote data', function () { | ||
70 | + var called = 0, | ||
71 | + capture = null; | ||
72 | + $httpBackend.expectGET('/bar').respond(404, 'Not Found'); | ||
73 | + spyOn($log, 'warn'); | ||
74 | + | ||
75 | + rs.get('/bar', function (data) { | ||
76 | + called++; | ||
77 | + capture = data; | ||
78 | + }); | ||
79 | + | ||
80 | + expect(called).toEqual(0); | ||
81 | + expect(capture).toBeNull(); | ||
82 | + $httpBackend.flush(); | ||
83 | + expect(called).toEqual(0); | ||
84 | + expect(capture).toBeNull(); | ||
85 | + expect($log.warn) | ||
86 | + .toHaveBeenCalledWith('Failed to retrieve JSON data: /bar', | ||
87 | + 404, 'Not Found'); | ||
88 | + }); | ||
89 | + | ||
90 | +}); |
... | @@ -29,6 +29,7 @@ module.exports = function(config) { | ... | @@ -29,6 +29,7 @@ module.exports = function(config) { |
29 | '../app/directives.js', | 29 | '../app/directives.js', |
30 | '../app/fw/util/util.js', | 30 | '../app/fw/util/util.js', |
31 | '../app/fw/svg/svg.js', | 31 | '../app/fw/svg/svg.js', |
32 | + '../app/fw/remote/remote.js', | ||
32 | // now load services etc. that augment the modules | 33 | // now load services etc. that augment the modules |
33 | '../app/**/*.js', | 34 | '../app/**/*.js', |
34 | 35 | ... | ... |
-
Please register or login to post a comment