Stone, Avi (as206k) | 548c5a2 | 2018-06-03 13:12:12 +0300 | [diff] [blame^] | 1 | import { HttpClientTestingModule } from '@angular/common/http/testing'; |
| 2 | import { TestBed, async, inject } from '@angular/core/testing'; |
| 3 | import { |
| 4 | BaseRequestOptions, |
| 5 | Http, |
| 6 | HttpModule, |
| 7 | Response, |
| 8 | ResponseOptions, |
| 9 | XHRBackend |
| 10 | } from '@angular/http'; |
| 11 | import { MockBackend } from '@angular/http/testing'; |
Stone, Avi (as206k) | 9b2ceb3 | 2018-04-12 16:36:39 +0300 | [diff] [blame] | 12 | import { v4 as genrateUuid } from 'uuid'; |
Stone, Avi (as206k) | 548c5a2 | 2018-06-03 13:12:12 +0300 | [diff] [blame^] | 13 | import { Store } from '../store/store'; |
| 14 | import { RestApiService } from './rest-api.service'; |
Stone, Avi (as206k) | 9b2ceb3 | 2018-04-12 16:36:39 +0300 | [diff] [blame] | 15 | |
| 16 | describe('RestApiService', () => { |
Stone, Avi (as206k) | 548c5a2 | 2018-06-03 13:12:12 +0300 | [diff] [blame^] | 17 | let service: RestApiService; |
| 18 | let backend: MockBackend; |
| 19 | |
| 20 | beforeEach( |
| 21 | async(() => { |
| 22 | TestBed.configureTestingModule({ |
| 23 | imports: [HttpModule, HttpClientTestingModule], |
| 24 | providers: [ |
| 25 | RestApiService, |
| 26 | Store, |
| 27 | MockBackend, |
| 28 | BaseRequestOptions, |
| 29 | { |
| 30 | provide: Http, |
| 31 | deps: [MockBackend, BaseRequestOptions], |
| 32 | useFactory: ( |
| 33 | backend: XHRBackend, |
| 34 | defaultOptions: BaseRequestOptions |
| 35 | ) => { |
| 36 | return new Http(backend, defaultOptions); |
| 37 | } |
| 38 | } |
| 39 | ] |
| 40 | }); |
| 41 | // Get the MockBackend |
| 42 | backend = TestBed.get(MockBackend); |
| 43 | service = TestBed.get(RestApiService); |
| 44 | }) |
| 45 | ); |
Stone, Avi (as206k) | 9b2ceb3 | 2018-04-12 16:36:39 +0300 | [diff] [blame] | 46 | |
| 47 | it( |
| 48 | 'should be created', |
Stone, Avi (as206k) | 548c5a2 | 2018-06-03 13:12:12 +0300 | [diff] [blame^] | 49 | inject([RestApiService], () => { |
Stone, Avi (as206k) | 9b2ceb3 | 2018-04-12 16:36:39 +0300 | [diff] [blame] | 50 | expect(service).toBeTruthy(); |
| 51 | }) |
| 52 | ); |
| 53 | |
Stone, Avi (as206k) | 548c5a2 | 2018-06-03 13:12:12 +0300 | [diff] [blame^] | 54 | it('should baseUrl match localhost', () => { |
| 55 | expect(service.baseUrl).toBe('http://localhost:8446'); |
| 56 | }); |
| 57 | |
| 58 | it('should headers user id get default', () => { |
| 59 | service.addHeaders(); |
| 60 | expect(service.headers.get('USER_ID')).toBe('ym903w'); |
| 61 | }); |
| 62 | |
| 63 | it('should headers Content-Type json', () => { |
| 64 | service.addHeaders(); |
| 65 | expect(service.headers.get('Content-Type')).toBe('application/json'); |
| 66 | }); |
| 67 | |
| 68 | it( |
| 69 | 'should get service instance from API', |
| 70 | async(() => { |
| 71 | const serviceInstances = [ |
| 72 | { |
| 73 | name: 'ciService669277f472b0', |
| 74 | category: 'Mobility' |
| 75 | } |
| 76 | ]; |
| 77 | |
| 78 | backend.connections.subscribe(connection => { |
| 79 | connection.mockRespond( |
| 80 | new Response( |
| 81 | new ResponseOptions({ |
| 82 | body: JSON.stringify(serviceInstances) |
| 83 | }) |
| 84 | ) |
| 85 | ); |
| 86 | }); |
| 87 | |
| 88 | service.getServiceInstances('123456').subscribe(_res => { |
| 89 | expect(_res.length).toBe(1); |
| 90 | expect(_res).toEqual(serviceInstances); |
| 91 | }); |
| 92 | }) |
| 93 | ); |
| 94 | |
| 95 | it( |
| 96 | 'should get template resources from API', |
| 97 | async(() => { |
| 98 | const template = [ |
| 99 | { |
| 100 | name: 'AviStone1234', |
| 101 | version: '0.1' |
| 102 | } |
| 103 | ]; |
| 104 | |
| 105 | backend.connections.subscribe(connection => { |
| 106 | connection.mockRespond( |
| 107 | new Response( |
| 108 | new ResponseOptions({ |
| 109 | body: JSON.stringify(template) |
| 110 | }) |
| 111 | ) |
| 112 | ); |
| 113 | }); |
| 114 | |
| 115 | service.getTemplateResources().subscribe(_res => { |
| 116 | expect(_res.length).toBe(1); |
| 117 | expect(_res).toEqual(template); |
| 118 | }); |
| 119 | }) |
| 120 | ); |
| 121 | |
| 122 | it( |
| 123 | 'should getCompositionMonitoringComponent from API', |
| 124 | async(() => { |
| 125 | const template = [ |
| 126 | { |
| 127 | name: 'AviStone1234', |
| 128 | version: '0.1' |
| 129 | } |
| 130 | ]; |
| 131 | |
| 132 | backend.connections.subscribe(connection => { |
| 133 | connection.mockRespond( |
| 134 | new Response( |
| 135 | new ResponseOptions({ |
| 136 | body: JSON.stringify(template) |
| 137 | }) |
| 138 | ) |
| 139 | ); |
| 140 | }); |
| 141 | |
| 142 | service.getCompositionMonitoringComponent('123456').subscribe(_res => { |
| 143 | expect(_res.length).toBe(1); |
| 144 | expect(_res).toEqual(template); |
| 145 | }); |
| 146 | }) |
| 147 | ); |
| 148 | |
| 149 | it( |
| 150 | 'importVFCMT from API', |
| 151 | async(() => { |
| 152 | const template = [ |
| 153 | { |
| 154 | name: 'AviStone1234', |
| 155 | version: '0.1' |
| 156 | } |
| 157 | ]; |
| 158 | |
| 159 | backend.connections.subscribe(connection => { |
| 160 | connection.mockRespond( |
| 161 | new Response( |
| 162 | new ResponseOptions({ |
| 163 | body: JSON.stringify(template) |
| 164 | }) |
| 165 | ) |
| 166 | ); |
| 167 | }); |
| 168 | |
| 169 | service.importVFCMT({}).subscribe(_res => { |
| 170 | expect(_res.length).toBe(1); |
| 171 | expect(_res).toEqual(template); |
| 172 | }); |
| 173 | }) |
| 174 | ); |
| 175 | |
| 176 | it( |
| 177 | 'deleteMonitoringComponent from API', |
| 178 | async(() => { |
| 179 | const template = [ |
| 180 | { |
| 181 | name: 'AviStone1234', |
| 182 | version: '0.1' |
| 183 | } |
| 184 | ]; |
| 185 | |
| 186 | backend.connections.subscribe(connection => { |
| 187 | connection.mockRespond( |
| 188 | new Response( |
| 189 | new ResponseOptions({ |
| 190 | body: JSON.stringify(template) |
| 191 | }) |
| 192 | ) |
| 193 | ); |
| 194 | }); |
| 195 | |
| 196 | service |
| 197 | .deleteMonitoringComponent( |
| 198 | { |
| 199 | contextType: 'service', |
| 200 | uuid: '123456' |
| 201 | }, |
| 202 | '45678', |
| 203 | 'liav' |
| 204 | ) |
| 205 | .subscribe(_res => { |
| 206 | console.log('delete', _res); |
| 207 | }); |
| 208 | }) |
| 209 | ); |
| 210 | |
| 211 | it( |
| 212 | 'deleteMonitoringComponentWithBlueprint from API', |
| 213 | async(() => { |
| 214 | const template = [ |
| 215 | { |
| 216 | name: 'AviStone1234', |
| 217 | version: '0.1' |
| 218 | } |
| 219 | ]; |
| 220 | |
| 221 | backend.connections.subscribe(connection => { |
| 222 | connection.mockRespond( |
| 223 | new Response( |
| 224 | new ResponseOptions({ |
| 225 | body: JSON.stringify(template) |
| 226 | }) |
| 227 | ) |
| 228 | ); |
| 229 | }); |
| 230 | |
| 231 | service |
| 232 | .deleteMonitoringComponentWithBlueprint( |
| 233 | { |
| 234 | contextType: 'service', |
| 235 | uuid: '123456' |
| 236 | }, |
| 237 | 'voskComp', |
| 238 | '45678', |
| 239 | 'liav' |
| 240 | ) |
| 241 | .subscribe(_res => { |
| 242 | console.log('delete', _res); |
| 243 | }); |
| 244 | }) |
| 245 | ); |
| 246 | |
| 247 | it( |
| 248 | 'createNewVFCMT from API', |
| 249 | async(() => { |
| 250 | const template = [ |
| 251 | { |
| 252 | name: 'AviStone1234', |
| 253 | version: '0.1' |
| 254 | } |
| 255 | ]; |
| 256 | |
| 257 | backend.connections.subscribe(connection => { |
| 258 | connection.mockRespond( |
| 259 | new Response( |
| 260 | new ResponseOptions({ |
| 261 | body: JSON.stringify(template) |
| 262 | }) |
| 263 | ) |
| 264 | ); |
| 265 | }); |
| 266 | |
| 267 | service.createNewVFCMT({}).subscribe(_res => { |
| 268 | expect(_res.length).toBe(1); |
| 269 | expect(_res).toEqual(template); |
| 270 | }); |
| 271 | }) |
| 272 | ); |
| 273 | |
| 274 | it( |
| 275 | 'saveMonitoringComponent from API', |
| 276 | async(() => { |
| 277 | const template = [ |
| 278 | { |
| 279 | name: 'AviStone1234', |
| 280 | version: '0.1' |
| 281 | } |
| 282 | ]; |
| 283 | |
| 284 | backend.connections.subscribe(connection => { |
| 285 | connection.mockRespond( |
| 286 | new Response( |
| 287 | new ResponseOptions({ |
| 288 | body: JSON.stringify(template) |
| 289 | }) |
| 290 | ) |
| 291 | ); |
| 292 | }); |
| 293 | |
| 294 | service |
| 295 | .saveMonitoringComponent({ |
| 296 | contextType: 'service', |
| 297 | serviceUuid: '123456', |
| 298 | vfiName: 'liavVfi', |
| 299 | vfcmtUuid: '987456', |
| 300 | cdump: {} |
| 301 | }) |
| 302 | .subscribe(_res => { |
| 303 | expect(_res.length).toBe(1); |
| 304 | expect(_res).toEqual(template); |
| 305 | }); |
| 306 | }) |
| 307 | ); |
| 308 | |
| 309 | it( |
| 310 | 'submitMonitoringComponent from API', |
| 311 | async(() => { |
| 312 | const template = [ |
| 313 | { |
| 314 | name: 'AviStone1234', |
| 315 | version: '0.1' |
| 316 | } |
| 317 | ]; |
| 318 | |
| 319 | backend.connections.subscribe(connection => { |
| 320 | connection.mockRespond( |
| 321 | new Response( |
| 322 | new ResponseOptions({ |
| 323 | body: JSON.stringify(template) |
| 324 | }) |
| 325 | ) |
| 326 | ); |
| 327 | }); |
| 328 | |
| 329 | service |
| 330 | .submitMonitoringComponent({ |
| 331 | contextType: 'service', |
| 332 | serviceUuid: '123456', |
| 333 | vfiName: 'liavVfi', |
| 334 | vfcmtUuid: '987456', |
| 335 | cdump: {}, |
| 336 | flowType: 'SNMP' |
| 337 | }) |
| 338 | .subscribe(_res => { |
| 339 | expect(_res.length).toBe(1); |
| 340 | expect(_res).toEqual(template); |
| 341 | }); |
| 342 | }) |
| 343 | ); |
| 344 | |
| 345 | it( |
| 346 | 'should get Vfcmt Reference Data from API', |
| 347 | async(() => { |
| 348 | const template = [ |
| 349 | { |
| 350 | name: 'AviStone1234', |
| 351 | version: '0.1' |
| 352 | } |
| 353 | ]; |
| 354 | |
| 355 | backend.connections.subscribe(connection => { |
| 356 | connection.mockRespond( |
| 357 | new Response( |
| 358 | new ResponseOptions({ |
| 359 | body: JSON.stringify(template) |
| 360 | }) |
| 361 | ) |
| 362 | ); |
| 363 | }); |
| 364 | |
| 365 | service.getVfcmtReferenceData('123456').subscribe(_res => { |
| 366 | expect(_res.length).toBe(1); |
| 367 | expect(_res).toEqual(template); |
| 368 | }); |
| 369 | }) |
| 370 | ); |
| 371 | |
| 372 | it( |
| 373 | 'should get vfcmt list from API', |
| 374 | async(() => { |
| 375 | const dummyVfcmts = [ |
| 376 | { |
| 377 | uuid: 'cba37ed8-94e1-406f-b4f5-b5edbc31ac85', |
| 378 | name: 'CIe4d5a9b271d6' |
| 379 | }, |
| 380 | { |
| 381 | uuid: '64471437-8feb-40d9-a8b0-9407a81dd5c0', |
| 382 | name: 'teSt.__.monitoring---TempLATE.6hnc' |
| 383 | } |
| 384 | ]; |
| 385 | |
| 386 | backend.connections.subscribe(connection => { |
| 387 | expect(connection.request.url).toMatch( |
| 388 | 'http://localhost:8446/service/123456/0.1/monitoringComponents' |
| 389 | ); |
| 390 | connection.mockRespond( |
| 391 | new Response( |
| 392 | new ResponseOptions({ |
| 393 | body: JSON.stringify(dummyVfcmts) |
| 394 | }) |
| 395 | ) |
| 396 | ); |
| 397 | }); |
| 398 | |
| 399 | service |
| 400 | .getMonitoringComponents({ |
| 401 | contextType: 'service', |
| 402 | uuid: '123456', |
| 403 | version: '0.1' |
| 404 | }) |
| 405 | .subscribe(_res => { |
| 406 | expect(_res.length).toBe(2); |
| 407 | expect(_res).toEqual(dummyVfcmts); |
| 408 | }); |
| 409 | }) |
| 410 | ); |
| 411 | |
| 412 | it( |
| 413 | 'should get migration vfcmt list from API', |
| 414 | async(() => { |
| 415 | const dummyVfcmts = [ |
| 416 | { |
| 417 | uuid: 'cba37ed8-94e1-406f-b4f5-b5edbc31ac85', |
| 418 | name: 'CIe4d5a9b271d6' |
| 419 | }, |
| 420 | { |
| 421 | uuid: '64471437-8feb-40d9-a8b0-9407a81dd5c0', |
| 422 | name: 'teSt.__.monitoring---TempLATE.6hnc' |
| 423 | } |
| 424 | ]; |
| 425 | |
| 426 | backend.connections.subscribe(connection => { |
| 427 | expect(connection.request.url).toMatch( |
| 428 | 'http://localhost:8446/service/123456/0.1/getVfcmtsForMigration' |
| 429 | ); |
| 430 | connection.mockRespond( |
| 431 | new Response( |
| 432 | new ResponseOptions({ |
| 433 | body: JSON.stringify(dummyVfcmts) |
| 434 | }) |
| 435 | ) |
| 436 | ); |
| 437 | }); |
| 438 | |
| 439 | service |
| 440 | .getVfcmtsForMigration({ |
| 441 | contextType: 'service', |
| 442 | uuid: '123456', |
| 443 | version: '0.1' |
| 444 | }) |
| 445 | .subscribe(_res => { |
| 446 | expect(_res.length).toBe(2); |
| 447 | expect(_res).toEqual(dummyVfcmts); |
| 448 | }); |
| 449 | }) |
| 450 | ); |
| 451 | |
| 452 | it( |
| 453 | 'should get flow type from API', |
| 454 | async(() => { |
| 455 | const flowType = ['syslog', 'SNMP']; |
| 456 | |
| 457 | backend.connections.subscribe(connection => { |
| 458 | expect(connection.request.url).toMatch( |
| 459 | 'http://localhost:8446/conf/composition' |
| 460 | ); |
| 461 | connection.mockRespond( |
| 462 | new Response( |
| 463 | new ResponseOptions({ |
| 464 | body: JSON.stringify(flowType) |
| 465 | }) |
| 466 | ) |
| 467 | ); |
| 468 | }); |
| 469 | |
| 470 | service.getFlowType().subscribe(_res => { |
| 471 | expect(_res.length).toBe(2); |
| 472 | expect(_res).toEqual(flowType); |
| 473 | }); |
| 474 | }) |
| 475 | ); |
| 476 | |
Stone, Avi (as206k) | 9b2ceb3 | 2018-04-12 16:36:39 +0300 | [diff] [blame] | 477 | it('should genrate deffrent uuid each time for request id', () => { |
| 478 | const firstUuid = genrateUuid(); |
| 479 | const secondUuid = genrateUuid(); |
| 480 | expect(firstUuid !== secondUuid).toBe(true); |
| 481 | }); |
| 482 | }); |