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';
21 describe('artifact-tab component', () => {
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>;
33 compositionMockService = {
34 updateInstance: jest.fn()
37 artifactsServiceMockService = {
38 deleteArtifact: jest.fn(),
39 openUpdateEnvParams: jest.fn(),
40 openArtifactModal: jest.fn()
43 const configure: ConfigureFn = (testBed) => {
44 testBed.configureTestingModule({
45 declarations: [ArtifactsTabComponent],
46 imports: [NgxsModule.forRoot([WorkspaceState])],
47 schemas: [NO_ERRORS_SCHEMA],
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}
58 configureTests(configure).then((testBed) => {
59 fixture = testBed.createComponent(ArtifactsTabComponent);
60 store = testBed.get(Store);
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;
70 fixture.componentInstance.delete(artifact);
71 expect(artifactsServiceMockService.deleteArtifact).toHaveBeenCalledWith(topologyTemplateType, topologyTemplateId, artifact);
74 it('should match current snapshot of artifact-tab component', () => {
75 expect(fixture).toMatchSnapshot();
79 it ('should get API Artifacts as Title', () => {
80 const artifactType = ArtifactType.SERVICE_API;
82 const res = fixture.componentInstance.getTitle(artifactType);
83 expect(res).toBe('API Artifacts');
87 it ('should get Deployment Artifacts as Title', () => {
88 const artifactType = ArtifactType.DEPLOYMENT;
90 const res = fixture.componentInstance.getTitle(artifactType);
91 expect(res).toBe('Deployment Artifacts');
94 it ('should get Informational Artifacts as Title', () => {
95 const artifactType = ArtifactType.INFORMATION;
97 const res = fixture.componentInstance.getTitle(artifactType);
98 expect(res).toBe('Informational Artifacts');
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');
105 const res = fixture.componentInstance.getTitle('SomeString');
106 expect(res).toBe('SomeString Artifacts');
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);
116 let res = fixture.componentInstance.isLicenseArtifact(artifact);
117 expect(res).toBe(false);
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;
128 artifact.artifactType = ArtifactType.VENDOR_LICENSE;
129 const res = fixture.componentInstance.isLicenseArtifact(artifact);
130 expect(res).toBe(true);
133 it ('should verify getEnvArtifact with match', () => {
134 const artifact = new ArtifactModel();
135 artifact.uniqueId = 'matchUniqueID';
137 const testItem1 = new ArtifactModel();
138 testItem1.generatedFromId = 'matchUniqueID';
140 const testItem2 = new ArtifactModel();
141 testItem2.generatedFromId = '123456';
143 const artifacts: ArtifactModel[] = [testItem1, testItem2];
145 const res = fixture.componentInstance.getEnvArtifact(artifact, artifacts);
146 expect(res.generatedFromId).toBe('matchUniqueID');
149 it ('should verify getEnvArtifact with no match', () => {
150 const artifact = new ArtifactModel();
151 artifact.uniqueId = 'matchUniqueID';
153 const testItem1 = new ArtifactModel();
154 testItem1.generatedFromId = '654321';
156 const testItem2 = new ArtifactModel();
157 testItem2.generatedFromId = '123456';
159 const artifacts: ArtifactModel[] = [testItem1, testItem2];
161 const res = fixture.componentInstance.getEnvArtifact(artifact, artifacts);
162 expect(res).toBe(undefined);
165 it ('on updateEnvParams -> openUpdateEnvParams is being called from artifactService when isComponentInstanceSelected = true', () => {
166 const artifact = new ArtifactModel();
167 artifact.envArtifact = new ArtifactModel();
169 const topologyTemplateType: string = undefined;
170 const topologyTemplateId: string = undefined;
172 const component = new Component();
173 component.uniqueId = 'id';
175 const isComponentInstanceSelected = true;
177 fixture.componentInstance.component = component;
178 fixture.componentInstance.isComponentInstanceSelected = isComponentInstanceSelected;
179 fixture.componentInstance.updateEnvParams(artifact);
181 expect(artifactsServiceMockService.openUpdateEnvParams).toHaveBeenCalledWith(topologyTemplateType, topologyTemplateId, undefined, component.uniqueId);
184 it ('on updateEnvParams -> openUpdateEnvParams is being called from artifactService when isComponentInstanceSelected = false', () => {
185 const artifact = new ArtifactModel();
187 const topologyTemplateType: string = undefined
188 const topologyTemplateId: string = undefined;
190 const component = new Component();
192 const isComponentInstanceSelected = false;
194 fixture.componentInstance.component = component;
195 fixture.componentInstance.isComponentInstanceSelected = isComponentInstanceSelected;
196 fixture.componentInstance.updateEnvParams(artifact);
198 expect(artifactsServiceMockService.openUpdateEnvParams).toHaveBeenCalledWith(topologyTemplateType, topologyTemplateId, artifact);
201 it ('on addOrUpdate -> openArtifactModal is being called from artifactService when isComponentInstanceSelected = true', () => {
202 const artifact = new ArtifactModel();
204 const topologyTemplateType: string = 'testType';
205 const topologyTemplateId: string = 'testID';
206 const type: string = 'testType';
207 const isViewOnly: boolean = false;
209 const component = new Component();
210 component.uniqueId = 'id';
212 const isComponentInstanceSelected = true;
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);
223 expect(artifactsServiceMockService.openArtifactModal).toHaveBeenCalledWith(topologyTemplateId, topologyTemplateType, artifact, type, isViewOnly, component.uniqueId);
226 it ('on addOrUpdate -> openArtifactModal is being called from artifactService when isComponentInstanceSelected = false', () => {
227 const artifact = new ArtifactModel();
229 const topologyTemplateType: string = 'testType';
230 const topologyTemplateId: string = 'testID';
231 const type: string = 'testType';
232 const isViewOnly: boolean = false;
234 const isComponentInstanceSelected = false;
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);
243 expect(artifactsServiceMockService.openArtifactModal).toHaveBeenCalledWith(topologyTemplateId, topologyTemplateType, artifact, type, isViewOnly);
247 it ('verify allowDeleteAndUpdateArtifact return false since isViewOnly=true', () => {
248 const artifact = new ArtifactModel();
249 fixture.componentInstance.isViewOnly = true;
251 const res = fixture.componentInstance.allowDeleteAndUpdateArtifact(artifact);
252 expect(res).toBe(false)
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;
260 fixture.componentInstance.isViewOnly = false;
262 const res = fixture.componentInstance.allowDeleteAndUpdateArtifact(artifact);
263 expect(res).toBe(!artifact.isFromCsar);
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;
273 fixture.componentInstance.isLicenseArtifact = jest.fn(() => false);
275 fixture.componentInstance.isViewOnly = false;
277 const res = fixture.componentInstance.allowDeleteAndUpdateArtifact(artifact);
278 expect(res).toBe(true )
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';
290 const action = new GetInstanceArtifactsByTypeAction(({
291 componentType: 'topologyTemplateType',
292 componentId: 'topologyTemplateId',
293 instanceId: 'uniqueId',
297 fixture.componentInstance.store.dispatch = jest.fn(() => Observable.of(true));
298 fixture.componentInstance.loadArtifacts();
300 expect(store.dispatch).toBeCalledWith(action);