Merge "Extract getSupplementaryFile out of VFM controller"
authorIttay Stern <ittay.stern@att.com>
Tue, 26 Nov 2019 08:49:59 +0000 (08:49 +0000)
committerGerrit Code Review <gerrit@onap.org>
Tue, 26 Nov 2019 08:49:59 +0000 (08:49 +0000)
vid-webpack-master/src/app/shared/components/genericForm/formControlsServices/basic.control.generator.spec.ts
vid-webpack-master/src/app/shared/components/genericForm/formControlsServices/basic.control.generator.ts
vid-webpack-master/src/app/shared/components/genericForm/formControlsServices/vfModuleGenerator/vfModule.control.generator.ts

index fc2eed4..8705fa8 100644 (file)
@@ -5,6 +5,7 @@ import {FeatureFlagsService} from "../../../services/featureFlag/feature-flags.s
 import {BasicControlGenerator} from "./basic.control.generator";
 import {NgRedux} from '@angular-redux/store';
 import {HttpClientTestingModule, HttpTestingController} from '@angular/common/http/testing';
+import {FileFormControl} from "../../../models/formControlModels/fileFormControl.model";
 
 class MockAppStore<T> {}
 
@@ -44,5 +45,13 @@ describe('Basic Control Generator', () => {
     const legacyRegionControl: FormControlModel = service.getLegacyRegion(instance);
     expect(legacyRegionControl.isVisible).toBeFalsy();
   });
+
+  test('given instance, get supp file from getSupplementaryFile ', () => {
+    const instance = {};
+    const suppFileForInstance: FileFormControl = service.getSupplementaryFile(instance);
+    expect(suppFileForInstance.isVisible).toBeTruthy();
+    expect(suppFileForInstance.hiddenFile.length).toBeGreaterThanOrEqual(1);
+    expect(suppFileForInstance.hiddenFile[0].validations[0].validatorName).toEqual("isFileTooBig");
+  });
 });
 
index cbbff3c..0261a72 100644 (file)
@@ -21,7 +21,9 @@ import {FormGeneralErrorsService} from "../../formGeneralErrors/formGeneralError
 import {Observable, of} from "rxjs";
 import {NodeModel} from "../../../models/nodeModel";
 import {Constants} from "../../../utils/constants";
+import {FileUnit} from "../../formControls/component/file/fileUnit.enum";
 
+const SUPPLEMENTARY_FILE = 'supplementaryFile';
 
 @Injectable()
 export class BasicControlGenerator {
@@ -237,4 +239,56 @@ export class BasicControlGenerator {
     return initialInstanceName;
   }
 
+  getSupplementaryFile(instance: any): FileFormControl {
+    return new FileFormControl({
+      controlName: SUPPLEMENTARY_FILE,
+      displayName: 'Supplementary Data File (JSON format)',
+      dataTestId: 'SupplementaryFile',
+      placeHolder: 'Choose file',
+      selectedFile:  !_.isNil(instance) ? instance.supplementaryFileName: null,
+      isVisible: true,
+      acceptedExtentions: "application/json",
+      hiddenFile : [new InputFormControl({
+        controlName: SUPPLEMENTARY_FILE + "_hidden",
+        isVisible: false,
+        validations: [new ValidatorModel(CustomValidatorOptions.isFileTooBig, "File size exceeds 5MB.", [FileUnit.MB, 5])]
+      }),
+        new InputFormControl({
+          controlName: SUPPLEMENTARY_FILE + "_hidden_content",
+          isVisible: false,
+          validations: [new ValidatorModel(CustomValidatorOptions.isValidJson,
+            "File is invalid, please make sure a legal JSON file is uploaded using name:value pairs.",[]),
+            new ValidatorModel(CustomValidatorOptions.isStringContainTags,
+              "File is invalid, please remove tags <>.",[])],
+          value: !_.isNil(instance) ? (instance.supplementaryFile_hidden_content): null,
+        })
+      ],
+      onDelete : this.getOnDeleteForSupplementaryFile(),
+      onChange : this.getOnChangeForSupplementaryFile()
+    })
+  };
+
+  private getOnDeleteForSupplementaryFile() {
+    return (form: FormGroup) => {
+      form.controls[SUPPLEMENTARY_FILE + "_hidden"].setValue(null);
+      form.controls[SUPPLEMENTARY_FILE + "_hidden_content"].setValue(null);
+    };
+  }
+
+  private getOnChangeForSupplementaryFile() {
+    return (files: FileList, form: FormGroup) => {
+      if (files.length > 0) {
+        const file = files.item(0);
+        let reader = new FileReader();
+        reader.onload = function (event) {
+          form.controls[SUPPLEMENTARY_FILE + "_hidden_content"].setValue(reader.result);
+          form.controls[SUPPLEMENTARY_FILE + "_hidden"].setValue(file);
+        };
+        reader.readAsText(file);
+      } else {
+        form.controls[SUPPLEMENTARY_FILE + "_hidden"].setValue(null);
+        form.controls[SUPPLEMENTARY_FILE + "_hidden_content"].setValue(null);
+      }
+    };
+  }
 }
