blob: cd859bafd3d6aa1667fe3669ce6ea268d99680c4 [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 Hunte6720442015-01-15 12:21:06 -080021 var $log, $httpBackend, fs, rs, promise;
22
23 beforeEach(module('onosUtil', 'onosRemote'));
24
Bri Prebilic Cole4fab8af2015-01-15 16:40:47 -080025 beforeEach(module(function($provide) {
26 $provide.factory('$location', function (){
27 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'};
33 }
Bri Prebilic Cole4fab8af2015-01-15 16:40:47 -080034 };
35 })
36 }));
37
Simon Hunte6720442015-01-15 12:21:06 -080038 beforeEach(inject(function (_$log_, _$httpBackend_, FnService, RestService) {
39 $log = _$log_;
40 $httpBackend = _$httpBackend_;
41 fs = FnService;
42 rs = RestService;
43 }));
44
45 it('should define RestService', function () {
46 expect(rs).toBeDefined();
47 });
48
49 it('should define api functions', function () {
50 expect(fs.areFunctions(rs, [
Matteo Scandolo231c7542016-05-20 11:13:11 -070051 'get',
52 'post'
Simon Hunte6720442015-01-15 12:21:06 -080053 ])).toBeTruthy();
54 });
55
56 var mockData = {
57 id: 1,
58 prop: 'abc'
59 };
60
61 it('should fetch remote data', function () {
62 var called = 0,
63 capture = null;
Bri Prebilic Cole4fab8af2015-01-15 16:40:47 -080064 $httpBackend.whenGET(/.*/).respond(mockData);
Simon Hunte6720442015-01-15 12:21:06 -080065 spyOn($log, 'warn');
66
Bri Prebilic Cole4fab8af2015-01-15 16:40:47 -080067 rs.get('bar', function (data) {
Simon Hunte6720442015-01-15 12:21:06 -080068 called++;
69 capture = data;
70 });
71
72 expect(called).toEqual(0);
73 expect(capture).toBeNull();
74 $httpBackend.flush();
75 expect(called).toEqual(1);
76 expect(capture).toEqual(mockData);
77 expect($log.warn).not.toHaveBeenCalled();
78 });
79
80 it('should fail to fetch remote data', function () {
81 var called = 0,
82 capture = null;
Bri Prebilic Cole4fab8af2015-01-15 16:40:47 -080083 $httpBackend.whenGET(/.*/).respond(404, 'Not Found');
Simon Hunte6720442015-01-15 12:21:06 -080084 spyOn($log, 'warn');
85
Bri Prebilic Cole4fab8af2015-01-15 16:40:47 -080086 rs.get('bar', function (data) {
Simon Hunte6720442015-01-15 12:21:06 -080087 called++;
88 capture = data;
89 });
90
91 expect(called).toEqual(0);
92 expect(capture).toBeNull();
93 $httpBackend.flush();
94 expect(called).toEqual(0);
95 expect(capture).toBeNull();
Bri Prebilic Cole4fab8af2015-01-15 16:40:47 -080096 expect($log.warn).toHaveBeenCalledWith(
Simon Huntaa26adf2015-01-20 10:32:49 -080097 'Failed to retrieve JSON data: http://foo:80/onos/ui/rs/bar',
98 404, 'Not Found');
Simon Hunte6720442015-01-15 12:21:06 -080099 });
100
101});