CI: Migrate Sonar Scan job to GHA
[portal/nonrtric-controlpanel.git] / webapp-frontend / src / app / policy / policy-instance-dialog / policy-instance-dialog.component.spec.ts
index 3bd9cd7..8881b64 100644 (file)
@@ -19,6 +19,8 @@
  */
 
 import { BrowserAnimationsModule } from "@angular/platform-browser/animations";
+import { By } from "@angular/platform-browser";
+import { ChangeDetectorRef, CUSTOM_ELEMENTS_SCHEMA } from "@angular/core";
 import { ComponentFixture, TestBed } from "@angular/core/testing";
 import { HarnessLoader } from "@angular/cdk/testing";
 import { MatButtonModule } from "@angular/material/button";
@@ -30,44 +32,39 @@ import {
 } from "@angular/material/dialog";
 import { MatSelectModule } from "@angular/material/select";
 import { MatInputModule } from "@angular/material/input";
+import { of } from "rxjs";
 import { ReactiveFormsModule } from "@angular/forms";
 import { TestbedHarnessEnvironment } from "@angular/cdk/testing/testbed";
 import { ToastrModule } from "ngx-toastr";
 import { MockComponent } from "ng-mocks";
 
-import { PolicyService } from "../../services/policy/policy.service";
-import { ErrorDialogService } from "../../services/ui/error-dialog.service";
-import { UiService } from "../../services/ui/ui.service";
+import { PolicyService } from "@services/policy/policy.service";
+import { UiService } from "@services/ui/ui.service";
 import { PolicyInstanceDialogComponent } from "./policy-instance-dialog.component";
-import { ChangeDetectorRef, CUSTOM_ELEMENTS_SCHEMA } from "@angular/core";
-import { TypedPolicyEditorComponent } from "../typed-policy-editor/typed-policy-editor.component";
-import { RicSelectorComponent } from "../ric-selector/ric-selector.component";
-import { NoTypePolicyEditorComponent } from "../no-type-policy-editor/no-type-policy-editor.component";
-import { By } from "@angular/platform-browser";
-import { CreatePolicyInstance } from "src/app/interfaces/policy.types";
-import { of } from "rxjs";
-import { NotificationService } from "src/app/services/ui/notification.service";
+import { TypedPolicyEditorComponent } from "@policy/typed-policy-editor/typed-policy-editor.component";
+import { RicSelectorComponent } from "@policy/ric-selector/ric-selector.component";
+import { NoTypePolicyEditorComponent } from "@policy/no-type-policy-editor/no-type-policy-editor.component";
+import { CreatePolicyInstance } from "@interfaces/policy.types";
+import { NotificationService } from "@services/ui/notification.service";
 import * as uuid from "uuid";
