diff options
author | 2019-12-18 21:04:38 +0200 | |
---|---|---|
committer | 2019-12-22 14:01:45 +0200 | |
commit | 4c87ecaa8cce40f078f8540a15928e7e805e4ef7 (patch) | |
tree | f31c55200451d13f108c8583b8d275ae7a7c8bab /vid-webpack-master/src/app/shared/components/genericForm/formControlsServices/sharedControlles | |
parent | 54af5cdca044c7b486f02f4163d16c39e990f701 (diff) |
Refactor Generic Generators
Issue-ID: VID-731
Signed-off-by: Yoav Schneiderman <yoav.schneiderman@intl.att.com>
Change-Id: Ib851ff4056d64e12eaf415c5d689c832196ec8b0
Signed-off-by: Yoav Schneiderman <yoav.schneiderman@intl.att.com>
Diffstat (limited to 'vid-webpack-master/src/app/shared/components/genericForm/formControlsServices/sharedControlles')
2 files changed, 452 insertions, 0 deletions
diff --git a/vid-webpack-master/src/app/shared/components/genericForm/formControlsServices/sharedControlles/shared.controllers.service.spec.ts b/vid-webpack-master/src/app/shared/components/genericForm/formControlsServices/sharedControlles/shared.controllers.service.spec.ts new file mode 100644 index 000000000..58ee33b53 --- /dev/null +++ b/vid-webpack-master/src/app/shared/components/genericForm/formControlsServices/sharedControlles/shared.controllers.service.spec.ts @@ -0,0 +1,263 @@ +import * as _ from "lodash"; +import {getTestBed, TestBed} from '@angular/core/testing'; +import {SharedControllersService} from "./shared.controllers.service"; +import {HttpClientTestingModule, HttpTestingController} from "@angular/common/http/testing"; +import {AppState} from "../../../../store/reducers"; +import {NgRedux} from "@angular-redux/store"; +import {AaiService} from "../../../../services/aaiService/aai.service"; +import {FeatureFlagsService} from "../../../../services/featureFlag/feature-flags.service"; +import {DropdownFormControl} from "../../../../models/formControlModels/dropdownFormControl.model"; +import {FormControlModel, ValidatorOptions} from "../../../../models/formControlModels/formControl.model"; +import {ControlGeneratorUtil} from "../control.generator.util.service"; + + + +describe('Shared Controllers Service', () => { + let injector; + let service: SharedControllersService; + let httpMock: HttpTestingController; + let store: NgRedux<AppState>; + let basicControlGenerator : ControlGeneratorUtil; + + beforeAll(done => (async () => { + TestBed.configureTestingModule({ + imports: [HttpClientTestingModule], + providers: [SharedControllersService, + AaiService, + ControlGeneratorUtil, + {provide:FeatureFlagsService, useClass: MockFeatureFlagsService}, + {provide: NgRedux, useClass: MockAppStore}] + }); + await TestBed.compileComponents(); + + injector = getTestBed(); + service = injector.get(SharedControllersService); + basicControlGenerator = injector.get(ControlGeneratorUtil); + httpMock = injector.get(HttpTestingController); + store = injector.get(NgRedux); + + })().then(done).catch(done.fail)); + + + + + + test('getLineOfBusinessControl', ()=> { + const lineOfBusinessControl :DropdownFormControl = service.getLineOfBusinessControl(); + expect(lineOfBusinessControl.name).toEqual('lineOfBusiness'); + expect(lineOfBusinessControl.controlName).toEqual('lineOfBusiness'); + expect(lineOfBusinessControl.displayName).toEqual('Line of business'); + expect(lineOfBusinessControl.dataTestId).toEqual('lineOfBusiness'); + expect(lineOfBusinessControl.placeHolder).toEqual('Select Line Of Business'); + expect(lineOfBusinessControl.onInitSelectedField).toEqual(['lineOfBusinessList']); + expect(lineOfBusinessControl.onInit).toBeDefined(); + expect(lineOfBusinessControl.value).toBeNull(); + expect(lineOfBusinessControl.validations.find((validation)=> validation.validatorName === ValidatorOptions.required)).toBeDefined(); + expect(lineOfBusinessControl.isDisabled).toBeFalsy(); + }); + + test('getTenantControl', ()=> { + const serviceId : string = Object.keys(store.getState().service.serviceInstance)[0]; + const vnfs = store.getState().service.serviceInstance[serviceId].vnfs; + const currentVnf = vnfs[Object.keys(vnfs)[0]]; + + const tanantControl :DropdownFormControl = service.getTenantControl(serviceId, currentVnf); + expect(tanantControl.name).toEqual('tenant'); + expect(tanantControl.controlName).toEqual('tenantId'); + expect(tanantControl.displayName).toEqual('Tenant'); + expect(tanantControl.dataTestId).toEqual('tenant'); + expect(tanantControl.placeHolder).toEqual('Select Tenant'); + expect(tanantControl.onInitSelectedField).toEqual(['lcpRegionsTenantsMap', currentVnf.lcpCloudRegionId]); + expect(tanantControl.onInit).toBeDefined(); + expect(tanantControl.validations.find((validation)=> validation.validatorName === ValidatorOptions.required)).toBeDefined(); + expect(tanantControl.isDisabled).toEqual(_.isNil(currentVnf.lcpCloudRegionId)); + }); + + + test('getRollbackOnFailureControl', ()=> { + const rollbackOnFailureControl :DropdownFormControl = service.getRollbackOnFailureControl(); + expect(rollbackOnFailureControl.controlName).toEqual('rollbackOnFailure'); + expect(rollbackOnFailureControl.displayName).toEqual('Rollback on failure'); + expect(rollbackOnFailureControl.dataTestId).toEqual('rollback'); + expect(rollbackOnFailureControl.placeHolder).toEqual('Rollback on failure'); + expect(rollbackOnFailureControl.onInit).toBeDefined(); + expect(rollbackOnFailureControl.validations.find((validation)=> validation.validatorName === ValidatorOptions.required)).toBeDefined(); + expect(rollbackOnFailureControl.isDisabled).toBeFalsy(); + }); + + test('getLcpRegionControl', ()=> { + const serviceId : string = Object.keys(store.getState().service.serviceInstance)[0]; + const vnfs = store.getState().service.serviceInstance[serviceId].vnfs; + const currentVnf = vnfs[Object.keys(vnfs)[0]]; + const lcpRegionControl :DropdownFormControl = service.getLcpRegionControl(serviceId, currentVnf, []); + expect(lcpRegionControl.controlName).toEqual('lcpCloudRegionId'); + expect(lcpRegionControl.displayName).toEqual('LCP region'); + expect(lcpRegionControl.dataTestId).toEqual('lcpRegion'); + expect(lcpRegionControl.placeHolder).toEqual('Select LCP Region'); + expect(lcpRegionControl.onInit).toBeDefined(); + expect(lcpRegionControl.onChange).toBeDefined(); + expect(lcpRegionControl.validations.find((validation)=> validation.validatorName === ValidatorOptions.required)).toBeDefined(); + expect(lcpRegionControl.isDisabled).toBeFalsy(); + }); + + test('sdn-preload checkbox is visible', () => { + const instance = {}; + const sdncPreload: FormControlModel = service.getSDNCControl(instance); + expect (sdncPreload.displayName).toEqual('SDN-C pre-load'); + expect (sdncPreload.value).toBeFalsy(); + }); + + test('getlegacyRegion with AAIAIC25 - isVisible true', () => { + const instance = {lcpCloudRegionId : 'AAIAIC25'}; + const legacyRegionControl: FormControlModel = service.getLegacyRegion(instance); + expect(legacyRegionControl.isVisible).toBeTruthy(); + }); + + test('getlegacyRegion without AAIAIC25 - isVisible false', () => { + const instance = {lcpCloudRegionId : 'olson3'}; + const legacyRegionControl: FormControlModel = service.getLegacyRegion(instance); + expect(legacyRegionControl.isVisible).toBeFalsy(); + }); +}); + +class MockAppStore<T> { + getState() { + return { + "global": { + "flags": { + "FLAG_NETWORK_TO_ASYNC_INSTANTIATION": false, + "FLAG_SHOW_ASSIGNMENTS": true, + "FLAG_FABRIC_CONFIGURATION_ASSIGNMENTS": true, + "FLAG_SHOW_VERIFY_SERVICE": false, + "FLAG_SERVICE_MODEL_CACHE": true, + "FLAG_ADD_MSO_TESTAPI_FIELD": true + } + }, + "service": { + "serviceHierarchy": { + "4117a0b6-e234-467d-b5b9-fe2f68c8b0fc": { + "service": { + "uuid": "4117a0b6-e234-467d-b5b9-fe2f68c8b0fc", + "invariantUuid": "e49fbd11-e60c-4a8e-b4bf-30fbe8f4fcc0", + "name": "ComplexService", + "version": "1.0", + "toscaModelURL": null, + "category": "Emanuel", + "serviceType": "", + "serviceRole": "", + "description": "ComplexService", + "serviceEcompNaming": "false", + "instantiationType": "Macro", + "inputs": {} + }, + "vnfs": { + "VF_vGeraldine 0": { + "uuid": "d6557200-ecf2-4641-8094-5393ae3aae60", + "invariantUuid": "4160458e-f648-4b30-a176-43881ffffe9e", + "description": "VSP_vGeraldine", + "name": "VF_vGeraldine", + "version": "2.0", + "customizationUuid": "91415b44-753d-494c-926a-456a9172bbb9", + "inputs": {}, + "commands": {}, + "properties": { + "max_instances": 10, + "min_instances": 1, + + }, + "type": "VF", + "modelCustomizationName": "VF_vGeraldine 0", + "vfModules": { + "vf_vgeraldine0..VfVgeraldine..vflorence_vlc..module-1": { + "uuid": "522159d5-d6e0-4c2a-aa44-5a542a12a830", + "invariantUuid": "98a7c88b-b577-476a-90e4-e25a5871e02b", + "customizationUuid": "55b1be94-671a-403e-a26c-667e9c47d091", + "description": null, + "name": "VfVgeraldine..vflorence_vlc..module-1", + "version": "2", + "modelCustomizationName": "VfVgeraldine..vflorence_vlc..module-1", + "properties": { + "minCountInstances": 0, + "maxCountInstances": null, + "initialCount": 0, + "vfModuleLabel": "vflorence_vlc" + }, + "inputs": {}, + "volumeGroupAllowed": true + } + } + } + } + } + }, + "serviceInstance": { + "4117a0b6-e234-467d-b5b9-fe2f68c8b0fc": { + "vnfs": { + "2017-388_PASQUALE-vPE 0": { + "action": "Create", + "inMaint": false, + "rollbackOnFailure": "true", + "originalName": "2017-388_PASQUALE-vPE 0", + "isMissingData": false, + "trackById": "eymgwlevh54", + "vfModules": {}, + "vnfStoreKey": "2017-388_PASQUALE-vPE 0", + "uuid": "afacccf6-397d-45d6-b5ae-94c39734b168", + "productFamilyId": "e433710f-9217-458d-a79d-1c7aff376d89", + "lcpCloudRegionId": "AAIAIC25", + "tenantId": "092eb9e8e4b7412e8787dd091bc58e86", + "lineOfBusiness": "ONAP", + "platformName": "platform", + "modelInfo": { + "modelInvariantId": "72e465fe-71b1-4e7b-b5ed-9496118ff7a8", + "modelVersionId": "afacccf6-397d-45d6-b5ae-94c39734b168", + "modelName": "2017-388_PASQUALE-vPE", + "modelVersion": "4.0", + "modelCustomizationId": "b3c76f73-eeb5-4fb6-9d31-72a889f1811c", + "modelCustomizationName": "2017-388_PASQUALE-vPE 0", + "uuid": "afacccf6-397d-45d6-b5ae-94c39734b168", + "modelUniqueId": "b3c76f73-eeb5-4fb6-9d31-72a889f1811c" + }, + "instanceName": "2017-388_PASQUALE-vPEAjXzainstanceName", + "legacyRegion": "some legacy region", + "instanceParams": [ + { + "vnf_config_template_version": "17.2", + "bandwidth_units": "Gbps", + "bandwidth": "10", + "AIC_CLLI": "ATLMY8GA", + "ASN": "AV_vPE", + "vnf_instance_name": "mtnj309me6" + } + ] + } + }, + "service": { + "vidNotions": { + "instantiationUI": "serviceWithVRF", + "modelCategory": "other", + "viewEditUI": "serviceWithVRF", + "instantiationType": "ALaCarte" + }, + "uuid": "4117a0b6-e234-467d-b5b9-fe2f68c8b0fc", + "invariantUuid": "7ee41ce4-4827-44b0-a48e-2707a59905d2", + "name": "VRF Service for Test", + "version": "1.0", + "toscaModelURL": null, + "category": "Network L4+", + "serviceType": "INFRASTRUCTURE", + "serviceRole": "Configuration", + "description": "xxx", + "serviceEcompNaming": "true", + "instantiationType": "A-La-Carte", + "inputs": {} + }, + "isALaCarte": true + } + } + } + } + } +} + +class MockFeatureFlagsService {} diff --git a/vid-webpack-master/src/app/shared/components/genericForm/formControlsServices/sharedControlles/shared.controllers.service.ts b/vid-webpack-master/src/app/shared/components/genericForm/formControlsServices/sharedControlles/shared.controllers.service.ts new file mode 100644 index 000000000..e82ea5d85 --- /dev/null +++ b/vid-webpack-master/src/app/shared/components/genericForm/formControlsServices/sharedControlles/shared.controllers.service.ts @@ -0,0 +1,189 @@ +import {Injectable} from "@angular/core"; +import {DropdownFormControl} from "../../../../models/formControlModels/dropdownFormControl.model"; +import {FormControlType} from "../../../../models/formControlModels/formControlTypes.enum"; +import { + FormControlModel, + ValidatorModel, + ValidatorOptions +} from "../../../../models/formControlModels/formControl.model"; +import {NgRedux} from "@angular-redux/store"; +import {AppState} from "../../../../store/reducers"; +import {AaiService} from "../../../../services/aaiService/aai.service"; +import {ControlGeneratorUtil, SDN_C_PRE_LOAD} from "../control.generator.util.service"; +import * as _ from "lodash"; +import {FormGroup} from "@angular/forms"; +import {Constants} from "../../../../utils/constants"; +import {CheckboxFormControl} from "../../../../models/formControlModels/checkboxFormControl.model"; +import {InputFormControl} from "../../../../models/formControlModels/inputFormControl.model"; +import {NodeModel} from "../../../../models/nodeModel"; + +@Injectable() +export class SharedControllersService { + constructor(private _store : NgRedux<AppState>, + private _aaiService : AaiService, + private _basicControlGenerator : ControlGeneratorUtil){} + + + getLineOfBusinessControl = (instance?: any): DropdownFormControl => { + return new DropdownFormControl({ + type: FormControlType.DROPDOWN, + controlName: 'lineOfBusiness', + displayName: 'Line of business', + dataTestId: 'lineOfBusiness', + placeHolder: 'Select Line Of Business', + isDisabled: false, + name: "lineOfBusiness", + value: instance ? instance.lineOfBusiness : null, + validations: [new ValidatorModel(ValidatorOptions.required, 'is required')], + onInitSelectedField: ['lineOfBusinessList'], + onInit: this._basicControlGenerator.getSubscribeInitResult.bind(null, this._aaiService.getCategoryParameters) + }) + }; + + getTenantControl = (serviceId: string, instance?: any): DropdownFormControl => { + const service = this._store.getState().service.serviceInstance[serviceId]; + const globalCustomerId: string = service.globalSubscriberId; + const serviceType: string = service.subscriptionServiceType; + return new DropdownFormControl({ + type: FormControlType.DROPDOWN, + controlName: 'tenantId', + displayName: 'Tenant', + dataTestId: 'tenant', + placeHolder: 'Select Tenant', + name: 'tenant', + isDisabled: _.isNil(instance) || _.isNil(instance.lcpCloudRegionId), + onInitSelectedField: instance ? ['lcpRegionsTenantsMap', instance.lcpCloudRegionId] : null, + value: instance ? instance.tenantId : null, + validations: [new ValidatorModel(ValidatorOptions.required, 'is required')], + onInit: instance ? this._basicControlGenerator.getSubscribeInitResult.bind( + this._aaiService, + this._aaiService.getLcpRegionsAndTenants.bind(this, globalCustomerId, serviceType)) : () => { + } + }) + }; + + getRollbackOnFailureControl = (instance?: any): DropdownFormControl => { + return new DropdownFormControl({ + type: FormControlType.DROPDOWN, + controlName: 'rollbackOnFailure', + displayName: 'Rollback on failure', + dataTestId: 'rollback', + placeHolder: 'Rollback on failure', + isDisabled: false, + validations: [new ValidatorModel(ValidatorOptions.required, 'is required')], + value: instance ? instance.rollbackOnFailure : 'true', + onInit: this._basicControlGenerator.getSubscribeInitResult.bind(null, this._basicControlGenerator.getRollBackOnFailureOptions) + }) + }; + + getLegacyRegion(instance: any): FormControlModel { + return new InputFormControl({ + controlName: 'legacyRegion', + displayName: 'Legacy Region', + dataTestId: 'lcpRegionText', + placeHolder: 'Type Legacy Region', + validations: [], + isVisible: this._basicControlGenerator.isLegacyRegionShouldBeVisible(instance), + isDisabled : _.isNil(instance) ? true : Constants.LegacyRegion.MEGA_REGION.indexOf(instance.lcpCloudRegionId), + value: instance ? instance.legacyRegion : null + }); + } + + getLcpRegionControl = (serviceId: string, instance: any, controls: FormControlModel[]): DropdownFormControl => { + const service = this._store.getState().service.serviceInstance[serviceId]; + const globalCustomerId: string = service.globalSubscriberId; + const serviceType: string = service.subscriptionServiceType; + return new DropdownFormControl({ + type: FormControlType.DROPDOWN, + controlName: 'lcpCloudRegionId', + displayName: 'LCP region', + dataTestId: 'lcpRegion', + placeHolder: 'Select LCP Region', + name: "lcpRegion", + isDisabled: false, + value: instance ? instance.lcpCloudRegionId : null, + validations: [new ValidatorModel(ValidatorOptions.required, 'is required')], + onInitSelectedField: ['lcpRegionList'], + onInit: this._basicControlGenerator.getSubscribeInitResult.bind( + this._aaiService, + this._aaiService.getLcpRegionsAndTenants.bind(this, globalCustomerId, serviceType)), + onChange: (param: string, form: FormGroup) => { + form.controls['tenantId'].enable(); + form.controls['tenantId'].reset(); + if (!_.isNil(globalCustomerId) && !_.isNil(serviceType)) { + this._basicControlGenerator.getSubscribeResult.bind(this, this._aaiService.getLcpRegionsAndTenants(globalCustomerId, serviceType).subscribe(res => { + controls.find(item => item.controlName === 'tenantId')['options$'] = res.lcpRegionsTenantsMap[param]; + if (res.lcpRegionsTenantsMap[param]) { + controls.find(item => item.controlName === 'tenantId')['hasEmptyOptions'] = res.lcpRegionsTenantsMap[param].length === 0; + } + })); + } + + if (Constants.LegacyRegion.MEGA_REGION.indexOf(param) !== -1) { + form.controls['legacyRegion'].enable(); + controls.find(item => item.controlName === 'legacyRegion').isVisible = true; + + } else { + controls.find(item => item.controlName === 'legacyRegion').isVisible = false; + form.controls['legacyRegion'].setValue(null); + form.controls['legacyRegion'].reset(); + form.controls['legacyRegion'].disable(); + } + } + }) + }; + + getSDNCControl = (instance: any): FormControlModel => { + return new CheckboxFormControl({ + controlName: SDN_C_PRE_LOAD, + displayName: 'SDN-C pre-load', + dataTestId: 'sdncPreLoad', + value: instance ? instance.sdncPreLoad : false, + validations: [new ValidatorModel(ValidatorOptions.required, 'is required')] + }) + }; + + getProductFamilyControl = (instance : any, controls : FormControlModel[], isMandatory?: boolean) : DropdownFormControl => { + return new DropdownFormControl({ + type : FormControlType.DROPDOWN, + controlName : 'productFamilyId', + displayName : 'Product family', + dataTestId : 'productFamily', + placeHolder : 'Select Product Family', + isDisabled : false, + name : "product-family-select", + value : instance ? instance.productFamilyId : null, + validations : _.isNil(isMandatory) || isMandatory === true ? [new ValidatorModel(ValidatorOptions.required, 'is required')]: [], + onInit : this._basicControlGenerator.getSubscribeResult.bind(this, this._aaiService.getProductFamilies), + }) + }; + + getInstanceNameController(instance: any, serviceId: string, isEcompGeneratedNaming: boolean, model: NodeModel): FormControlModel { + let validations: ValidatorModel[] = this._basicControlGenerator.createValidationsForInstanceName(instance, serviceId, isEcompGeneratedNaming); + return new InputFormControl({ + controlName: 'instanceName', + displayName: 'Instance name', + dataTestId: 'instanceName', + placeHolder: (!isEcompGeneratedNaming) ? 'Instance name' : 'Automatically generated when not provided', + validations: validations, + isVisible : true, + value : (!isEcompGeneratedNaming || (!_.isNil(instance) && !_.isNil(instance.instanceName))) + ? this._basicControlGenerator.getDefaultInstanceName(instance, model) : null, + onKeypress : (event) => { + const pattern:RegExp = ControlGeneratorUtil.INSTANCE_NAME_REG_EX; + if(pattern){ + if(!pattern.test(event['key'])){ + event.preventDefault(); + } + } + return event; + } + }); + } + + getInstanceName(instance : any, serviceId : string, isEcompGeneratedNaming: boolean): FormControlModel { + let formControlModel:FormControlModel = this.getInstanceNameController(instance, serviceId, isEcompGeneratedNaming, new NodeModel()); + formControlModel.value = instance ? instance.instanceName : null; + return formControlModel; + } +} |