Simon Hunt | e672044 | 2015-01-15 12:21:06 -0800 | [diff] [blame] | 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 |
Simon Hunt | e672044 | 2015-01-15 12:21:06 -0800 | [diff] [blame] | 19 | */ |
| 20 | describe('factory: fw/remote/rest.js', function() { |
| 21 | var $log, $httpBackend, fs, rs, promise; |
| 22 | |
| 23 | beforeEach(module('onosUtil', 'onosRemote')); |
| 24 | |
Bri Prebilic Cole | 4fab8af | 2015-01-15 16:40:47 -0800 | [diff] [blame] | 25 | beforeEach(module(function($provide) { |
| 26 | $provide.factory('$location', function (){ |
| 27 | return { |
| 28 | protocol: function () { return 'http'; }, |
| 29 | host: function () { return 'foo'; }, |
| 30 | port: function () { return '80'; } |
| 31 | }; |
| 32 | }) |
| 33 | })); |
| 34 | |
Simon Hunt | e672044 | 2015-01-15 12:21:06 -0800 | [diff] [blame] | 35 | beforeEach(inject(function (_$log_, _$httpBackend_, FnService, RestService) { |
| 36 | $log = _$log_; |
| 37 | $httpBackend = _$httpBackend_; |
| 38 | fs = FnService; |
| 39 | rs = RestService; |
| 40 | })); |
| 41 | |
| 42 | it('should define RestService', function () { |
| 43 | expect(rs).toBeDefined(); |
| 44 | }); |
| 45 | |
| 46 | it('should define api functions', function () { |
| 47 | expect(fs.areFunctions(rs, [ |
| 48 | 'get' |
| 49 | ])).toBeTruthy(); |
| 50 | }); |
| 51 | |
| 52 | var mockData = { |
| 53 | id: 1, |
| 54 | prop: 'abc' |
| 55 | }; |
| 56 | |
| 57 | it('should fetch remote data', function () { |
| 58 | var called = 0, |
| 59 | capture = null; |
Bri Prebilic Cole | 4fab8af | 2015-01-15 16:40:47 -0800 | [diff] [blame] | 60 | $httpBackend.whenGET(/.*/).respond(mockData); |
Simon Hunt | e672044 | 2015-01-15 12:21:06 -0800 | [diff] [blame] | 61 | spyOn($log, 'warn'); |
| 62 | |
Bri Prebilic Cole | 4fab8af | 2015-01-15 16:40:47 -0800 | [diff] [blame] | 63 | rs.get('bar', function (data) { |
Simon Hunt | e672044 | 2015-01-15 12:21:06 -0800 | [diff] [blame] | 64 | called++; |
| 65 | capture = data; |
| 66 | }); |
| 67 | |
| 68 | expect(called).toEqual(0); |
| 69 | expect(capture).toBeNull(); |
| 70 | $httpBackend.flush(); |
| 71 | expect(called).toEqual(1); |
| 72 | expect(capture).toEqual(mockData); |
| 73 | expect($log.warn).not.toHaveBeenCalled(); |
| 74 | }); |
| 75 | |
| 76 | it('should fail to fetch remote data', function () { |
| 77 | var called = 0, |
| 78 | capture = null; |
Bri Prebilic Cole | 4fab8af | 2015-01-15 16:40:47 -0800 | [diff] [blame] | 79 | $httpBackend.whenGET(/.*/).respond(404, 'Not Found'); |
Simon Hunt | e672044 | 2015-01-15 12:21:06 -0800 | [diff] [blame] | 80 | spyOn($log, 'warn'); |
| 81 | |
Bri Prebilic Cole | 4fab8af | 2015-01-15 16:40:47 -0800 | [diff] [blame] | 82 | rs.get('bar', function (data) { |
Simon Hunt | e672044 | 2015-01-15 12:21:06 -0800 | [diff] [blame] | 83 | called++; |
| 84 | capture = data; |
| 85 | }); |
| 86 | |
| 87 | expect(called).toEqual(0); |
| 88 | expect(capture).toBeNull(); |
| 89 | $httpBackend.flush(); |
| 90 | expect(called).toEqual(0); |
| 91 | expect(capture).toBeNull(); |
Bri Prebilic Cole | 4fab8af | 2015-01-15 16:40:47 -0800 | [diff] [blame] | 92 | expect($log.warn).toHaveBeenCalledWith( |
Simon Hunt | aa26adf | 2015-01-20 10:32:49 -0800 | [diff] [blame] | 93 | 'Failed to retrieve JSON data: http://foo:80/onos/ui/rs/bar', |
| 94 | 404, 'Not Found'); |
Simon Hunt | e672044 | 2015-01-15 12:21:06 -0800 | [diff] [blame] | 95 | }); |
| 96 | |
| 97 | }); |