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