blob: a8916e706353db67ad7afdd9fca196cd0161d1ea [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 Scandolo812aa5a2016-04-19 18:12:45 -070020xdescribe('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'; },
30 port: function () { return '80'; }
31 };
32 })
33 }));
34
Simon Hunte6720442015-01-15 12:21:06 -080035 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 Cole4fab8af2015-01-15 16:40:47 -080060 $httpBackend.whenGET(/.*/).respond(mockData);
Simon Hunte6720442015-01-15 12:21:06 -080061 spyOn($log, 'warn');
62
Bri Prebilic Cole4fab8af2015-01-15 16:40:47 -080063 rs.get('bar', function (data) {
Simon Hunte6720442015-01-15 12:21:06 -080064 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 Cole4fab8af2015-01-15 16:40:47 -080079 $httpBackend.whenGET(/.*/).respond(404, 'Not Found');
Simon Hunte6720442015-01-15 12:21:06 -080080 spyOn($log, 'warn');
81
Bri Prebilic Cole4fab8af2015-01-15 16:40:47 -080082 rs.get('bar', function (data) {
Simon Hunte6720442015-01-15 12:21:06 -080083 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 Cole4fab8af2015-01-15 16:40:47 -080092 expect($log.warn).toHaveBeenCalledWith(
Simon Huntaa26adf2015-01-20 10:32:49 -080093 'Failed to retrieve JSON data: http://foo:80/onos/ui/rs/bar',
94 404, 'Not Found');
Simon Hunte6720442015-01-15 12:21:06 -080095 });
96
97});