summaryrefslogtreecommitdiffstats
path: root/catalog-ui/src/app/ng2/pages/composition/panel/panel-tabs/artifacts-tab/artifact-tab.component.spec.ts
blob: 258f2295ab7bf90b884a96bc8f647b865ba379d7 (plain)
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
import { async, ComponentFixture } from '@angular/core/testing';
import { ConfigureFn, configureTests } from '../../../../../../../jest/test-config.helper';
import { NgxsModule, Store } from '@ngxs/store';
import { WorkspaceState } from '../../../../../store/states/workspace.state';
import { NO_ERRORS_SCHEMA } from '@angular/core';
import { ArtifactsTabComponent } from './artifacts-tab.component';
import { CompositionService } from '../../../composition.service';
import { WorkspaceService } from '../../../../workspace/workspace.service';
import { ComponentInstanceServiceNg2 } from '../../../../../services/component-instance-services/component-instance.service';
import { TopologyTemplateService } from '../../../../../services/component-services/topology-template.service';
import { ArtifactsService } from '../../../../../components/forms/artifacts-form/artifacts.service';
import { ArtifactModel } from '../../../../../../models/artifacts';
import { ArtifactType } from '../../../../../../utils/constants';
import { FullComponentInstance } from '../../../../../../models/componentsInstances/fullComponentInstance';
import { ComponentInstance } from '../../../../../../models/componentsInstances/componentInstance';
import { Component } from '../../../../../../models/components/component';
import { GetInstanceArtifactsByTypeAction } from '../../../../../store/actions/instance-artifacts.actions';
import { Observable } from 'rxjs';