index 7b72d3b..a2dd521 100644 (file)
@@ -38,7 +38,6 @@ export enum FormControlNames {
   TENANT_ID = 'tenantId',
   ROLLBACK_ON_FAILURE = 'rollbackOnFailure',
   SDN_C_PRE_LOAD = 'sdncPreLoad',
-  SUPPLEMENTARY_FILE = 'supplementaryFile'
 }
 
 
@@ -106,7 +105,7 @@ export class VfModuleControlGenerator {
       result = this.pushInstanceAndVGToForm(result, vfModuleInstance, serviceId, vnfModel, false);
     }
     if(this.store.getState().global.flags['FLAG_SUPPLEMENTARY_FILE']) {
-      let suppFileInput:FileFormControl = <FileFormControl>(this.getSupplementaryFile(vfModuleInstance));
+      let suppFileInput:FileFormControl = <FileFormControl>(this._basicControlGenerator.getSupplementaryFile(vfModuleInstance));
       result.push(suppFileInput);
       result = result.concat(suppFileInput.hiddenFile);
     }
@@ -143,7 +142,7 @@ export class VfModuleControlGenerator {
     result.push(this.getRollbackOnFailureControl(vfModuleInstance, result));
     result.push(this.getSDNCControl(vfModuleInstance, result));
     if(this.store.getState().global.flags['FLAG_SUPPLEMENTARY_FILE']) {
-      let suppFileInput:FileFormControl = <FileFormControl>(this.getSupplementaryFile(vfModuleInstance));
+      let suppFileInput:FileFormControl = <FileFormControl>(this._basicControlGenerator.getSupplementaryFile(vfModuleInstance));
       result.push(suppFileInput);
       result = result.concat(suppFileInput.hiddenFile);
     }
@@ -211,52 +210,6 @@ export class VfModuleControlGenerator {
 
   }
 
-  getSupplementaryFile(instance: any): FormControlModel {
-    return new FileFormControl({
-      controlName: FormControlNames.SUPPLEMENTARY_FILE,
-      displayName: 'Supplementary Data File (JSON format)',
-      dataTestId: 'SupplementaryFile',
-      placeHolder: 'Choose file',
-      selectedFile:  !_.isNil(instance) ? instance.supplementaryFileName: null,
-      isVisible: true,
-      acceptedExtentions: "application/json",
-      hiddenFile : [new InputFormControl({
-        controlName: FormControlNames.SUPPLEMENTARY_FILE + "_hidden",
-        isVisible: false,
-        validations: [new ValidatorModel(CustomValidatorOptions.isFileTooBig, "File size exceeds 5MB.", [FileUnit.MB, 5])]
-      }),
-        new InputFormControl({
-          controlName: FormControlNames.SUPPLEMENTARY_FILE + "_hidden_content",
-          isVisible: false,
-          validations: [new ValidatorModel(CustomValidatorOptions.isValidJson,
-            "File is invalid, please make sure a legal JSON file is uploaded using name:value pairs.",[]),
-            new ValidatorModel(CustomValidatorOptions.isStringContainTags,
-              "File is invalid, please remove tags <>.",[])],
-          value: !_.isNil(instance) ? (instance.supplementaryFile_hidden_content): null,
-        })
-      ],
-      onDelete : (form : FormGroup) => {
-        form.controls[FormControlNames.SUPPLEMENTARY_FILE + "_hidden"].setValue(null);
-        form.controls[FormControlNames.SUPPLEMENTARY_FILE + "_hidden_content"].setValue(null);
-      },
-      onChange : (files: FileList, form : FormGroup)  => {
-        if (files.length > 0) {
-          const file = files.item(0);
-          let reader = new FileReader();
-          reader.onload = function(event) {
-            form.controls[FormControlNames.SUPPLEMENTARY_FILE + "_hidden_content"].setValue(reader.result);
-            form.controls[FormControlNames.SUPPLEMENTARY_FILE + "_hidden"].setValue(file);
-          };
-          reader.readAsText(file);
-        }
-        else {
-          form.controls[FormControlNames.SUPPLEMENTARY_FILE + "_hidden"].setValue(null);
-          form.controls[FormControlNames.SUPPLEMENTARY_FILE + "_hidden_content"].setValue(null);
-        }
-      }
-    })
-  };
-
   getTenantControl = (serviceId: string, instance: any, controls: FormControlModel[]): DropdownFormControl => {
     const service = this.store.getState().service.serviceInstance[serviceId];
     const globalCustomerId: string = service.globalSubscriberId;