2 * ========================LICENSE_START=================================
5 * Copyright (C) 2019 Nordix Foundation
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ========================LICENSE_END===================================
21 import { BrowserAnimationsModule } from "@angular/platform-browser/animations";
22 import { ComponentFixture, TestBed } from "@angular/core/testing";
23 import { HarnessLoader } from "@angular/cdk/testing";
24 import { MatButtonModule } from "@angular/material/button";
25 import { MatButtonHarness } from "@angular/material/button/testing";
30 } from "@angular/material/dialog";
31 import { MatSelectModule } from "@angular/material/select";
32 import { MatInputModule } from "@angular/material/input";
33 import { AbstractControl, ReactiveFormsModule } from "@angular/forms";
34 import { TestbedHarnessEnvironment } from "@angular/cdk/testing/testbed";
35 import { ToastrModule } from "ngx-toastr";
37 import { PolicyService } from "../../services/policy/policy.service";
38 import { ErrorDialogService } from "../../services/ui/error-dialog.service";
39 import { UiService } from "../../services/ui/ui.service";
40 import { PolicyInstanceDialogComponent } from "./policy-instance-dialog.component";
44 CUSTOM_ELEMENTS_SCHEMA,
45 } from "@angular/core";
46 import { TypedPolicyEditorComponent } from "../typed-policy-editor/typed-policy-editor.component";
47 import { RicSelectorComponent } from "../ric-selector/ric-selector.component";
48 import { NoTypePolicyEditorComponent } from "../no-type-policy-editor/no-type-policy-editor.component";
49 import { PolicyTypeSchema } from "../../interfaces/policy.types";
51 describe("PolicyInstanceDialogComponent", () => {
52 const untypedSchema = "{}";
53 const untypedSchemaObject = {
56 schemaObject: untypedSchema,
57 } as PolicyTypeSchema;
59 '{ "description": "Type 1 policy type", "title": "1", "type": "object", "properties": { "priorityLevel": "number" }, "required": [ "priorityLevel" ]}';
60 const typedSchemaObject = {
63 schemaObject: typedSchema,
64 } as PolicyTypeSchema;
66 let component: PolicyInstanceDialogComponent;
67 let fixture: ComponentFixture<PolicyInstanceDialogComponent>;
68 let loader: HarnessLoader;
69 let policyServiceSpy: jasmine.SpyObj<PolicyService>;
70 let errDialogServiceSpy: jasmine.SpyObj<ErrorDialogService>;
72 beforeEach(async () => {
73 policyServiceSpy = jasmine.createSpyObj("PolicyService", ["putPolicy"]);
74 errDialogServiceSpy = jasmine.createSpyObj("ErrorDialogService", [
78 TestBed.configureTestingModule({
80 BrowserAnimationsModule,
86 ToastrModule.forRoot(),
88 schemas: [CUSTOM_ELEMENTS_SCHEMA],
90 PolicyInstanceDialogComponent,
91 RicSelectorStubComponent,
92 NoTypePolicyEditorStubComponent,
93 TypedPolicyEditorStubComponent,
97 { provide: MatDialogRef, useValue: component },
98 { provide: PolicyService, useValue: policyServiceSpy },
99 { provide: ErrorDialogService, useValue: errDialogServiceSpy },
100 { provide: MAT_DIALOG_DATA, useValue: true },
106 describe("content when creating policy without type", () => {
107 beforeEach(async () => {
109 createSchema: untypedSchemaObject,
111 TestBed.overrideProvider(MAT_DIALOG_DATA, { useValue: policyData }); // Should be provided with a policy
112 ({ fixture, component, loader } = compileAndGetComponents(
119 it("should contain oran logo and create title and no instance info", async () => {
120 let ele = fixture.debugElement.nativeElement.querySelector("img");
121 expect(ele.src).toContain("assets/oran-logo.png");
123 ele = fixture.debugElement.nativeElement.querySelector("text");
124 expect(ele.textContent).toEqual(
125 "Create new policy instance of type < No Type >"
128 ele = fixture.debugElement.nativeElement.querySelector("#instanceInfo");
129 expect(ele).toBeFalsy();
132 it("should contain ric select", async () => {
133 const ele = fixture.debugElement.nativeElement.querySelector(
136 expect(ele).toBeTruthy();
139 it("should contain json editor", async () => {
140 const ele = fixture.debugElement.nativeElement.querySelector(
141 "nrcp-no-type-policy-editor"
143 expect(ele).toBeTruthy();
146 it("should contain enabled Close button and disabled Submit button", async () => {
147 component.ngOnInit();
149 let closeButton: MatButtonHarness = await loader.getHarness(
150 MatButtonHarness.with({ selector: "#closeButton" })
152 expect(await closeButton.isDisabled()).toBeFalsy();
153 expect(await closeButton.getText()).toEqual("Close");
155 let submitButton: MatButtonHarness = await loader.getHarness(
156 MatButtonHarness.with({ selector: "#submitButton" })
158 // expect(await submitButton.isDisabled()).toBeTruthy();
159 expect(await submitButton.getText()).toEqual("Submit");
163 describe("content when creating policy with type", () => {
164 beforeEach(async () => {
167 createSchema: typedSchemaObject,
169 TestBed.overrideProvider(MAT_DIALOG_DATA, { useValue: policyData }); // Should be provided with a policy
170 ({ fixture, component, loader } = compileAndGetComponents(
177 it("should contain oran logo and create title and no instance info", async () => {
178 let ele = fixture.debugElement.nativeElement.querySelector("img");
179 expect(ele.src).toContain("assets/oran-logo.png");
181 ele = fixture.debugElement.nativeElement.querySelector("text");
182 expect(ele.textContent).toEqual(
183 "Create new policy instance of type Type 1"
186 ele = fixture.debugElement.nativeElement.querySelector("#instanceInfo");
187 expect(ele).toBeFalsy();
190 it("should contain ric select", async () => {
191 const ele = fixture.debugElement.nativeElement.querySelector(
194 expect(ele).toBeTruthy();
197 it("should contain typed json editor", async () => {
198 const ele = fixture.debugElement.nativeElement.querySelector(
199 "nrcp-typed-policy-editor"
201 expect(ele).toBeTruthy();
204 it("should contain enabled Close button and disabled Submit button", async () => {
205 component.ngOnInit();
207 let closeButton: MatButtonHarness = await loader.getHarness(
208 MatButtonHarness.with({ selector: "#closeButton" })
210 expect(await closeButton.isDisabled()).toBeFalsy();
211 expect(await closeButton.getText()).toEqual("Close");
213 let submitButton: MatButtonHarness = await loader.getHarness(
214 MatButtonHarness.with({ selector: "#submitButton" })
216 // expect(await submitButton.isDisabled()).toBeTruthy();
217 expect(await submitButton.getText()).toEqual("Submit");
221 describe("content when editing policy without type", () => {
222 beforeEach(async () => {
224 createSchema: untypedSchemaObject,
225 instanceId: "instanceId",
226 instanceJson: '{"qosObjectives": {"priorityLevel": 3100}}',
230 TestBed.overrideProvider(MAT_DIALOG_DATA, { useValue: policyData }); // Should be provided with a policy
231 ({ fixture, component, loader } = compileAndGetComponents(
238 it("should contain oran logo and instance info", async () => {
239 let ele = fixture.debugElement.nativeElement.querySelector("img");
240 expect(ele.src).toContain("assets/oran-logo.png");
242 ele = fixture.debugElement.nativeElement.querySelector("text");
243 expect(ele.childNodes[0].childNodes[0]).toBeFalsy(); // No create title
245 ele = fixture.debugElement.nativeElement.querySelector("#instanceInfo");
246 expect(ele).toBeTruthy();
247 expect(ele.innerText).toEqual("[ric1] Instance ID: instanceId");
250 it("should not contain ric select", async () => {
251 const ele = fixture.debugElement.nativeElement.querySelector(
254 expect(ele).toBeFalsy();
257 it("should contain json editor", async () => {
258 const ele = fixture.debugElement.nativeElement.querySelector(
259 "nrcp-no-type-policy-editor"
261 expect(ele).toBeTruthy();
264 it("should contain enabled Close and Submit buttons", async () => {
265 let closeButton: MatButtonHarness = await loader.getHarness(
266 MatButtonHarness.with({ selector: "#closeButton" })
268 expect(await closeButton.isDisabled()).toBeFalsy();
269 expect(await closeButton.getText()).toEqual("Close");
271 let submitButton: MatButtonHarness = await loader.getHarness(
272 MatButtonHarness.with({ selector: "#submitButton" })
274 expect(await submitButton.isDisabled()).toBeFalsy();
275 expect(await submitButton.getText()).toEqual("Submit");
279 describe("content when editing policy with type", () => {
280 beforeEach(async () => {
282 createSchema: typedSchemaObject,
283 instanceId: "instanceId",
284 instanceJson: '{"qosObjectives": {"priorityLevel": 3100}}',
288 TestBed.overrideProvider(MAT_DIALOG_DATA, { useValue: policyData }); // Should be provided with a policy
289 ({ fixture, component, loader } = compileAndGetComponents(
296 it("should contain oran logo and instance info", async () => {
297 let ele = fixture.debugElement.nativeElement.querySelector("img");
298 expect(ele.src).toContain("assets/oran-logo.png");
300 ele = fixture.debugElement.nativeElement.querySelector("text");
301 expect(ele.childNodes[0].childNodes[0]).toBeFalsy(); // No create title
303 ele = fixture.debugElement.nativeElement.querySelector("#instanceInfo");
304 expect(ele).toBeTruthy();
305 expect(ele.innerText).toEqual("[ric1] Instance ID: instanceId");
308 it("should not contain ric select", async () => {
309 const ele = fixture.debugElement.nativeElement.querySelector(
312 expect(ele).toBeFalsy();
315 it("should contain typed json editor", async () => {
316 const ele = fixture.debugElement.nativeElement.querySelector(
317 "nrcp-typed-policy-editor"
319 expect(ele).toBeTruthy();
322 it("should contain enabled Close and Submit buttons", async () => {
323 let closeButton: MatButtonHarness = await loader.getHarness(
324 MatButtonHarness.with({ selector: "#closeButton" })
326 expect(await closeButton.isDisabled()).toBeFalsy();
327 expect(await closeButton.getText()).toEqual("Close");
329 let submitButton: MatButtonHarness = await loader.getHarness(
330 MatButtonHarness.with({ selector: "#submitButton" })
332 expect(await submitButton.isDisabled()).toBeFalsy();
333 expect(await submitButton.getText()).toEqual("Submit");
338 function compileAndGetComponents(
339 fixture: ComponentFixture<PolicyInstanceDialogComponent>,
340 component: PolicyInstanceDialogComponent,
341 loader: HarnessLoader
343 TestBed.compileComponents();
345 fixture = TestBed.createComponent(PolicyInstanceDialogComponent);
346 component = fixture.componentInstance;
347 fixture.detectChanges();
348 loader = TestbedHarnessEnvironment.loader(fixture);
349 return { fixture, component, loader };
353 selector: "nrcp-ric-selecor",
356 { provide: RicSelectorComponent, useClass: RicSelectorStubComponent },
359 class RicSelectorStubComponent {
360 get selectedRic(): string {
366 selector: "nrcp-no-type-policy-editor",
370 provide: NoTypePolicyEditorComponent,
371 useClass: NoTypePolicyEditorStubComponent,
375 class NoTypePolicyEditorStubComponent {
376 get policyJsonTextArea(): AbstractControl {
377 const textArea = { value: "{}" } as AbstractControl;
383 selector: "nrcp-typed-policy-editor",
387 provide: TypedPolicyEditorComponent,
388 useClass: TypedPolicyEditorStubComponent,
392 class TypedPolicyEditorStubComponent {
393 get formIsValid(): boolean {