X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=webapp-frontend%2Fsrc%2Fapp%2Fpolicy%2Fpolicy-instance-dialog%2Fpolicy-instance-dialog.component.spec.ts;h=8881b64cc111ea2c2e95e99d0138e889ea3a9fdc;hb=HEAD;hp=b609904afa5c671e2d951cbfc9453269f8878063;hpb=44b8c1c44248550475dd7e1977f600180c0ca4f3;p=portal%2Fnonrtric-controlpanel.git diff --git a/webapp-frontend/src/app/policy/policy-instance-dialog/policy-instance-dialog.component.spec.ts b/webapp-frontend/src/app/policy/policy-instance-dialog/policy-instance-dialog.component.spec.ts index b609904..8881b64 100644 --- a/webapp-frontend/src/app/policy/policy-instance-dialog/policy-instance-dialog.component.spec.ts +++ b/webapp-frontend/src/app/policy/policy-instance-dialog/policy-instance-dialog.component.spec.ts @@ -39,7 +39,6 @@ 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 { PolicyInstanceDialogComponent } from "./policy-instance-dialog.component"; import { TypedPolicyEditorComponent } from "@policy/typed-policy-editor/typed-policy-editor.component"; @@ -48,26 +47,24 @@ import { NoTypePolicyEditorComponent } from "@policy/no-type-policy-editor/no-ty 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; let loader: HarnessLoader; let dialogRefSpy: MatDialogRef; let policyServiceSpy: jasmine.SpyObj; - let errDialogServiceSpy: jasmine.SpyObj; 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 &&