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";
39 } from "@angular/forms";
40 import { TestbedHarnessEnvironment } from "@angular/cdk/testing/testbed";
41 import { ToastrModule } from "ngx-toastr";
43 import { PolicyService } from "../../services/policy/policy.service";
44 import { ErrorDialogService } from "../../services/ui/error-dialog.service";
45 import { UiService } from "../../services/ui/ui.service";
46 import { PolicyInstanceDialogComponent } from "./policy-instance-dialog.component";
50 CUSTOM_ELEMENTS_SCHEMA,
52 } from "@angular/core";
53 import { TypedPolicyEditorComponent } from "../typed-policy-editor/typed-policy-editor.component";
54 import { RicSelectorComponent } from "../ric-selector/ric-selector.component";
55 import { NoTypePolicyEditorComponent } from "../no-type-policy-editor/no-type-policy-editor.component";
56 import { By } from "@angular/platform-browser";
58 describe("PolicyInstanceDialogComponent", () => {
59 const untypedSchema = "{}";
61 '{ "description": "Type 1 policy type", "title": "1", "type": "object", "properties": { "priorityLevel": "number" }}';
63 let component: PolicyInstanceDialogComponent;
64 let fixture: ComponentFixture<PolicyInstanceDialogComponent>;
65 let loader: HarnessLoader;
66 let policyServiceSpy: jasmine.SpyObj<PolicyService>;
67 let errDialogServiceSpy: jasmine.SpyObj<ErrorDialogService>;
69 beforeEach(async () => {
70 policyServiceSpy = jasmine.createSpyObj("PolicyService", ["putPolicy"]);
71 errDialogServiceSpy = jasmine.createSpyObj("ErrorDialogService", [
75 TestBed.configureTestingModule({
77 BrowserAnimationsModule,
83 ToastrModule.forRoot(),
85 schemas: [CUSTOM_ELEMENTS_SCHEMA],
87 PolicyInstanceDialogComponent,
88 RicSelectorStubComponent,
89 NoTypePolicyEditorStubComponent,
90 TypedPolicyEditorStubComponent,
94 { provide: MatDialogRef, useValue: component },
95 { provide: PolicyService, useValue: policyServiceSpy },
96 { provide: ErrorDialogService, useValue: errDialogServiceSpy },
97 { provide: MAT_DIALOG_DATA, useValue: true },
103 describe("content when creating policy without type", () => {
104 beforeEach(async () => {
106 createSchema: untypedSchema,
108 TestBed.overrideProvider(MAT_DIALOG_DATA, { useValue: policyData }); // Should be provided with a policy
109 ({ fixture, component, loader } = compileAndGetComponents(
116 it("should contain oran logo and create title and no instance info", async () => {
117 let ele = fixture.debugElement.nativeElement.querySelector("img");
118 expect(ele.src).toContain("assets/oran-logo.png");
120 ele = fixture.debugElement.nativeElement.querySelector("text");
121 expect(ele.textContent).toEqual(
122 "Create new policy instance of type < No Type >"
125 ele = fixture.debugElement.nativeElement.querySelector("#instanceInfo");
126 expect(ele).toBeFalsy();
129 it("should contain ric select with no policy type", async () => {
130 const ricSelector: RicSelectorComponent = fixture.debugElement.query(By.directive(RicSelectorComponent)).componentInstance;
131 expect(ricSelector).toBeTruthy();
132 expect(ricSelector.policyTypeName).toBeFalsy();
135 it("should contain json editor with empty JSON", async () => {
136 const noTypePolicyEditor: NoTypePolicyEditorComponent = fixture.debugElement.query(By.directive(NoTypePolicyEditorComponent)).componentInstance;
137 expect(noTypePolicyEditor).toBeTruthy();
138 expect(noTypePolicyEditor.policyJson).toEqual("{}");
141 it("should contain enabled Close button and Submit button", async () => {
142 component.ngOnInit();
144 let closeButton: MatButtonHarness = await loader.getHarness(
145 MatButtonHarness.with({ selector: "#closeButton" })
147 expect(await closeButton.isDisabled()).toBeFalsy();
148 expect(await closeButton.getText()).toEqual("Close");
150 let submitButton: MatButtonHarness = await loader.getHarness(
151 MatButtonHarness.with({ selector: "#submitButton" })
153 expect(await submitButton.getText()).toEqual("Submit");
157 describe("content when creating policy with type", () => {
158 beforeEach(async () => {
161 createSchema: typedSchema,
163 TestBed.overrideProvider(MAT_DIALOG_DATA, { useValue: policyData }); // Should be provided with a policy
164 ({ fixture, component, loader } = compileAndGetComponents(
171 it("should contain oran logo and create title and no instance info", async () => {
172 let ele = fixture.debugElement.nativeElement.querySelector("img");
173 expect(ele.src).toContain("assets/oran-logo.png");
175 ele = fixture.debugElement.nativeElement.querySelector("text");
176 expect(ele.textContent).toEqual(
177 "Create new policy instance of type Type 1"
180 ele = fixture.debugElement.nativeElement.querySelector("#instanceInfo");
181 expect(ele).toBeFalsy();
184 it("should contain ric select with provided policy type", async () => {
185 const ricSelector: RicSelectorComponent = fixture.debugElement.query(By.directive(RicSelectorComponent)).componentInstance;
186 expect(ricSelector).toBeTruthy();
187 expect(ricSelector.policyTypeName).toEqual("Type 1");
190 it("should contain typed json editor with empty JSON, schema and dark mode true", async () => {
191 const typedPolicyEditor: TypedPolicyEditorComponent = fixture.debugElement.query(By.directive(TypedPolicyEditorComponent)).componentInstance;
192 expect(typedPolicyEditor).toBeTruthy();
193 expect(typedPolicyEditor.jsonObject).toBeFalsy();
194 expect(typedPolicyEditor.jsonSchemaObject).toEqual(typedSchema);
195 expect(typedPolicyEditor.darkMode).toBeTruthy();
198 it("should contain enabled Close button and Submit button", async () => {
199 component.ngOnInit();
201 let closeButton: MatButtonHarness = await loader.getHarness(
202 MatButtonHarness.with({ selector: "#closeButton" })
204 expect(await closeButton.isDisabled()).toBeFalsy();
205 expect(await closeButton.getText()).toEqual("Close");
207 let submitButton: MatButtonHarness = await loader.getHarness(
208 MatButtonHarness.with({ selector: "#submitButton" })
210 expect(await submitButton.getText()).toEqual("Submit");
214 describe("content when editing policy without type", () => {
215 const instanceJson = '{"qosObjectives": {"priorityLevel": 3100}}';
216 beforeEach(async () => {
218 createSchema: untypedSchema,
219 instanceId: "instanceId",
220 instanceJson: instanceJson,
224 TestBed.overrideProvider(MAT_DIALOG_DATA, { useValue: policyData }); // Should be provided with a policy
225 ({ fixture, component, loader } = compileAndGetComponents(
232 it("should contain oran logo and instance info", async () => {
233 let ele = fixture.debugElement.nativeElement.querySelector("img");
234 expect(ele.src).toContain("assets/oran-logo.png");
236 ele = fixture.debugElement.nativeElement.querySelector("text");
237 expect(ele.childNodes[0].childNodes[0]).toBeFalsy(); // No create title
239 ele = fixture.debugElement.nativeElement.querySelector("#instanceInfo");
240 expect(ele).toBeTruthy();
241 expect(ele.innerText).toEqual("[ric1] Instance ID: instanceId");
244 it("should not contain ric select", async () => {
245 const ricSelector = fixture.debugElement.query(By.directive(RicSelectorComponent));
246 expect(ricSelector).toBeFalsy();
249 it("should contain json editor with json data", async () => {
250 const noTypePolicyEditor: NoTypePolicyEditorComponent = fixture.debugElement.query(By.directive(NoTypePolicyEditorComponent)).componentInstance;
251 expect(noTypePolicyEditor).toBeTruthy();
252 expect(unescapeQuotes(noTypePolicyEditor.policyJson)).toEqual('"' + instanceJson + '"');
255 it("should contain enabled Close and Submit buttons when all inputs are valid", async () => {
256 let closeButton: MatButtonHarness = await loader.getHarness(
257 MatButtonHarness.with({ selector: "#closeButton" })
259 expect(await closeButton.isDisabled()).toBeFalsy();
260 expect(await closeButton.getText()).toEqual("Close");
262 let submitButton: MatButtonHarness = await loader.getHarness(
263 MatButtonHarness.with({ selector: "#submitButton" })
265 expect(await submitButton.isDisabled()).toBeFalsy();
266 expect(await submitButton.getText()).toEqual("Submit");
270 describe("content when editing policy with type", () => {
271 const instanceJson = '{"qosObjectives": {"priorityLevel": 3100}}';
272 beforeEach(async () => {
274 createSchema: typedSchema,
275 instanceId: "instanceId",
276 instanceJson: instanceJson,
280 TestBed.overrideProvider(MAT_DIALOG_DATA, { useValue: policyData }); // Should be provided with a policy
281 ({ fixture, component, loader } = compileAndGetComponents(
288 it("should contain oran logo and instance info", async () => {
289 let ele = fixture.debugElement.nativeElement.querySelector("img");
290 expect(ele.src).toContain("assets/oran-logo.png");
292 ele = fixture.debugElement.nativeElement.querySelector("text");
293 expect(ele.childNodes[0].childNodes[0]).toBeFalsy(); // No create title
295 ele = fixture.debugElement.nativeElement.querySelector("#instanceInfo");
296 expect(ele).toBeTruthy();
297 expect(ele.innerText).toEqual("[ric1] Instance ID: instanceId");
300 it("should not contain ric select", async () => {
301 const ricSelector = fixture.debugElement.query(By.directive(RicSelectorComponent));
302 expect(ricSelector).toBeFalsy();
305 it("should contain typed json editor with instance JSON, schema and dark mode true", async () => {
306 const typedPolicyEditor: TypedPolicyEditorComponent = fixture.debugElement.query(By.directive(TypedPolicyEditorComponent)).componentInstance;
307 expect(typedPolicyEditor).toBeTruthy();
308 expect(unescapeQuotes(typedPolicyEditor.jsonObject)).toEqual(instanceJson);
309 expect(typedPolicyEditor.jsonSchemaObject).toEqual(typedSchema);
310 expect(typedPolicyEditor.darkMode).toBeTruthy();
313 it("should contain enabled Close and Submit buttons when all inputs are valid", async () => {
314 let closeButton: MatButtonHarness = await loader.getHarness(
315 MatButtonHarness.with({ selector: "#closeButton" })
317 expect(await closeButton.isDisabled()).toBeFalsy();
318 expect(await closeButton.getText()).toEqual("Close");
320 let submitButton: MatButtonHarness = await loader.getHarness(
321 MatButtonHarness.with({ selector: "#submitButton" })
323 expect(await submitButton.isDisabled()).toBeFalsy();
324 expect(await submitButton.getText()).toEqual("Submit");
329 function compileAndGetComponents(
330 fixture: ComponentFixture<PolicyInstanceDialogComponent>,
331 component: PolicyInstanceDialogComponent,
332 loader: HarnessLoader
334 TestBed.compileComponents();
336 fixture = TestBed.createComponent(PolicyInstanceDialogComponent);
337 component = fixture.componentInstance;
338 fixture.detectChanges();
339 loader = TestbedHarnessEnvironment.loader(fixture);
340 return { fixture, component, loader };
343 function unescapeQuotes(string: string): string {
344 return string.replace(/\\"/g, '"');
348 selector: "nrcp-ric-selector",
352 provide: RicSelectorComponent,
353 useClass: RicSelectorStubComponent,
357 class RicSelectorStubComponent {
358 @Input() policyTypeName: string = "";
360 get selectedRic(): string {
366 selector: "nrcp-no-type-policy-editor",
370 provide: NoTypePolicyEditorComponent,
371 useClass: NoTypePolicyEditorStubComponent,
375 class NoTypePolicyEditorStubComponent {
376 @Input() policyJson: string;
378 get policyJsonTextArea(): AbstractControl {
379 const textArea = { value: "{}" } as AbstractControl;
385 selector: "nrcp-typed-policy-editor",
389 provide: TypedPolicyEditorComponent,
390 useClass: TypedPolicyEditorStubComponent,
394 class TypedPolicyEditorStubComponent {
395 @Input() jsonSchemaObject: any = {};
396 @Input() jsonObject: any = {};
397 @Input() darkMode: boolean;
399 prettyLiveFormData = '"A": "string"';
400 get formIsValid(): boolean {