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 |
| 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 | }); |