1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
|
/*
============LICENSE_START==========================================
===================================================================
Copyright (C) 2018 AT&T Intellectual Property. All rights reserved.
===================================================================
Unless otherwise specified, all software contained herein is licensed
under the Apache License, Version 2.0 (the License);
you may not use this software except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
ECOMP is a trademark and service mark of AT&T Intellectual Property.
============LICENSE_END============================================
*/
import { async, ComponentFixture, TestBed,inject } from '@angular/core/testing';
import { By } from '@angular/platform-browser';
import { DebugElement } from '@angular/core';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { RouterTestingModule } from '@angular/router/testing';
import { SimpleNotificationsModule } from 'angular2-notifications';
import { Http, Response, Headers, RequestOptions, HttpModule } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import { Subscription } from 'rxjs/Subscription';
import 'rxjs/add/observable/from';
import 'rxjs/add/observable/empty';
import 'rxjs/add/observable/throw';
import { TestComponent } from './test.component';
import { NotificationService } from '../shared/services/notification.service';
import { ParamShareService } from '.././shared/services/paramShare.service';
import { MappingEditorService } from '../shared/services/mapping-editor.service';
import { HttpUtilService } from '../shared/services/httpUtil/http-util.service';
import { UtilityService } from '../shared/services/utilityService/utility.service';
import { environment } from '../.././environments/environment';
import { NgProgress } from 'ngx-progressbar';
import { NgProgressModule } from 'ngx-progressbar';
class MockService {
doStuff() {
return this;
}
}
describe( 'TestComponent', () => {
let component: TestComponent;
let fixture: ComponentFixture<TestComponent>;
let http = new MockService();
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [TestComponent],
imports: [
FormsModule,
RouterTestingModule,
SimpleNotificationsModule,
HttpModule,
NgProgressModule
],
providers: [
NotificationService,
ParamShareService,
MappingEditorService,
HttpUtilService,
UtilityService,
NgProgress,
{provide: Http, useValue: http}
]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(TestComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should ...', inject([HttpUtilService], (service: HttpUtilService) => {
let spy = spyOn(service, 'post').and.returnValue(Observable.empty());
fixture.detectChanges(); // onInit()
component.pollTestStatus();
expect(service).toBeTruthy();
expect(spy).toHaveBeenCalled();
expect(spy.calls.any()).toBe(true, 'test');
}));
// Test download Method
describe('Test download Method', () => {
it('Should have download method', () => {
expect(component.download).toBeDefined();
});
it('test if apiRequest if true', inject( [SimpleNotificationsModule], (service: SimpleNotificationsModule) => {
component.apiRequest = '{"input":{"common-header":{"timestamp":"2018-03-05T07:41:14.329Z","api-ver":"2.00","originator-id":"CDT","request-id":"1520235674330","sub-request-id":"1520235674330","flags":{"mode":"NORMAL","force":"TRUE","ttl":3600}},"action":"ConfigModify","action-identifiers":{"vnf-id":"testVnf","vserver-id":"test"},"payload":""';
component.apiResponse = '';
component.action = 'ConfigModify';
component.actionIdentifiers['vnf-id'] = 'testVnf';
let fileName = 'test_' + component.action + '_' + component.actionIdentifiers['vnf-id'] + '_request';
let theJSON = component.apiRequest;
var blob = new Blob([theJSON], {
type: 'text/json'
});
component.download();
expect(service instanceof SimpleNotificationsModule).toBeTruthy();
expect(component.apiRequest).not.toBe('');
expect(fileName).not.toBe('');
expect(fileName).not.toBeNull();
expect(fileName).toContain('test_');
expect(fileName).toContain('_request');
expect(typeof (blob)).toBe('object');
}));
it('test method if apiResponse is true', () => {
component.apiResponse = '{"input":{"common-header":{"timestamp":"2018-03-05T07:41:14.329Z","api-ver":"2.00","originator-id":"CDT","request-id":"1520235674330","sub-request-id":"1520235674330","flags":{"mode":"NORMAL","force":"TRUE","ttl":3600}},"action":"ConfigModify","action-identifiers":{"vnf-id":"testvnf","vserver-id":"test"},"payload":""';
component.apiRequest = '';
component.action = 'ConfigModify';
component.actionIdentifiers['vnf-id'] = 'testvnf';
let fileName = 'test_' + component.action + '_' + component.actionIdentifiers['vnf-id'] + '_response';
let theJSON = component.apiRequest;
var blob = new Blob([theJSON], {
type: 'text/json'
});
component.download();
expect(component.apiResponse).not.toBe('');
expect(fileName).not.toBe('');
expect(fileName).not.toBeNull();
expect(fileName).toContain('test_');
expect(fileName).toContain('_response');
expect(typeof (blob)).toBe('object');
});
});
// Test abortTest Method
describe('Test abortTest Method', () => {
it('Should have abortTest method', () => {
expect(component.abortTest).toBeDefined();
});
it('Test abortTest Method', () => {
const temp = component.abortTest();
expect(component.enableBrowse).toBeTruthy();
expect(component.enableTestButton).toBeTruthy();
expect(component.enablePollButton).toBeTruthy();
});
});
// Test excelBrowseOption Method
describe('Test excelBrowseOption Method', () => {
it('test excelBrowseOption', () => {
spyOn(component, 'excelBrowseOption');
let button = fixture.debugElement.query(By.css('#excelInputFile ~ button.browse'));
button.nativeElement.click();
expect(component.excelBrowseOption).toHaveBeenCalled()
});
});
// Test Upload Method
describe('Test Upload Method', () => {
it('Should have upload method', () => {
expect(component.upload).toBeDefined();
});
it('should execute if file extension is XLS, XLSX', () => {
let fileExtension = 'XLS';
let event = { isTrusted: true, type: "change", target: {files: [{name:'foo.XLS', size: 500001}]} }
let reader = new FileReader();
spyOn(reader, 'onload');
component.upload(event);
expect(reader instanceof FileReader).toBeTruthy();
expect(reader.onload).toHaveBeenCalled();
expect(component.enableTestButton).toBeTruthy();
console.log('aaa',component.enableTestButton, component.uploadFileName);
});
it('should return an error if file extension is not XLS, XLSX', () => {
spyOn(component, 'upload');
let ele = fixture.debugElement.query(By.css('#filesparam'));
const target = { files: ['']};
component.uploadFileName = 'test.doc';
let fileExtension = 'DOC';
component.upload('change');
expect(target.files.length).toEqual(1)
expect(fileExtension).not.toBe('XLS');
expect(fileExtension).not.toBe('XLSX');
});
});
// Test processUploadedFile Method
describe('Test processUploadedFile Method', () => {
it('should return valid payload', () => {
let data = [
{"TagName":"action","Value":"ConfigModify"},
{"List Name":"action-identifiers","TagName":"vserver-id","Value":"test"},
{"List Name":"payload","List Name_1":"request-parameters","TagName":"vnf-name","Value":"testVnf"},
{"List Name":"payload","List Name_1":"request-parameters","List Name_2":"[vm]","List Name_3":"vnfc","TagName":"vnfc-name","Value":"testVnfcName"},
{"List Name":"payload","List Name_1":"configuration-parameters","TagName":"testConfigParam","Value":"testConfigValue"}
]
let payload = component.processUploadedFile(data);
});
});
// Test uploadedFileResult Method
describe('Test uploadedFileResult', () => {
it('should return success message', inject([SimpleNotificationsModule],(service: SimpleNotificationsModule) => {
component.action = 'ConfigModify';
component.actionIdentifiers['vnf-id'] = 'testvnf';
component.uploadedFileResult();
expect(service instanceof SimpleNotificationsModule).toBeTruthy();
}));
it('should return error message', inject([SimpleNotificationsModule], (service: SimpleNotificationsModule) => {
component.action = '';
component.actionIdentifiers['vnf-id'] = '';
component.uploadedFileResult();
expect(service instanceof SimpleNotificationsModule).toBeTruthy();
}));
});
// Test constructTestPayload Method
describe('Test constructTestPayload Method', () => {
//3rd , 4th columnd, tag name , tag value
it('Should have constructTestPayload method', () => {
expect(component.constructTestPayload).toBeDefined();
});
it('test if listName2, listName3 are undefined', () => {
let temp = component.constructTestPayload(undefined, undefined, 'vnfc-type', 'testvnfc');
expect(component.subPayload['vnfc-type']).toEqual('testvnfc')
});
it('test if lastName2 is not undefined', () => {
let temp = component.constructTestPayload(['vm'], undefined, 'vnfc-type', 'testvnfc');
expect(typeof(component.vmJson)).toEqual('object');
expect(typeof(component.vnfcJson)).toEqual('object');
expect(component.vmJson['vnfc-type']).toBe('testvnfc');
expect(component.flag).toBe(0);
});
it('test if lastNmae2, lastName3 are not undefined', () => {
let temp = component.constructTestPayload(['vm'], 'vnfc', 'vnfc-type', 'testvnfc');
expect(component.vnfcJson['vnfc-type']).toEqual('testvnfc');
expect(component.vmJson['vnfc']['vnfc-type']).toEqual('testvnfc');
expect(component.flag).toBe(1);
})
});
// Test constructRequest Method
describe('Test constructRequest Method', () => {
it('Should have constructRequest method', () => {
expect(component.constructRequest).toBeDefined();
});
it('test method', () => {
let temp = component.constructRequest();
});
});
// Test testVnf Method
describe('Test testVnf Method', () => {
it('Should have testVnf method', () => {
expect(component.testVnf).toBeDefined();
});
it('should return response on success', inject([HttpUtilService, NgProgress], (httpUtilService: HttpUtilService, ngProgress: NgProgress) => {
let spy = spyOn(httpUtilService, 'post').and.callFake( ({}) => {
return Observable.empty();
});
component.action = 'ConfigModify';
component.testVnf();
expect(component.enableBrowse).toBeFalsy()
expect(component.enableTestButton).toBeFalsy();
expect(component.enablePollButton).toBeFalsy();
expect(spy).toHaveBeenCalled();
}));
it('should return an error if fails', inject([HttpUtilService],( httpUtilService: HttpUtilService) => {
let error = 'Error in connecting to APPC Server';
let spy = spyOn(httpUtilService, 'post').and.returnValue(Observable.throw(error));
component.action = 'ConfigModify';
component.testVnf();
expect(spy).toHaveBeenCalled();
expect(component.enableBrowse).toBeTruthy();
expect(component.enableTestButton).toBeTruthy();
expect(component.enablePollButton).toBeTruthy();
expect(component.enableCounterDiv).toBeFalsy();
}));
it('test setTimeout', inject([NgProgress], (ngProgress: NgProgress) => {
let spy = spyOn(ngProgress, 'done');
component.action = 'ConfigModify';
component.testVnf();
}));
});
// Test pollTestStatus Method
describe('Test pollTestStatus Method', () => {
it('Should have pollTestStatus method', () => {
expect(component.pollTestStatus).toBeDefined();
});
it('test method', () => {
let temp = component.pollTestStatus();
let requestId = new Date().getTime().toString();
let actionIdentifiers = 123456;
});
it('should call fake server', inject([HttpUtilService], (httpUtilService: HttpUtilService) => {
let spy = spyOn(httpUtilService, 'post').and.callFake(({}) => {
return Observable.empty();
});
component.pollTestStatus();
expect(spy).toHaveBeenCalled();
}));
it('should return an error if fails', inject([HttpUtilService], (httpUtilService: HttpUtilService) => {
let error = 'Error Connecting to APPC server';
let spy = spyOn(httpUtilService, 'post').and.callFake( ({}) => {
return Observable.throw(error);
});
component.requestId = null;
component.actionIdentifiers['vnf-id'] = false;
component.pollTestStatus();
expect(spy).toHaveBeenCalled();
}));
});
// Test getUrlEndPoint Method
describe('Test getUrlEndPoint Method', () => {
it('Should have getUrlEndPoint method', () => {
expect(component.getUrlEndPoint).toBeDefined();
});
it('getUrlEndPoint Should return value', () => {
expect(component.getUrlEndPoint('configmodify')).toEqual('config-modify');
expect(component.getUrlEndPoint('configbackup')).toEqual('config-backup');
expect(component.getUrlEndPoint('configrestore')).toEqual('config-restore');
expect(component.getUrlEndPoint('healthcheck')).toEqual('health-check');
expect(component.getUrlEndPoint('quiescetraffic')).toEqual('quiesce-traffic');
expect(component.getUrlEndPoint('resumetraffic')).toEqual('resume-traffic');
expect(component.getUrlEndPoint('startapplication')).toEqual('start-application');
expect(component.getUrlEndPoint('stopapplication')).toEqual('stop-application');
expect(component.getUrlEndPoint('upgradebackout')).toEqual('upgrade-backout');
expect(component.getUrlEndPoint('upgradepostcheck')).toEqual('upgrade-post-check');
expect(component.getUrlEndPoint('upgradeprecheck')).toEqual('upgrade-pre-check');
expect(component.getUrlEndPoint('upgradesoftware')).toEqual('upgrade-software');
expect(component.getUrlEndPoint('DeFaultCASE')).toEqual('defaultcase');
});
});
})
|