mapping-editor service- added test case
[appc/cdt.git] / src / app / shared / services / mapping-editor.service.spec.ts
1 /*
2 ============LICENSE_START==========================================
3 ===================================================================
4 Copyright (C) 2018 IBM Intellectual Property. All rights reserved.
5 ===================================================================
6
7 Unless otherwise specified, all software contained herein is licensed
8 under the Apache License, Version 2.0 (the License);
9 you may not use this software except in compliance with the License.
10 You may obtain a copy of the License at
11
12     http://www.apache.org/licenses/LICENSE-2.0
13
14 Unless required by applicable law or agreed to in writing, software
15 distributed under the License is distributed on an "AS IS" BASIS,
16 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 See the License for the specific language governing permissions and
18 limitations under the License.
19
20 ============LICENSE_END============================================
21 */
22
23 import { inject, TestBed, ComponentFixture } from '@angular/core/testing';
24 import { By, BrowserModule } from '@angular/platform-browser';
25 import { Component, OnInit, ViewChild, Input, NO_ERRORS_SCHEMA } from '@angular/core';
26 import { MappingEditorService } from './mapping-editor.service';
27 import { GoldenConfigurationComponent } from '../../vnfs/build-artifacts/template-holder/template-configuration/template-configuration.component';
28 import { ArtifactRequest } from '../../shared/models/index';
29 import { UtilityService } from '../../shared/services/utilityService/utility.service';
30 import { ActivatedRoute, Router } from "@angular/router";
31 import { NgxSpinnerService } from 'ngx-spinner';
32 import { saveAs } from "file-saver";
33 import { NotificationService } from '../../shared/services/notification.service';
34 import { HttpUtilService } from '../../shared/services/httpUtil/http-util.service';
35 import { NotificationsService } from "angular2-notifications"
36 import { ParamShareService } from '../../shared/services/paramShare.service';
37 import { DialogService } from "ng2-bootstrap-modal";
38 import { ConfirmComponent } from '../../shared/confirmModal/confirm.component';
39 import { BuildDesignComponent } from '../../vnfs/build-artifacts/build-artifacts.component';
40 import { ModalComponent } from 'ng2-bs3-modal/ng2-bs3-modal'
41 import { FormsModule } from '@angular/forms';
42 import { RouterTestingModule } from '@angular/router/testing';
43 import { HomeComponent } from '../../home/home/home.component';
44 import { LogoutComponent } from '../../shared/components/logout/logout.component';
45 import { HelpComponent } from '../../shared/components/help/help/help.component';
46 import { AboutUsComponent } from '../../about-us/aboutus.component';
47 import { TestComponent } from '../../test/test.component';
48 import { HttpModule } from '@angular/http';
49 import { AceEditorComponent } from 'ng2-ace-editor';
50 import { Ng2Bs3ModalModule } from 'ng2-bs3-modal/ng2-bs3-modal';
51 import { SimpleNotificationsModule } from 'angular2-notifications';
52 import { NgProgress } from 'ngx-progressbar';
53 import { BaseRequestOptions, Response, ResponseOptions, Http } from '@angular/http';
54 import { MockBackend, MockConnection } from '@angular/http/testing';
55
56
57 describe('MappingEditorService', () => {
58     let service;
59     let component: GoldenConfigurationComponent;
60     let fixture: ComponentFixture<GoldenConfigurationComponent>;
61     let httpUtil: HttpUtilService;
62     const routes = [
63         {
64             path: 'home',
65             component: HomeComponent
66         }, {
67             path: 'vnfs',
68             loadChildren: '../../../../vnfs/vnfs.module#VnfsModule'
69         }, {
70             path: 'test',
71             component: TestComponent
72         },
73         {
74             path: 'help',
75             component: HelpComponent
76         }, {
77             path: 'aboutUs',
78             component: AboutUsComponent
79         }, {
80             path: 'logout',
81             component: LogoutComponent
82         }, {
83             path: '',
84             redirectTo: '/home',
85             pathMatch: 'full'
86         }
87     ];
88     beforeEach(() => {
89         TestBed.configureTestingModule({
90             imports: [FormsModule, BrowserModule, RouterTestingModule.withRoutes(routes), HttpModule, Ng2Bs3ModalModule, SimpleNotificationsModule.forRoot()],
91             declarations: [GoldenConfigurationComponent, HomeComponent, TestComponent, HelpComponent, AboutUsComponent, LogoutComponent, AceEditorComponent],
92             providers: [MappingEditorService, BuildDesignComponent, NgProgress, ParamShareService, DialogService, NotificationService, NgxSpinnerService, MockBackend,
93                 BaseRequestOptions, UtilityService,
94                 {
95                     provide: Http,
96                     useFactory: (backend: MockBackend, defaultOptions: BaseRequestOptions) => {
97                         return new Http(backend, defaultOptions);
98                     },
99                     deps: [MockBackend, BaseRequestOptions],
100                 },
101                 HttpUtilService, MappingEditorService, NotificationsService],
102             schemas: [NO_ERRORS_SCHEMA]
103             // providers: [MappingEditorService]
104         });
105     });
106
107     beforeEach(() => {
108         service = new MappingEditorService();
109         service.editor = {};
110     })
111
112     it('should be created', inject([MappingEditorService], (service: MappingEditorService) => {
113         expect(service).toBeTruthy();
114     }));
115
116     it('should test setSelectedWord function', () => {
117         service.setSelectedWord('word');
118         expect(service.selectedWord).toBe('word');
119     });
120
121     it('should test getSelectedWord function', () => {
122         service.setSelectedWord('word');
123         expect(service.getSelectedWord()).toBe('word');
124     });
125
126     it('should test changeNav function', () => {
127         service.changeNav(null);
128         expect(service._navItem).toBe(null);
129         expect(service.referenceNameObjects).toBe(null);
130     });
131
132     it('should test changeNavAppData function', () => {
133         service.changeNavAppData(null);
134         expect(service._navItem).toBe(null);
135         expect(service.appDataObject).toBe(null);
136     });
137
138     it('should test changeNavDownloadData function', () => {
139         service.changeNavDownloadData(null);
140         expect(service._navItem).toBe(null);
141         expect(service.downloadDataObject).toBe(null);
142     });
143
144     it('should test saveLatestAction function', () => {
145         service.saveLatestAction(null);
146         expect(service.latestAction).toBe(null);
147     });
148
149     it('should test saveLatestIdentifier function', () => {
150         service.saveLatestIdentifier(null);
151         expect(service.identifier).toBe(null);
152     });
153
154     it('should test getParamContent function', () => {
155         let paramContetnt = service.getParamContent('{}');
156         expect(paramContetnt).toBe('{}');
157     });
158
159     it('should test saveTempAllData function', () => {
160         service.saveTempAllData(null);
161         expect(service.tempAllData).toBe(null);
162     });
163
164     it('should test navItem function', () => {
165         service.changeNavAppData(null);
166         let navItem = service.navItem();
167         expect(service._navItem).toBe(null);
168     });
169
170     it('should test checkToDataAdd function to return true', () => {
171         let toAdd = service.checkToDataAdd('><');
172         expect(toAdd).toBe(true);
173     });
174
175     it('should test setParamContent function', () => {
176         service.setParamContent('');
177         expect(service.paramContent).toBe('');
178     });
179
180     it('should test checkToDataAdd function to return false', () => {
181         let toAdd = service.checkToDataAdd('.');
182         expect(toAdd).toBe(false);
183     });
184
185     it('should test getStartBeforeAfterSelection function', () => {
186         let selection = { start: { column: 12 }, end: { column: 12 } };
187         let beforeCOunt = 2;
188         let afterCount = 4;
189
190         expect(service.getStartBeforeAfterSelection(selection, beforeCOunt, afterCount)).toEqual({ start: { column: 10 }, end: { column: 16 } });
191     });
192
193     it('should test autoAnnotateDataForParams function', () => {
194         fixture = TestBed.createComponent(GoldenConfigurationComponent);
195         component = fixture.componentInstance;
196         component.configMappingEditorContent = "<configuration xmlns=\"http://xml.juniper.net/xnm/1.1/xnm\" \n    xmlns:a=\"http://xml.juniper.net/junos/15.1X49/junos\" >\n            <version>15.1X49-D50.3</version>\n            <groups>\n                <name>node0</name>\n                <system>\n                   <tacplus-server>\n                        <name>${sync_auto-pop_name1}</name>\n                        <source-address>${sync_auto-pop_address1}</source-address>\n                    </tacplus-server>\n                    <tacplus-server>\n                        <name>${node0_tacplus_server_name2}</name>\n                        <source-address>${sync_auto-pop_address1}</source-address>\n                    </tacplus-server>\n                </system>         \n           </groups>\n    </configuration>";
197
198         localStorage["paramsContent"] = JSON.stringify({ "sync_auto-pop_name1": "10.0.1.34", "sync_auto-pop_address1": "", "node0_tacplus_server_name2": "192.34.45.5" });
199         service.initialise(component.templateeditor.getEditor(), component.configMappingEditorContent, component.modal);
200         service.autoAnnotateDataForParams();
201
202     });
203
204     it('should test checkApplied function', () => {
205         fixture = TestBed.createComponent(GoldenConfigurationComponent);
206         component = fixture.componentInstance;
207         component.configMappingEditorContent = "<configuration xmlns=\"http://xml.juniper.net/xnm/1.1/xnm\" \n    xmlns:a=\"http://xml.juniper.net/junos/15.1X49/junos\" >\n            <version>15.1X49-D50.3</version>\n            <groups>\n                <name>node0</name>\n                <system>\n                   <tacplus-server>\n                        <name>${sync_auto-pop_name1}</name>\n                        <source-address>${sync_auto-pop_address1}</source-address>\n                    </tacplus-server>\n                    <tacplus-server>\n                        <name>${node0_tacplus_server_name2}</name>\n                        <source-address>${sync_auto-pop_address1}</source-address>\n                    </tacplus-server>\n                </system>         \n           </groups>\n    </configuration>";
208         service.initialise(component.templateeditor.getEditor(), component.configMappingEditorContent, component.modal);
209         service.checkApplied({ start: { column: 12 }, end: { column: 12 } });
210     });
211
212     it('should test checkComments function', () => {
213         fixture = TestBed.createComponent(GoldenConfigurationComponent);
214         component = fixture.componentInstance;
215         component.configMappingEditorContent = "<configuration xmlns=\"http://xml.juniper.net/xnm/1.1/xnm\" \n    xmlns:a=\"http://xml.juniper.net/junos/15.1X49/junos\" >\n            <version>15.1X49-D50.3</version>\n            <groups>\n                <name>node0</name>\n                <system>\n                   <tacplus-server>\n                        <name>${sync_auto-pop_name1}</name>\n                        <source-address>${sync_auto-pop_address1}</source-address>\n                    </tacplus-server>\n                    <tacplus-server>\n                        <name>${node0_tacplus_server_name2}</name>\n                        <source-address>${sync_auto-pop_address1}</source-address>\n                    </tacplus-server>\n                </system>         \n           </groups>\n    </configuration>";
216         service.initialise(component.templateeditor.getEditor(), component.configMappingEditorContent, component.modal);
217         service.checkComments({ start: { column: 12 }, end: { column: 12 } });
218     });
219
220     it('should test checkDelimiters function', () => {
221         fixture = TestBed.createComponent(GoldenConfigurationComponent);
222         component = fixture.componentInstance;
223         component.configMappingEditorContent = "<configuration xmlns=\"http://xml.juniper.net/xnm/1.1/xnm\" \n    xmlns:a=\"http://xml.juniper.net/junos/15.1X49/junos\" >\n            <version>15.1X49-D50.3</version>\n            <groups>\n                <name>node0</name>\n                <system>\n                   <tacplus-server>\n                        <name>${sync_auto-pop_name1}</name>\n                        <source-address>${sync_auto-pop_address1}</source-address>\n                    </tacplus-server>\n                    <tacplus-server>\n                        <name>${node0_tacplus_server_name2}</name>\n                        <source-address>${sync_auto-pop_address1}</source-address>\n                    </tacplus-server>\n                </system>         \n           </groups>\n    </configuration>";
224         service.initialise(component.templateeditor.getEditor(), component.configMappingEditorContent, component.modal);
225         service.checkDelimiters({ start: { column: 12 }, end: { column: 12 } });
226     });
227
228     it('should test checkAppliedForNamesOnly function', () => {
229         fixture = TestBed.createComponent(GoldenConfigurationComponent);
230         component = fixture.componentInstance;
231         component.configMappingEditorContent = "<configuration xmlns=\"http://xml.juniper.net/xnm/1.1/xnm\" \n    xmlns:a=\"http://xml.juniper.net/junos/15.1X49/junos\" >\n            <version>15.1X49-D50.3</version>\n            <groups>\n                <name>node0</name>\n                <system>\n                   <tacplus-server>\n                        <name>${sync_auto-pop_name1}</name>\n                        <source-address>${sync_auto-pop_address1}</source-address>\n                    </tacplus-server>\n                    <tacplus-server>\n                        <name>${node0_tacplus_server_name2}</name>\n                        <source-address>${sync_auto-pop_address1}</source-address>\n                    </tacplus-server>\n                </system>         \n           </groups>\n    </configuration>";
232         service.initialise(component.templateeditor.getEditor(), component.configMappingEditorContent, component.modal);
233         service.checkAppliedForNamesOnly({ start: { column: 12 }, end: { column: 12 } });
234     });
235
236     it('should test checkToDataAddForJson function', () => {
237         fixture = TestBed.createComponent(GoldenConfigurationComponent);
238         component = fixture.componentInstance;
239         component.configMappingEditorContent = "<configuration xmlns=\"http://xml.juniper.net/xnm/1.1/xnm\" \n    xmlns:a=\"http://xml.juniper.net/junos/15.1X49/junos\" >\n            <version>15.1X49-D50.3</version>\n            <groups>\n                <name>node0</name>\n                <system>\n                   <tacplus-server>\n                        <name>${sync_auto-pop_name1}</name>\n                        <source-address>${sync_auto-pop_address1}</source-address>\n                    </tacplus-server>\n                    <tacplus-server>\n                        <name>${node0_tacplus_server_name2}</name>\n                        <source-address>${sync_auto-pop_address1}</source-address>\n                    </tacplus-server>\n                </system>         \n           </groups>\n    </configuration>";
240         service.initialise(component.templateeditor.getEditor(), component.configMappingEditorContent, component.modal);
241         expect(service.checkToDataAddForJson("dsjfds")).toEqual(true);
242     });
243
244
245     it('should test replaceNamesWithBlankValues function', () => {
246         fixture = TestBed.createComponent(GoldenConfigurationComponent);
247         component = fixture.componentInstance;
248         component.configMappingEditorContent = "<configuration xmlns=\"http://xml.juniper.net/xnm/1.1/xnm\" \n    xmlns:a=\"http://xml.juniper.net/junos/15.1X49/junos\" >\n            <version>15.1X49-D50.3</version>\n            <groups>\n                <name>node0</name>\n                <system>\n                   <tacplus-server>\n                        <name>${sync_auto-pop_name1}</name>\n                        <source-address>${sync_auto-pop_address1}</source-address>\n                    </tacplus-server>\n                    <tacplus-server>\n                        <name>${node0_tacplus_server_name2}</name>\n                        <source-address>${sync_auto-pop_address1}</source-address>\n                    </tacplus-server>\n                </system>         \n           </groups>\n    </configuration>";
249         service.initialise(component.templateeditor.getEditor(), component.configMappingEditorContent, component.modal);
250         service.replaceNamesWithBlankValues();
251     });
252
253
254     it('should test autoAnnotateTemplateForParam function', () => {
255         fixture = TestBed.createComponent(GoldenConfigurationComponent);
256         component = fixture.componentInstance;
257         component.configMappingEditorContent = "<configuration xmlns=\"http://xml.juniper.net/xnm/1.1/xnm\" \n    xmlns:a=\"http://xml.juniper.net/junos/15.1X49/junos\" >\n            <version>15.1X49-D50.3</version>\n            <groups>\n                <name>node0</name>\n                <system>\n                   <tacplus-server>\n                        <name>${sync_auto-pop_name1}</name>\n                        <source-address>${sync_auto-pop_address1}</source-address>\n                    </tacplus-server>\n                    <tacplus-server>\n                        <name>${node0_tacplus_server_name2}</name>\n                        <source-address>${sync_auto-pop_address1}</source-address>\n                    </tacplus-server>\n                </system>         \n           </groups>\n    </configuration>";
258         service.initialise(component.templateeditor.getEditor(), component.configMappingEditorContent, component.modal);
259         service.autoAnnotateTemplateForParam();
260     });
261
262     it('should test generateTemplate function', () => {
263         fixture = TestBed.createComponent(GoldenConfigurationComponent);
264         component = fixture.componentInstance;
265         component.configMappingEditorContent = "<configuration xmlns=\"http://xml.juniper.net/xnm/1.1/xnm\" \n    xmlns:a=\"http://xml.juniper.net/junos/15.1X49/junos\" >\n            <version>15.1X49-D50.3</version>\n            <groups>\n                <name>node0</name>\n                <system>\n                   <tacplus-server>\n                        <name>${sync_auto-pop_name1}</name>\n                        <source-address>${sync_auto-pop_address1}</source-address>\n                    </tacplus-server>\n                    <tacplus-server>\n                        <name>${node0_tacplus_server_name2}</name>\n                        <source-address>${sync_auto-pop_address1}</source-address>\n                    </tacplus-server>\n                </system>         \n           </groups>\n    </configuration>";
266         service.initialise(component.templateeditor.getEditor(), component.configMappingEditorContent, component.modal);
267         service.generateTemplate(component.templateeditor.getEditor());
268     });
269
270     it('should test generateParams function', () => {
271         fixture = TestBed.createComponent(GoldenConfigurationComponent);
272         component = fixture.componentInstance;
273         component.configMappingEditorContent = "<configuration xmlns=\"http://xml.juniper.net/xnm/1.1/xnm\" \n    xmlns:a=\"http://xml.juniper.net/junos/15.1X49/junos\" >\n            <version>15.1X49-D50.3</version>\n            <groups>\n                <name>node0</name>\n                <system>\n                   <tacplus-server>\n                        <name>${sync_auto-pop_name1}</name>\n                        <source-address>${sync_auto-pop_address1}</source-address>\n                    </tacplus-server>\n                    <tacplus-server>\n                        <name>${node0_tacplus_server_name2}</name>\n                        <source-address>${sync_auto-pop_address1}</source-address>\n                    </tacplus-server>\n                </system>         \n           </groups>\n    </configuration>";
274         service.initialise(component.templateeditor.getEditor(), component.configMappingEditorContent, component.modal);
275         service.generateParams(component.templateeditor.getEditor(), component.templateeditor.getEditor());
276     });
277
278     it('should test removeTheSelectedMarkers function', () => {
279         service.initialise(component.templateeditor.getEditor(), component.configMappingEditorContent, component.modal);
280         service.removeTheSelectedMarkers();
281     });
282
283
284     it('should test getsaveMarkers function', () => {
285         service.initialise(component.templateeditor.getEditor(), component.configMappingEditorContent, component.modal);
286         service.getsaveMarkers();
287     });
288
289     it('should test getTemplateMappingDataFromStore function', () => {
290         service.getTemplateMappingDataFromStore();
291     });
292
293     it('should test setTemplateMappingDataFromStore function', () => {
294         service.setTemplateMappingDataFromStore({ data: 'data' });
295     });
296
297     it('should test setReferenceList function', () => {
298         service.setReferenceList();
299     });
300
301     it('should test getReferenceList function', () => {
302         service.getReferenceList();
303     });
304
305     it('should test getKeysForValues function to return key value', ()=>{        
306         service.paramContent = '{"Value":"value","key":"key"}';
307         let value = service.getKeysForValues('value');
308         expect(value).toBe('Value');
309     });
310
311     it('should call refreshEditor() function', ()=> {
312         let spy = spyOn(MappingEditorService.prototype, 'refreshEditor');      
313         service.handlekeyCompletion();
314         expect(spy).toHaveBeenCalled();
315     });
316
317     it('should test setTemplateDataForStore function', ()=>{
318         service.setTemplateDataForStore('data');
319         let tmpDaa = service.getTemplateDataFromStore();
320         expect(service.storedTemplateData).toBe('data');
321     });
322 });