From fd5306f16f1e1f1bd07c706f073d57a700eff603 Mon Sep 17 00:00:00 2001 From: "Henry.Sun" Date: Tue, 24 Mar 2020 15:16:46 +0800 Subject: add coverage test for JS Issue-ID: POLICY-2311 Change-Id: I12a041f59bf07ed4e8688545522e64a338cdb326 Signed-off-by: Henry.Sun --- .../src/webapp/js/__test__/MonitoringChart.test.js | 40 +++++++ .../src/webapp/js/__test__/MonitoringMain.test.js | 60 +++++++++++ .../src/webapp/js/__test__/MonitoringTable.test.js | 33 ++++++ .../src/webapp/js/__test__/MonitoringUtils.test.js | 120 ++++++++++++++++++--- .../js/__test__/PdpEngineWorkerStatus.test.js | 37 +++++++ .../src/webapp/js/__test__/PdpInformation.test.js | 31 ++++++ .../src/webapp/js/__test__/PdpListView.test.js | 64 +++++++++++ .../js/__test__/PdpStatisticsSummary.test.js | 38 +++++++ 8 files changed, 408 insertions(+), 15 deletions(-) create mode 100644 gui-pdp-monitoring/src/webapp/js/__test__/MonitoringChart.test.js create mode 100644 gui-pdp-monitoring/src/webapp/js/__test__/MonitoringMain.test.js create mode 100644 gui-pdp-monitoring/src/webapp/js/__test__/MonitoringTable.test.js create mode 100644 gui-pdp-monitoring/src/webapp/js/__test__/PdpEngineWorkerStatus.test.js create mode 100644 gui-pdp-monitoring/src/webapp/js/__test__/PdpInformation.test.js create mode 100644 gui-pdp-monitoring/src/webapp/js/__test__/PdpListView.test.js create mode 100644 gui-pdp-monitoring/src/webapp/js/__test__/PdpStatisticsSummary.test.js (limited to 'gui-pdp-monitoring/src/webapp/js/__test__') diff --git a/gui-pdp-monitoring/src/webapp/js/__test__/MonitoringChart.test.js b/gui-pdp-monitoring/src/webapp/js/__test__/MonitoringChart.test.js new file mode 100644 index 0000000..6fa0e05 --- /dev/null +++ b/gui-pdp-monitoring/src/webapp/js/__test__/MonitoringChart.test.js @@ -0,0 +1,40 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2020 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +import * as MonitoringChart from "../MonitoringChart"; + +const data = MonitoringChart.generateRandomData(); +const container = null; +const title = null; +const unit = null; +const lineStroke = ""; +const nodeColour = ""; + +test("test create chart is available", () => { + MonitoringChart.createChart(data, container, title, unit, lineStroke, nodeColour); + expect(data.length).toBe(30); +}); + +test("update Chart", () => { + MonitoringChart.initTooltip; + expect(data.length).toBe(30); +}); + + diff --git a/gui-pdp-monitoring/src/webapp/js/__test__/MonitoringMain.test.js b/gui-pdp-monitoring/src/webapp/js/__test__/MonitoringMain.test.js new file mode 100644 index 0000000..cfbfaf6 --- /dev/null +++ b/gui-pdp-monitoring/src/webapp/js/__test__/MonitoringMain.test.js @@ -0,0 +1,60 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2020 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +import * as sut from "../MonitoringMain"; + +import * as MonitoringChart from "../MonitoringChart"; +import * as MonitoringUtils from "../MonitoringUtils"; +import * as PdpInformation from "../PdpInformation"; +import * as PdpStatisticsSummary from "../PdpStatisticsSummary"; +import * as PdpEngineWorkerStatus from "../PdpEngineWorkerStatus"; + +test("test ready", () => { + MonitoringChart.initTooltip = jest.fn(); + MonitoringUtils.setUpPage = jest.fn(); + MonitoringUtils.getEngineURL = jest.fn(); + sut.readyCallback(); + expect(MonitoringChart.initTooltip).toHaveBeenCalled(); + expect(MonitoringUtils.setUpPage).toHaveBeenCalled(); + expect(MonitoringUtils.getEngineURL).toHaveBeenCalled(); +}); + +test("test service callback", () => { + const storedData = { + useHttps: "http", + hostname: "localhost", + port: 7979, + username: "username", + password: "password", + }; + + const data = { + server: "localhost", + port: 7979, + } + window.localStorage.setItem("pap-monitor-services", JSON.stringify(storedData)); + PdpInformation.setEngineServiceData = jest.fn(); + PdpStatisticsSummary.setEngineSummaryData = jest.fn(); + PdpEngineWorkerStatus.setEngineStatusData = jest.fn(); + sut.servicesCallback(data); + expect(PdpInformation.setEngineServiceData).toHaveBeenCalled(); + expect(PdpStatisticsSummary.setEngineSummaryData).toHaveBeenCalled(); + expect(PdpEngineWorkerStatus.setEngineStatusData).toHaveBeenCalled(); +}); \ No newline at end of file diff --git a/gui-pdp-monitoring/src/webapp/js/__test__/MonitoringTable.test.js b/gui-pdp-monitoring/src/webapp/js/__test__/MonitoringTable.test.js new file mode 100644 index 0000000..2f67925 --- /dev/null +++ b/gui-pdp-monitoring/src/webapp/js/__test__/MonitoringTable.test.js @@ -0,0 +1,33 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2020 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +import $ from 'jquery'; +import * as MonitoringTable from "../MonitoringTable" + +var wrapper = document.createElement("example") +wrapper.setAttribute("id", "engineSummary_wrapper"); +wrapper.setAttribute("class", "wrapper_borderless"); + +test("create necessary engine table", () => { + MonitoringTable.createEngineTable($(wrapper), null, null); + expect(document.getElementById("engineTableBody")).toBeDefined(); + expect(document.getElementById("engineTableHeader")).toBeDefined(); + expect(document.getElementById("engineTableHeaderRow")).toBeDefined(); + }); \ No newline at end of file diff --git a/gui-pdp-monitoring/src/webapp/js/__test__/MonitoringUtils.test.js b/gui-pdp-monitoring/src/webapp/js/__test__/MonitoringUtils.test.js index 45fae79..35eb297 100644 --- a/gui-pdp-monitoring/src/webapp/js/__test__/MonitoringUtils.test.js +++ b/gui-pdp-monitoring/src/webapp/js/__test__/MonitoringUtils.test.js @@ -1,19 +1,109 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2020 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + import $ from 'jquery'; -import { ajax_get, ajax_get_statistics } from "../MonitoringUtils"; - -$.ajax = jest.fn().mockImplementation(() => { - const fakeResponse = { - data: { - id: 1, - name: "All", - value: "Dummy Data" +import * as sut from "../MonitoringUtils"; +import * as PdpInformation from "../PdpInformation"; +import * as PdpStatisticsSummary from "../PdpStatisticsSummary"; +import * as PdpListView from '../PdpListView'; + +const requestURL = "http://localhost:7979"; + +const serviceData = { + useHttps: "http", + hostname: "localhost", + port: 7979, + username: "username", + password: "password", +}; + +test('ajax_get ok', () => { + const callback = jest.fn(); + $.ajax = jest.fn().mockImplementation((args) => { args.success(); }); + sut.ajax_get(requestURL, callback, serviceData.useHttps, serviceData.hostname, serviceData.port, + serviceData.username, serviceData.password, {}, null); + expect(callback).toHaveBeenCalled(); +}); + +test('ajax_get error', () => { + const errorCallback = jest.fn(); + const jqXHR = { status: 500, responseText: "" }; + $.ajax = jest.fn().mockImplementation( + args => { + args.error(jqXHR, null, null); } - }; - return Promise.resolve(fakeResponse); + ); + sut.ajax_get(requestURL, null, serviceData.useHttps, serviceData.hostname, serviceData.port, + serviceData.username, serviceData.password, {}, errorCallback); + expect(errorCallback).toHaveBeenCalled(); }); -test('ajax_get return ok', () => { - ajax_get().then(response => { - expect(response.data.id).toBe(1); - }); -}); \ No newline at end of file +test('ajax_get_statistics ok', () => { + const callback = jest.fn(); + $.ajax = jest.fn().mockImplementation((args) => { args.success(); }); + sut.ajax_get_statistics(requestURL, callback, serviceData.useHttps, serviceData.hostname, serviceData.port, + serviceData.username, serviceData.password, "", {}, null); + expect(callback).toHaveBeenCalled(); +}); + +test('ajax_get_statistics error', () => { + const errorCallback = jest.fn(); + const jqXHR = { status: 500, responseText: "" }; + $.ajax = jest.fn().mockImplementation( + args => { + args.error(jqXHR, null, null); + } + ); + PdpInformation.createEngineServiceTable = jest.fn(); + PdpStatisticsSummary.createEngineSummaryTable = jest.fn(); + sut.ajax_get_statistics(requestURL, null, serviceData.useHttps, serviceData.hostname, serviceData.port, + serviceData.username, serviceData.password, "", {}, errorCallback); + + expect(errorCallback).toHaveBeenCalled(); + expect(PdpInformation.createEngineServiceTable).toHaveBeenCalled(); + expect(PdpStatisticsSummary.createEngineSummaryTable).toHaveBeenCalled(); +}); + +test('getEngineURL popup dialog', () => { + window.localStorage.clear(); + sut.getEngineURL("message"); + $('#submit').click(); + expect($("papDialogDiv")).toHaveLength(1); +}); + +test('getEngineURL read from localStorage', () => { + window.localStorage.setItem("pap-monitor-services", JSON.stringify(serviceData)); + const data = { + groups: [{ + pdpSubgroups: [{ + pdpType: "apex", + pdpInstances: [{ + instanceId: "apex-pdp1", + }], + }] + } + ], + }; + + $.ajax = jest.fn().mockImplementation((args) => { args.success(data); }); + PdpListView.RenderPdpList = jest.fn(); + sut.getEngineURL("message"); + expect(PdpListView.RenderPdpList.mock.calls[0][0]).toHaveLength(1); +}); diff --git a/gui-pdp-monitoring/src/webapp/js/__test__/PdpEngineWorkerStatus.test.js b/gui-pdp-monitoring/src/webapp/js/__test__/PdpEngineWorkerStatus.test.js new file mode 100644 index 0000000..39b74f5 --- /dev/null +++ b/gui-pdp-monitoring/src/webapp/js/__test__/PdpEngineWorkerStatus.test.js @@ -0,0 +1,37 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2020 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +import * as PdpEngineWorkerStatus from "../PdpEngineWorkerStatus"; +import * as MonitoringChart from "../MonitoringChart"; + +test("set engine status data", () => { + var temp = MonitoringChart.generateRandomData(); + var engineStatusData = [{id : 1, lastPolicyDuration : 1, averagePolicyDuration: 1}]; + var changed = jest.fn(() => { + return false; + }); + + engineStatusData[0].lastPolicyDuration = temp; + engineStatusData[0].averagePolicyDuration = temp; + window.engineStatusTables = []; + PdpEngineWorkerStatus.setEngineStatusData(engineStatusData, changed); + + expect(window.engineStatusTables[0]).toBeDefined(); +}); \ No newline at end of file diff --git a/gui-pdp-monitoring/src/webapp/js/__test__/PdpInformation.test.js b/gui-pdp-monitoring/src/webapp/js/__test__/PdpInformation.test.js new file mode 100644 index 0000000..993eb54 --- /dev/null +++ b/gui-pdp-monitoring/src/webapp/js/__test__/PdpInformation.test.js @@ -0,0 +1,31 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2020 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +import * as PdpInformation from "../PdpInformation" + +test("create engine service table", () => { + PdpInformation.createEngineServiceTable(); + expect(document.getElementById("string")).toBeDefined(); +}) + +test("ser engine service data", () => { + PdpInformation.setEngineServiceData("testing", "testing", "testing", "testing", "testing", null, null); + expect(document.getElementById("testing")).toBeDefined(); +}) \ No newline at end of file diff --git a/gui-pdp-monitoring/src/webapp/js/__test__/PdpListView.test.js b/gui-pdp-monitoring/src/webapp/js/__test__/PdpListView.test.js new file mode 100644 index 0000000..6152ac0 --- /dev/null +++ b/gui-pdp-monitoring/src/webapp/js/__test__/PdpListView.test.js @@ -0,0 +1,64 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2020 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +import * as PdpListView from "../PdpListView"; +const pdpArray = []; + +test("render pdp list", () => { + const data = { + groups: [{ + pdpSubgroups: [{ + pdpType: "apex", + pdpInstances: [{ + instanceId: "apex-pdp1", + }], + }] + } + ], + }; + + for (let i = 0; i < data.groups.length; i++) { + var map = {}; + map.title = data.groups[i].name; + map.children = []; + (data.groups[i].pdpSubgroups).forEach((pdpSubgroup, index) => { + map.children[index] = {}; + map.children[index].title = pdpSubgroup.pdpType; + const instanceId = []; + pdpSubgroup.pdpInstances.forEach(pdpInstance => { + var instanceIdMap = {}; + instanceIdMap.title = pdpInstance.instanceId; + instanceId.push(instanceIdMap) + }); + map.children[index].children = instanceId; + }); + pdpArray.push(map); + }; + + document.body.innerHTML = ''; + PdpListView.RenderPdpList(pdpArray, "pdps__list"); + expect(document.querySelector('a.' + 'pdps__link').innerHTML).toBe("apex-pdp1"); +}); + +test("high light selected", () => { + PdpListView.highlightSelected(1); + console.log(document.querySelector(`.pdps__link[href*="${1}"]`)); + expect(document.querySelector(`.pdps__link[href*="${1}"]`)).toBeDefined(); +}) \ No newline at end of file diff --git a/gui-pdp-monitoring/src/webapp/js/__test__/PdpStatisticsSummary.test.js b/gui-pdp-monitoring/src/webapp/js/__test__/PdpStatisticsSummary.test.js new file mode 100644 index 0000000..03404b7 --- /dev/null +++ b/gui-pdp-monitoring/src/webapp/js/__test__/PdpStatisticsSummary.test.js @@ -0,0 +1,38 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2020 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +import * as PdpEngineSummary from "../PdpStatisticsSummary"; + +var data = [{averagePolicyDuration : [ + { timestamp: 1584979433034, value: 95 }, + { timestamp: 1584979428034, value: 78 }, + { timestamp: 1584979423034, value: 71 }, + { timestamp: 1584979418034, value: 79 }, + { timestamp: 1584979413034, value: 21 }]}] + +test("create engine summary table", () => { + PdpEngineSummary.createEngineSummaryTable(); + expect(document.getElementById("string")).toBeDefined(); +}) + +test("set engine service data", () => { + PdpEngineSummary.setEngineSummaryData(data, null, 1, 2, 3, 1, 2, 3) + expect(document.getElementById("engineSummaryTable")).toBeDefined(); +}) \ No newline at end of file -- cgit 1.2.3-korg