describe('artifact-tab component', () => {

    let fixture: ComponentFixture<ArtifactsTabComponent>;
    let compositionMockService: Partial<CompositionService>;
    const workspaceMockService: Partial<WorkspaceService>;
    const componentInstanceMockService: Partial<ComponentInstanceServiceNg2>;
    const topologyTemplateMockService: Partial<TopologyTemplateService>;
    let artifactsServiceMockService: Partial<ArtifactsService>;
    let store: Store;

    beforeEach(
        async(() => {
            compositionMockService = {
                updateInstance:  jest.fn()
            }

            artifactsServiceMockService = {
                deleteArtifact: jest.fn(),
                openUpdateEnvParams: jest.fn(),
                openArtifactModal: jest.fn()
            }

            const configure: ConfigureFn = (testBed) => {
                testBed.configureTestingModule({
                    declarations: [ArtifactsTabComponent],
                    imports: [NgxsModule.forRoot([WorkspaceState])],
                    schemas: [NO_ERRORS_SCHEMA],
                    providers: [
                        {provide: CompositionService, useValue: compositionMockService},
                        {provide: WorkspaceService, useValue: workspaceMockService},
                        {provide: ComponentInstanceServiceNg2, useValue: componentInstanceMockService},
                        {provide: TopologyTemplateService, useValue: topologyTemplateMockService},
                        {provide: ArtifactsService, useValue: artifactsServiceMockService}
                    ],
                });
            };

            configureTests(configure).then((testBed) => {
                fixture = testBed.createComponent(ArtifactsTabComponent);
                store = testBed.get(Store);
            });
        })
    );

    it ('on delete -> deleteArtifact is being called from artifactService', () => {
        const artifact = new ArtifactModel();
        const topologyTemplateType: string = undefined;
        const topologyTemplateId: string = undefined;

        fixture.componentInstance.delete(artifact);
        expect(artifactsServiceMockService.deleteArtifact).toHaveBeenCalledWith(topologyTemplateType, topologyTemplateId, artifact);
    });

    it('should match current snapshot of artifact-tab component', () => {
        expect(fixture).toMatchSnapshot();
    });


    it ('should get API Artifacts as Title', () => {
        const artifactType = ArtifactType.SERVICE_API;

        const res = fixture.componentInstance.getTitle(artifactType);
        expect(res).toBe('API Artifacts');
    });


    it ('should get Deployment Artifacts as Title', () => {
        const artifactType = ArtifactType.DEPLOYMENT;

        const res = fixture.componentInstance.getTitle(artifactType);
        expect(res).toBe('Deployment Artifacts');
    });

    it ('should get Informational Artifacts as Title', () => {
        const artifactType = ArtifactType.INFORMATION;

        const res = fixture.componentInstance.getTitle(artifactType);
        expect(res).toBe('Informational Artifacts');
    });

    it ('should get SomeString as Title - This is the default case (return the last val)', () => {
        // So the last value will be "SomeString"
        fixture.componentInstance.getTitle('SomeString');

        const res = fixture.componentInstance.getTitle('SomeString');
        expect(res).toBe('SomeString Artifacts');
    });


    it ('should return isLicenseArtifact false', () => {
        const artifact = new ArtifactModel();
        const componentInstance = new ComponentInstance();
        const component = new Component();
        fixture.componentInstance.component = new FullComponentInstance(componentInstance, component);

        let res = fixture.componentInstance.isLicenseArtifact(artifact);
        expect(res).toBe(false);
    });

    it ('should return isLicenseArtifact true', () => {
        const artifact = new ArtifactModel();
        const componentInstance = new ComponentInstance();
        const component = new Component();
        fixture.componentInstance.component = new FullComponentInstance(componentInstance, component);
        fixture.componentInstance.component.isResource =  jest.fn(() => true);
        fixture.componentInstance.component.isCsarComponent =  true;

        artifact.artifactType = ArtifactType.VENDOR_LICENSE;
        const res = fixture.componentInstance.isLicenseArtifact(artifact);
        expect(res).toBe(true);
    });

    it ('should verify getEnvArtifact with match', () => {
        const artifact = new ArtifactModel();
        artifact.uniqueId = 'matchUniqueID';

        const testItem1 = new ArtifactModel();
        testItem1.generatedFromId = 'matchUniqueID';

        const testItem2 = new ArtifactModel();
        testItem2.generatedFromId = '123456';

        const artifacts: ArtifactModel[] = [testItem1, testItem2];

        const res = fixture.componentInstance.getEnvArtifact(artifact, artifacts);
        expect(res.generatedFromId).toBe('matchUniqueID');
    });

    it ('should verify getEnvArtifact with no match', () => {
        const artifact = new ArtifactModel();
        artifact.uniqueId = 'matchUniqueID';

        const testItem1 = new ArtifactModel();
        testItem1.generatedFromId = '654321';

        const testItem2 = new ArtifactModel();
        testItem2.generatedFromId = '123456';

        const artifacts: ArtifactModel[] = [testItem1, testItem2];

        const res = fixture.componentInstance.getEnvArtifact(artifact, artifacts);
        expect(res).toBe(undefined);
    });

    it ('on updateEnvParams -> openUpdateEnvParams is being called from artifactService when isComponentInstanceSelected = true', () => {
        const artifact = new ArtifactModel();
        artifact.envArtifact = new ArtifactModel();

        const topologyTemplateType: string = undefined;
        const topologyTemplateId: string = undefined;

        const component = new Component();
        component.uniqueId = 'id';

        const isComponentInstanceSelected = true;

        fixture.componentInstance.component = component;
        fixture.componentInstance.isComponentInstanceSelected = isComponentInstanceSelected;
        fixture.componentInstance.updateEnvParams(artifact);

        expect(artifactsServiceMockService.openUpdateEnvParams).toHaveBeenCalledWith(topologyTemplateType, topologyTemplateId, undefined, component.uniqueId);
    });

    it ('on updateEnvParams -> openUpdateEnvParams is being called from artifactService when isComponentInstanceSelected = false', () => {
        const artifact = new ArtifactModel();

        const topologyTemplateType: string = undefined
        const topologyTemplateId: string = undefined;

        const component = new Component();

        const isComponentInstanceSelected = false;

        fixture.componentInstance.component = component;
        fixture.componentInstance.isComponentInstanceSelected = isComponentInstanceSelected;
        fixture.componentInstance.updateEnvParams(artifact);

        expect(artifactsServiceMockService.openUpdateEnvParams).toHaveBeenCalledWith(topologyTemplateType, topologyTemplateId, artifact);
    });

    it ('on addOrUpdate -> openArtifactModal is being called from artifactService when isComponentInstanceSelected = true', () => {
        const artifact = new ArtifactModel();

        const topologyTemplateType: string = 'testType';
        const topologyTemplateId: string = 'testID';
        const type: string = 'testType';
        const isViewOnly: boolean = false;

        const component = new Component();
        component.uniqueId = 'id';

        const isComponentInstanceSelected = true;

        fixture.componentInstance.component = component;
        fixture.componentInstance.type = type;
        fixture.componentInstance.topologyTemplateId = topologyTemplateId;
        fixture.componentInstance.topologyTemplateType = topologyTemplateType;
        fixture.componentInstance.isComponentInstanceSelected = isComponentInstanceSelected;
        fixture.componentInstance.isViewOnly = isViewOnly;
        fixture.componentInstance.addOrUpdate(artifact);


        expect(artifactsServiceMockService.openArtifactModal).toHaveBeenCalledWith(topologyTemplateId, topologyTemplateType, artifact, type, isViewOnly, component.uniqueId);
    });

    it ('on addOrUpdate -> openArtifactModal is being called from artifactService when isComponentInstanceSelected = false', () => {
        const artifact = new ArtifactModel();

        const topologyTemplateType: string = 'testType';
        const topologyTemplateId: string = 'testID';
        const type: string = 'testType';
        const isViewOnly: boolean = false;

        const isComponentInstanceSelected = false;

        fixture.componentInstance.type = type;
        fixture.componentInstance.isComponentInstanceSelected = isComponentInstanceSelected;
        fixture.componentInstance.topologyTemplateId = topologyTemplateId;
        fixture.componentInstance.topologyTemplateType = topologyTemplateType;
        fixture.componentInstance.isViewOnly = isViewOnly;
        fixture.componentInstance.addOrUpdate(artifact);

        expect(artifactsServiceMockService.openArtifactModal).toHaveBeenCalledWith(topologyTemplateId, topologyTemplateType, artifact, type, isViewOnly);
    });


    it ('verify allowDeleteAndUpdateArtifact return false since isViewOnly=true', () => {
        const artifact = new ArtifactModel();
        fixture.componentInstance.isViewOnly = true;

        const res = fixture.componentInstance.allowDeleteAndUpdateArtifact(artifact);
        expect(res).toBe(false)
    });

    it ('verify allowDeleteAndUpdateArtifact return artifact.isFromCsar since isViewOnly=false && artifactGroupType = DEPLOYMENT', () => {
        const artifact = new ArtifactModel();
        artifact.artifactGroupType = ArtifactType.DEPLOYMENT;
        artifact.isFromCsar = false;

        fixture.componentInstance.isViewOnly = false;

        const res = fixture.componentInstance.allowDeleteAndUpdateArtifact(artifact);
        expect(res).toBe(!artifact.isFromCsar);
    });

    it ('verify allowDeleteAndUpdateArtifact return !artifact.isHEAT() && !artifact.isThirdParty() &&' +
        ' !this.isLicenseArtifact(artifact) since isViewOnly=false && artifactGroupType != DEPLOYMENT', () => {
        const artifact = new ArtifactModel();
        artifact.artifactGroupType = 'NOT_DEPLOYMENT';
        artifact.isHEAT = () =>  false;
        artifact.isThirdParty = () =>  false;

        fixture.componentInstance.isLicenseArtifact = jest.fn(() => false);

        fixture.componentInstance.isViewOnly = false;

        const res = fixture.componentInstance.allowDeleteAndUpdateArtifact(artifact);
        expect(res).toBe(true )
    });

    it('verify action on loadArtifacts in case isComponentInstanceSelected = true', () => {
        fixture.componentInstance.isComponentInstanceSelected = true;
        fixture.componentInstance.topologyTemplateType = 'topologyTemplateType';
        fixture.componentInstance.topologyTemplateId = 'topologyTemplateId';
        const component = new Component();
        component.uniqueId = 'uniqueId';
        fixture.componentInstance.component = component;
        fixture.componentInstance.type = 'type';

        const action = new GetInstanceArtifactsByTypeAction(({
            componentType: 'topologyTemplateType',
            componentId: 'topologyTemplateId',
            instanceId: 'uniqueId',
            artifactType: 'type'
        }))

        fixture.componentInstance.store.dispatch = jest.fn(() => Observable.of(true));
        fixture.componentInstance.loadArtifacts();

        expect(store.dispatch).toBeCalledWith(action);

    });
});