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