From cd6f933375c412c2f79a12e909821322d58a8499 Mon Sep 17 00:00:00 2001 From: "andre.schmid" Date: Wed, 29 Jan 2020 17:25:21 +0000 Subject: Configure a new Artifact Type Centralizes artifact configuration in one yaml entry. Allow the configuration of a new artifact type without the need of code changes. The configuration file now is used as a source of artifacts types instead the artifact type enum. The enum will be used as a source of base artifact types and also in hard coded business rules. Change-Id: Id0383d9fca9bce0519a4d52a4ecb3a68c8713f0f Issue-ID: SDC-2754 Signed-off-by: andre.schmid --- .../artifact-form.component.spec.ts.snap | 1 + .../artifacts-form/artifact-form.component.spec.ts | 100 +++++++++------------ .../artifacts-form/artifact-form.component.ts | 41 ++++----- .../forms/artifacts-form/artifacts.service.ts | 10 ++- 4 files changed, 68 insertions(+), 84 deletions(-) (limited to 'catalog-ui/src/app/ng2/components') diff --git a/catalog-ui/src/app/ng2/components/forms/artifacts-form/__snapshots__/artifact-form.component.spec.ts.snap b/catalog-ui/src/app/ng2/components/forms/artifacts-form/__snapshots__/artifact-form.component.spec.ts.snap index 8cd085e248..440dc74d72 100644 --- a/catalog-ui/src/app/ng2/components/forms/artifacts-form/__snapshots__/artifact-form.component.spec.ts.snap +++ b/catalog-ui/src/app/ng2/components/forms/artifacts-form/__snapshots__/artifact-form.component.spec.ts.snap @@ -2,6 +2,7 @@ exports[`artifact form component should match current snapshot of artifact form component 1`] = ` { let fixture: ComponentFixture; let cacheServiceMock: Partial; + let artifactConfigService: Partial; let onValidationChangeMock: Partial>; let artifactModel = new ArtifactModel(); + artifactConfigService = { + findAllTypeBy: jest.fn() + }; beforeEach( async(() => { onValidationChangeMock = { next: jest.fn() - } + }; cacheServiceMock = { contains: jest.fn(), remove: jest.fn(), set: jest.fn(), get: jest.fn() - } + }; const configure: ConfigureFn = testBed => { testBed.configureTestingModule({ @@ -41,6 +46,7 @@ describe('artifact form component', () => { schemas: [NO_ERRORS_SCHEMA], providers: [ {provide: CacheService, useValue: cacheServiceMock}, + {provide: ArtifactConfigService, useValue: artifactConfigService}, {provide: TranslateService, useValue: {}} ], }); @@ -55,91 +61,73 @@ describe('artifact form component', () => { it('should verify initArtifactTypes for DEPLOYMENT and ArtifactType = HEAT_ENV', () =>{ - cacheServiceMock.get.mockImplementation(() =>{ - return { - artifacts: { - deployment:{ - resourceInstanceDeploymentArtifacts: [{name: "Dummy Value Returned from ui api"}], - } - } + artifactConfigService.findAllTypeBy.mockImplementation((artifactType, componentType, resourceType) => { + if (artifactType == 'DEPLOYMENT' && componentType == ComponentType.RESOURCE_INSTANCE && resourceType == 'VF') { + return ['Val1', 'Val2', 'Val3', ArtifactType.HEAT_ENV]; } }); fixture.componentInstance.artifactType = 'DEPLOYMENT'; + fixture.componentInstance.resourceType = 'VF'; fixture.componentInstance.artifact = artifactModel; - fixture.componentInstance.artifact.artifactType = 'HEAT_ENV'; + fixture.componentInstance.artifact.artifactType = ArtifactType.HEAT_ENV; fixture.componentInstance.initArtifactTypes(); - expect(fixture.componentInstance.selectedFileType).toEqual(undefined); + expect(fixture.componentInstance.selectedFileType).toEqual({"label": ArtifactType.HEAT_ENV, "value": ArtifactType.HEAT_ENV}); }); - it('should verify initArtifactTypes for DEPLOYMENT and ComponentType = RESOURCE', () =>{ + it('should verify initArtifactTypes for DEPLOYMENT and ComponentType = RESOURCE', () => { - cacheServiceMock.get.mockImplementation(() =>{ - return { - artifacts: { - deployment:{ - resourceDeploymentArtifacts: [{name: "Dummy Value Returned from ui api"}], - } - } + const expectedSelectedValue = 'Val3'; + const artifactType = 'DEPLOYMENT'; + const resourceType = 'VF'; + artifactConfigService.findAllTypeBy.mockImplementation((artifactType1, componentType1, resourceType1) => { + if (artifactType1 == artifactType && componentType1 == ComponentType.RESOURCE && resourceType1 == resourceType) { + return ['Val1', 'Val2', expectedSelectedValue, 'Val4']; } - }); - - fixture.componentInstance.artifactType = 'DEPLOYMENT'; - fixture.componentInstance.artifact = artifactModel; - fixture.componentInstance.componentType = 'RESOURCE'; - - fixture.componentInstance.initArtifactTypes(); - - expect(fixture.componentInstance.selectedFileType).toEqual(undefined); - - }); - - it('should verify initArtifactTypes for DEPLOYMENT and NOT ComponentType = RESOURCE OR NOT ArtifactType = HEAT_ENV', () =>{ - cacheServiceMock.get.mockImplementation(() =>{ - return { - artifacts: { - deployment:{ - serviceDeploymentArtifacts: [{name: "Dummy Value Returned from ui api"}], - } - } - } + return []; }); - fixture.componentInstance.artifactType = 'DEPLOYMENT'; + fixture.componentInstance.artifactType = artifactType; fixture.componentInstance.artifact = artifactModel; - // fixture.componentInstance.componentType = 'RESOURCE'; + fixture.componentInstance.resourceType = resourceType; + fixture.componentInstance.componentType = ComponentType.RESOURCE; + fixture.componentInstance.artifact.artifactType = expectedSelectedValue; fixture.componentInstance.initArtifactTypes(); - expect(fixture.componentInstance.selectedFileType).toEqual(undefined); + expect(fixture.componentInstance.selectedFileType).toEqual({'label': expectedSelectedValue, 'value': expectedSelectedValue}); }); - it('should verify initArtifactTypes for INFORMATION', () =>{ + it('should verify initArtifactTypes for INFORMATIONAL', () => { - cacheServiceMock.get.mockImplementation(() =>{ - return { - artifacts: { - other: [{name: "Val1"}, {name: "ExpectedValToBeSelected"}, {name: "Val3"}] - } + const expectedSelectedValue = 'Val3'; + const artifactType = 'INFORMATIONAL'; + const resourceType = null; + artifactConfigService.findAllTypeBy.mockImplementation((artifactType1, componentType1, resourceType1) => { + if (artifactType1 == artifactType && componentType1 == ComponentType.SERVICE && resourceType1 == resourceType) { + return ['Val1', 'Val2', expectedSelectedValue, 'Val4']; } + + return []; }); - fixture.componentInstance.artifactType = 'INFORMATIONAL'; + fixture.componentInstance.artifactType = artifactType; fixture.componentInstance.artifact = artifactModel; - fixture.componentInstance.artifact.artifactType = 'ExpectedValToBeSelected'; + fixture.componentInstance.resourceType = resourceType; + fixture.componentInstance.componentType = ComponentType.SERVICE; + fixture.componentInstance.artifact.artifactType = expectedSelectedValue; fixture.componentInstance.initArtifactTypes(); - expect(fixture.componentInstance.selectedFileType).toEqual({"label": "ExpectedValToBeSelected", "value": "ExpectedValToBeSelected"}); + expect(fixture.componentInstance.selectedFileType).toEqual({'label': expectedSelectedValue, 'value': expectedSelectedValue}); }); - it('should match current snapshot of artifact form component', () => { expect(fixture).toMatchSnapshot(); }); @@ -147,7 +135,7 @@ describe('artifact form component', () => { it('should verify onUploadFile -> file gets file name', () => { let file = { filename:'dummyFileName' - } + }; fixture.componentInstance.verifyTypeAndFileWereFilled = jest.fn(); fixture.componentInstance.artifact = artifactModel; diff --git a/catalog-ui/src/app/ng2/components/forms/artifacts-form/artifact-form.component.ts b/catalog-ui/src/app/ng2/components/forms/artifacts-form/artifact-form.component.ts index 905d1a25ad..f7dbf09e3f 100644 --- a/catalog-ui/src/app/ng2/components/forms/artifacts-form/artifact-form.component.ts +++ b/catalog-ui/src/app/ng2/components/forms/artifacts-form/artifact-form.component.ts @@ -9,6 +9,7 @@ import { ArtifactModel } from '../../../../models'; import { ArtifactType, ComponentType } from '../../../../utils'; import { Dictionary } from '../../../../utils/dictionary/dictionary'; import { CacheService } from '../../../services/cache.service'; +import { ArtifactConfigService } from '../../../services/artifact-config.service'; @Component({ selector: 'artifact-form', @@ -20,6 +21,7 @@ export class ArtifactFormComponent { @Input() artifact: ArtifactModel; @Input() artifactType: ArtifactType; @Input() componentType: string; + @Input() resourceType: string; @Input() instanceId: string; @Input() isViewOnly: boolean; @@ -31,7 +33,8 @@ export class ArtifactFormComponent { private descriptionIsValid: boolean; private labelIsValid: boolean; - constructor(private cacheService: CacheService) { + constructor(private cacheService: CacheService, + private artifactConfigService: ArtifactConfigService) { } ngOnInit(): void { @@ -44,7 +47,7 @@ export class ArtifactFormComponent { public onTypeChange = (selectedFileType: IDropDownOption) => { this.artifact.artifactType = selectedFileType.value; this.verifyTypeAndFileWereFilled(); - } + }; public onUploadFile = (file) => { if (file) { @@ -55,34 +58,24 @@ export class ArtifactFormComponent { this.artifact.artifactName = null; } this.verifyTypeAndFileWereFilled(); - } + }; private initArtifactTypes = (): void => { - const artifactTypes: any = this.cacheService.get('UIConfiguration'); - let validExtensions: string[]; let artifactTypesList: string[]; - switch (this.artifactType) { case ArtifactType.DEPLOYMENT: if (this.artifact.artifactType === ArtifactType.HEAT_ENV || this.instanceId) { - validExtensions = artifactTypes.artifacts.deployment.resourceInstanceDeploymentArtifacts; - } else if (this.componentType === ComponentType.RESOURCE) { - validExtensions = artifactTypes.artifacts.deployment.resourceDeploymentArtifacts; + artifactTypesList = this.artifactConfigService.findAllTypeBy(this.artifactType, ComponentType.RESOURCE_INSTANCE, this.resourceType); } else { - validExtensions = artifactTypes.artifacts.deployment.serviceDeploymentArtifacts; - } - if (validExtensions) { - artifactTypesList = Object.keys(validExtensions); + artifactTypesList = this.artifactConfigService.findAllTypeBy(this.artifactType, this.componentType, this.resourceType); } break; case ArtifactType.INFORMATION: - artifactTypesList = artifactTypes.artifacts.other.map((element: any) => { - return element.name; - }); - _.remove(artifactTypesList, (item: string) => { - return _.has(ArtifactType.THIRD_PARTY_RESERVED_TYPES, item) || - _.has(ArtifactType.TOSCA, item); - }); + if (this.instanceId) { + artifactTypesList = this.artifactConfigService.findAllTypeBy(this.artifactType, ComponentType.RESOURCE_INSTANCE, this.resourceType); + } else { + artifactTypesList = this.artifactConfigService.findAllTypeBy(this.artifactType, this.componentType, this.resourceType); + } break; } @@ -94,7 +87,7 @@ export class ArtifactFormComponent { return artifactType.value === this.artifact.artifactType; }); - } + }; // Verify that the Type and the Name (file) are filled in the Modal // For Description and Label - I used this.descriptionIsValid:boolean & this.labelIsValid:boolean as part of the sdc-validation Element @@ -116,17 +109,17 @@ export class ArtifactFormComponent { this.onValidationChange.next(false); } } - } + }; // sdc-validation for Description private onDescriptionChange = (isValid: boolean): void => { this.descriptionIsValid = isValid; this.onValidationChange.next(isValid) && this.verifyTypeAndFileWereFilled(); - } + }; // sdc-validation for Label private onLabelChange = (isValid: boolean): void => { this.labelIsValid = isValid; this.onValidationChange.next(isValid) && this.verifyTypeAndFileWereFilled(); - } + }; } diff --git a/catalog-ui/src/app/ng2/components/forms/artifacts-form/artifacts.service.ts b/catalog-ui/src/app/ng2/components/forms/artifacts-form/artifacts.service.ts index f9400e9d18..ead85a6afa 100644 --- a/catalog-ui/src/app/ng2/components/forms/artifacts-form/artifacts.service.ts +++ b/catalog-ui/src/app/ng2/components/forms/artifacts-form/artifacts.service.ts @@ -25,12 +25,13 @@ export class ArtifactsService { private store: Store) { } - public dispatchArtifactAction = (componentId: string, componentType: string, artifact: ArtifactModel, artifactType: ArtifactGroupType, instanceId: string) => { + public dispatchArtifactAction = (componentId: string, componentType: string, artifact: ArtifactModel, artifactType: ArtifactGroupType, instanceId: string, resourceType?: string) => { const artifactObj = { componentType, componentId, instanceId, - artifact + artifact, + resourceType }; // Create or update instance artifact @@ -48,14 +49,14 @@ export class ArtifactsService { } } - public openArtifactModal = (componentId: string, componentType: string, artifact: ArtifactModel, artifactType: ArtifactGroupType, isViewOnly?: boolean, instanceId?: string) => { + public openArtifactModal = (componentId: string, componentType: string, artifact: ArtifactModel, artifactType: ArtifactGroupType, isViewOnly?: boolean, instanceId?: string, resourceType?: string) => { let modalInstance; const onOkPressed = () => { const updatedArtifact = modalInstance.innerModalContent.instance.artifact; this.serviceLoader.activate(); - this.dispatchArtifactAction(componentId, componentType, updatedArtifact, artifactType, instanceId) + this.dispatchArtifactAction(componentId, componentType, updatedArtifact, artifactType, instanceId, resourceType) .subscribe().add(() => this.serviceLoader.deactivate()); }; @@ -82,6 +83,7 @@ export class ArtifactsService { artifactType, instanceId, componentType, + resourceType: resourceType, isViewOnly }); -- cgit 1.2.3-korg