blob: 406da0399578b9c49753a2b5e0657fbfc853fa99 [file] [log] [blame]
Simon Hunte6720442015-01-15 12:21:06 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Simon Hunte6720442015-01-15 12:21:06 -08003 *
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 Hunte6720442015-01-15 12:21:06 -080019 */
Matteo Scandolo231c7542016-05-20 11:13:11 -070020describe('factory: fw/remote/rest.js', function() {
Simon Huntfa875bb2016-08-24 11:41:02 -070021 var $log, $httpBackend, fs, rs;
Simon Hunte6720442015-01-15 12:21:06 -080022
23 beforeEach(module('onosUtil', 'onosRemote'));
24
Bri Prebilic Cole4fab8af2015-01-15 16:40:47 -080025 beforeEach(module(function($provide) {
Simon Huntfa875bb2016-08-24 11:41:02 -070026 $provide.factory('$location', function () {
Bri Prebilic Cole4fab8af2015-01-15 16:40:47 -080027 return {
28 protocol: function () { return 'http'; },
29 host: function () { return 'foo'; },
Matteo Scandolo231c7542016-05-20 11:13:11 -070030 port: function () { return '80'; },
31 search: function() {
32 return {debug: 'true'};
Simon Huntfa875bb2016-08-24 11:41:02 -070033 },
34 absUrl: function () {
35 return 'http://foo:123/onos/ui/rs/path';
Matteo Scandolo231c7542016-05-20 11:13:11 -070036 }
Bri Prebilic Cole4fab8af2015-01-15 16:40:47 -080037 };
Simon Huntfa875bb2016-08-24 11:41:02 -070038 });
Bri Prebilic Cole4fab8af2015-01-15 16:40:47 -080039 }));
40
Simon Hunte6720442015-01-15 12:21:06 -080041 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 Scandolo231c7542016-05-20 11:13:11 -070054 'get',
55 'post'
Simon Hunte6720442015-01-15 12:21:06 -080056 ])).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 Cole4fab8af2015-01-15 16:40:47 -080067 $httpBackend.whenGET(/.*/).respond(mockData);
Simon Hunte6720442015-01-15 12:21:06 -080068 spyOn($log, 'warn');
69
Bri Prebilic Cole4fab8af2015-01-15 16:40:47 -080070 rs.get('bar', function (data) {
Simon Hunte6720442015-01-15 12:21:06 -080071 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 Cole4fab8af2015-01-15 16:40:47 -080086 $httpBackend.whenGET(/.*/).respond(404, 'Not Found');
Simon Hunte6720442015-01-15 12:21:06 -080087 spyOn($log, 'warn');
88
Bri Prebilic Cole4fab8af2015-01-15 16:40:47 -080089 rs.get('bar', function (data) {
Simon Hunte6720442015-01-15 12:21:06 -080090 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 Cole4fab8af2015-01-15 16:40:47 -080099 expect($log.warn).toHaveBeenCalledWith(
Simon Huntaa26adf2015-01-20 10:32:49 -0800100 'Failed to retrieve JSON data: http://foo:80/onos/ui/rs/bar',
101 404, 'Not Found');
Simon Hunte6720442015-01-15 12:21:06 -0800102 });
103
104});