await deleteButton.click();
expect(confirmServiceSpy.openConfirmDialog).toHaveBeenCalledWith(
+ "Delete Policy",
"Are you sure you want to delete this policy instance?"
);
expect(policyServiceSpy.deletePolicy).toHaveBeenCalledWith("policy1");
deleteInstance(instance: PolicyInstance): void {
this.confirmDialogService
.openConfirmDialog(
+ "Delete Policy",
"Are you sure you want to delete this policy instance?"
)
.afterClosed()
HttpTestingController,
} from "@angular/common/http/testing";
import {
+ CreatePolicyInstance,
PolicyInstance,
PolicyInstances,
+ PolicyStatus,
+ PolicyType,
PolicyTypes,
} from "@interfaces/policy.types";
+import { Ric, Rics } from "@app/interfaces/ric";
describe("PolicyService", () => {
let apiVersion2 = "v2";
})
);
+ beforeEach(() => {
+ policyService = TestBed.inject(PolicyService);
+ httpTestingController = TestBed.inject(HttpTestingController);
+ });
+
afterEach(() => {
httpTestingController.verify();
});
- describe("#getPolicyTypes", () => {
- let expectedPolicytypes: PolicyTypes;
- let emptyPolicyType: PolicyTypes;
-
- beforeEach(() => {
- policyService = TestBed.inject(PolicyService);
- httpTestingController = TestBed.inject(HttpTestingController);
- expectedPolicytypes = {
- policytype_ids: ["", "1"],
- } as PolicyTypes;
- });
- //Policy Type Test Case 1:
- it("should return all policy types", () => {
- policyService
- .getPolicyTypes()
- .subscribe(
- (policytypes) =>
- expect(policytypes).toEqual(
- expectedPolicytypes,
- "should return expected PolicyTypes"
- ),
- fail
- );
- const req = httpTestingController.expectOne(
- basePath + apiVersion2 + "/" + policyService.policyTypesPath
+ it("#getPolicyTypes should return policy types", () => {
+ const expectedPolicytypes = {
+ policytype_ids: ["", "1"],
+ } as PolicyTypes;
+
+ policyService
+ .getPolicyTypes()
+ .subscribe(
+ (policytypes) =>
+ expect(policytypes).toEqual(
+ expectedPolicytypes,
+ "should return expected PolicyTypes"
+ ),
+ fail
);
- expect(req.request.method).toEqual("GET");
+ const req = httpTestingController.expectOne(
+ basePath + apiVersion2 + "/" + policyService.policyTypesPath
+ );
+ expect(req.request.method).toEqual("GET");
+
+ req.flush(expectedPolicytypes);
+ });
- req.flush(expectedPolicytypes);
- });
+ it("#getPolicyType", () => {
+ const policyTypeId = "1";
+ const expectedPolicyType = { policy_schema: "schema" } as PolicyType;
- //Policy Type Test Case 2:
- emptyPolicyType = {
- policytype_ids: [],
- } as PolicyTypes;
- it("should return no policy types", () => {
- policyService
- .getPolicyTypes()
- .subscribe(
- (policytypes) =>
- expect(policytypes).toEqual(
- emptyPolicyType,
- "should return empty array of Policy Types"
- ),
- fail
- );
-
- const req = httpTestingController.expectOne(
- basePath + apiVersion2 + "/" + policyService.policyTypesPath
+ policyService
+ .getPolicyType(policyTypeId)
+ .subscribe(
+ (policyType) =>
+ expect(policyType).toEqual(
+ expectedPolicyType,
+ "should return expected policy type"
+ ),
+ fail
);
- req.flush(emptyPolicyType); //Return empty data
- });
+
+ const req = httpTestingController.expectOne(
+ basePath +
+ apiVersion2 +
+ "/" +
+ policyService.policyTypesPath +
+ "/" +
+ policyTypeId
+ );
+ expect(req.request.method).toEqual("GET");
+
+ req.flush(expectedPolicyType);
});
- describe("#getPolicyInstances", () => {
- let expectedPolicyInstances: PolicyInstances;
- let emptyPolicyInstances: PolicyInstances;
- let policyTypeId = "1";
- beforeEach(() => {
- policyService = TestBed.inject(PolicyService);
- httpTestingController = TestBed.inject(HttpTestingController);
- expectedPolicyInstances = {
- policy_ids: ["2100", "2000"],
- } as PolicyInstances;
- });
- //Policy Instances Test Case 1:
- it("should return all policy instances", () => {
- policyService
- .getPolicyInstancesByType(policyTypeId)
- .subscribe(
- (policyinstances) =>
- expect(policyinstances).toEqual(
- expectedPolicyInstances,
- "should return expected Policy Instances"
- ),
- fail
- );
- const req = httpTestingController.expectOne(
- basePath +
- apiVersion2 +
- "/" +
- policyService.policyPath +
- "?" +
- "policytype_id=" +
- policyTypeId
- );
- expect(req.request.method).toEqual("GET");
- req.flush(expectedPolicyInstances);
- });
- //Policy Instances Test Case 2:
- emptyPolicyInstances = {
- policy_ids: [],
+ it("#getPolicyInstancesByType should return policy instances", () => {
+ const policyTypeId = "1";
+ const expectedPolicyInstances = {
+ policy_ids: ["2100", "2000"],
} as PolicyInstances;
- it("should return no policy instances", () => {
- policyService
- .getPolicyInstancesByType(policyTypeId)
- .subscribe(
- (policyinstances) =>
- expect(policyinstances.policy_ids.length).toEqual(
- 0,
- "should return empty array of Policy Instances"
- ),
- fail
- );
- const req = httpTestingController.expectOne(
- basePath +
- apiVersion2 +
- "/" +
- policyService.policyPath +
- "?" +
- "policytype_id=" +
- policyTypeId
+
+ policyService
+ .getPolicyInstancesByType(policyTypeId)
+ .subscribe(
+ (policyinstances) =>
+ expect(policyinstances).toEqual(
+ expectedPolicyInstances,
+ "should return expected Policy Instances"
+ ),
+ fail
+ );
+
+ const req = httpTestingController.expectOne(
+ basePath +
+ apiVersion2 +
+ "/" +
+ policyService.policyPath +
+ "?" +
+ "policytype_id=" +
+ policyTypeId
+ );
+ expect(req.request.method).toEqual("GET");
+
+ req.flush(expectedPolicyInstances);
+ });
+
+ it("#getPolicyInstance should return one policy instance", () => {
+ const policyId = "2000";
+ const expectedPolicyInstance = {
+ policy_id: "2000",
+ policytype_id: "1",
+ ric_id: "ric1",
+ policy_data: "",
+ service_id: "service1",
+ lastModified: "",
+ } as PolicyInstance;
+
+ policyService
+ .getPolicyInstance(policyId)
+ .subscribe(
+ (policyinstance) =>
+ expect(policyinstance).toEqual(
+ expectedPolicyInstance,
+ "should return expected Policy Instances"
+ ),
+ fail
+ );
+
+ const req = httpTestingController.expectOne(
+ basePath + apiVersion2 + "/" + policyService.policyPath + "/" + policyId
+ );
+ expect(req.request.method).toEqual("GET");
+
+ req.flush(expectedPolicyInstance);
+ });
+
+ it("#getPolicyStatus should return policy status", () => {
+ const policyId = "2000";
+ const expectedPolicyStatus = {
+ last_modified: "modified",
+ } as PolicyStatus;
+
+ policyService
+ .getPolicyStatus(policyId)
+ .subscribe(
+ (policyinstance) =>
+ expect(policyinstance).toEqual(
+ expectedPolicyStatus,
+ "should return expected Policy status"
+ ),
+ fail
+ );
+
+ const req = httpTestingController.expectOne(
+ basePath +
+ apiVersion2 +
+ "/" +
+ policyService.policyPath +
+ "/" +
+ policyId +
+ "/status"
+ );
+ expect(req.request.method).toEqual("GET");
+
+ req.flush(expectedPolicyStatus);
+ });
+
+ it("#putPolicy should return ok response", () => {
+ const createPolicyInstance = { policy_id: "2000" } as CreatePolicyInstance;
+
+ policyService
+ .putPolicy(createPolicyInstance)
+ .subscribe(
+ (response) =>
+ expect(response.status).toEqual(
+ 200,
+ "should return expected response"
+ ),
+ fail
+ );
+
+ const req = httpTestingController.expectOne(
+ basePath + apiVersion2 + "/" + policyService.policyPath
+ );
+ expect(req.request.method).toEqual("PUT");
+
+ req.flush(200);
+ });
+
+ it("#deletePolicy should return ok response", () => {
+ const policyId = "2000";
+
+ policyService
+ .deletePolicy(policyId)
+ .subscribe(
+ (response) =>
+ expect(response.status).toEqual(
+ 200,
+ "should return expected response"
+ ),
+ fail
);
- req.flush(emptyPolicyInstances); //Return empty data
- });
+
+ const req = httpTestingController.expectOne(
+ basePath + apiVersion2 + "/" + policyService.policyPath + "/2000"
+ );
+ expect(req.request.method).toEqual("DELETE");
+
+ req.flush(200);
});
- describe("#getPolicyInstance", () => {
- let expectedPolicyInstance: PolicyInstance;
- let emptyPolicyInstances: PolicyInstances;
- let policyId = "2000";
- beforeEach(() => {
- policyService = TestBed.inject(PolicyService);
- httpTestingController = TestBed.inject(HttpTestingController);
- expectedPolicyInstance = {
- policy_id: "2000",
- policytype_id: "1",
- ric_id: "ric1",
- policy_data: "",
- service_id: "service1",
- lastModified: "",
- } as PolicyInstance;
- });
- //Policy Instances Test Case 1:
- it("should return one policy instance", () => {
- policyService
- .getPolicyInstance(policyId)
- .subscribe(
- (policyinstance) =>
- expect(policyinstance).toEqual(
- expectedPolicyInstance,
- "should return expected Policy Instances"
- ),
- fail
- );
- const req = httpTestingController.expectOne(
- basePath + apiVersion2 + "/" + policyService.policyPath + "/" + policyId
+ it("#getRics should return rics", () => {
+ const policyTypeId = "2000";
+ const expectedRic = { ric_id: "1" } as Ric;
+ const expectedRics = {
+ rics: [expectedRic],
+ } as Rics;
+
+ policyService
+ .getRics(policyTypeId)
+ .subscribe(
+ (rics) =>
+ expect(rics).toEqual(
+ expectedRics,
+ "should return expected Rics"
+ ),
+ fail
);
- expect(req.request.method).toEqual("GET");
- req.flush(expectedPolicyInstance);
- });
+
+ const req = httpTestingController.expectOne(
+ basePath +
+ apiVersion2 +
+ "/rics?policytype_id=2000"
+ );
+ expect(req.request.method).toEqual("GET");
+
+ req.flush(expectedRics);
});
});
return this.httpClient.get<PolicyStatus>(url);
}
- /**
- * Gets policy parameters.
- * @returns Observable that should yield a policy instance
- */
- getPolicy(policyTypeId: string, policyInstanceId: string): Observable<any> {
- const url =
- this.buildPath(this.policyPath, policyInstanceId) +
- "?type=" +
- policyTypeId;
- return this.httpClient.get<any>(url);
- }
-
/**
* Creates or replaces policy instance.
* @param policyTypeId ID of the policy type that the instance will have
* ========================LICENSE_END===================================
*/
-import { TestBed } from '@angular/core/testing';
+import { TestBed } from "@angular/core/testing";
-import { ConfirmDialogService } from './confirm-dialog.service';
-import { MatDialogModule } from '@angular/material/dialog';
-import {UiService} from './ui.service';
+import { ConfirmDialogService } from "./confirm-dialog.service";
+import { MatDialog, MatDialogModule } from "@angular/material/dialog";
+import { UiService } from "./ui.service";
+import { ConfirmDialogComponent } from "@app/ui/confirm-dialog/confirm-dialog.component";
+import { BrowserAnimationsModule } from "@angular/platform-browser/animations";
-describe('ConfirmDialogService', () => {
- beforeEach(() => TestBed.configureTestingModule({
- imports: [ MatDialogModule ],
- providers: [UiService]
- }));
+describe("ConfirmDialogService", () => {
+ let matDialogSpy: jasmine.SpyObj<MatDialog>;
+ let service: ConfirmDialogService;
- it('should be created', () => {
- const service: ConfirmDialogService = TestBed.inject(ConfirmDialogService);
+ beforeEach(() => {
+ matDialogSpy = jasmine.createSpyObj("MatDialog", ["open"]);
+
+ TestBed.configureTestingModule({
+ imports: [BrowserAnimationsModule, MatDialogModule],
+ providers: [
+ { provide: MatDialog, useValue: matDialogSpy },
+ UiService,
+ ],
+ });
+
+ service = TestBed.inject(ConfirmDialogService);
+ });
+
+ it("should be created", () => {
expect(service).toBeTruthy();
});
+
+ it("should open confirm dialog with correct dark mode and data", () => {
+ const uiService: UiService = TestBed.inject(UiService);
+ uiService.darkModeState.next(false);
+
+ service.openConfirmDialog("Heading", "Message");
+
+ expect(matDialogSpy.open).toHaveBeenCalledWith(ConfirmDialogComponent, {
+ panelClass: "",
+ width: "480px",
+ position: { top: "100px" },
+ data: {
+ heading: "Heading",
+ message: "Message",
+ },
+ });
+
+ uiService.darkModeState.next(true);
+
+ service.openConfirmDialog("Heading", "Message");
+
+ expect(matDialogSpy.open).toHaveBeenCalledWith(ConfirmDialogComponent, {
+ panelClass: "dark-theme",
+ width: "480px",
+ position: { top: "100px" },
+ data: {
+ heading: "Heading",
+ message: "Message",
+ },
+ });
+ });
});
* ========================LICENSE_END===================================
*/
-import { Injectable } from '@angular/core';
-import { MatDialog } from '@angular/material/dialog';
-import { ConfirmDialogComponent } from '@ui/confirm-dialog/confirm-dialog.component';
-import { UiService } from './ui.service';
+import { Injectable } from "@angular/core";
+import { MatDialog, MatDialogRef } from "@angular/material/dialog";
+import { ConfirmDialogComponent } from "@ui/confirm-dialog/confirm-dialog.component";
+import { UiService } from "./ui.service";
@Injectable({
- providedIn: 'root'
+ providedIn: "root",
})
export class ConfirmDialogService {
+ constructor(private dialog: MatDialog, private ui: UiService) {}
- darkMode: boolean;
- panelClass = '';
-
- constructor(private dialog: MatDialog,
- private ui: UiService) { }
-
- openConfirmDialog(msg: string) {
+ openConfirmDialog(heading: string, msg: string): MatDialogRef<any> {
+ let panelClass = "";
+ let darkMode: boolean;
this.ui.darkModeState.subscribe((isDark) => {
- this.darkMode = isDark;
+ darkMode = isDark;
});
- if (this.darkMode) {
- this.panelClass = 'dark-theme';
- } else {
- this.panelClass = '';
+ if (darkMode) {
+ panelClass = "dark-theme";
}
return this.dialog.open(ConfirmDialogComponent, {
- panelClass: this.panelClass,
- width: '480px',
- position: { top: '100px' },
+ panelClass: panelClass,
+ width: "480px",
+ position: { top: "100px" },
data: {
- message: msg
- }
+ heading: heading,
+ message: msg,
+ },
});
}
}
* ========================LICENSE_END===================================
*/
-import { TestBed } from '@angular/core/testing';
+import { async, TestBed } from "@angular/core/testing";
-import { NotificationService } from './notification.service';
-import { ToastrModule } from 'ngx-toastr';
+import { NotificationService } from "./notification.service";
+import { ToastrService } from "ngx-toastr";
-describe('NotificationService', () => {
- beforeEach(() => TestBed.configureTestingModule({
- imports: [ToastrModule.forRoot()],
- providers: [
- {provide: ToastrModule}
- ]
+describe("NotificationService", () => {
+ let service: NotificationService;
+ let toastrSpy: jasmine.SpyObj<ToastrService>;
+ beforeEach(async(() => {
+ toastrSpy = jasmine.createSpyObj("ToastrService", [
+ "success",
+ "warning",
+ "error",
+ ]);
+
+ TestBed.configureTestingModule({
+ providers: [{ provide: ToastrService, useValue: toastrSpy }],
+ });
+ service = TestBed.inject(NotificationService);
}));
- it('should be created', () => {
- const service: NotificationService = TestBed.inject(NotificationService);
+ it("should be created", () => {
expect(service).toBeTruthy();
});
+
+ it("should open success with provided message and correct configuration", () => {
+ service.success("Success!");
+
+ expect(toastrSpy.success).toHaveBeenCalledWith("Success!", "", {
+ timeOut: 10000,
+ closeButton: true,
+ });
+ });
+
+ it("should open error with provided message and correct configuration", () => {
+ service.error("Error!");
+
+ expect(toastrSpy.error).toHaveBeenCalledWith("Error!", "", {
+ disableTimeOut: true,
+ closeButton: true,
+ });
+ });
});
* ========================LICENSE_END===================================
*/
-import { Injectable } from '@angular/core';
-import { ToastrService } from 'ngx-toastr';
+import { Injectable } from "@angular/core";
+import { ToastrService } from "ngx-toastr";
@Injectable({
- providedIn: 'root'
+ providedIn: "root",
})
export class NotificationService {
-
- constructor(public toastr: ToastrService) { }
+ constructor(public toastr: ToastrService) {}
successConfig = {
timeOut: 10000,
- closeButton: true
- };
-
- warningConfig = {
- disableTimeOut: true,
- closeButton: true
+ closeButton: true,
};
errorConfig = {
disableTimeOut: true,
- closeButton: true
+ closeButton: true,
};
success(msg: string) {
- this.toastr.success(msg, '', this.successConfig);
- }
-
- warn(msg: string) {
- this.toastr.warning(msg, '', this.warningConfig);
+ this.toastr.success(msg, "", this.successConfig);
}
error(msg: string) {
- this.toastr.error(msg, '', this.errorConfig);
+ this.toastr.error(msg, "", this.errorConfig);
}
-
}
========================LICENSE_END===================================
-->
- <div mat-dialog-title>
+ <div id="title" mat-dialog-title>
+ {{data.heading}}
</div>
- <div mat-dialog-content>
+ <div id="message" mat-dialog-content>
{{data.message}}
</div>
<div mat-dialog-actions class="modal-footer justify-content-center">
- <button mat-button class="mat-raised-button" [mat-dialog-close]="false">Cancel</button>
- <button mat-button class="mat-raised-button mat-primary" [mat-dialog-close]="true">OK</button>
+ <button id="cancelButton" mat-button class="mat-raised-button" mat-dialog-close>Cancel</button>
+ <button id="okButton" mat-button class="mat-raised-button mat-primary" [mat-dialog-close]="true">OK</button>
</div>
\ No newline at end of file
* ========================LICENSE_END===================================
*/
-import { async, ComponentFixture, TestBed } from '@angular/core/testing';
+import { async, ComponentFixture, TestBed } from "@angular/core/testing";
-import { ConfirmDialogComponent } from './confirm-dialog.component';
-import { MatDialogModule } from '@angular/material/dialog';
-import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material/dialog';
+import { ConfirmDialogComponent } from "./confirm-dialog.component";
+import { MatDialogModule } from "@angular/material/dialog";
+import { MAT_DIALOG_DATA, MatDialogRef } from "@angular/material/dialog";
+import { TestbedHarnessEnvironment } from "@angular/cdk/testing/testbed";
+import { HarnessLoader } from "@angular/cdk/testing";
+import { MatButtonHarness } from "@angular/material/button/testing";
-describe('ConfirmDialogComponent', () => {
+describe("ConfirmDialogComponent", () => {
let component: ConfirmDialogComponent;
let fixture: ComponentFixture<ConfirmDialogComponent>;
+ let loader: HarnessLoader;
+
+ let dialogRefSpy: jasmine.SpyObj<MatDialogRef<any>>;
beforeEach(async(() => {
+ dialogRefSpy = jasmine.createSpyObj("MatDialogRef", ["close"]);
+
TestBed.configureTestingModule({
- declarations: [ ConfirmDialogComponent ],
- imports: [ MatDialogModule ],
+ declarations: [ConfirmDialogComponent],
+ imports: [MatDialogModule],
providers: [
- { provide: MAT_DIALOG_DATA, useValue: {} },
- { provide: MatDialogRef, useValue: {} }
- ]
- })
- .compileComponents();
+ {
+ provide: MAT_DIALOG_DATA,
+ useValue: { heading: "Delete Policy", message: "Do?" },
+ },
+ { provide: MatDialogRef, useValue: dialogRefSpy },
+ ],
+ }).compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(ConfirmDialogComponent);
component = fixture.componentInstance;
fixture.detectChanges();
+ loader = TestbedHarnessEnvironment.loader(fixture);
});
- it('should create', () => {
+ it("should create and contain correct title, message and buttons", async () => {
expect(component).toBeTruthy();
+
+ const title = fixture.debugElement.nativeElement.querySelector("#title");
+ expect(title.innerText).toEqual("Delete Policy");
+
+ const message = fixture.debugElement.nativeElement.querySelector(
+ "#message"
+ );
+ expect(message.innerText).toEqual("Do?");
+
+ const cancelButton = fixture.debugElement.nativeElement.querySelector(
+ "#cancelButton"
+ );
+ expect(cancelButton).toBeTruthy();
+
+ const okButton = fixture.debugElement.nativeElement.querySelector(
+ "#okButton"
+ );
+ expect(okButton).toBeTruthy();
+ });
+
+ it("should close dialog with true when Ok button clicked", async () =>{
+ let okButton: MatButtonHarness = await loader.getHarness(
+ MatButtonHarness.with({ selector: "#okButton" })
+ );
+ await okButton.click();
+
+ expect(dialogRefSpy.close).toHaveBeenCalledWith(true);
+ });
+
+ it("should close dialog without data when Cancel button clicked", async () =>{
+ let cancelButton: MatButtonHarness = await loader.getHarness(
+ MatButtonHarness.with({ selector: "#cancelButton" })
+ );
+ await cancelButton.click();
+
+ expect(dialogRefSpy.close).toHaveBeenCalledWith("");
});
});
selector: 'nrcp-confirm-dialog',
templateUrl: './confirm-dialog.component.html',
})
-export class ConfirmDialogComponent implements OnInit {
+export class ConfirmDialogComponent {
constructor(@Inject(MAT_DIALOG_DATA) public data,
public dialogRef: MatDialogRef<ConfirmDialogComponent>) { }
-
- ngOnInit() {
- }
-
- closeDialog() {
- this.dialogRef.close(false);
- }
}