blob: 3107b7f294e6cc13ef566de6c5ebbfdfe5d0a3b7 [file] [log] [blame]
Matteo Scandoloba678a92016-06-20 17:16:15 -07001/**
2 * © OpenCORD
3 *
4 * Visit http://guide.xosproject.org/devguide/addview/ for more information
5 *
6 * Created by teone on 6/22/16.
7 */
8
9(function () {
10 'use strict';
11 describe('The Services Encoder Service', () => {
12
13 var service, rootScope, ArchiveManagerSpy, toscaBase;
14
15 const toscaBaseDefault = {
16 topology_template: {
17 node_templates: {}
18 }
19 };
20
21 beforeEach(module('xos.serviceGrid'));
22 beforeEach(module('templates'));
23
24 beforeEach(inject(($rootScope, ArchiveManager, ServiceEncoder) => {
25 rootScope = $rootScope;
26 toscaBase = angular.copy(toscaBaseDefault);
27
28 ArchiveManagerSpy = ArchiveManager;
29 spyOn(ArchiveManagerSpy, 'createArchive');
30 spyOn(ArchiveManagerSpy, 'addFile');
31 service = ServiceEncoder;
32 }));
33
34 describe('the formatServiceProperties method', () => {
35
36 it('should return only the existing properties', (done) => {
37 service.formatServiceProperties({name: 'test', kind: 'vCPE'}, toscaBase)
38 .then(res => {
39 expect(res).toEqual({
40 topology_template:{
41 node_templates: {
42 'service#test': {
43 type: 'tosca.nodes.VSGService',
44 properties: {kind: 'vCPE'}
45 }
46 }
47 }
48 });
49 done();
50 });
51 rootScope.$apply();
52 });
53
54 it('should return all properties', (done) => {
55 service.formatServiceProperties({
56 name: 'test',
57 kind: 'vCPE',
58 view_url: 'view_url',
59 icon_url: 'icon_url',
60 private_key_fn: 'private_key_fn'
61 }, toscaBase)
62 .then(res => {
63 expect(res).toEqual({
64 topology_template:{
65 node_templates: {
66 'service#test': {
67 type: 'tosca.nodes.VSGService',
68 properties: {
69 kind: 'vCPE',
70 view_url: 'view_url',
71 icon_url: 'icon_url',
72 private_key_fn: 'private_key_fn'
73 }
74 }
75 }
76 }
77 });
78 done();
79 });
80 rootScope.$apply();
81 });
82
83 describe('when a public key is provided', () => {
84 it('should add public_key and artifacts properties', (done) => {
85
86 let expected = {
87 topology_template:{
88 node_templates: {
89 'service#test': {
90 type: 'tosca.nodes.VSGService',
91 properties: {
92 kind: 'vCPE',
93 public_key: '{ get_artifact: [ SELF, pubkey, LOCAL_FILE] }'
94 },
95 artifacts: {
96 pubkey: '/opt/xos/tosca/test/test_rsa.pub'
97 }
98 }
99 }
100 }
101 };
102
103 service.formatServiceProperties({
104 kind: 'vCPE',
105 public_key: 'pkey',
106 name: 'test'
107 }, toscaBase)
108 .then(res => {
109 expect(res).toEqual(expected);
110 done();
111 });
112 rootScope.$apply();
113 });
114
115 it('should add public_key file to the archive', (done) => {
116 service.formatServiceProperties({
117 kind: 'vCPE',
118 public_key: 'pkey',
119 name: 'test'
120 }, toscaBase)
121 .then(res => {
122 expect(ArchiveManagerSpy.addFile).toHaveBeenCalledWith('test_rsa.pub', 'pkey');
123 done();
124 });
125 rootScope.$apply();
126 });
127 });
128 });
129
130 describe('the getServiceRequirements method', () => {
131 let TenantSpy, ServiceSpy, tenantQueryPromise;
132 beforeEach(inject(function(Tenants, Services, $q){
133
134 tenantQueryPromise= $q.defer();
135
136 TenantSpy = Tenants;
137 spyOn(TenantSpy, 'query').and.callFake(function(){
138 return {$promise: tenantQueryPromise.promise};
139 });
140
141 ServiceSpy = Services;
142 spyOn(ServiceSpy, 'get').and.callFake(function(p){
143 let d = $q.defer();
144 d.resolve({name: `deps_${p.id}`});
145 return {$promise: d.promise};
146 });
147 }));
148
149 it('should call the tenants service with correct params', () => {
150 service.getServiceRequirements({id: 1});
151 expect(TenantSpy.query).toHaveBeenCalledWith({subscriber_service: 1});
152 });
153
154 it('should not add requirements if the current service has no dependency', (done) => {
155 service.getServiceRequirements({id: 1}, {})
156 .then(res => {
157 expect(res).toEqual({});
158 done();
159 });
160 tenantQueryPromise.resolve();
161 rootScope.$apply();
162 });
163
164 it('should return a list of required service', () => {
165 service.getServiceRequirements({id: 1, name: 'test'}, {topology_template: {node_templates: {'service#test': {}}}})
166 .then(res => {
167 expect(res.topology_template.node_templates['service#test'].requirements).toEqual([
168 {
169 deps_3_tenant: {
170 node: 'service#deps_3',
171 relationship: 'tosca.relationships.TenantOfService'
172 }
173 },
174 {
175 deps_4_tenant: {
176 node: 'service#deps_4',
177 relationship: 'tosca.relationships.TenantOfService'
178 }
179 }
180 ]);
181 });
182 tenantQueryPromise.resolve([
183 {
184 subscriber_service: 1,
185 provider_service: 3
186 },
187 {
188 subscriber_service: 1,
189 provider_service: 4
190 }
191 ]);
192 rootScope.$apply();
193 });
194
195 it('should return a list of unique required service', () => {
196 service.getServiceRequirements({id: 1, name: 'test'}, {topology_template: {node_templates: {'service#test': {}}}})
197 .then(res => {
198 expect(res.topology_template.node_templates['service#test'].requirements).toEqual([
199 {
200 deps_3_tenant: {
201 node: 'service#deps_3',
202 relationship: 'tosca.relationships.TenantOfService'
203 }
204 }
205 ]);
206 });
207 tenantQueryPromise.resolve([
208 {
209 subscriber_service: 1,
210 provider_service: 3
211 },
212 {
213 subscriber_service: 1,
214 provider_service: 3
215 }
216 ]);
217 rootScope.$apply();
218 });
219 });
220 });
221
222})();
223