Catalog alignment
[sdc.git] / catalog-ui / src / app / ng2 / pages / composition / panel / panel-tabs / artifacts-tab / artifact-tab.component.spec.ts
1 import { async, ComponentFixture } from '@angular/core/testing';
2 import { ConfigureFn, configureTests } from '../../../../../../../jest/test-config.helper';
3 import { NgxsModule, Store } from '@ngxs/store';
4 import { WorkspaceState } from '../../../../../store/states/workspace.state';
5 import { NO_ERRORS_SCHEMA } from '@angular/core';
6 import { ArtifactsTabComponent } from './artifacts-tab.component';
7 import { CompositionService } from '../../../composition.service';
8 import { WorkspaceService } from '../../../../workspace/workspace.service';
9 import { ComponentInstanceServiceNg2 } from '../../../../../services/component-instance-services/component-instance.service';
10 import { TopologyTemplateService } from '../../../../../services/component-services/topology-template.service';
11 import { ArtifactsService } from '../../../../../components/forms/artifacts-form/artifacts.service';
12 import { ArtifactModel } from '../../../../../../models/artifacts';
13 import { ArtifactType } from '../../../../../../utils/constants';
14 import { FullComponentInstance } from '../../../../../../models/componentsInstances/fullComponentInstance';
15 import { ComponentInstance } from '../../../../../../models/componentsInstances/componentInstance';
16 import { Component } from '../../../../../../models/components/component';
17 import { GetInstanceArtifactsByTypeAction } from '../../../../../store/actions/instance-artifacts.actions';
18 import { Observable } from 'rxjs';
19
20
21 describe('artifact-tab component', () => {
22
23     let fixture: ComponentFixture<ArtifactsTabComponent>;
24     let compositionMockService: Partial<CompositionService>;
25     const workspaceMockService: Partial<WorkspaceService>;
26     const componentInstanceMockService: Partial<ComponentInstanceServiceNg2>;
27     const topologyTemplateMockService: Partial<TopologyTemplateService>;
28     let artifactsServiceMockService: Partial<ArtifactsService>;
29     let store: Store;
30
31     beforeEach(
32         async(() => {
33             compositionMockService = {
34                 updateInstance:  jest.fn()
35             }
36
37             artifactsServiceMockService = {
38                 deleteArtifact: jest.fn(),
39                 openUpdateEnvParams: jest.fn(),
40                 openArtifactModal: jest.fn()
41             }
42
43             const configure: ConfigureFn = (testBed) => {
44                 testBed.configureTestingModule({
45                     declarations: [ArtifactsTabComponent],
46                     imports: [NgxsModule.forRoot([WorkspaceState])],
47                     schemas: [NO_ERRORS_SCHEMA],
48                     providers: [
49                         {provide: CompositionService, useValue: compositionMockService},
50                         {provide: WorkspaceService, useValue: workspaceMockService},
51                         {provide: ComponentInstanceServiceNg2, useValue: componentInstanceMockService},
52                         {provide: TopologyTemplateService, useValue: topologyTemplateMockService},
53                         {provide: ArtifactsService, useValue: artifactsServiceMockService}
54                     ],
55                 });
56             };
57
58             configureTests(configure).then((testBed) => {
59                 fixture = testBed.createComponent(ArtifactsTabComponent);
60                 store = testBed.get(Store);
61             });
62         })
63     );
64
65     it ('on delete -> deleteArtifact is being called from artifactService', () => {
66         const artifact = new ArtifactModel();
67         const topologyTemplateType: string = undefined;
68         const topologyTemplateId: string = undefined;
69
70         fixture.componentInstance.delete(artifact);
71         expect(artifactsServiceMockService.deleteArtifact).toHaveBeenCalledWith(topologyTemplateType, topologyTemplateId, artifact);
72     });
73
74     it('should match current snapshot of artifact-tab component', () => {
75         expect(fixture).toMatchSnapshot();
76     });
77
78
79     it ('should get API Artifacts as Title', () => {
80         const artifactType = ArtifactType.SERVICE_API;
81
82         const res = fixture.componentInstance.getTitle(artifactType);
83         expect(res).toBe('API Artifacts');
84     });
85
86
87     it ('should get Deployment Artifacts as Title', () => {
88         const artifactType = ArtifactType.DEPLOYMENT;
89
90         const res = fixture.componentInstance.getTitle(artifactType);
91         expect(res).toBe('Deployment Artifacts');
92     });
93
94     it ('should get Informational Artifacts as Title', () => {
95         const artifactType = ArtifactType.INFORMATION;
96
97         const res = fixture.componentInstance.getTitle(artifactType);
98         expect(res).toBe('Informational Artifacts');
99     });
100
101     it ('should get SomeString as Title - This is the default case (return the last val)', () => {
102         // So the last value will be "SomeString"
103         fixture.componentInstance.getTitle('SomeString');
104
105         const res = fixture.componentInstance.getTitle('SomeString');
106         expect(res).toBe('SomeString Artifacts');
107     });
108
109
110     it ('should return isLicenseArtifact false', () => {
111         const artifact = new ArtifactModel();
112         const componentInstance = new ComponentInstance();
113         const component = new Component();
114         fixture.componentInstance.component = new FullComponentInstance(componentInstance, component);
115
116         let res = fixture.componentInstance.isLicenseArtifact(artifact);
117         expect(res).toBe(false);
118     });
119
120     it ('should return isLicenseArtifact true', () => {
121         const artifact = new ArtifactModel();
122         const componentInstance = new ComponentInstance();
123         const component = new Component();
124         fixture.componentInstance.component = new FullComponentInstance(componentInstance, component);
125         fixture.componentInstance.component.isResource =  jest.fn(() => true);
126         fixture.componentInstance.component.isCsarComponent =  true;
127
128         artifact.artifactType = ArtifactType.VENDOR_LICENSE;
129         const res = fixture.componentInstance.isLicenseArtifact(artifact);
130         expect(res).toBe(true);
131     });
132
133     it ('should verify getEnvArtifact with match', () => {
134         const artifact = new ArtifactModel();
135         artifact.uniqueId = 'matchUniqueID';
136
137         const testItem1 = new ArtifactModel();
138         testItem1.generatedFromId = 'matchUniqueID';
139
140         const testItem2 = new ArtifactModel();
141         testItem2.generatedFromId = '123456';
142
143         const artifacts: ArtifactModel[] = [testItem1, testItem2];
144
145         const res = fixture.componentInstance.getEnvArtifact(artifact, artifacts);
146         expect(res.generatedFromId).toBe('matchUniqueID');
147     });
148
149     it ('should verify getEnvArtifact with no match', () => {
150         const artifact = new ArtifactModel();
151         artifact.uniqueId = 'matchUniqueID';
152
153         const testItem1 = new ArtifactModel();
154         testItem1.generatedFromId = '654321';
155
156         const testItem2 = new ArtifactModel();
157         testItem2.generatedFromId = '123456';
158
159         const artifacts: ArtifactModel[] = [testItem1, testItem2];
160
161         const res = fixture.componentInstance.getEnvArtifact(artifact, artifacts);
162         expect(res).toBe(undefined);
163     });
164
165     it ('on updateEnvParams -> openUpdateEnvParams is being called from artifactService when isComponentInstanceSelected = true', () => {
166         const artifact = new ArtifactModel();
167         artifact.envArtifact = new ArtifactModel();
168
169         const topologyTemplateType: string = undefined;
170         const topologyTemplateId: string = undefined;
171
172         const component = new Component();
173         component.uniqueId = 'id';
174
175         const isComponentInstanceSelected = true;
176
177         fixture.componentInstance.component = component;
178         fixture.componentInstance.isComponentInstanceSelected = isComponentInstanceSelected;
179         fixture.componentInstance.updateEnvParams(artifact);
180
181         expect(artifactsServiceMockService.openUpdateEnvParams).toHaveBeenCalledWith(topologyTemplateType, topologyTemplateId, undefined, component.uniqueId);
182     });
183
184     it ('on updateEnvParams -> openUpdateEnvParams is being called from artifactService when isComponentInstanceSelected = false', () => {
185         const artifact = new ArtifactModel();
186
187         const topologyTemplateType: string = undefined
188         const topologyTemplateId: string = undefined;
189
190         const component = new Component();
191
192         const isComponentInstanceSelected = false;
193
194         fixture.componentInstance.component = component;
195         fixture.componentInstance.isComponentInstanceSelected = isComponentInstanceSelected;
196         fixture.componentInstance.updateEnvParams(artifact);
197
198         expect(artifactsServiceMockService.openUpdateEnvParams).toHaveBeenCalledWith(topologyTemplateType, topologyTemplateId, artifact);
199     });
200
201     it ('on addOrUpdate -> openArtifactModal is being called from artifactService when isComponentInstanceSelected = true', () => {
202         const artifact = new ArtifactModel();
203
204         const topologyTemplateType: string = 'testType';
205         const topologyTemplateId: string = 'testID';
206         const type: string = 'testType';
207         const isViewOnly: boolean = false;
208
209         const component = new Component();
210         component.uniqueId = 'id';
211
212         const isComponentInstanceSelected = true;
213
214         fixture.componentInstance.component = component;
215         fixture.componentInstance.type = type;
216         fixture.componentInstance.topologyTemplateId = topologyTemplateId;
217         fixture.componentInstance.topologyTemplateType = topologyTemplateType;
218         fixture.componentInstance.isComponentInstanceSelected = isComponentInstanceSelected;
219         fixture.componentInstance.isViewOnly = isViewOnly;
220         fixture.componentInstance.addOrUpdate(artifact);
221
222
223         expect(artifactsServiceMockService.openArtifactModal).toHaveBeenCalledWith(topologyTemplateId, topologyTemplateType, artifact, type, isViewOnly, component.uniqueId);
224     });
225
226     it ('on addOrUpdate -> openArtifactModal is being called from artifactService when isComponentInstanceSelected = false', () => {
227         const artifact = new ArtifactModel();
228
229         const topologyTemplateType: string = 'testType';
230         const topologyTemplateId: string = 'testID';
231         const type: string = 'testType';
232         const isViewOnly: boolean = false;
233
234         const isComponentInstanceSelected = false;
235
236         fixture.componentInstance.type = type;
237         fixture.componentInstance.isComponentInstanceSelected = isComponentInstanceSelected;
238         fixture.componentInstance.topologyTemplateId = topologyTemplateId;
239         fixture.componentInstance.topologyTemplateType = topologyTemplateType;
240         fixture.componentInstance.isViewOnly = isViewOnly;
241         fixture.componentInstance.addOrUpdate(artifact);
242
243         expect(artifactsServiceMockService.openArtifactModal).toHaveBeenCalledWith(topologyTemplateId, topologyTemplateType, artifact, type, isViewOnly);
244     });
245
246
247     it ('verify allowDeleteAndUpdateArtifact return false since isViewOnly=true', () => {
248         const artifact = new ArtifactModel();
249         fixture.componentInstance.isViewOnly = true;
250
251         const res = fixture.componentInstance.allowDeleteAndUpdateArtifact(artifact);
252         expect(res).toBe(false)
253     });
254
255     it ('verify allowDeleteAndUpdateArtifact return artifact.isFromCsar since isViewOnly=false && artifactGroupType = DEPLOYMENT', () => {
256         const artifact = new ArtifactModel();
257         artifact.artifactGroupType = ArtifactType.DEPLOYMENT;
258         artifact.isFromCsar = false;
259
260         fixture.componentInstance.isViewOnly = false;
261
262         const res = fixture.componentInstance.allowDeleteAndUpdateArtifact(artifact);
263         expect(res).toBe(!artifact.isFromCsar);
264     });
265
266     it ('verify allowDeleteAndUpdateArtifact return !artifact.isHEAT() && !artifact.isThirdParty() &&' +
267         ' !this.isLicenseArtifact(artifact) since isViewOnly=false && artifactGroupType != DEPLOYMENT', () => {
268         const artifact = new ArtifactModel();
269         artifact.artifactGroupType = 'NOT_DEPLOYMENT';
270         artifact.isHEAT = () =>  false;
271         artifact.isThirdParty = () =>  false;
272
273         fixture.componentInstance.isLicenseArtifact = jest.fn(() => false);
274
275         fixture.componentInstance.isViewOnly = false;
276
277         const res = fixture.componentInstance.allowDeleteAndUpdateArtifact(artifact);
278         expect(res).toBe(true )
279     });
280
281     it('verify action on loadArtifacts in case isComponentInstanceSelected = true', () => {
282         fixture.componentInstance.isComponentInstanceSelected = true;
283         fixture.componentInstance.topologyTemplateType = 'topologyTemplateType';
284         fixture.componentInstance.topologyTemplateId = 'topologyTemplateId';
285         const component = new Component();
286         component.uniqueId = 'uniqueId';
287         fixture.componentInstance.component = component;
288         fixture.componentInstance.type = 'type';
289
290         const action = new GetInstanceArtifactsByTypeAction(({
291             componentType: 'topologyTemplateType',
292             componentId: 'topologyTemplateId',
293             instanceId: 'uniqueId',
294             artifactType: 'type'
295         }))
296
297         fixture.componentInstance.store.dispatch = jest.fn(() => Observable.of(true));
298         fixture.componentInstance.loadArtifacts();
299
300         expect(store.dispatch).toBeCalledWith(action);
301
302     });
303 });