+import { HttpErrorResponse } from "@angular/common/http";
 
 describe("PolicyInstanceDialogComponent", () => {
-  const untypedSchema = "{}";
-  const typedSchema =
-    '{ "description": "Type 1 policy type", "title": "1", "type": "object", "properties": { "priorityLevel": "number" }}';
+  const untypedSchema = JSON.parse("{}");
+  const typedSchema = JSON.parse(
+    '{ "description": "Type 1 policy type", "title": "1", "type": "object", "properties": { "priorityLevel": "number" }}'
+  );
 
   let component: PolicyInstanceDialogComponent;
   let fixture: ComponentFixture<PolicyInstanceDialogComponent>;
   let loader: HarnessLoader;
   let dialogRefSpy: MatDialogRef<PolicyInstanceDialogComponent>;
   let policyServiceSpy: jasmine.SpyObj<PolicyService>;
-  let errDialogServiceSpy: jasmine.SpyObj<ErrorDialogService>;
   let notificationServiceSpy: NotificationService;
 
   beforeEach(async () => {
     dialogRefSpy = jasmine.createSpyObj("MatDialogRef", ["close"]);
     policyServiceSpy = jasmine.createSpyObj("PolicyService", ["putPolicy"]);
-    errDialogServiceSpy = jasmine.createSpyObj("ErrorDialogService", [
-      "displayError",
-    ]);
     notificationServiceSpy = jasmine.createSpyObj("NotificationService", [
       "success",
     ]);
@@ -93,7 +90,6 @@ describe("PolicyInstanceDialogComponent", () => {
         ChangeDetectorRef,
         { provide: MatDialogRef, useValue: dialogRefSpy },
         { provide: PolicyService, useValue: policyServiceSpy },
-        { provide: ErrorDialogService, useValue: errDialogServiceSpy },
         { provide: NotificationService, useValue: notificationServiceSpy },
         { provide: MAT_DIALOG_DATA, useValue: true },
         UiService,
@@ -199,7 +195,7 @@ describe("PolicyInstanceDialogComponent", () => {
       expect(await submitButton.isDisabled()).toBeFalsy();
     });
 
-    it("should generate policy ID when submitting new policy", async () => {
+    it("should generate policy ID when submitting new policy and close dialog", async () => {
       const ricSelector: RicSelectorComponent = fixture.debugElement.query(
         By.directive(RicSelectorComponent)
       ).componentInstance;
@@ -210,17 +206,41 @@ describe("PolicyInstanceDialogComponent", () => {
         MatButtonHarness.with({ selector: "#submitButton" })
       );
 
-      spyOn(uuid, "v4").and.returnValue(1234567890);
+      spyOn(uuid, "v4").and.returnValue("1234567890");
       ricSelector.selectedRic.emit("ric1");
       noTypePolicyEditor.validJson.emit("{}");
+
+      policyServiceSpy.putPolicy.and.returnValue(of("Success"));
+
       await submitButton.click();
 
       const policyInstance = {} as CreatePolicyInstance;
-      policyInstance.policy_data = "{}";
+      policyInstance.policy_data = JSON.parse("{}");
       policyInstance.policy_id = "1234567890";
+      policyInstance.policytype_id = "";
       policyInstance.ric_id = "ric1";
       policyInstance.service_id = "controlpanel";
       expect(policyServiceSpy.putPolicy).toHaveBeenCalledWith(policyInstance);
+
+      expect(dialogRefSpy.close).toHaveBeenCalledWith("ok");
+    });
+
+    it("should not close dialog when error from server", async () => {
+      let submitButton: MatButtonHarness = await loader.getHarness(
+        MatButtonHarness.with({ selector: "#submitButton" })
+      );
+
+      const errorResponse = {
+        status: 400,
+        statusText: "Bad Request",
+      } as HttpErrorResponse;
+      policyServiceSpy.putPolicy.and.returnValue(errorResponse);
+
+      await submitButton.click();
+
+      expect(policyServiceSpy.putPolicy).toHaveBeenCalled();
+
+      expect(dialogRefSpy.close).not.toHaveBeenCalled();
     });
   });
 
@@ -307,13 +327,14 @@ describe("PolicyInstanceDialogComponent", () => {
   });
 
   describe("content when editing policy without type", () => {
-    const instanceJson = '{"qosObjectives": {"priorityLevel": 3100}}';
+    const instanceJson = JSON.parse(
+      '{"qosObjectives": {"priorityLevel": 3100}}'
+    );
     beforeEach(async () => {
       const policyData = {
         createSchema: untypedSchema,
         instanceId: "instanceId",
         instanceJson: instanceJson,
-        name: "Type 1",
         ric: "ric1",
       };
       TestBed.overrideProvider(MAT_DIALOG_DATA, { useValue: policyData }); // Should be provided with a policy
@@ -349,9 +370,7 @@ describe("PolicyInstanceDialogComponent", () => {
         By.directive(NoTypePolicyEditorComponent)
       ).componentInstance;
       expect(noTypePolicyEditor).toBeTruthy();
-      expect(unescapeQuotes(noTypePolicyEditor.policyJson)).toEqual(
-        instanceJson
-      );
+      expect(noTypePolicyEditor.policyJson).toEqual(instanceJson);
     });
 
     it("should contain enabled Close and Submit buttons when all inputs are valid", async () => {
@@ -379,6 +398,7 @@ describe("PolicyInstanceDialogComponent", () => {
       const policyInstance = {} as CreatePolicyInstance;
       policyInstance.policy_data = instanceJson;
       policyInstance.policy_id = "instanceId";
+      policyInstance.policytype_id = "";
       policyInstance.ric_id = "ric1";
       policyInstance.service_id = "controlpanel";
       expect(policyServiceSpy.putPolicy).toHaveBeenCalledWith(policyInstance);
@@ -474,11 +494,14 @@ function unescapeQuotes(string: string): string {
 }
 
 function policyTester(first, second) {
-  if (typeof first === "object" && typeof second === "object") {
-    const policy1 = first as CreatePolicyInstance;
-    const policy2 = second as CreatePolicyInstance;
+  if (typeof first[0] === "object" && typeof second[0] === "object") {
+    const policy1 = first[0] as CreatePolicyInstance;
+    const policy2 = second[0] as CreatePolicyInstance;
     return (
-      policy1.policy_data === policy2.policy_data &&
+      typeof policy1.policy_data === "object" &&
+      typeof policy2.policy_data === "object" &&
+      JSON.stringify(policy1.policy_data) ===
+        JSON.stringify(policy2.policy_data) &&
       policy1.policy_id === policy2.policy_id &&
       policy1.policytype_id === policy2.policytype_id &&
       policy1.ric_id === policy2.ric_id &&