Extract getSupplementaryFile out of VFM controller

Issue-ID: VID-603
Change-Id: I8ccc2e3cdc79faded73f5811fee99cc78c8ebd47
Signed-off-by: Amir Skalka <amir.skalka@intl.att.com>
Signed-off-by: Eylon Malin <eylon.malin@intl.att.com>
diff --git a/vid-webpack-master/src/app/shared/components/genericForm/formControlsServices/basic.control.generator.spec.ts b/vid-webpack-master/src/app/shared/components/genericForm/formControlsServices/basic.control.generator.spec.ts
index fc2eed4..8705fa8 100644
--- a/vid-webpack-master/src/app/shared/components/genericForm/formControlsServices/basic.control.generator.spec.ts
+++ b/vid-webpack-master/src/app/shared/components/genericForm/formControlsServices/basic.control.generator.spec.ts
@@ -5,6 +5,7 @@
 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 @@
     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");
+  });
 });
 
diff --git a/vid-webpack-master/src/app/shared/components/genericForm/formControlsServices/basic.control.generator.ts b/vid-webpack-master/src/app/shared/components/genericForm/formControlsServices/basic.control.generator.ts
index cbbff3c..0261a72 100644
--- a/vid-webpack-master/src/app/shared/components/genericForm/formControlsServices/basic.control.generator.ts
+++ b/vid-webpack-master/src/app/shared/components/genericForm/formControlsServices/basic.control.generator.ts
@@ -21,7 +21,9 @@
 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 @@
     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);
+      }
+    };
+  }
 }
diff --git a/vid-webpack-master/src/app/shared/components/genericForm/formControlsServices/vfModuleGenerator/vfModule.control.generator.ts b/vid-webpack-master/src/app/shared/components/genericForm/formControlsServices/vfModuleGenerator/vfModule.control.generator.ts
index 7b72d3b..a2dd521 100644
--- a/vid-webpack-master/src/app/shared/components/genericForm/formControlsServices/vfModuleGenerator/vfModule.control.generator.ts
+++ b/vid-webpack-master/src/app/shared/components/genericForm/formControlsServices/vfModuleGenerator/vfModule.control.generator.ts
@@ -38,7 +38,6 @@
   TENANT_ID = 'tenantId',
   ROLLBACK_ON_FAILURE = 'rollbackOnFailure',
   SDN_C_PRE_LOAD = 'sdncPreLoad',
-  SUPPLEMENTARY_FILE = 'supplementaryFile'
 }
 
 
@@ -106,7 +105,7 @@
       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 @@
     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 @@
 
   }
 
-  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;