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