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,
46 } from "@angular/core";
47 import { TypedPolicyEditorComponent } from "../typed-policy-editor/typed-policy-editor.component";
48 import { RicSelectorComponent } from "../ric-selector/ric-selector.component";
49 import { NoTypePolicyEditorComponent } from "../no-type-policy-editor/no-type-policy-editor.component";
50 import { By } from "@angular/platform-browser";
52 describe("PolicyInstanceDialogComponent", () => {
53 const untypedSchema = "{}";
55 '{ "description": "Type 1 policy type", "title": "1", "type": "object", "properties": { "priorityLevel": "number" }}';
57 let component: PolicyInstanceDialogComponent;
58 let fixture: ComponentFixture<PolicyInstanceDialogComponent>;
59 let loader: HarnessLoader;
60 let policyServiceSpy: jasmine.SpyObj<PolicyService>;
61 let errDialogServiceSpy: jasmine.SpyObj<ErrorDialogService>;
63 beforeEach(async () => {
64 policyServiceSpy = jasmine.createSpyObj("PolicyService", ["putPolicy"]);
65 errDialogServiceSpy = jasmine.createSpyObj("ErrorDialogService", [
69 TestBed.configureTestingModule({
71 BrowserAnimationsModule,
77 ToastrModule.forRoot(),
79 schemas: [CUSTOM_ELEMENTS_SCHEMA],
81 PolicyInstanceDialogComponent,
82 RicSelectorStubComponent,
83 NoTypePolicyEditorStubComponent,
84 TypedPolicyEditorStubComponent,
88 { provide: MatDialogRef, useValue: component },
89 { provide: PolicyService, useValue: policyServiceSpy },
90 { provide: ErrorDialogService, useValue: errDialogServiceSpy },
91 { provide: MAT_DIALOG_DATA, useValue: true },
97 describe("content when creating policy without type", () => {
98 beforeEach(async () => {
100 createSchema: untypedSchema,
102 TestBed.overrideProvider(MAT_DIALOG_DATA, { useValue: policyData }); // Should be provided with a policy
103 ({ fixture, component, loader } = compileAndGetComponents(
110 it("should contain oran logo and create title and no instance info", async () => {
111 let ele = fixture.debugElement.nativeElement.querySelector("img");
112 expect(ele.src).toContain("assets/oran-logo.png");
114 ele = fixture.debugElement.nativeElement.querySelector("text");
115 expect(ele.textContent).toEqual(
116 "Create new policy instance of type < No Type >"
119 ele = fixture.debugElement.nativeElement.querySelector("#instanceInfo");
120 expect(ele).toBeFalsy();
123 it("should contain ric select with no policy type", async () => {
124 const ricSelector: RicSelectorComponent = fixture.debugElement.query(
125 By.directive(RicSelectorComponent)
127 expect(ricSelector).toBeTruthy();
128 expect(ricSelector.policyTypeName).toBeFalsy();
131 it("should contain json editor with empty JSON", async () => {
132 const noTypePolicyEditor: NoTypePolicyEditorComponent = fixture.debugElement.query(
133 By.directive(NoTypePolicyEditorComponent)
135 expect(noTypePolicyEditor).toBeTruthy();
136 expect(noTypePolicyEditor.policyJson).toEqual("{}");
139 it("should contain enabled Close button and Submit button", async () => {
140 component.ngOnInit();
142 let closeButton: MatButtonHarness = await loader.getHarness(
143 MatButtonHarness.with({ selector: "#closeButton" })
145 expect(await closeButton.isDisabled()).toBeFalsy();
146 expect(await closeButton.getText()).toEqual("Close");
148 let submitButton: MatButtonHarness = await loader.getHarness(
149 MatButtonHarness.with({ selector: "#submitButton" })
151 expect(await submitButton.getText()).toEqual("Submit");
155 describe("content when creating policy with type", () => {
156 beforeEach(async () => {
159 createSchema: typedSchema,
161 TestBed.overrideProvider(MAT_DIALOG_DATA, { useValue: policyData }); // Should be provided with a policy
162 ({ fixture, component, loader } = compileAndGetComponents(
169 it("should contain oran logo and create title and no instance info", async () => {
170 let ele = fixture.debugElement.nativeElement.querySelector("img");
171 expect(ele.src).toContain("assets/oran-logo.png");
173 ele = fixture.debugElement.nativeElement.querySelector("text");
174 expect(ele.textContent).toEqual(
175 "Create new policy instance of type Type 1"
178 ele = fixture.debugElement.nativeElement.querySelector("#instanceInfo");
179 expect(ele).toBeFalsy();
182 it("should contain ric select with provided policy type", async () => {
183 const ricSelector: RicSelectorComponent = fixture.debugElement.query(
184 By.directive(RicSelectorComponent)
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(
192 By.directive(TypedPolicyEditorComponent)
194 expect(typedPolicyEditor).toBeTruthy();
195 expect(typedPolicyEditor.jsonObject).toBeFalsy();
196 expect(typedPolicyEditor.jsonSchemaObject).toEqual(typedSchema);
197 expect(typedPolicyEditor.darkMode).toBeTruthy();
200 it("should contain enabled Close button and Submit button", async () => {
201 component.ngOnInit();
203 let closeButton: MatButtonHarness = await loader.getHarness(
204 MatButtonHarness.with({ selector: "#closeButton" })
206 expect(await closeButton.isDisabled()).toBeFalsy();
207 expect(await closeButton.getText()).toEqual("Close");
209 let submitButton: MatButtonHarness = await loader.getHarness(
210 MatButtonHarness.with({ selector: "#submitButton" })
212 expect(await submitButton.getText()).toEqual("Submit");
216 describe("content when editing policy without type", () => {
217 const instanceJson = '{"qosObjectives": {"priorityLevel": 3100}}';
218 beforeEach(async () => {
220 createSchema: untypedSchema,
221 instanceId: "instanceId",
222 instanceJson: instanceJson,
226 TestBed.overrideProvider(MAT_DIALOG_DATA, { useValue: policyData }); // Should be provided with a policy
227 ({ fixture, component, loader } = compileAndGetComponents(
234 it("should contain oran logo and instance info", async () => {
235 let ele = fixture.debugElement.nativeElement.querySelector("img");
236 expect(ele.src).toContain("assets/oran-logo.png");
238 ele = fixture.debugElement.nativeElement.querySelector("text");
239 expect(ele.childNodes[0].childNodes[0]).toBeFalsy(); // No create title
241 ele = fixture.debugElement.nativeElement.querySelector("#instanceInfo");
242 expect(ele).toBeTruthy();
243 expect(ele.innerText).toEqual("[ric1] Instance ID: instanceId");
246 it("should not contain ric select", async () => {
247 const ricSelector = fixture.debugElement.query(
248 By.directive(RicSelectorComponent)
250 expect(ricSelector).toBeFalsy();
253 it("should contain json editor with json data", async () => {
254 const noTypePolicyEditor: NoTypePolicyEditorComponent = fixture.debugElement.query(
255 By.directive(NoTypePolicyEditorComponent)
257 expect(noTypePolicyEditor).toBeTruthy();
258 expect(unescapeQuotes(noTypePolicyEditor.policyJson)).toEqual(
259 '"' + instanceJson + '"'
263 it("should contain enabled Close and Submit buttons when all inputs are valid", async () => {
264 let closeButton: MatButtonHarness = await loader.getHarness(
265 MatButtonHarness.with({ selector: "#closeButton" })
267 expect(await closeButton.isDisabled()).toBeFalsy();
268 expect(await closeButton.getText()).toEqual("Close");
270 let submitButton: MatButtonHarness = await loader.getHarness(
271 MatButtonHarness.with({ selector: "#submitButton" })
273 expect(await submitButton.isDisabled()).toBeFalsy();
274 expect(await submitButton.getText()).toEqual("Submit");
278 describe("content when editing policy with type", () => {
279 const instanceJson = '{"qosObjectives": {"priorityLevel": 3100}}';
280 beforeEach(async () => {
282 createSchema: typedSchema,
283 instanceId: "instanceId",
284 instanceJson: instanceJson,
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 ricSelector = fixture.debugElement.query(
310 By.directive(RicSelectorComponent)
312 expect(ricSelector).toBeFalsy();
315 it("should contain typed json editor with instance JSON, schema and dark mode true", async () => {
316 const typedPolicyEditor: TypedPolicyEditorComponent = fixture.debugElement.query(
317 By.directive(TypedPolicyEditorComponent)
319 expect(typedPolicyEditor).toBeTruthy();
320 expect(unescapeQuotes(typedPolicyEditor.jsonObject)).toEqual(
323 expect(typedPolicyEditor.jsonSchemaObject).toEqual(typedSchema);
324 expect(typedPolicyEditor.darkMode).toBeTruthy();
327 it("should contain enabled Close and Submit buttons when all inputs are valid", async () => {
328 let closeButton: MatButtonHarness = await loader.getHarness(
329 MatButtonHarness.with({ selector: "#closeButton" })
331 expect(await closeButton.isDisabled()).toBeFalsy();
332 expect(await closeButton.getText()).toEqual("Close");
334 let submitButton: MatButtonHarness = await loader.getHarness(
335 MatButtonHarness.with({ selector: "#submitButton" })
337 expect(await submitButton.isDisabled()).toBeFalsy();
338 expect(await submitButton.getText()).toEqual("Submit");
343 function compileAndGetComponents(
344 fixture: ComponentFixture<PolicyInstanceDialogComponent>,
345 component: PolicyInstanceDialogComponent,
346 loader: HarnessLoader
348 TestBed.compileComponents();
350 fixture = TestBed.createComponent(PolicyInstanceDialogComponent);
351 component = fixture.componentInstance;
352 fixture.detectChanges();
353 loader = TestbedHarnessEnvironment.loader(fixture);
354 return { fixture, component, loader };
357 function unescapeQuotes(string: string): string {
358 return string.replace(/\\"/g, '"');
362 selector: "nrcp-ric-selector",
366 provide: RicSelectorComponent,
367 useClass: RicSelectorStubComponent,
371 class RicSelectorStubComponent {
372 @Input() policyTypeName: string = "";
374 get selectedRic(): string {
380 selector: "nrcp-no-type-policy-editor",
384 provide: NoTypePolicyEditorComponent,
385 useClass: NoTypePolicyEditorStubComponent,
389 class NoTypePolicyEditorStubComponent {
390 @Input() policyJson: string;
392 get policyJsonTextArea(): AbstractControl {
393 const textArea = { value: "{}" } as AbstractControl;
399 selector: "nrcp-typed-policy-editor",
403 provide: TypedPolicyEditorComponent,
404 useClass: TypedPolicyEditorStubComponent,
408 class TypedPolicyEditorStubComponent {
409 @Input() jsonSchemaObject: any = {};
410 @Input() jsonObject: any = {};
411 @Input() darkMode: boolean;
413 prettyLiveFormData = '"A": "string"';
414 get formIsValid(): boolean {