blob: 40825effb7361f7f7ee2e2418c3736cf9ee5b776 [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 Scandoloba678a92016-06-20 17:16:15 -070019(function () {
20 'use strict';
21 describe('The Tosca Encoder Service', () => {
22
23 var service, httpBackend, rootScope, ArchiveManagerSpy, toscaBase;
24
25 const serviceData = {
26 id: 1,
27 name: 'vsg',
28 kind: 'vCPE'
29 };
30
31 const toscaBaseDefault = {
32 topology_template: {
33 node_templates: {}
34 }
35 };
36
37 beforeEach(module('xos.serviceGrid'));
38 beforeEach(module('templates'));
39
40 beforeEach(inject(function($httpBackend, $rootScope, ToscaEncoder, ArchiveManager){
41
42 httpBackend = $httpBackend;
43 rootScope = $rootScope;
44 toscaBase = angular.copy(toscaBaseDefault);
45 service = ToscaEncoder;
46 ArchiveManagerSpy = ArchiveManager;
47 spyOn(ArchiveManagerSpy, 'createArchive');
48 spyOn(ArchiveManagerSpy, 'addFile');
49 spyOn(ArchiveManagerSpy, 'download');
50 }));
51
52 describe('the serviceToTosca method', () => {
53
54 const fakePropertiesDefault = {
55 tosca_definitions_version: 'tosca_simple_yaml_1_0',
56 description: 'Just enough Tosca to get the vSG slice running on the CORD POD',
57 imports: [
58 'custom_types/xos.yaml'
59 ],
60 topology_template:{
61 node_templates: {
62 'service#vsg': {
63 type: 'tosca.nodes.VSGService',
64 properties: {
65 view_url: 'viewUrl',
66 icon_url: 'iconUrl',
67 kind: 'vCPE'
68 }
69 }
70 }
71 }
72 };
73
74 const fakeRequirements = {
75 tosca_definitions_version: 'tosca_simple_yaml_1_0',
76 description: 'Just enough Tosca to get the vSG slice running on the CORD POD',
77 imports: [
78 'custom_types/xos.yaml'
79 ],
80 topology_template:{
81 node_templates: {
82 'service#vsg': {
83 type: 'tosca.nodes.VSGService',
84 properties: {
85 view_url: 'viewUrl',
86 icon_url: 'iconUrl',
87 kind: 'vCPE'
88 },
89 requirements: [
90 {
91 node: 'service#vrouter',
92 relationship: 'tosca.relationships.TenantOfService'
93 },
94 {
95 node: 'service#volt',
96 relationship: 'tosca.relationships.TenantOfService'
97 }
98 ]
99 }
100 }
101 }
102 };
103
104 const expectedWithoutRequirements = `tosca_definitions_version: tosca_simple_yaml_1_0
105description: Just enough Tosca to get the vSG slice running on the CORD POD
106imports:
107 - custom_types/xos.yaml
108topology_template:
109 node_templates:
110 service#vsg:
111 type: tosca.nodes.VSGService
112 properties:
113 view_url: viewUrl
114 icon_url: iconUrl
115 kind: vCPE
116`;
117
118 const expectedWithRequirements = `tosca_definitions_version: tosca_simple_yaml_1_0
119description: Just enough Tosca to get the vSG slice running on the CORD POD
120imports:
121 - custom_types/xos.yaml
122topology_template:
123 node_templates:
124 service#vsg:
125 type: tosca.nodes.VSGService
126 properties:
127 view_url: viewUrl
128 icon_url: iconUrl
129 kind: vCPE
130 requirements:
131 - node: service#vrouter
132 relationship: tosca.relationships.TenantOfService
133 - node: service#volt
134 relationship: tosca.relationships.TenantOfService
135`;
136
137 const expectedWithSlices = `tosca_definitions_version: tosca_simple_yaml_1_0
138description: Just enough Tosca to get the vSG slice running on the CORD POD
139imports:
140 - custom_types/xos.yaml
141topology_template:
142 node_templates:
143 service#vsg:
144 type: tosca.nodes.VSGService
145 properties:
146 view_url: viewUrl
147 icon_url: iconUrl
148 kind: vCPE
149 service_slice:
150 description: A service slice
151 type: tosca.nodes.Slice
152 properties:
153 network: noauto
154`;
155
156 let formatPromise, requirementPromise, slicesPromise, fakeProperties, serviceEncoderSpy, slicesEncoderSpy;
157
158 beforeEach(inject(($q, ServiceEncoder, SlicesEncoder) => {
159
160 serviceEncoderSpy = ServiceEncoder;
161 slicesEncoderSpy = SlicesEncoder;
162
163 // clone the base property for mock
164 fakeProperties = angular.copy(fakePropertiesDefault);
165
166 // create the promises
167 // this will be resolved in the single IT block,
168 // to allow different resolutions
169 formatPromise = $q.defer();
170 requirementPromise = $q.defer();
171 slicesPromise = $q.defer();
172
173 // mock functions and return promises
174 spyOn(serviceEncoderSpy, 'formatServiceProperties').and.callFake(function(){
175 return formatPromise.promise;
176 });
177 spyOn(serviceEncoderSpy, 'getServiceRequirements').and.callFake(function(){
178 return requirementPromise.promise;
179 });
180 spyOn(slicesEncoderSpy, 'getServiceSlices').and.callFake(function(){
181 return slicesPromise.promise;
182 });
183 }));
184
185 it('should create a new archive', () => {
186 service.serviceToTosca(serviceData);
187 expect(ArchiveManagerSpy.createArchive).toHaveBeenCalled();
188 });
189
190 it('should add the service file to the archive', (done) => {
191 service.serviceToTosca(serviceData)
192 .then(() => {
193 expect(ArchiveManagerSpy.addFile).toHaveBeenCalledWith('vsg_service.yaml', expectedWithoutRequirements);
194 expect(ArchiveManagerSpy.download).toHaveBeenCalledWith('vsg');
195 done();
196 });
197 formatPromise.resolve(fakeProperties);
198 requirementPromise.resolve(fakeProperties);
199 slicesPromise.resolve(fakeProperties);
200 rootScope.$apply();
201 });
202
203 // IS IT REALLY USEFULL TO TEST THE CONVERTION TO YAML?
204 xit('should create a tosca spec with no requirements', (done) => {
205 service.serviceToTosca(serviceData)
206 .then(res => {
207 expect(res).toEqual(expectedWithoutRequirements);
208 done();
209 });
210 formatPromise.resolve(fakeProperties);
211 requirementPromise.resolve(fakeProperties);
212 slicesPromise.resolve(fakeProperties);
213 rootScope.$apply();
214 });
215
216 xit('should create a tosca spec with requirements', (done) => {
217 service.serviceToTosca(serviceData)
218 .then(res => {
219 expect(res).toEqual(expectedWithRequirements);
220 done();
221 });
222 formatPromise.resolve(fakeProperties);
223 requirementPromise.resolve(fakeRequirements);
224 slicesPromise.resolve(fakeProperties);
225 rootScope.$apply();
226 });
227
228 xit('should create a tosca spec with additional slices', (done) => {
229
230 // this is dirty, we are changing an object and shouldn't be done in tests
231 angular.extend(
232 fakeProperties.topology_template.node_templates, {service_slice: {
233 description: 'A service slice',
234 type: 'tosca.nodes.Slice',
235 properties: {
236 network: 'noauto'
237 }
238 }});
239
240 service.serviceToTosca(serviceData)
241 .then(res => {
242 expect(res).toEqual(expectedWithSlices);
243 done();
244 });
245 formatPromise.resolve(fakeProperties);
246 requirementPromise.resolve(fakeProperties);
247 slicesPromise.resolve(fakeProperties);
248 rootScope.$apply();
249 });
250 });
251 });
252}());