blob: 749250e570f778401b111accafe16c1892f5e026 [file] [log] [blame]
Matteo Scandolod2044a42017-08-07 16:08:28 -07001
2/*
3 * Copyright 2017-present Open Networking Foundation
4
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8
9 * http://www.apache.org/licenses/LICENSE-2.0
10
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18
Matteo Scandoloef969922016-08-11 13:49:12 -070019'use strict';
20
21describe('The ExploreCmd service', () => {
22
23 var ExploreCmd, ErrorHandler, ResponseHandler, done, shell, ResourceMock, rootScope;
24
25 beforeEach(module('xos.UITutorial'));
26 beforeEach(module('templates'));
27
28 beforeEach(() => {
29 module(function ($provide) {
30 $provide.value('Resource', ResourceMock);
31 })
32 });
33
34 beforeEach(inject(function (_ExploreCmd_, _ErrorHandler_, _ResponseHandler_, $rootScope) {
35 // The injector unwraps the underscores (_) from around the parameter names when matching
36 ExploreCmd = _ExploreCmd_;
37 ErrorHandler = _ErrorHandler_;
38 ResponseHandler = _ResponseHandler_;
39 rootScope = $rootScope;
40 done = jasmine.createSpy('done');
41 shell = {
42 setCommandHandler: jasmine.createSpy('setCommandHandler'),
43 bestMatch: jasmine.createSpy('bestMatch')
44 };
45 // binding the mock shell to the service (for easy testing)
46 ExploreCmd.shell = shell;
47 spyOn(console, 'error');
48
49 ResourceMock = {
50 query: jasmine.createSpy('query').and.callFake(() => {
51 var deferred = $q.defer();
52 deferred.resolve('Remote call result');
53 return {$promise: deferred.promise};
54 })
55 };
56 }));
57
58 it('should set the resouce command handler', () => {
59 ExploreCmd.setup(shell);
60 expect(shell.setCommandHandler).toHaveBeenCalledWith('resource', { exec: ExploreCmd.resourceExec, completion: ExploreCmd.resourceCompletion })
61 });
62
63 describe('the resourceCompletion function', () => {
64
65 beforeEach(() => {
66 spyOn(ExploreCmd, 'getAvailableResources').and.returnValue(['Sites', 'Slices']);
67 });
68
69 it('should suggest a resource list', () => {
70 ExploreCmd.resourceCompletion('resource', '', {text: 'resource'}, done)
71 expect(shell.bestMatch).toHaveBeenCalledWith('', [ 'list', 'Sites', 'Slices' ]);
72
73 ExploreCmd.resourceCompletion('resource', 'S', {text: 'resource S'}, done)
74 expect(shell.bestMatch).toHaveBeenCalledWith('S', [ 'list', 'Sites', 'Slices' ]);
75 });
76
77 it('should suggest a method list', () => {
78 ExploreCmd.resourceCompletion('resource', '', {text: 'resource Sites '}, done)
79 expect(shell.bestMatch).toHaveBeenCalledWith('', [ 'query', 'get', 'save', '$save', 'delete' ]);
80
81 ExploreCmd.resourceCompletion('resource', 'q', {text: 'resource Sites q'}, done)
82 expect(shell.bestMatch).toHaveBeenCalledWith('q', [ 'query', 'get', 'save', '$save', 'delete' ]);
83 });
84 });
85
86 describe('the resourceExec function', () => {
87
88 beforeEach(() => {
89 spyOn(ExploreCmd, 'listAvailableResources');
90 spyOn(ExploreCmd, 'consumeResource');
91 });
92
93 it('should list available resources', () => {
94 ExploreCmd.resourceExec('explore', ['list'], done);
95 expect(ExploreCmd.listAvailableResources).toHaveBeenCalledWith(done);
96 });
97
98 it('should use a resource', () => {
99 ExploreCmd.resourceExec('explore', ['Resource', 'query'], done);
100 expect(ExploreCmd.consumeResource).toHaveBeenCalledWith('Resource', 'query', [], done);
101 });
102 });
103
104 describe('the getAvailableResources function', () => {
105
106 beforeEach(() => {
107 spyOn(angular, 'module').and.returnValue({
108 _invokeQueue: [
109 ['$provide', 'service', ['Sites', ['$resource']]],
110 ['$provide', 'service', ['Slices', ['$q', '$resource']]],
111 ['$provide', 'factory', ['_', []]],
112 ['$provide', 'service', ['helper', ['Slices']]]
113 ]
114 });
115 });
116
117 it('should return a list of resources in the angular app', () => {
118 const resources = ExploreCmd.getAvailableResources();
119 expect(resources).toEqual(['Sites', 'Slices']);
120 });
121 });
122
123 describe('the listAvailableResources function', () => {
124 beforeEach(() => {
125 spyOn(ExploreCmd, 'getAvailableResources').and.returnValue(['Sites', 'Slices']);
126 });
127
128 it('should format resource in an html template', () => {
129 ExploreCmd.listAvailableResources(done);
130 expect(ExploreCmd.getAvailableResources).toHaveBeenCalled();
131 expect(done).toHaveBeenCalledWith(`Sites<br/>Slices<br/>`);
132 });
133 });
134
135 describe('the consumeResource function', () => {
136 beforeEach(() => {
137 spyOn(ExploreCmd, 'getAvailableResources').and.returnValue(['Resource', 'Fake']);
138 spyOn(ErrorHandler, 'print');
139 });
140
141 it('should notify that a resource does not exists', () => {
142 ExploreCmd.consumeResource('Test', null, null, done);
143 expect(ErrorHandler.print).toHaveBeenCalledWith(`Resource "Test" does not exists`, done)
144 });
145
146 it('should notify that a method does not exists', () => {
147 ExploreCmd.consumeResource('Resource', 'test', null, done);
148 expect(ErrorHandler.print).toHaveBeenCalledWith(`Method "test" not allowed`, done)
149 });
150
151 const methodsWithParams = ['get', '$save', 'delete'];
152 methodsWithParams.forEach(method => {
153 it(`should notify that the ${method} method require parameters`, () => {
154 ExploreCmd.consumeResource('Resource', method, [], done);
155 expect(ErrorHandler.print).toHaveBeenCalledWith(`Method "${method}" require parameters`, done)
156 });
157 });
158
159 it('should not accept id as parameter for the query method', () => {
160 ExploreCmd.consumeResource('Resource', 'query', ['{id:1}'], done);
161 expect(ErrorHandler.print).toHaveBeenCalledWith(`Is not possible to use "id" as filter in method "query", use "get" instead!`, done)
162 });
163
164 it('should notify the user in case of malformed parameters', () => {
165 ExploreCmd.consumeResource('Resource', 'query', ['{child: 3}'], done);
166 expect(ErrorHandler.print).toHaveBeenCalledWith(`Parameter is not valid, it shoudl be in the form of: <code>{id:1}</code>, with no spaces`, done)
167 pending();
168 });
169
170 describe('when called with the correct parameters', () => {
171
172 let deferred;
173 beforeEach(inject(($q) => {
174 spyOn(ResponseHandler, 'parse');
175
176 deferred = $q.defer();
177 ResourceMock = {
178 query: jasmine.createSpy('query').and.callFake(function(){
179 return {$promise: deferred.promise};
180 })
181 };
182 }));
183
184 it('should notify the user if an error occurred while loading the resource', () => {
185 ExploreCmd.consumeResource('Fake', 'query', [], done);
186 expect(console.error).toHaveBeenCalled();
187 expect(ErrorHandler.print).toHaveBeenCalledWith('Failed to inject resource "Fake"', done);
188 });
189
190 it('should call a resource and return the results', () => {
191 ExploreCmd.consumeResource('Resource', 'query', [], done);
192 deferred.resolve([]);
193 rootScope.$apply();
194 expect(ErrorHandler.print).not.toHaveBeenCalled()
195 expect(ResponseHandler.parse).toHaveBeenCalledWith([], 'Resource.query()', done);
196 });
197
198 it('should call a resource with parameters and return the results', () => {
199 ExploreCmd.consumeResource('Resource', 'query', ['{child:3}'], done);
200 deferred.resolve([]);
201 rootScope.$apply();
202 expect(ErrorHandler.print).not.toHaveBeenCalled()
203 expect(ResponseHandler.parse).toHaveBeenCalledWith([], 'Resource.query({"child":3})', done);
204 });
205
206 it('should call a resource and display a not found message', () => {
207 ExploreCmd.consumeResource('Resource', 'query', [], done);
208 deferred.reject({status: 404, data: {detail: 'Not Found.'}});
209 rootScope.$apply();
210 expect(ErrorHandler.print).toHaveBeenCalledWith('Resource with method "query" and parameters {} Not Found.', done)
211 expect(ResponseHandler.parse).not.toHaveBeenCalled();
212 });
213 });
214 });
215});