From 770049ea162573d5afa1aaefed6131fe5e0a77f7 Mon Sep 17 00:00:00 2001 From: danielhanrahan Date: Tue, 15 Feb 2022 18:02:23 +0000 Subject: Refactor apex-editor frontend build Moved frontend from src/main/resources/webapp to src/main/webapp Moved static resources such as jquery-ui to webapp/dist folder Configured jest to exclude webapp/dist from coverage report Removed unused ZIP file assembly Cleaned up POM Issue-ID: POLICY-3896 Signed-off-by: danielhanrahan Change-Id: Ia006eb94a39586219029866eef94aef38912ad65 --- .../src/main/webapp/js/__test__/ApexAjax.test.js | 207 ++++++++++++++ .../js/__test__/ApexContextAlbumEditForm.test.js | 144 ++++++++++ .../webapp/js/__test__/ApexContextAlbumTab.test.js | 27 ++ .../js/__test__/ApexContextSchemaEditForm.test.js | 110 ++++++++ .../js/__test__/ApexContextSchemaTab.test.js | 54 ++++ .../webapp/js/__test__/ApexEventEditForm.test.js | 193 +++++++++++++ .../main/webapp/js/__test__/ApexEventTab.test.js | 119 ++++++++ .../src/main/webapp/js/__test__/ApexFiles.test.js | 57 ++++ .../main/webapp/js/__test__/ApexFormUtils.test.js | 35 +++ .../js/__test__/ApexKeyInformationTab.test.js | 76 +++++ .../src/main/webapp/js/__test__/ApexMain.test.js | 103 +++++++ .../webapp/js/__test__/ApexModelHandling.test.js | 96 +++++++ .../webapp/js/__test__/ApexNewModelForm.test.js | 46 ++++ .../webapp/js/__test__/ApexPageControl.test.js | 116 ++++++++ .../webapp/js/__test__/ApexPolicyEditForm.test.js | 174 ++++++++++++ .../js/__test__/ApexPolicyEditForm_State.test.js | 174 ++++++++++++ .../main/webapp/js/__test__/ApexPolicyTab.test.js | 305 +++++++++++++++++++++ .../main/webapp/js/__test__/ApexResultForm.test.js | 37 +++ .../src/main/webapp/js/__test__/ApexTable.test.js | 43 +++ .../webapp/js/__test__/ApexTaskEditForm.test.js | 179 ++++++++++++ .../main/webapp/js/__test__/ApexTaskTab.test.js | 52 ++++ .../src/main/webapp/js/__test__/ApexUtils.test.js | 116 ++++++++ .../main/webapp/js/__test__/contextMenu.test.js | 118 ++++++++ .../main/webapp/js/__test__/dropdownList.test.js | 68 +++++ .../src/main/webapp/js/__test__/fileMenu.test.js | 31 +++ .../webapp/js/__test__/showhideElement.test.js | 49 ++++ .../webapp/js/__test__/showhideTextArea.test.js | 48 ++++ .../src/main/webapp/js/__test__/test-env.js | 2 + 28 files changed, 2779 insertions(+) create mode 100644 gui-editors/gui-editor-apex/src/main/webapp/js/__test__/ApexAjax.test.js create mode 100644 gui-editors/gui-editor-apex/src/main/webapp/js/__test__/ApexContextAlbumEditForm.test.js create mode 100644 gui-editors/gui-editor-apex/src/main/webapp/js/__test__/ApexContextAlbumTab.test.js create mode 100644 gui-editors/gui-editor-apex/src/main/webapp/js/__test__/ApexContextSchemaEditForm.test.js create mode 100644 gui-editors/gui-editor-apex/src/main/webapp/js/__test__/ApexContextSchemaTab.test.js create mode 100644 gui-editors/gui-editor-apex/src/main/webapp/js/__test__/ApexEventEditForm.test.js create mode 100644 gui-editors/gui-editor-apex/src/main/webapp/js/__test__/ApexEventTab.test.js create mode 100644 gui-editors/gui-editor-apex/src/main/webapp/js/__test__/ApexFiles.test.js create mode 100644 gui-editors/gui-editor-apex/src/main/webapp/js/__test__/ApexFormUtils.test.js create mode 100644 gui-editors/gui-editor-apex/src/main/webapp/js/__test__/ApexKeyInformationTab.test.js create mode 100644 gui-editors/gui-editor-apex/src/main/webapp/js/__test__/ApexMain.test.js create mode 100644 gui-editors/gui-editor-apex/src/main/webapp/js/__test__/ApexModelHandling.test.js create mode 100644 gui-editors/gui-editor-apex/src/main/webapp/js/__test__/ApexNewModelForm.test.js create mode 100644 gui-editors/gui-editor-apex/src/main/webapp/js/__test__/ApexPageControl.test.js create mode 100644 gui-editors/gui-editor-apex/src/main/webapp/js/__test__/ApexPolicyEditForm.test.js create mode 100644 gui-editors/gui-editor-apex/src/main/webapp/js/__test__/ApexPolicyEditForm_State.test.js create mode 100644 gui-editors/gui-editor-apex/src/main/webapp/js/__test__/ApexPolicyTab.test.js create mode 100644 gui-editors/gui-editor-apex/src/main/webapp/js/__test__/ApexResultForm.test.js create mode 100644 gui-editors/gui-editor-apex/src/main/webapp/js/__test__/ApexTable.test.js create mode 100644 gui-editors/gui-editor-apex/src/main/webapp/js/__test__/ApexTaskEditForm.test.js create mode 100644 gui-editors/gui-editor-apex/src/main/webapp/js/__test__/ApexTaskTab.test.js create mode 100644 gui-editors/gui-editor-apex/src/main/webapp/js/__test__/ApexUtils.test.js create mode 100644 gui-editors/gui-editor-apex/src/main/webapp/js/__test__/contextMenu.test.js create mode 100644 gui-editors/gui-editor-apex/src/main/webapp/js/__test__/dropdownList.test.js create mode 100644 gui-editors/gui-editor-apex/src/main/webapp/js/__test__/fileMenu.test.js create mode 100644 gui-editors/gui-editor-apex/src/main/webapp/js/__test__/showhideElement.test.js create mode 100644 gui-editors/gui-editor-apex/src/main/webapp/js/__test__/showhideTextArea.test.js create mode 100644 gui-editors/gui-editor-apex/src/main/webapp/js/__test__/test-env.js (limited to 'gui-editors/gui-editor-apex/src/main/webapp/js/__test__') diff --git a/gui-editors/gui-editor-apex/src/main/webapp/js/__test__/ApexAjax.test.js b/gui-editors/gui-editor-apex/src/main/webapp/js/__test__/ApexAjax.test.js new file mode 100644 index 0000000..dadbaa0 --- /dev/null +++ b/gui-editors/gui-editor-apex/src/main/webapp/js/__test__/ApexAjax.test.js @@ -0,0 +1,207 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2020-2021 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========================================================= + */ + +const mod = require('../ApexAjax'); + +const requestURL = "http://localhost:7979"; +let data = {}; + +beforeEach(() => { + data = { + useHttps: 'useHttps', + hostname: 'hostname', + port: 'port', + username: 'username', + password: 'password', + messages: { + message: '' + }, + content: ['01', '02'], + result: 'ok', + ok: true + }; +}); + +test('Test ajax_get error', () => { + const callback = jest.fn(); + $.ajax = jest.fn().mockImplementation((args) => { + args.error(data, null, null); + }); + const mock_get_error = jest.fn(mod.ajax_get(requestURL, callback)); + mock_get_error(); + expect(mock_get_error).toHaveBeenCalled(); +}); + +test('Test ajax_get success', (done) => { + const callback = jest.fn((actualData) => { + expect(actualData).toEqual(data); + done(); + }); + const jqXHR = { status: 200, responseText: "" }; + $.ajax = jest.fn().mockImplementation((args) => { + args.success(data, null, jqXHR); + }); + mod.ajax_get(requestURL, callback); +}); + +test('Test ajax_getWithKeyInfo success', (done) => { + const myCallback = jest.fn((actual) => { + expect(actual).toEqual({ + key: { + name: "name1", + version: "version1" + }, + uuid: "UUID1", + description: "description1" + }); + done(); + }); + data.messages = { + message: [ + '{"apexKeyInfo": {"UUID": "UUID1", "description": "description1", "key":{"name": "name1", "version":' + + ' "version1"}}, "objectType": {"key": {"name": "name1", "version": "version1"}}}' + ] + }; + const jqXHR = {status: 200, responseText: ""}; + + $.ajax = jest.fn().mockImplementation((args) => { + args.success(data, null, jqXHR); + }); + mod.ajax_getWithKeyInfo("requestUrl", "objectType", myCallback, undefined); +}); + +test('Test ajax_getWithKeyInfo with custom key success', (done) => { + const myCallback = jest.fn((actual) => { + expect(actual).toEqual({ + customKey: { + name: "name1", + version: "version1" + }, + uuid: "UUID1", + description: "description1" + }); + done(); + }); + data.messages = { + message: [ + '{"apexKeyInfo": {"UUID": "UUID1", "description": "description1", "key":{"name": "name1",' + + ' "version": "version1"}}, "objectType": {"customKey": {"name": "name1", "version": "version1"}}}' + ] + }; + const jqXHR = {status: 200, responseText: ""}; + + $.ajax = jest.fn().mockImplementation((args) => { + args.success(data, null, jqXHR); + }); + mod.ajax_getWithKeyInfo("requestUrl", "objectType", myCallback, "customKey"); +}); + +test('Test ajax_delete error', () => { + const callback = jest.fn(); + const jqXHR = { status: 500, responseText: "" }; + $.ajax = jest.fn().mockImplementation((args) => { + args.error(jqXHR, null, null); + }); + const mock_delete_error = jest.fn(mod.ajax_delete(requestURL, callback)); + mock_delete_error(); + expect(mock_delete_error).toHaveBeenCalled(); +}); + +test('Test ajax_delete success', (done) => { + const callback = jest.fn((actualData) => { + expect(actualData).toEqual(data); + done(); + }); + const jqXHR = { status: 200, responseText: "" }; + $.ajax = jest.fn().mockImplementation((args) => { + args.success(data, null, jqXHR); + }); + mod.ajax_delete(requestURL, callback); +}); + +test('Test ajax_post error', () => { + const callback = jest.fn(); + const jqXHR = { status: 500, responseText: "" }; + $.ajax = jest.fn().mockImplementation((args) => { + args.error(jqXHR, null, null); + }); + const mock_post_error = jest.fn(mod.ajax_post(requestURL, data, callback)); + mock_post_error(); + expect(mock_post_error).toHaveBeenCalled(); +}); + +test('Test ajax_post success', (done) => { + const callback = jest.fn((actualData) => { + expect(actualData).toEqual(data); + done(); + }); + const jqXHR = { status: 200, responseText: "" }; + $.ajax = jest.fn().mockImplementation((args) => { + args.success(data, null, jqXHR); + }); + mod.ajax_post(requestURL, data, callback); +}); + +test('Test ajax_put error', () => { + const callback = jest.fn(); + const jqXHR = { status: 500, responseText: "" }; + $.ajax = jest.fn().mockImplementation((args) => { + args.error(jqXHR, null, null); + }); + const mock_put_error = jest.fn(mod.ajax_put(requestURL, callback)); + mock_put_error(); + expect(mock_put_error).toHaveBeenCalled(); +}); + +test('Test ajax_put success', (done) => { + const callback = jest.fn((actualData) => { + expect(actualData).toEqual(data); + done(); + }); + const jqXHR = { status: 200, responseText: "" }; + + $.ajax = jest.fn().mockImplementation((args) => { + args.success(data, null, jqXHR); + }); + mod.ajax_put(requestURL, data, callback); +}); + +test('Test ajax_getOKOrFail error', () => { + const callback = jest.fn(); + const jqXHR = { status: 500, responseText: "" }; + $.ajax = jest.fn().mockImplementation((args) => { + args.error(jqXHR, null, null); + }); + const mock_getOKOrFail_error = jest.fn(mod.ajax_getOKOrFail(requestURL, callback)); + mock_getOKOrFail_error(); + expect(mock_getOKOrFail_error).toHaveBeenCalled(); +}); + +test('Test ajax_getOKOrFail success', (done) => { + const callback = jest.fn((actualData) => { + expect(actualData).toEqual(data); + done(); + }); + const jqXHR = { status: 200, responseText: "" }; + + $.ajax = jest.fn().mockImplementation((args) => { + args.success(data, null, jqXHR); + }); + mod.ajax_getOKOrFail(requestURL, callback); +}); diff --git a/gui-editors/gui-editor-apex/src/main/webapp/js/__test__/ApexContextAlbumEditForm.test.js b/gui-editors/gui-editor-apex/src/main/webapp/js/__test__/ApexContextAlbumEditForm.test.js new file mode 100644 index 0000000..8bc3ed0 --- /dev/null +++ b/gui-editors/gui-editor-apex/src/main/webapp/js/__test__/ApexContextAlbumEditForm.test.js @@ -0,0 +1,144 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2020-2022 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========================================================= + */ + +const mod = require('../ApexContextAlbumEditForm'); +const apexUtils = require('../ApexUtils'); +const contextAlbumTab_reset = require('../ApexContextAlbumTab'); +const keyInformationTab_reset = require('../ApexKeyInformationTab'); +const formUtils_generateDescription = require('../ApexFormUtils'); + +let data = { + messages: { + message: [ + '{"apexContextSchema": {"key":{"name": "name1", "version": "version1"}}, "apexTask":{"key":{"name": "name1", "version": "version1"}},' + + '"apexContextAlbum":{"key":{"name": "name1", "version": "version1"},"itemSchema":{}},"apexEvent":{"key":{"name": "name1", "version": "version1"}},' + + '"apexPolicy":{"policyKey":{"name": "name1", "version": "version1"}}, "apexKeyInfo":{"key":{"name": "name1", "version": "version1"}}}' + ] + }, + ok: true +}; + +test('Test mock_editContextAlbumForm_activate', () => { + const mock_editContextAlbumForm_activate = jest.fn(mod.editContextAlbumForm_activate); + + const contextAlbum = { + name: 'testName', + version: '0.0.1', + scope: 'test', + uuid: 'testUUID', + description: 'testDesc', + writeable: true + }; + const contextSchema = { + name: 'testName', + version: '0.0.1', + schemaFlavour: 'testFlav', + schemaDefinition: 'testDef', + uuid: 'testUUID', + description: 'testDesc' + } + + mock_editContextAlbumForm_activate('parentTest', 'CREATE', contextAlbum, contextSchema); + expect(mock_editContextAlbumForm_activate).toHaveBeenCalledTimes(1); +}); + +test('Test Create Context Album', () => { + const jqXHR = { status: 200, responseText: "" }; + $.ajax = jest.fn().mockImplementation((args) => { + args.success(data, null, jqXHR); + }); + const mock_activate = jest.fn(mod.editContextAlbumForm_createContextAlbum); + mock_activate('parentTest'); + expect(mock_activate).toBeCalled(); +}); + +test('Test Delete Context Album', () => { + global.confirm = () => true + global.window.restRootURL = () => 'http://localhost' + const jqXHR = { status: 200, responseText: "" }; + $.ajax = jest.fn().mockImplementation((args) => { + args.success(data, null, jqXHR); + }); + jest.spyOn(keyInformationTab_reset, 'keyInformationTab_reset').mockReturnValueOnce(null); + jest.spyOn(apexUtils, 'apexUtils_removeElement').mockReturnValueOnce(null); + jest.spyOn(contextAlbumTab_reset, 'contextAlbumTab_reset').mockReturnValueOnce(null); + const mock_activate = jest.fn(mod.editContextAlbumForm_deleteContextAlbum); + mock_activate('parentTest', 'name', 'version'); + expect(mock_activate).toBeCalled(); +}); + +test('Test View Context Album', () => { + global.window.restRootURL = () => 'http://localhost' + const jqXHR = { status: 200, responseText: "" }; + $.ajax = jest.fn().mockImplementation((args) => { + args.success(data, null, jqXHR); + }); + const mock_activate = jest.fn(mod.editContextAlbumForm_viewContextAlbum); + mock_activate('parentTest', 'name', 'version'); + expect(mock_activate).toBeCalled(); +}); + +test('Test Edit Context Album', () => { + global.window.restRootURL = () => 'http://localhost' + const jqXHR = { status: 200, responseText: "" }; + $.ajax = jest.fn().mockImplementation((args) => { + args.success(data, null, jqXHR); + }); + const mock_activate = jest.fn(mod.editContextAlbumForm_editContextAlbum); + mock_activate('parentTest', 'name', 'version'); + expect(mock_activate).toBeCalled(); +}); + +test('Test Submit Pressed', () => { + jest.spyOn(apexUtils, 'apexUtils_removeElement').mockReturnValueOnce(null); + jest.spyOn(contextAlbumTab_reset, 'contextAlbumTab_reset').mockReturnValueOnce(null); + const mock_activate = jest.fn(mod.editContextAlbumForm_submitPressed); + mock_activate(); + expect(mock_activate).toBeCalled(); +}); + +test('Test Generate UUID Pressed', () => { + let documentSpy = jest.spyOn(document, 'getElementById'); + let elementMock = document.createElement("editContextAlbumFormUuidInput"); + elementMock.value = 'one' + documentSpy.mockReturnValue(elementMock); + const mock_activate = jest.fn(mod.editContextAlbumForm_generateUUIDPressed); + mock_activate(); + expect(mock_activate).toBeCalled(); +}); + +test('Test Generate Description Pressed', () => { + jest.spyOn(formUtils_generateDescription, 'formUtils_generateDescription').mockReturnValueOnce(null); + let documentSpy = jest.spyOn(document, 'getElementById'); + let elementMock = document.createElement("editContextAlbumFormDescriptionTextArea"); + elementMock.value = 'one' + documentSpy.mockReturnValue(elementMock); + const mock_activate = jest.fn(mod.editContextAlbumForm_generateDescriptionPressed); + mock_activate(); + expect(mock_activate).toBeCalled(); +}); + +test('Test editContextAlbumForm_cancelPressed', () => { + jest.spyOn(apexUtils, 'apexUtils_removeElement').mockReturnValue(null); + jest.spyOn(document, 'getElementById').mockReturnValue(null); + jest.spyOn(contextAlbumTab_reset, 'contextAlbumTab_reset').mockReturnValue(null); + const mock_activate = jest.fn(mod.editContextAlbumForm_cancelPressed); + mock_activate(); + expect(mock_activate).toBeCalled(); +}); diff --git a/gui-editors/gui-editor-apex/src/main/webapp/js/__test__/ApexContextAlbumTab.test.js b/gui-editors/gui-editor-apex/src/main/webapp/js/__test__/ApexContextAlbumTab.test.js new file mode 100644 index 0000000..e109174 --- /dev/null +++ b/gui-editors/gui-editor-apex/src/main/webapp/js/__test__/ApexContextAlbumTab.test.js @@ -0,0 +1,27 @@ +/* + * ============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========================================================= + */ + +const mod = require('../ApexContextAlbumTab'); +const utils = require('../ApexUtils'); + +test('Test contextAlbumTab_create', () => { + const mock_activate = jest.fn(mod.contextAlbumTab_activate); + mock_activate(); + expect(mock_activate).toBeCalled(); +}); diff --git a/gui-editors/gui-editor-apex/src/main/webapp/js/__test__/ApexContextSchemaEditForm.test.js b/gui-editors/gui-editor-apex/src/main/webapp/js/__test__/ApexContextSchemaEditForm.test.js new file mode 100644 index 0000000..87b4f82 --- /dev/null +++ b/gui-editors/gui-editor-apex/src/main/webapp/js/__test__/ApexContextSchemaEditForm.test.js @@ -0,0 +1,110 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2020-2021 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========================================================= + */ + +const mod = require('../ApexContextSchemaEditForm'); +const apexUtils = require('../ApexUtils'); +const apexContextSchemaTab = require('../ApexContextSchemaTab'); +const keyInformationTab_reset = require('../ApexKeyInformationTab'); +const apexAjax = require('../ApexAjax'); +const formUtils_generateDescription = require('../ApexFormUtils'); + +let data = { + messages: { + message: [] + }, + ok: true +}; +const contextSchema = { + name: 'testName', + version: '0.0.1', + schemaFlavour: 'testFlav', + schemaDefinition: 'testDef', + uuid: 'testUUID', + description: 'testDesc' +} + +test('Test editContextSchemaForm_createContextSchema', () => { + const mock_editContextSchemaForm_createContextSchema = jest.fn(mod.editContextSchemaForm_createContextSchema); + mock_editContextSchemaForm_createContextSchema('parentTest', 'CREATE', contextSchema); + expect(mock_editContextSchemaForm_createContextSchema).toBeCalled(); +}); + +test('Test Delete Context Schema', () => { + global.confirm = () => true + const jqXHR = { status: 200, responseText: "" }; + $.ajax = jest.fn().mockImplementation((args) => { + args.success(data, null, jqXHR); + }); + jest.spyOn(keyInformationTab_reset, 'keyInformationTab_reset').mockReturnValueOnce(null); + jest.spyOn(apexContextSchemaTab, 'contextSchemaTab_reset').mockReturnValueOnce(null); + const mock_activate = jest.fn(mod.editContextSchemaForm_deleteContextSchema); + mock_activate('parent', 'name', 'version'); + expect(mock_activate).toBeCalled(); +}); + +test('Test View Context Schema', () => { + jest.spyOn(apexAjax, 'ajax_getWithKeyInfo').mockReturnValueOnce(null); + const mock_activate = jest.fn(mod.editContextSchemaForm_viewContextSchema); + mock_activate('parent', 'name', 'version'); + expect(mock_activate).toBeCalled(); +}); + +test('Test Activate Context Schema', () => { + const mock_activate = jest.fn(mod.editContextSchemaForm_activate); + mock_activate('parent', 'operation', contextSchema); + expect(mock_activate).toBeCalled(); +}); + +test('Test Generate UUID Pressed', () => { + let documentSpy = jest.spyOn(document, 'getElementById'); + let elementMock = document.createElement("editContextSchemaFormUuidInput"); + elementMock.value = 'one' + documentSpy.mockReturnValue(elementMock); + const mock_activate = jest.fn(mod.editContextSchemaForm_generateUUIDPressed); + mock_activate(); + expect(mock_activate).toBeCalled(); +}); + +test('Test Generate Description Pressed', () => { + jest.spyOn(formUtils_generateDescription, 'formUtils_generateDescription').mockReturnValueOnce(null); + let documentSpy = jest.spyOn(document, 'getElementById'); + let elementMock = document.createElement("editContextSchemaFormDescriptionTextArea"); + elementMock.value = 'one' + documentSpy.mockReturnValue(elementMock); + const mock_activate = jest.fn(mod.editContextSchemaForm_generateDescriptionPressed); + mock_activate(); + expect(mock_activate).toBeCalled(); +}); + +test('Test Cancel Pressed', () => { + jest.spyOn(apexUtils, 'apexUtils_removeElement').mockReturnValueOnce(null); + jest.spyOn(apexContextSchemaTab, 'contextSchemaTab_reset').mockReturnValueOnce(null); + const mock_activate = jest.fn(mod.editContextSchemaForm_cancelPressed); + mock_activate(); + expect(mock_activate).toBeCalled(); +}); + +test('Test Submit Pressed', () => { + jest.spyOn(apexUtils, 'apexUtils_removeElement').mockReturnValueOnce(null); + jest.spyOn(apexContextSchemaTab, 'contextSchemaTab_reset').mockReturnValueOnce(null); + const mock_activate = jest.fn(mod.editContextSchemaForm_submitPressed); + mock_activate(); + expect(mock_activate).toBeCalled(); +}); \ No newline at end of file diff --git a/gui-editors/gui-editor-apex/src/main/webapp/js/__test__/ApexContextSchemaTab.test.js b/gui-editors/gui-editor-apex/src/main/webapp/js/__test__/ApexContextSchemaTab.test.js new file mode 100644 index 0000000..3883bd5 --- /dev/null +++ b/gui-editors/gui-editor-apex/src/main/webapp/js/__test__/ApexContextSchemaTab.test.js @@ -0,0 +1,54 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2020-2021 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========================================================= + */ + +const mod = require('../ApexContextSchemaTab'); + +let data = { + messages: { + message: [ + '{"apexContextSchema": {"key":{"name": "name1", "version": "version1"}}, "apexTask":{"key":{"name": "name1", "version": "version1"}},' + + '"apexContextAlbum":{"key":{"name": "name1", "version": "version1"}},"apexEvent":{"key":{"name": "name1", "version": "version1"}},' + + '"apexPolicy":{"policyKey":{"name": "name1", "version": "version1"}}, "apexKeyInfo":{"key":{"name": "name1", "version": "version1"}}}' + ] + }, + ok: true +}; + +test('Test activateContextSchema', () => { + const jqXHR = { status: 200, responseText: "" }; + $.ajax = jest.fn().mockImplementation((args) => { + args.success(data, null, jqXHR); + }); + const mock_activate = jest.fn(mod.contextSchemaTab_activate); + mock_activate(); + expect(mock_activate).toBeCalled(); +}); + +test('Test deactivate', () => { + const mock_deactivate = jest.fn(mod.contextSchemaTab_deactivate); + mock_deactivate(); + expect(mock_deactivate).toBeCalledWith(); +}); + +test('Test reset', () => { + const mock_deactivate = jest.fn(mod.contextSchemaTab_reset); + mock_deactivate(); + expect(mock_deactivate).toBeCalledWith(); +}); \ No newline at end of file diff --git a/gui-editors/gui-editor-apex/src/main/webapp/js/__test__/ApexEventEditForm.test.js b/gui-editors/gui-editor-apex/src/main/webapp/js/__test__/ApexEventEditForm.test.js new file mode 100644 index 0000000..48573e8 --- /dev/null +++ b/gui-editors/gui-editor-apex/src/main/webapp/js/__test__/ApexEventEditForm.test.js @@ -0,0 +1,193 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2020-2022 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========================================================= + */ + +const mod = require('../ApexEventEditForm'); +const eventTab_reset = require('../ApexEventTab'); +const apexUtils = require('../ApexUtils'); +const formUtils_generateDescription = require('../ApexFormUtils'); +const keyInformationTab_reset = require('../ApexKeyInformationTab'); + +let data = { + messages: { + message: [ + '{"apexContextSchema": {"key":{"name": "name1", "version": "version1"}}, "apexTask":{"key":{"name": "name1", "version": "version1"}},' + + '"apexContextAlbum":{"key":{"name": "name1", "version": "version1"}},"apexEvent":{"key":{"name": "name1", "version": "version1"}},' + + '"apexPolicy":{"policyKey":{"name": "name1", "version": "version1"}}, "apexKeyInfo":{"key":{"name": "name1", "version": "version1"}}}' + ] + }, + ok: true +}; + +const contextSchema = { + name: 'testName', + version: '0.0.1', + schemaFlavour: 'testFlav', + schemaDefinition: 'testDef', + uuid: 'testUUID', + description: 'testDesc' +} + +const parentTBody = document.createElement('table'); + +test('Test Activate', () => { + const mock_activate = jest.fn(mod.editEventForm_activate); + mock_activate(null, 'CREATE', null, contextSchema); + expect(mock_activate).toBeCalled(); +}); + +test('Test Activate Edit', () => { + + const event = { + key: { + name: 'testName', + version: '0.0.1', + }, + nameSpace: 'test', + source: 'test', + target: 'test', + uuid: 'test', + description: 'test', + } + + const mock_activate = jest.fn(mod.editEventForm_activate); + mock_activate(null, 'EDIT', event, contextSchema); + expect(mock_activate).toBeCalled(); +}); + +test('Test Activate !=Create/Edit', () => { + + const event = { + key: { + name: 'testName', + version: '0.0.1', + }, + nameSpace: 'test', + source: 'test', + target: 'test', + uuid: 'test', + description: 'test', + } + + const mock_activate = jest.fn(mod.editEventForm_activate); + mock_activate(null, 'TEST', event, contextSchema); + expect(mock_activate).toBeCalled(); +}); + +test('Test Delete Event', () => { + global.confirm = () => true + global.window.restRootURL = () => 'http://localhost' + const jqXHR = { status: 200, responseText: "" }; + $.ajax = jest.fn().mockImplementation((args) => { + args.success(data, null, jqXHR); + }); + jest.spyOn(keyInformationTab_reset, 'keyInformationTab_reset').mockReturnValueOnce(null); + jest.spyOn(apexUtils, 'apexUtils_removeElement').mockReturnValueOnce(null); + jest.spyOn(eventTab_reset, 'eventTab_reset').mockReturnValueOnce(null); + const mock_activate = jest.fn(mod.editEventForm_deleteEvent); + mock_activate('parentTest', 'name', 'version'); + expect(mock_activate).toBeCalled(); +}); + +test('Test Event Edit Form Inner', () => { + global.window.restRootURL = () => 'http://localhost' + const jqXHR = { status: 200, responseText: "" }; + $.ajax = jest.fn().mockImplementation((args) => { + args.success(data, null, jqXHR); + }); + jest.spyOn(apexUtils, 'apexUtils_removeElement').mockReturnValueOnce(null); + jest.spyOn(apexUtils, 'apexUtils_emptyElement').mockReturnValueOnce(null); + const mock_activate = jest.fn(mod.editEventForm_editEvent_inner); + mock_activate('parentTest', 'name', 'version', 'edit'); + expect(mock_activate).toBeCalled(); +}); + +test('Test View Event', () => { + const mock_activate = jest.fn(mod.editEventForm_viewEvent); + mock_activate('parentTest', 'name', 'version'); + expect(mock_activate).toBeCalled(); +}); + +test('Test Edit Event', () => { + const mock_activate = jest.fn(mod.editEventForm_editEvent); + mock_activate('parentTest', 'name', 'version'); + expect(mock_activate).toBeCalled(); +}); + +test('Test Create Event', () => { + const jqXHR = { status: 200, responseText: "" }; + $.ajax = jest.fn().mockImplementation((args) => { + args.success(data, null, jqXHR); + }); + const mock_activate = jest.fn(mod.editEventForm_createEvent); + mock_activate('parentTest'); + expect(mock_activate).toBeCalled(); +}); + +test('Test editEventForm_addEventParam', () => { + let contextSchemas = new Array(); + contextSchemas.push(contextSchema); + const mock_activate = jest.fn(mod.editEventForm_addEventParam); + mock_activate(parentTBody, false, 'name', null, contextSchema, contextSchemas); + expect(mock_activate).toBeCalled(); +}); + +test('Test Edit Event Generate UUID Pressed', () => { + let documentSpy = jest.spyOn(document, 'getElementById'); + let elementMock = document.createElement("editContextSchemaFormDescriptionTextArea"); + documentSpy.mockReturnValue(elementMock); + const mock_activate = jest.fn(mod.editEventForm_generateUUIDPressed); + mock_activate(); + expect(mock_activate).toBeCalled(); +}); + +test('Test Edit Event Generate Description Pressed', () => { + jest.spyOn(formUtils_generateDescription, 'formUtils_generateDescription').mockReturnValueOnce(null); + let documentSpy = jest.spyOn(document, 'getElementById'); + let elementMock = document.createElement("editContextSchemaFormDescriptionTextArea"); + documentSpy.mockReturnValue(elementMock); + const mock_activate = jest.fn(mod.editEventForm_generateDescriptionPressed); + mock_activate(); + expect(mock_activate).toBeCalled(); +}); +test('Test editEventForm_cancelPressed', () => { + jest.spyOn(keyInformationTab_reset, 'keyInformationTab_reset').mockReturnValueOnce(null); + jest.spyOn(apexUtils, 'apexUtils_removeElement').mockReturnValueOnce(null); + jest.spyOn(eventTab_reset, 'eventTab_reset').mockReturnValueOnce(null); + const mock_activate = jest.fn(mod.editEventForm_cancelPressed); + mock_activate(); + expect(mock_activate).toBeCalled(); +}); + +test('Test editTaskForm_submitPressed', () => { + jest.spyOn(keyInformationTab_reset, 'keyInformationTab_reset').mockReturnValueOnce(null); + jest.spyOn(apexUtils, 'apexUtils_removeElement').mockReturnValueOnce(null); + jest.spyOn(eventTab_reset, 'eventTab_reset').mockReturnValueOnce(null); + + let documentSpy = jest.spyOn(document, 'getElementById'); + let elementMock = document.createElement("editEventForm"); + elementMock.setAttribute("createEditOrView", "CREATE"); + elementMock.value = 'name_version' + elementMock.rows = 2 + documentSpy.mockReturnValue(elementMock); + + const mock_activate = jest.fn(mod.editEventForm_submitPressed); + mock_activate(); + expect(mock_activate).toBeCalled(); +}); diff --git a/gui-editors/gui-editor-apex/src/main/webapp/js/__test__/ApexEventTab.test.js b/gui-editors/gui-editor-apex/src/main/webapp/js/__test__/ApexEventTab.test.js new file mode 100644 index 0000000..16042af --- /dev/null +++ b/gui-editors/gui-editor-apex/src/main/webapp/js/__test__/ApexEventTab.test.js @@ -0,0 +1,119 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2020-2021 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========================================================= + */ + +const mod = require('../ApexEventTab'); +const ApexUtils = require('../ApexUtils'); + +afterEach(() => { + document.body.innerHTML = ''; +}); + +test('Test activate', () => { + document.body.innerHTML = '
'; + + const data = { + useHttps: 'useHttps', + hostname: 'hostname', + port: 'port', + username: 'username', + password: 'password', + messages: { + message: [ + '{"apexEvent" : {"key": {"name": "name1", "version":"version1"}, "nameSpace":"nameSpace1",' + + ' "source":"source1", "target":"target1", "parameter": ' + + '{"entry": [{"key": "key1", "value": {"optional":"optional", "fieldSchemaKey": ' + + '{"name": "name2", "version":"version2"}}}]}}}' + ] + }, + content: ['01', '02'], + result: 'ok', + ok: true + }; + + $.ajax = jest.fn().mockImplementation((args) => { + args.success(data, null, null); + }); + + const expected = '
' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '
EventName SpaceSourceTargetParameters
name1:version1nameSpace1source1target1' + + '' + + '
ParameterParameter Type/SchemaOptional
key1name2:version2optional
'; + + mod.eventTab_activate(); + expect(document.body.innerHTML).toBe(expected); +}); + +test('Test deactivate', (done) => { + ApexUtils.apexUtils_removeElement = jest.fn(id => { + expect(id).toBe('eventTabContent'); + done() + }); + mod.eventTab_deactivate(); +}); + + +test('Test create eventTabContent exists', () => { + document.body.innerHTML = '
'; + mod.eventTab_create(); + expect(document.body.innerHTML).toBe('
'); +}); + +test('Test create eventsTab does not exist', () => { + document.body.innerHTML = '
'; + mod.eventTab_create(); + expect(document.body.innerHTML).toBe('
'); +}); + +test('Test create', () => { + document.body.innerHTML = '
'; + + const expected = '
' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '
EventName SpaceSourceTargetParameters
' + + '
'; + + mod.eventTab_create(); + expect(document.body.innerHTML).toBe(expected); +}); + + +// These are being tested indirectly +// But could be tested individually here if needed +test.todo('Test reset'); diff --git a/gui-editors/gui-editor-apex/src/main/webapp/js/__test__/ApexFiles.test.js b/gui-editors/gui-editor-apex/src/main/webapp/js/__test__/ApexFiles.test.js new file mode 100644 index 0000000..f5be3cc --- /dev/null +++ b/gui-editors/gui-editor-apex/src/main/webapp/js/__test__/ApexFiles.test.js @@ -0,0 +1,57 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2020-2021 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========================================================= + */ + +const mod = require('../ApexFiles'); +const resultForm_activate = require('../ApexResultForm'); + +let data = { + messages: { + message: [ + '{"apexContextSchema": {"key":{"name": "name1", "version": "version1"}}, "apexTask":{"key":{"name": "name1", "version": "version1"}},' + + '"apexContextAlbum":{"key":{"name": "name1", "version": "version1"},"itemSchema":{}},"apexEvent":{"key":{"name": "name1", "version": "version1"}},' + + '"apexPolicy":{"policyKey":{"name": "name1", "version": "version1"}}, "apexKeyInfo":{"key":{"name": "name1", "version": "version1"}}}' + ] + }, + ok: true + }; + +test('test files_open', () => { + const open = jest.fn(mod.files_fileOpen); + open(); + expect(open).toBeCalledTimes(1); +}); + +test('test files_download', () => { + const download = jest.fn(mod.files_fileDownload); + download(); + expect(download).toHaveBeenCalledTimes(1); +}); + +test('Test files_upload', () => { + global.window.restRootURL = () => 'http://localhost' + const jqXHR = { status: 200, responseText: "" }; + $.ajax = jest.fn().mockImplementation((args) => { + args.success(data, null, jqXHR); + }); + jest.spyOn(resultForm_activate, 'resultForm_activate').mockReturnValueOnce(null); + const upload = jest.fn(mod.files_fileUpload); + upload(); + expect(upload).toBeCalled(); +}); \ No newline at end of file diff --git a/gui-editors/gui-editor-apex/src/main/webapp/js/__test__/ApexFormUtils.test.js b/gui-editors/gui-editor-apex/src/main/webapp/js/__test__/ApexFormUtils.test.js new file mode 100644 index 0000000..0df839d --- /dev/null +++ b/gui-editors/gui-editor-apex/src/main/webapp/js/__test__/ApexFormUtils.test.js @@ -0,0 +1,35 @@ +/* + * ============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========================================================= + */ + +const mod = require('../ApexFormUtils'); + +test('test generate', () => { + const mock = jest.fn(mod.formUtils_generateUUID); + mock(); + expect(mock).toBeCalledTimes(1); +}) + +test('test edit', () => { + const mock = jest.fn(mod.formUtils_generateDescription); + mock(); + expect(mock(null, null, null)).toBe("Generated description for a concept called \"null\" with version \"null\" and UUID \"null\""); + expect(mock).toBeCalledTimes(2); + + +}) \ No newline at end of file diff --git a/gui-editors/gui-editor-apex/src/main/webapp/js/__test__/ApexKeyInformationTab.test.js b/gui-editors/gui-editor-apex/src/main/webapp/js/__test__/ApexKeyInformationTab.test.js new file mode 100644 index 0000000..2dcc7c2 --- /dev/null +++ b/gui-editors/gui-editor-apex/src/main/webapp/js/__test__/ApexKeyInformationTab.test.js @@ -0,0 +1,76 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2021 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========================================================= + */ + +const ApexUtils = require("../ApexUtils"); +const ApexKeyInformationTab = require("../ApexKeyInformationTab"); + +test("Test keyInformationTab_activate", () => { + document.body.innerHTML = '
'; + const data = { + useHttps: 'useHttps', + hostname: 'hostname', + port: 'port', + username: 'username', + password: 'password', + messages: { + message: [ + '{"apexKeyInfo": {"UUID": "UUID1", "description": "description1", "key":{"name": "name1", "version":' + + ' "version1"}}, "objectType": {"key": {"name": "name1", "version": "version1"}}}' + ] + }, + content: ['01', '02'], + result: 'ok', + ok: true + }; + $.ajax = jest.fn().mockImplementation((args) => { + args.success(data, null, null); + }); + ApexKeyInformationTab.keyInformationTab_activate(); + + const actual = document.getElementById("keyInformationTabContent"); + const expected = /name1:version1<\/td>UUID1<\/uuid><\/td>description1<\/desc><\/td>/; + expect(actual.innerHTML).toMatch(expected); +}); + +test("Test keyInformationTab_deactivate", (done) => { + ApexUtils.apexUtils_removeElement = jest.fn((id) => { + expect(id).toBe("keyInformationTabContent"); + done(); + }); + ApexKeyInformationTab.keyInformationTab_deactivate() +}) + +test("Test keyInformationTab_create, key information tab exists", () => { + document.body.innerHTML = '
'; + + ApexKeyInformationTab.keyInformationTab_create(); + const actual = document.getElementById("keyInformationTab"); + expect(actual).toBeNull(); +}); + +test("Test keyInformationTab_create, ", () => { + document.body.innerHTML = '
'; + + ApexKeyInformationTab.keyInformationTab_create(); + const actual = document.getElementById("keyInformationTabContent"); + const expected = '
Key InformationUUIDDescription
' + expect(actual.innerHTML).toBe(expected); +}); + + diff --git a/gui-editors/gui-editor-apex/src/main/webapp/js/__test__/ApexMain.test.js b/gui-editors/gui-editor-apex/src/main/webapp/js/__test__/ApexMain.test.js new file mode 100644 index 0000000..13fd658 --- /dev/null +++ b/gui-editors/gui-editor-apex/src/main/webapp/js/__test__/ApexMain.test.js @@ -0,0 +1,103 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2022 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========================================================= + */ + +const mod = require('../ApexMain'); +const $ = require('jquery'); +const apexPageControl = require('../ApexPageControl'); +const apexContextSchemaTab = require('../ApexContextSchemaTab'); +const apexEventTab = require('../ApexEventTab'); +const apexAlbumTab = require('../ApexContextAlbumTab'); +const apexTaskTab = require('../ApexTaskTab'); +const apexPolicyTab = require('../ApexPolicyTab'); +const keyInformationTab = require('../ApexKeyInformationTab'); + +require('../../dist/js/jquery-ui-1.12.1/jquery-ui.js'); + +const data = { + messages: { + message: [ + '{' + + ' "apexArtifactKey": {' + + ' "key": {' + + ' "name": "name1",' + + ' "version": "version1"' + + ' }' + + ' }' + + '}' + ] + }, + content: ['01', '02'], + result: 'ok', + ok: true +}; + +const jqXHR = { status: 200, responseText: "" }; + +test('Test main_getRestRootURL', () => { + document.documentElement.innerHTML = '' + + '' + + ''; + $.ajax = jest.fn().mockImplementation((args) => { + args.success(data, null, jqXHR); + }); + jest.spyOn(window.localStorage.__proto__, 'getItem'); + window.localStorage.__proto__.getItem = jest.fn(() => true); + jest.spyOn(apexPageControl, 'pageControl_modelMode').mockReturnValueOnce(null); + jest.spyOn(apexContextSchemaTab, 'contextSchemaTab_activate').mockReturnValueOnce(null); + jest.spyOn(apexEventTab, 'eventTab_activate').mockReturnValueOnce(null); + jest.spyOn(apexAlbumTab, 'contextAlbumTab_activate').mockReturnValueOnce(null); + jest.spyOn(apexTaskTab, 'taskTab_activate').mockReturnValueOnce(null); + jest.spyOn(apexPolicyTab, 'policyTab_activate').mockReturnValueOnce(null); + jest.spyOn(keyInformationTab, 'keyInformationTab_activate').mockReturnValueOnce(null); + const mock_main_getRestRootURL = jest.fn(mod.main_getRestRootURL); + mock_main_getRestRootURL(); + const expected = ''+ + '
' + expect(mock_main_getRestRootURL).toBeCalled(); + expect(document.documentElement.innerHTML).toEqual(expected); +}); + +test('Test main_getRestRootURL false', () => { + document.documentElement.innerHTML = '
'; + $.ajax = jest.fn().mockImplementation((args) => { + args.success(data, null, jqXHR); + }); + jest.spyOn(window.localStorage.__proto__, 'getItem'); + window.localStorage.__proto__.getItem = jest.fn(() => false); + const mock_main_getRestRootURL = jest.fn(mod.main_getRestRootURL); + mock_main_getRestRootURL(); + expect(mock_main_getRestRootURL).toBeCalled(); + expect(document.documentElement.innerHTML).toEqual('
REST root URL set to: http://localhost/apexservices/editor/false
'); +}); + +test('Test clearLocalStorage', () => { + const mock_clearLocalStorage = jest.fn(mod.clearLocalStorage); + mock_clearLocalStorage(); + expect(mock_clearLocalStorage).toBeCalled(); +}); + +test('test ready', () => { + document.documentElement.innerHTML = ''; + window.$ = $; + $("#menu li").click(); + let h1 = document.querySelector('ul'); + expect(h1.textContent).toEqual('menuFileNew'); +}); diff --git a/gui-editors/gui-editor-apex/src/main/webapp/js/__test__/ApexModelHandling.test.js b/gui-editors/gui-editor-apex/src/main/webapp/js/__test__/ApexModelHandling.test.js new file mode 100644 index 0000000..2144506 --- /dev/null +++ b/gui-editors/gui-editor-apex/src/main/webapp/js/__test__/ApexModelHandling.test.js @@ -0,0 +1,96 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2020-2021 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========================================================= + */ + +const mod = require('../ApexModelHandling'); +const ApexResultForm = require("../ApexResultForm"); + +afterEach(() => { + document.body.innerHTML = ''; +}); + +test('Test modelHandling_analyse', (done) => { + const data = { + useHttps: 'useHttps', + hostname: 'hostname', + port: 'port', + username: 'username', + password: 'password', + messages: { + message: [ + '{"apexKeyInfo": null}' + ] + }, + content: ['01'], + result: 'ok', + ok: true + }; + const expectedMessage = '{"apexKeyInfo": null}'; + document.body.innerHTML = '
'; + + $.ajax = jest.fn().mockImplementation((args) => { + args.success(data, null, null); + }); + + ApexResultForm.resultForm_activate = jest.fn((element, heading, message) => { + expect(element).not.toBeNull(); + expect(heading).toBe('Model Analysis Result'); + expect(message).toBe(expectedMessage); + done(); + }); + + mod.modelHandling_analyse(); +}); + +test('Test modelHandling_validate', (done) => { + const data = { + useHttps: 'useHttps', + hostname: 'hostname', + port: 'port', + username: 'username', + password: 'password', + messages: { + message: [ + '{"apexKeyInfo": null}', + '{"apexPolicy": null}', + '{"apexEvent": null}' + ] + }, + content: ['01'], + result: 'ok', + ok: true + }; + + const expectedMessage = '{"apexPolicy": null}\n{"apexEvent": null}\n'; + + document.body.innerHTML = '
'; + + $.ajax = jest.fn().mockImplementation((args) => { + args.success(data, null, null); + }); + + ApexResultForm.resultForm_activate = jest.fn((element, heading, message) => { + expect(element).not.toBeNull(); + expect(heading).toBe('Model Validation Result'); + expect(message).toBe(expectedMessage); + done(); + }); + + mod.modelHandling_validate(); +}); diff --git a/gui-editors/gui-editor-apex/src/main/webapp/js/__test__/ApexNewModelForm.test.js b/gui-editors/gui-editor-apex/src/main/webapp/js/__test__/ApexNewModelForm.test.js new file mode 100644 index 0000000..5088988 --- /dev/null +++ b/gui-editors/gui-editor-apex/src/main/webapp/js/__test__/ApexNewModelForm.test.js @@ -0,0 +1,46 @@ +/* + * ============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========================================================= + */ + +const mod = require('../ApexNewModelForm'); + +test('test activate', () => { + document.innerHTML = + '
' + + '
' + + '
' + + '
'; + + const mock_activate = jest.fn(mod.newModelForm_activate); + mock_activate(document.createElement("elementTest")); + expect(mock_activate).toBeCalled(); +}); + +test('Test generateUUIDPressed', () => { + document.innerHTML = + '
'; + + const mock_generateUuid = jest.fn(mod.newModelForm_generateUUIDPressed); + mock_generateUuid.mockImplementation(() => { + document.createElement("newModelFormUuidInput"); + document.getElementsByTagName("newModelFormUuidInput").value = 'test'; + }); + mock_generateUuid(); + expect(mock_generateUuid).toBeCalled(); +}); \ No newline at end of file diff --git a/gui-editors/gui-editor-apex/src/main/webapp/js/__test__/ApexPageControl.test.js b/gui-editors/gui-editor-apex/src/main/webapp/js/__test__/ApexPageControl.test.js new file mode 100644 index 0000000..b414bb1 --- /dev/null +++ b/gui-editors/gui-editor-apex/src/main/webapp/js/__test__/ApexPageControl.test.js @@ -0,0 +1,116 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2021 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========================================================= + */ + +const mod = require('../ApexPageControl'); +const apexContextSchemaTab = require('../ApexContextSchemaTab'); +const apexEventTab = require('../ApexEventTab'); +const apexAlbumTab = require('../ApexContextAlbumTab'); +const apexTaskTab = require('../ApexTaskTab'); +const apexPolicyTab = require('../ApexPolicyTab'); +const keyInformationTab = require('../ApexKeyInformationTab'); + +test('Test showPlaceholder show', () => { + const mock_activate = jest.fn(mod.showPlaceholder); + mock_activate('show'); + expect(mock_activate).toBeCalled(); +}); + +test('Test showPlaceholder hide', () => { + const mock_activate = jest.fn(mod.showPlaceholder); + mock_activate(''); + expect(mock_activate).toBeCalled(); +}); + +test('Test pageControl_status', () => { + let data = { + messages: { + message: [] + }, + ok: true + }; + const mock_activate = jest.fn(mod.pageControl_status); + mock_activate(data); + expect(mock_activate).toBeCalled(); +}); + +test('Test pageControl_status when data is not present', () => { + let data = { + messages: { + message: [] + } + }; + const mock_activate = jest.fn(mod.pageControl_status); + mock_activate(data); + expect(mock_activate).toBeCalled(); +}); + +test('Test pageControl_restError', () => { + const mock_activate = jest.fn(mod.pageControl_restError); + mock_activate('', '', '', ''); + expect(mock_activate).toBeCalled(); +}); + +test('Test pageControl_modelMode', () => { + jest.spyOn(apexContextSchemaTab, 'contextSchemaTab_activate').mockReturnValueOnce(null); + jest.spyOn(apexEventTab, 'eventTab_activate').mockReturnValueOnce(null); + jest.spyOn(apexAlbumTab, 'contextAlbumTab_activate').mockReturnValueOnce(null); + jest.spyOn(apexTaskTab, 'taskTab_activate').mockReturnValueOnce(null); + jest.spyOn(apexPolicyTab, 'policyTab_activate').mockReturnValueOnce(null); + jest.spyOn(keyInformationTab, 'keyInformationTab_activate').mockReturnValueOnce(null); + const mock_activate = jest.fn(mod.pageControl_modelMode); + mock_activate('name', 'version', 'fileName'); + expect(mock_activate).toBeCalled(); +}); + +test('Test pageControl_noModelMode', () => { + jest.spyOn(apexContextSchemaTab, 'contextSchemaTab_activate').mockReturnValueOnce(null); + jest.spyOn(apexEventTab, 'eventTab_activate').mockReturnValueOnce(null); + jest.spyOn(apexAlbumTab, 'contextAlbumTab_activate').mockReturnValueOnce(null); + jest.spyOn(apexTaskTab, 'taskTab_activate').mockReturnValueOnce(null); + jest.spyOn(apexPolicyTab, 'policyTab_activate').mockReturnValueOnce(null); + jest.spyOn(keyInformationTab, 'keyInformationTab_activate').mockReturnValueOnce(null); + const mock_activate = jest.fn(mod.pageControl_noModelMode); + mock_activate(); + expect(mock_activate).toBeCalled(); +}); + +test('Test pageControl_busyMode', () => { + jest.spyOn(apexContextSchemaTab, 'contextSchemaTab_activate').mockReturnValueOnce(null); + jest.spyOn(apexEventTab, 'eventTab_activate').mockReturnValueOnce(null); + jest.spyOn(apexAlbumTab, 'contextAlbumTab_activate').mockReturnValueOnce(null); + jest.spyOn(apexTaskTab, 'taskTab_activate').mockReturnValueOnce(null); + jest.spyOn(apexPolicyTab, 'policyTab_activate').mockReturnValueOnce(null); + jest.spyOn(keyInformationTab, 'keyInformationTab_activate').mockReturnValueOnce(null); + const mock_activate = jest.fn(mod.pageControl_busyMode); + mock_activate(); + expect(mock_activate).toBeCalled(); +}); + +test('Test pageControl_readyMode', () => { + jest.spyOn(apexContextSchemaTab, 'contextSchemaTab_activate').mockReturnValueOnce(null); + jest.spyOn(apexEventTab, 'eventTab_activate').mockReturnValueOnce(null); + jest.spyOn(apexAlbumTab, 'contextAlbumTab_activate').mockReturnValueOnce(null); + jest.spyOn(apexTaskTab, 'taskTab_activate').mockReturnValueOnce(null); + jest.spyOn(apexPolicyTab, 'policyTab_activate').mockReturnValueOnce(null); + jest.spyOn(keyInformationTab, 'keyInformationTab_activate').mockReturnValueOnce(null); + const mock_activate = jest.fn(mod.pageControl_readyMode); + mock_activate(); + expect(mock_activate).toBeCalled(); +}); \ No newline at end of file diff --git a/gui-editors/gui-editor-apex/src/main/webapp/js/__test__/ApexPolicyEditForm.test.js b/gui-editors/gui-editor-apex/src/main/webapp/js/__test__/ApexPolicyEditForm.test.js new file mode 100644 index 0000000..b3153a9 --- /dev/null +++ b/gui-editors/gui-editor-apex/src/main/webapp/js/__test__/ApexPolicyEditForm.test.js @@ -0,0 +1,174 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2020-2021 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========================================================= + */ +const apexUtils = require('../ApexUtils'); +const apexPageControl = require('../ApexPageControl'); +const apexPolicyTab = require('../ApexPolicyTab'); +const keyInformationTab_reset = require('../ApexKeyInformationTab'); +const mod = require('../ApexPolicyEditForm'); +const policy = { + policyKey: { + name: 'testName', + version: 'testVersion', + uuid: 'testUUID' + }, + uuid: 'testUUID' +} +let data = { + messages: { + message: [ + '{"apexContextSchema": {"key":{"name": "name1", "version": "version1"}}, "apexTask":{"key":{"name": "name1", "version": "version1"}},' + + '"apexContextAlbum":{"key":{"name": "name1", "version": "version1"}},"apexEvent":{"key":{"name": "name1", "version": "version1"}},' + + '"apexPolicy":{"policyKey":{"name": "name1", "version": "version1"}}, "apexKeyInfo":{"key":{"name": "name1", "version": "version1"}}}' + ] + }, + ok: true +}; + +test('Test Create Policy', () => { + const mock_activate = jest.fn(mod.editPolicyForm_createPolicy); + mock_activate('test'); + expect(mock_activate).toBeCalled(); +}); + +test('Test Delete Policy', () => { + global.confirm = () => true + global.window.restRootURL = () => 'http://localhost' + const jqXHR = { status: 200, responseText: "" }; + $.ajax = jest.fn().mockImplementation((args) => { + args.success(data, null, jqXHR); + }); + jest.spyOn(apexPageControl, 'pageControl_successStatus').mockReturnValueOnce(policy); + jest.spyOn(apexPolicyTab, 'policyTab_reset').mockReturnValueOnce(null); + jest.spyOn(keyInformationTab_reset, 'keyInformationTab_reset').mockReturnValueOnce(null); + jest.spyOn(apexUtils, 'apexUtils_removeElement').mockReturnValueOnce(null); + const mock_activate = jest.fn(mod.editPolicyForm_deletePolicy); + mock_activate('test', policy.policyKey.name, policy.policyKey.version); + expect(mock_activate).toBeCalled(); +}); + +test('Test View Policy', () => { + const jqXHR = { status: 200, responseText: "" }; + $.ajax = jest.fn().mockImplementation((args) => { + args.success(data, null, jqXHR); + }); + jest.spyOn(apexPageControl, 'pageControl_successStatus').mockReturnValueOnce(policy); + const mock_activate = jest.fn(mod.editPolicyForm_viewPolicy); + mock_activate('test', policy.policyKey.name, policy.policyKey.version); + expect(mock_activate).toBeCalled(); +}); + +test('Test Edit Policy', () => { + const jqXHR = { status: 200, responseText: "" }; + $.ajax = jest.fn().mockImplementation((args) => { + args.success(data, null, jqXHR); + }); + const mock_activate = jest.fn(mod.editPolicyForm_editPolicy); + mock_activate('test', policy.policyKey.name, policy.policyKey.version); + expect(mock_activate).toBeCalled(); +}); + +test('Test Edit Policy Inner', () => { + const jqXHR = { status: 200, responseText: "" }; + $.ajax = jest.fn().mockImplementation((args) => { + args.success(data, null, jqXHR); + }); + jest.spyOn(apexPageControl, 'pageControl_successStatus').mockReturnValueOnce(policy); + jest.spyOn(apexUtils, 'apexUtils_removeElement').mockReturnValueOnce(null); + const mock_activate = jest.fn(mod.editPolicyForm_editPolicy_inner); + mock_activate('test', policy, 'view'); + expect(mock_activate).toBeCalled(); +}); + +test('Test Add New State Policy', () => { + let documentSpy = jest.spyOn(document, 'getElementById'); + let elementMock = document.createElement("editEventFormNewStateInput"); + elementMock.value = 'one' + documentSpy.mockReturnValue(elementMock); + const mock_activate = jest.fn(mod.editPolicyForm_addNewState); + const node = document.body.parentNode; + mock_activate(node, 'New State', policy, 'tasks', 'events', 'contextS', 'contextI'); + expect(mock_activate).toBeCalled(); +}); + +test('Test Add State Policy', () => { + const mock_activate = jest.fn(mod.editPolicyForm_addState); + mock_activate('state','test', 'CREATE', policy, 'tasks', 'events', 'contextS', 'contextI'); + expect(mock_activate).toBeCalled(); +}); + +test('Test Generate Description', () => { + const mock_activate = jest.fn(mod.editPolicyForm_generateDescriptionPressed); + mock_activate(); + expect(mock_activate).toBeCalled(); +}); + +test('Test Submit Pressed', () => { + jest.spyOn(apexUtils, 'apexUtils_removeElement').mockReturnValueOnce(null); + jest.spyOn(apexPolicyTab, 'policyTab_reset').mockReturnValueOnce(null); + const mock_activate = jest.fn(mod.editPolicyForm_submitPressed); + mock_activate(); + expect(mock_activate).toBeCalled(); +}); + +test('Test Submit Pressed When createEditOrView is CREATE', () => { + let documentSpy = jest.spyOn(document, 'getElementById'); + let editPolicyFormElementMock = document.createElement("editPolicyForm"); + editPolicyFormElementMock.setAttribute("createEditOrView", "CREATE") + documentSpy.mockReturnValue(editPolicyFormElementMock) + const mock_activate = jest.fn(mod.editPolicyForm_submitPressed); + mock_activate(); + expect(mock_activate).toBeCalled(); +}); + +test('Test Update Trigger Event', () => { + const mock_activate = jest.fn(mod.editPolicyForm_updateTriggerEventOptions); + mock_activate('events'); + expect(mock_activate).toBeCalled(); +}); + +test('Test activate CREATE', () => { + jest.spyOn(apexUtils, 'apexUtils_removeElement').mockReturnValueOnce(null); + const mock_activate = jest.fn(mod.editPolicyForm_activate); + + mock_activate('test', 'CREATE', policy, 'tasks', 'events', 'contextS', 'contextI'); + expect(mock_activate).toBeCalled(); +}); + +test('Test activate EDIT', () => { + jest.spyOn(apexUtils, 'apexUtils_removeElement').mockReturnValueOnce(null); + const mock_activate = jest.fn(mod.editPolicyForm_activate); + + mock_activate('test', 'EDIT', policy, 'tasks', 'events', 'contextS', 'contextI'); + expect(mock_activate).toBeCalled(); +}); + +test('Test activate !CREATE/EDIT', () => { + jest.spyOn(apexUtils, 'apexUtils_removeElement').mockReturnValueOnce(null); + const mock_activate = jest.fn(mod.editPolicyForm_activate); + + mock_activate('test', 'TEST', policy, 'tasks', 'events', 'contextS', 'contextI'); + expect(mock_activate).toBeCalled(); +}); + +test('Test editPolicyForm_editPolicy_inner', () => { + jest.spyOn(apexUtils, 'apexUtils_removeElement').mockReturnValueOnce(null); + const mock_editPolicyForm_editPolicy_inner = jest.fn(mod.editPolicyForm_editPolicy_inner); + mock_editPolicyForm_editPolicy_inner('formParent', policy, 'VIEW'); + expect(mock_editPolicyForm_editPolicy_inner).toBeCalled(); +}) \ No newline at end of file diff --git a/gui-editors/gui-editor-apex/src/main/webapp/js/__test__/ApexPolicyEditForm_State.test.js b/gui-editors/gui-editor-apex/src/main/webapp/js/__test__/ApexPolicyEditForm_State.test.js new file mode 100644 index 0000000..8b73319 --- /dev/null +++ b/gui-editors/gui-editor-apex/src/main/webapp/js/__test__/ApexPolicyEditForm_State.test.js @@ -0,0 +1,174 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2020-2022 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========================================================= + */ + +const mod = require('../ApexPolicyEditForm_State'); + +const contextAlbum = { + name: 'testName', + version: '0.0.1', + scope: 'test', + uuid: 'testUUID', + description: 'testDesc', + writeable: true +}; +const contextSchema = { + name: 'testName', + version: '0.0.1', + schemaFlavour: 'testFlav', + schemaDefinition: 'testDef', + uuid: 'testUUID', + description: 'testDesc' +} + +const task = { + key: { + name: 'testName', + version: 'testVersion' + }, + uuid: 'testUUID' +}; + +const event = { + key: { + name: 'testName', + version: '0.0.1', + }, + nameSpace: 'test', + source: 'test', + target: 'test', + uuid: 'test', + description: 'test', +} + +const policy = { + policyKey: { + name: 'testName', + version: 'testVersion', + uuid: 'testUUID' + }, + uuid: 'testUUID' +} + +const state = { + trigger: { + name: 'testName', + version: '0.0.1', + } +} + +const parentTBody = document.createElement('table'); + +test('Test editPolicyForm_State_generateStateDiv CREATE', () => { + const mock_editPolicyForm_State_generateStateDiv = jest.fn(mod.editPolicyForm_State_generateStateDiv); + mock_editPolicyForm_State_generateStateDiv('CREATE', policy, 'stateName', state, task, event, contextAlbum, contextSchema); + expect(mock_editPolicyForm_State_generateStateDiv).toBeCalledWith('CREATE', policy, 'stateName', state, task, event, contextAlbum, contextSchema); +}); + +test('Test editPolicyForm_State_generateStateDiv VIEW', () => { + const mock_editPolicyForm_State_generateStateDiv = jest.fn(mod.editPolicyForm_State_generateStateDiv); + mock_editPolicyForm_State_generateStateDiv('VIEW', policy, 'stateName', state, task, event, contextAlbum, contextSchema); + expect(mock_editPolicyForm_State_generateStateDiv).toBeCalledWith('VIEW', policy, 'stateName', state, task, event, contextAlbum, contextSchema); +}); + +test('Test editPolicyForm_State_generateStateDiv EDIT', () => { + const mock_editPolicyForm_State_generateStateDiv = jest.fn(mod.editPolicyForm_State_generateStateDiv); + mock_editPolicyForm_State_generateStateDiv('EDIT', policy, 'stateName', state, task, event, contextAlbum, contextSchema); + expect(mock_editPolicyForm_State_generateStateDiv).toBeCalledWith('EDIT', policy, 'stateName', state, task, event, contextAlbum, contextSchema); +}); + +test('Test editPolicyForm_State_addStateLogicOutput', () => { + const mock_editPolicyForm_State_addStateLogicOutput = jest.fn(mod.editPolicyForm_State_addStateLogicOutput); + mock_editPolicyForm_State_addStateLogicOutput(parentTBody, false, 'stateName', state, 'outputName', 'logic', 'flavour'); + expect(mock_editPolicyForm_State_addStateLogicOutput).toBeCalledWith(parentTBody, false, 'stateName', state, 'outputName', 'logic', 'flavour'); +}); + +test('Test editPolicyForm_State_addStateDirectOutput', () => { + const mock_editPolicyForm_State_addStateDirectOutput = jest.fn(mod.editPolicyForm_State_addStateDirectOutput); + mock_editPolicyForm_State_addStateDirectOutput(parentTBody, false, 'stateName', state, 'outputName', state, event, 'options', event); + expect(mock_editPolicyForm_State_addStateDirectOutput).toBeCalledWith(parentTBody, false, 'stateName', state, 'outputName', state, event, 'options', event); +}); + +test('Test editPolicyForm_State_addPolicyContext', () => { + const mock_editPolicyForm_State_addPolicyContext = jest.fn(mod.editPolicyForm_State_addPolicyContext); + mock_editPolicyForm_State_addPolicyContext(parentTBody, false, 'stateName', 'contextName', 'ref', contextAlbum); + expect(mock_editPolicyForm_State_addPolicyContext).toBeCalledWith(parentTBody, false, 'stateName', 'contextName', 'ref', contextAlbum); +}); + +test('Test editPolicyForm_State_addPolicyTask', () => { + const mock_editPolicyForm_State_addPolicyTask = jest.fn(mod.editPolicyForm_State_addPolicyTask); + mock_editPolicyForm_State_addPolicyTask(parentTBody, false, false, state, 'stateName', 'ref', task, 'options'); + expect(mock_editPolicyForm_State_addPolicyTask).toBeCalledWith(parentTBody, false, false, state, 'stateName', 'ref', task, 'options'); + +}); + +test('Test editPolicyForm_State_getStateBean StateName is Null', () => { + jest.spyOn(global.console, 'error').mockImplementation(() => {}); + mod.editPolicyForm_State_getStateBean(null); + expect(console.error).toHaveBeenCalledTimes(2); + mod.editPolicyForm_State_getStateBean('FakeState'); + expect(console.error).toHaveBeenCalledTimes(4); + global.console.error.mockRestore(); +}); + +test('Test editPolicyForm_State_getLogicOutputMappingOptions with page', () => { + document.documentElement.innerHTML = '' + + '' + + '' + + '' + + '' + + '
cell 1cell 2
cell 3cell4
cell 5cell6
' + + ''; + let documentSpy = jest.spyOn(document, 'getElementById'); + let elementMock = document.createElement("editPolicyFormLogicOutputsTable_stateName"); + elementMock.value = "localName"; + elementMock.rows = document.getElementById("editPolicyFormLogicOutputsTable_stateName").rows; + documentSpy.mockReturnValue(elementMock); + const mock_activate = jest.fn(mod.editPolicyForm_State_getLogicOutputMappingOptions); + mock_activate('stateName'); + expect(mock_activate).toBeCalledWith('stateName'); +}); + +test('Test editPolicyForm_State_getStateBean with page', () => { + document.documentElement.innerHTML = '' + + '
' + + '
' + + '' + + '' + + '' + + '' + + '
cell 1cell 2
cell 3cell4
cell 5cell6
' + + '
' + + ''; + let documentSpy = jest.spyOn(document, 'getElementById'); + let elementMock = document.createElement("editPolicyFormDirOutputsTable_stateName"); + elementMock.setAttribute("context_id", "a0"); + elementMock.setAttribute("task_id", "d0"); + elementMock.rows = document.getElementById("editPolicyFormContextsTable_stateName").rows; + elementMock.key = {"name": "name1", "version": "version1"}; + elementMock.selectedOption = + {"name": "name1", "version": "version1", + "album": { "key": { "name": 'testAlbumName', "version": '0.0.1'}}, + "event": { "key": { "name": 'testEventName', "version": '0.1.1'}}, + "task": { "key": { "name": 'tname', "version": '1.0.0'}}}; + elementMock.value = "localName"; + documentSpy.mockReturnValue(elementMock); + const mock_activate = jest.fn(mod.editPolicyForm_State_getStateBean); + mock_activate('stateName'); + expect(mock_activate).toBeCalledWith('stateName'); +}); diff --git a/gui-editors/gui-editor-apex/src/main/webapp/js/__test__/ApexPolicyTab.test.js b/gui-editors/gui-editor-apex/src/main/webapp/js/__test__/ApexPolicyTab.test.js new file mode 100644 index 0000000..4136fd6 --- /dev/null +++ b/gui-editors/gui-editor-apex/src/main/webapp/js/__test__/ApexPolicyTab.test.js @@ -0,0 +1,305 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2020-2021 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========================================================= + */ + +const mod = require('../ApexPolicyTab'); +const ApexUtils = require('../ApexUtils'); + +afterEach(() => { + document.body.innerHTML = ''; +}); + +test('Test policyTab_activate', () => { + document.body.innerHTML = '
'; + + const data = { + useHttps: 'useHttps', + hostname: 'hostname', + port: 'port', + username: 'username', + password: 'password', + messages: { + message: [ + '{' + + ' "apexPolicy": {' + + ' "policyKey": {' + + ' "name": "name1",' + + ' "version": "version1"' + + ' },' + + ' "template": "template1",' + + ' "firstState": "key11",' + + ' "state": {' + + ' "entry": [' + + ' {' + + ' "key": "key1",' + + ' "value": {' + + ' "trigger": {' + + ' "name": "name2",' + + ' "version": "version2"' + + ' },' + + ' "taskReferences": {' + + ' "entry": [' + + ' {' + + ' "key": {' + + ' "name": "name3",' + + ' "version": "version3"' + + ' },' + + ' "version": "version2",' + + ' "value": {' + + ' "outputType": "outputType1",' + + ' "output": {' + + ' "localName": "localName1"' + + ' }' + + ' }' + + ' }' + + ' ]' + + ' },' + + ' "defaultTask": {' + + ' "name": "name4",' + + ' "version": "version4"' + + ' },' + + ' "taskSelectionLogic": {' + + ' "logicFlavour": "logicFlavour1"' + + ' },' + + ' "stateOutputs": {' + + ' "entry": [' + + ' {' + + ' "key": "key2",' + + ' "value": {' + + ' "nextState": {' + + ' "localName": "localName2"' + + ' },' + + ' "outgoingEvent": {' + + ' "name": "name4",' + + ' "version": "version4"' + + ' }' + + ' }' + + ' }' + + ' ]' + + ' },' + + ' "stateFinalizerLogicMap": {' + + ' "entry": [' + + ' {' + + ' "key": "key3",' + + ' "value": {' + + ' "logicFlavour": "logicFlavour2"' + + ' }' + + ' }' + + ' ]' + + ' },' + + ' "contextAlbumReference": [' + + ' {' + + ' "name": "name5",' + + ' "version": "version5"' + + ' }' + + ' ]' + + ' }' + + ' },' + + ' {' + + ' "key": "key11",' + + ' "value": {' + + ' "trigger": {' + + ' "name": "name12",' + + ' "version": "version12"' + + ' },' + + ' "taskReferences": {' + + ' "entry": [' + + ' {' + + ' "key": {' + + ' "name": "name13",' + + ' "version": "version13"' + + ' },' + + ' "version": "version12",' + + ' "value": {' + + ' "outputType": "outputType11",' + + ' "output": {' + + ' "localName": "localName11"' + + ' }' + + ' }' + + ' }' + + ' ]' + + ' },' + + ' "defaultTask": {' + + ' "name": "name14",' + + ' "version": "version14"' + + ' },' + + ' "taskSelectionLogic": {' + + ' "logicFlavour": "logicFlavour11"' + + ' },' + + ' "stateOutputs": {' + + ' "entry": [' + + ' {' + + ' "key": "key12",' + + ' "value": {' + + ' "nextState": {' + + ' "localName": "localName12"' + + ' },' + + ' "outgoingEvent": {' + + ' "name": "name14",' + + ' "version": "version14"' + + ' }' + + ' }' + + ' }' + + ' ]' + + ' },' + + ' "stateFinalizerLogicMap": {' + + ' "entry": [' + + ' {' + + ' "key": "key13",' + + ' "value": {' + + ' "logicFlavour": "logicFlavour12"' + + ' }' + + ' }' + + ' ]' + + ' },' + + ' "contextAlbumReference": [' + + ' {' + + ' "name": "name15",' + + ' "version": "version15"' + + ' }' + + ' ]' + + ' }' + + ' }' + + ' ]' + + ' }' + + ' }' + + '}' + ] + }, + content: ['01', '02'], + result: 'ok', + ok: true + }; + + $.ajax = jest.fn().mockImplementation((args) => { + args.success(data, null, null); + }); + + const expected = '
' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '
PolicyTemplateFirst StateStates
name1:version1template1key11' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '
StateTriggerReferenced TasksDefault TaskTSLState OutputsState Finsalizer LogicContext Album References
key11name12:version12' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '
Task ReferenceOutput TypeOutput
name13:version13outputType11localName11
name14:version14logicFlavour11' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '
NameNext StateEvent
key12localName12name14:version14
' + + '
NameType
key13logicFlavour12
name15:version15
key1name2:version2' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '
Task ReferenceOutput TypeOutput
name3:version3outputType1localName1
name4:version4logicFlavour1' + + '' + + '' + + '' + + '' + + '
NameNext StateEvent
key2localName2name4:version4
' + + '
NameType
key3logicFlavour2
name5:version5
'; + + mod.policyTab_activate(); + expect(document.body.innerHTML).toBe(expected); +}); + +test('Test policyTab_deactivate', (done) => { + ApexUtils.apexUtils_removeElement = jest.fn(id => { + expect(id).toBe('policyTabContent'); + done(); + }); + + mod.policyTab_deactivate(); +}); + +test('Test policyTab_create policyTabContent exists', () => { + document.body.innerHTML = '
'; + + mod.policyTab_create(); + expect(document.body.innerHTML).toBe('
'); +}); + +test('Test policyTab_create policiesTab does not exists', () => { + document.body.innerHTML = '
'; + + mod.policyTab_create(); + expect(document.body.innerHTML).toBe('
'); +}); + +test('Test policyTab_create', () => { + document.body.innerHTML = '
'; + + const expected = '
' + + '' + + '' + + '' + + '' + + '' + + '' + + '
PolicyTemplateFirst StateStates
'; + + mod.policyTab_create(); + expect(document.body.innerHTML).toBe(expected); +}); diff --git a/gui-editors/gui-editor-apex/src/main/webapp/js/__test__/ApexResultForm.test.js b/gui-editors/gui-editor-apex/src/main/webapp/js/__test__/ApexResultForm.test.js new file mode 100644 index 0000000..a219ac9 --- /dev/null +++ b/gui-editors/gui-editor-apex/src/main/webapp/js/__test__/ApexResultForm.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========================================================= + */ + +const mod = require('../ApexResultForm'); + +test('resultForm test', () => { + document.innerHTML = + '
' + + '
' + + '
' + + '
'; + + const content = document.getElementById("elementTest"); + const mock_activate = jest.fn(mod.resultForm_activate); + mock_activate(document.createElement("elementTest"), 'heading', 'text'); + expect(mock_activate).toBeCalled(); + + const mock_pressedOK = jest.fn(mod.resultForm_okPressed); + mock_pressedOK(); +}); \ No newline at end of file diff --git a/gui-editors/gui-editor-apex/src/main/webapp/js/__test__/ApexTable.test.js b/gui-editors/gui-editor-apex/src/main/webapp/js/__test__/ApexTable.test.js new file mode 100644 index 0000000..864b0c9 --- /dev/null +++ b/gui-editors/gui-editor-apex/src/main/webapp/js/__test__/ApexTable.test.js @@ -0,0 +1,43 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2020-2021 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========================================================= + */ + +const ApexTable = require("../ApexTable"); + +test("Test createTable", () => { + const expected = document.createElement("table"); + expected.id = "my-id"; + expected.className = "apexTable ebTable elTablelib-Table-table ebTable_striped"; + + const actual = ApexTable.createTable(expected.id); + expect(actual).toEqual(expected); +}); + +test("Test setRowHover", () => { + const element = { + className: null, + onmouseover: null, + onmouseout: null + }; + + ApexTable.setRowHover(element); + + expect(element.className).toBe("ebTableRow"); + expect(typeof element.onmouseover).toBe("function"); + expect(typeof element.onmouseout).toBe("function"); +}); diff --git a/gui-editors/gui-editor-apex/src/main/webapp/js/__test__/ApexTaskEditForm.test.js b/gui-editors/gui-editor-apex/src/main/webapp/js/__test__/ApexTaskEditForm.test.js new file mode 100644 index 0000000..b9982f5 --- /dev/null +++ b/gui-editors/gui-editor-apex/src/main/webapp/js/__test__/ApexTaskEditForm.test.js @@ -0,0 +1,179 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2020-2022 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========================================================= + */ + +const mod = require('../ApexTaskEditForm'); +const apexUtils = require('../ApexUtils'); +const apexTaskTab = require('../ApexTaskTab'); +const keyInformationTab_reset = require('../ApexKeyInformationTab'); + +const contextSchema = { + name: 'testName', + version: '0.0.1', + schemaFlavour: 'testFlav', + schemaDefinition: 'testDef', + uuid: 'testUUID', + description: 'testDesc' +}; + +const task = { + key: { + name: 'testName', + version: 'testVersion' + }, + uuid: 'testUUID' +}; + +let data = { + messages: { + message: [ + '{"apexContextSchema": {"key":{"name": "name1", "version": "version1"}}, "apexTask":{"key":{"name": "name1", "version": "version1"}},' + + '"apexContextAlbum":{"key":{"name": "name1", "version": "version1"}},"apexEvent":{"key":{"name": "name1", "version": "version1"}},' + + '"apexPolicy":{"policyKey":{"name": "name1", "version": "version1"}}, "apexKeyInfo":{"key":{"name": "name1", "version": "version1"}}}' + ] + }, + ok: true +}; + + let contextAlbumReference = { + "name" : "TestAlbum", + "version" : "0.0.1" +}; + +const parentTBody = document.createElement('table'); + +test('Test editTaskForm_activate CREATE', () => { + const mock_activate = jest.fn(mod.editTaskForm_activate); + mock_activate('test', 'CREATE', 'task', contextSchema, 'album'); + expect(mock_activate).toBeCalled(); +}); + +test('Test editTaskForm_activate EDIT', () => { + const mock_activate = jest.fn(mod.editTaskForm_activate); + mock_activate('test', 'EDIT', task, contextSchema, 'album'); + expect(mock_activate).toBeCalled(); +}); + +test('Test Create Task', () => { + const jqXHR = { status: 200, responseText: "" }; + $.ajax = jest.fn().mockImplementation((args) => { + args.success(data, null, jqXHR); + }); + const mock_activate = jest.fn(mod.editTaskForm_createTask); + mock_activate('test'); + expect(mock_activate).toBeCalled(); +}); + +test('Test Delete Task', () => { + global.confirm = () => true + global.window.restRootURL = () => 'http://localhost' + const jqXHR = { status: 200, responseText: "" }; + $.ajax = jest.fn().mockImplementation((args) => { + args.success(data, null, jqXHR); + }); + jest.spyOn(apexTaskTab, 'taskTab_reset').mockReturnValueOnce(null); + jest.spyOn(keyInformationTab_reset, 'keyInformationTab_reset').mockReturnValueOnce(null); + jest.spyOn(apexUtils, 'apexUtils_removeElement').mockReturnValueOnce(null); + const mock_activate = jest.fn(mod.editTaskForm_deleteTask); + mock_activate('test'); + expect(mock_activate).toBeCalled(); +}); + +test('Test Edit Task Inner', () => { + const jqXHR = { status: 200, responseText: "" }; + $.ajax = jest.fn().mockImplementation((args) => { + args.success(data, null, jqXHR); + }); + const mock_activate = jest.fn(mod.editTaskForm_editTask_inner); + mock_activate('test', 'name', 'version', 'Edit'); + expect(mock_activate).toBeCalled(); +}); + +test('Test editTaskForm_addTaskInputField', () => { + const mock_activate = jest.fn(mod.editTaskForm_addTaskInputField); + let contextSchemas = new Array(); + contextSchemas.push(contextSchema); + mock_activate(parentTBody, true, 'name', null, contextSchema, contextSchemas); + expect(mock_activate).toBeCalled(); +}); + +test('Test editTaskForm_addTaskOutputField', () => { + const mock_activate = jest.fn(mod.editTaskForm_addTaskOutputField); + let contextSchemas = new Array(); + contextSchemas.push(contextSchema); + mock_activate(parentTBody, true, 'name', null, contextSchema, contextSchemas); + expect(mock_activate).toBeCalled(); +}); + +test('Test editTaskForm_addTaskParameter', () => { + const mock_activate = jest.fn(mod.editTaskForm_addTaskParameter); + mock_activate(parentTBody, true, 'name', null); + expect(mock_activate).toBeCalled(); +}); + +test('Test editTaskForm_addTaskContext', () => { + const mock_activate = jest.fn(mod.editTaskForm_addTaskContext); + let contextAlbums = new Array(); + contextAlbums.push(contextAlbumReference); + mock_activate(parentTBody, true, 'name', null, contextAlbumReference, contextAlbums); + expect(mock_activate).toBeCalled(); +}); + +test('Test editTaskForm_generateUUIDPressed', () => { + let documentSpy = jest.spyOn(document, 'getElementById'); + let editTaskFormUuidInput = document.createElement("editTaskFormUuidInput"); + documentSpy.mockReturnValue(editTaskFormUuidInput); + const mock_activate = jest.fn(mod.editTaskForm_generateUUIDPressed); + mock_activate(); + expect(mock_activate).toBeCalled(); +}); + +test('Test editTaskForm_generateDescriptionPressed', () => { + let documentSpy = jest.spyOn(document, 'getElementById'); + let editTaskFormDescriptionTextArea = document.createElement("editTaskFormDescriptionTextArea"); + documentSpy.mockReturnValue(editTaskFormDescriptionTextArea); + const mock_activate = jest.fn(mod.editTaskForm_generateDescriptionPressed); + mock_activate(); + expect(mock_activate).toBeCalled(); +}); + +test('Test editTaskForm_cancelPressed', () => { + jest.spyOn(apexTaskTab, 'taskTab_reset').mockReturnValueOnce(null); + jest.spyOn(keyInformationTab_reset, 'keyInformationTab_reset').mockReturnValueOnce(null); + jest.spyOn(apexUtils, 'apexUtils_removeElement').mockReturnValueOnce(null); + const mock_activate = jest.fn(mod.editTaskForm_cancelPressed); + mock_activate(); + expect(mock_activate).toBeCalled(); +}); + +test('Test editTaskForm_submitPressed', () => { + jest.spyOn(apexTaskTab, 'taskTab_reset').mockReturnValueOnce(null); + jest.spyOn(keyInformationTab_reset, 'keyInformationTab_reset').mockReturnValueOnce(null); + jest.spyOn(apexUtils, 'apexUtils_removeElement').mockReturnValueOnce(null); + + let documentSpy = jest.spyOn(document, 'getElementById'); + let elementMock = document.createElement("editTaskForm"); + elementMock.setAttribute("createEditOrView", "CREATE"); + elementMock.value = 'logictype' + documentSpy.mockReturnValue(elementMock); + + const mock_activate = jest.fn(mod.editTaskForm_submitPressed); + mock_activate(); + expect(mock_activate).toBeCalled(); +}); diff --git a/gui-editors/gui-editor-apex/src/main/webapp/js/__test__/ApexTaskTab.test.js b/gui-editors/gui-editor-apex/src/main/webapp/js/__test__/ApexTaskTab.test.js new file mode 100644 index 0000000..f593bd8 --- /dev/null +++ b/gui-editors/gui-editor-apex/src/main/webapp/js/__test__/ApexTaskTab.test.js @@ -0,0 +1,52 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2020-2021 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========================================================= + */ + +const mod = require('../ApexTaskTab'); + +let data = { + messages: { + message: [ + '{"apexContextSchema": {"key":{"name": "name1", "version": "version1"}}, "apexTask":{"key":{"name": "name1", "version": "version1"},' + + '"taskLogic":{"logicFlavour":"logicFlavour"},"inputFields":{"entry": [{"key":"","value":{"fieldSchemaKey":{"name":"name"}}}]},' + + '"outputFields":{"entry": [{"key":"","value":{"fieldSchemaKey":{"name":"name"}}}]},' + + '"taskParameters":{"entry": [{"key":"","value":{"fieldSchemaKey":{"name":"name"}}}]},'+ + '"contextAlbumReference":[{"name":"name", "version":"version"}]},'+ + '"apexContextAlbum":{"key":{"name": "name1", "version": "version1"}},"apexEvent":{"key":{"name": "name1", "version": "version1"}},' + + '"apexPolicy":{"policyKey":{"name": "name1", "version": "version1"}}, "apexKeyInfo":{"key":{"name": "name1", "version": "version1"}}}' + ] + }, + ok: true +}; + +test('test dom : taskTab_activate', () => { + const jqXHR = { status: 200, responseText: "" }; + $.ajax = jest.fn().mockImplementation((args) => { + args.success(data, null, jqXHR); + }); + const activate_mock = jest.fn(mod.taskTab_activate); + activate_mock(); + expect(activate_mock).toBeCalled(); +}); + +test('test reset', () => { + const reset_mock = jest.fn(mod.taskTab_reset); + reset_mock(); + expect(reset_mock).toBeCalled(); +}); \ No newline at end of file diff --git a/gui-editors/gui-editor-apex/src/main/webapp/js/__test__/ApexUtils.test.js b/gui-editors/gui-editor-apex/src/main/webapp/js/__test__/ApexUtils.test.js new file mode 100644 index 0000000..d00327e --- /dev/null +++ b/gui-editors/gui-editor-apex/src/main/webapp/js/__test__/ApexUtils.test.js @@ -0,0 +1,116 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2021-2022 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========================================================= + */ + +const ApexUtils = require('../ApexUtils'); + +afterEach(() => { + delete global.confirm; + + document.body.innerHTML = ''; +}); + +test('test apexUtils_areYouSure', () => { + const errorMsg = 'My message'; + const returned = {}; + global.confirm = jest + .fn() + .mockImplementation((message) => { + expect(message).toBe(errorMsg); + return returned; + }); + + const actual = ApexUtils.apexUtils_areYouSure(errorMsg); + expect(actual).toBe(returned); +}); + +test('apexUtils_emptyElement found', () => { + document.body.innerHTML = + '
' + + ' ' + + ' ' + + '
'; + + ApexUtils.apexUtils_emptyElement('tested'); + + expect(document.body.innerHTML).toBe('
') +}); + +test('apexUtils_emptyElement found', () => { + const text = + '
' + + ' ' + + ' ' + + '
'; + document.body.innerHTML = text; + ApexUtils.apexUtils_emptyElement('tested'); + + expect(document.body.innerHTML).toBe(text) +}); + +test('apexUtils_removeElement not found', () => { + const expected = /
\s*' + + '
'; + + ApexUtils.apexUtils_removeElement('tested'); + expect(document.body.innerHTML).toMatch(expected); +}); + +test('apexUtils_escapeHtml', () => { + const actual = ApexUtils.apexUtils_escapeHtml('&"\'/`=\n\t d'); + expect(actual).toBe('&<ab>"'/`=
     d') +}); + +test('createAddFormButton no text', () => { + const expected = document.createElement('div'); + expected.setAttribute('class','add-field'); + expected.innerHTML = 'Add'; + const actual = ApexUtils.createAddFormButton(); + expect(actual).toEqual(expected); +}); + +test('createAddFormButton with text', () => { + const expected = document.createElement('div'); + expected.setAttribute('class','add-field'); + expected.innerHTML = 'My_text'; + const actual = ApexUtils.createAddFormButton('My_text'); + expect(actual).toEqual(expected); +}); + +test('test EMPTY createEditArea', () => { + const mock_activate = jest.fn(ApexUtils.createEditArea); + mock_activate('id', 'options', ''); + expect(mock_activate).toBeCalled(); +}); + +test('test getHomepageURL', () => { + const mock_activate = jest.fn(ApexUtils.getHomepageURL); + mock_activate(); + expect(mock_activate).toBeCalled(); +}); + +test('test isFirefox', () => { + const mock_activate = jest.fn(ApexUtils.isFirefox); + mock_activate(); + expect(mock_activate).toBeCalled(); +}); \ No newline at end of file diff --git a/gui-editors/gui-editor-apex/src/main/webapp/js/__test__/contextMenu.test.js b/gui-editors/gui-editor-apex/src/main/webapp/js/__test__/contextMenu.test.js new file mode 100644 index 0000000..d804b6d --- /dev/null +++ b/gui-editors/gui-editor-apex/src/main/webapp/js/__test__/contextMenu.test.js @@ -0,0 +1,118 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2021 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========================================================= + */ + +const mod = require('../contextMenu'); +const apexContextEdit = require('../ApexContextSchemaEditForm'); +const apexEventEdit = require('../ApexEventEditForm'); +const apexTaskEdit = require('../ApexTaskEditForm'); +const apexPolicyEdit = require('../ApexPolicyEditForm'); +const apexContextAlbumEdit = require('../ApexContextAlbumEditForm'); + +test('Test rightClickMenu', () => { + let documentSpy = jest.spyOn(document, 'getElementById'); + let elementMock = document.createElement("rightClickMenu"); + documentSpy.mockReturnValue(elementMock); + const event = new MouseEvent('click'); + const mock_activate = jest.fn(mod.rightClickMenu); + mock_activate(event, 'type', 'name', 'version'); + expect(mock_activate).toBeCalled(); +}); + +test('Test rightClickMenuCreate when Type is CONTEXTSCHEMA', () => { + global.confirm = () => true + jest.spyOn(apexContextEdit, 'editContextSchemaForm_createContextSchema').mockReturnValueOnce(null); + const mock_activate = jest.fn(mod.rightClickMenuCreate); + mock_activate('parent', 'CONTEXTSCHEMA'); + expect(mock_activate).toBeCalled(); +}); + +test('Test rightClickMenuCreate when Type is EVENT or TASK or POLICY or CONTEXTALBUM or EMPTY', () => { + global.confirm = () => true + jest.spyOn(apexEventEdit, 'editEventForm_createEvent').mockReturnValueOnce(null); + const mock_activate = jest.fn(mod.rightClickMenuCreate); + mock_activate('parent', 'EVENT'); + expect(mock_activate).toBeCalled(); + mock_activate('parent', 'TASK'); + expect(mock_activate).toBeCalled(); + mock_activate('parent', 'POLICY'); + expect(mock_activate).toBeCalled(); + mock_activate('parent', 'CONTEXTALBUM'); + expect(mock_activate).toBeCalled(); + mock_activate('parent', ''); + expect(mock_activate).toBeCalled(); + +}); + +test('Test rightClickMenuView when Type is CONTEXTSCHEMA or EVENT or TASK or POLICY or CONTEXTALBUM or EMPTY', () => { + global.confirm = () => true + jest.spyOn(apexContextEdit, 'editContextSchemaForm_viewContextSchema').mockReturnValueOnce(null); + const mock_activate = jest.fn(mod.rightClickMenuView); + mock_activate('parent', 'CONTEXTSCHEMA'); + expect(mock_activate).toBeCalled(); + mock_activate('parent', 'EVENT'); + expect(mock_activate).toBeCalled(); + mock_activate('parent', 'TASK'); + expect(mock_activate).toBeCalled(); + mock_activate('parent', 'POLICY'); + expect(mock_activate).toBeCalled(); + mock_activate('parent', 'CONTEXTALBUM'); + expect(mock_activate).toBeCalled(); + mock_activate('parent', ''); + expect(mock_activate).toBeCalled(); +}); + +test('Test rightClickMenuEdit when Type is CONTEXTSCHEMA or EVENT or TASK or POLICY or CONTEXTALBUM or EMPTY', () => { + global.confirm = () => true + jest.spyOn(apexContextEdit, 'editContextSchemaForm_viewContextSchema').mockReturnValueOnce(null); + const mock_activate = jest.fn(mod.rightClickMenuEdit); + mock_activate('parent', 'CONTEXTSCHEMA'); + expect(mock_activate).toBeCalled(); + mock_activate('parent', 'EVENT'); + expect(mock_activate).toBeCalled(); + mock_activate('parent', 'TASK'); + expect(mock_activate).toBeCalled(); + mock_activate('parent', 'POLICY'); + expect(mock_activate).toBeCalled(); + mock_activate('parent', 'CONTEXTALBUM'); + expect(mock_activate).toBeCalled(); + mock_activate('parent', ''); + expect(mock_activate).toBeCalled(); +}); + +test('Test rightClickMenuDelete when Type is CONTEXTSCHEMA or EVENT or TASK or POLICY or CONTEXTALBUM or EMPTY', () => { + jest.spyOn(apexContextEdit, 'editContextSchemaForm_deleteContextSchema').mockReturnValueOnce(null); + jest.spyOn(apexEventEdit, 'editEventForm_deleteEvent').mockReturnValueOnce(null); + jest.spyOn(apexTaskEdit, 'editTaskForm_deleteTask').mockReturnValueOnce(null); + jest.spyOn(apexPolicyEdit, 'editPolicyForm_deletePolicy').mockReturnValueOnce(null); + jest.spyOn(apexContextAlbumEdit, 'editContextAlbumForm_deleteContextAlbum').mockReturnValueOnce(null); + const mock_activate = jest.fn(mod.rightClickMenuDelete); + mock_activate('parent', 'CONTEXTSCHEMA'); + expect(mock_activate).toBeCalled(); + mock_activate('parent', 'EVENT'); + expect(mock_activate).toBeCalled(); + mock_activate('parent', 'TASK'); + expect(mock_activate).toBeCalled(); + mock_activate('parent', 'POLICY'); + expect(mock_activate).toBeCalled(); + mock_activate('parent', 'CONTEXTALBUM'); + expect(mock_activate).toBeCalled(); + mock_activate('parent', ''); + expect(mock_activate).toBeCalled(); +}); \ No newline at end of file diff --git a/gui-editors/gui-editor-apex/src/main/webapp/js/__test__/dropdownList.test.js b/gui-editors/gui-editor-apex/src/main/webapp/js/__test__/dropdownList.test.js new file mode 100644 index 0000000..1c314af --- /dev/null +++ b/gui-editors/gui-editor-apex/src/main/webapp/js/__test__/dropdownList.test.js @@ -0,0 +1,68 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2022 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========================================================= + */ + +const mod = require('../dropdownList'); + +let selectedOption = { + displaytext : 'displaytext' +}; + +const page = '' + +'' + +'' + +'
' + +''; + +test('test dropdownList option select', () => { + document.documentElement.innerHTML = page; + mod.dropdownList_option_select('divName',selectedOption, false); + let expected = '' + + '
displaytext
' + + '' + + '
' + + ''; + expect(document.documentElement.innerHTML).toBe(expected); +}); + +test('test dropdownList display click', () => { + document.documentElement.innerHTML = page; + mod.dropdownList_display_click('divName',selectedOption, false, false); + const expected = '' + + '' + + '' + + '
'; + expect(document.documentElement.innerHTML).toBe(expected); + mod.dropdownList_display_click('divName',selectedOption, true, false); +}); + +test('test dropdownList filter', () => { + document.documentElement.innerHTML = '' + + '' + + '
  • test
  • ' + + ''; + let documentSpy = jest.spyOn(document, 'getElementById'); + let elementMock = document.createElement("optionDiv_search"); + elementMock.value = '1' + elementMock.id = 'divName' + documentSpy.mockReturnValue(elementMock); + mod.dropdownList_filter('optionDiv','optionUl'); + const expected = '

    tosearch

  • test
  • '; + expect(document.documentElement.innerHTML).toBe(expected); +}); \ No newline at end of file diff --git a/gui-editors/gui-editor-apex/src/main/webapp/js/__test__/fileMenu.test.js b/gui-editors/gui-editor-apex/src/main/webapp/js/__test__/fileMenu.test.js new file mode 100644 index 0000000..ffc9e2c --- /dev/null +++ b/gui-editors/gui-editor-apex/src/main/webapp/js/__test__/fileMenu.test.js @@ -0,0 +1,31 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2022 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========================================================= + */ + +const mod = require('../fileMenu'); + +test('test hideMenu', () => { + document.documentElement.innerHTML = ''; + $ = require('jquery'); + window.$ = $; + $('#menu').fileMenu(); + $('#menu').click(); + let h1 = document.querySelector('ul'); + expect(h1.textContent).toEqual('Hello world!'); +}); diff --git a/gui-editors/gui-editor-apex/src/main/webapp/js/__test__/showhideElement.test.js b/gui-editors/gui-editor-apex/src/main/webapp/js/__test__/showhideElement.test.js new file mode 100644 index 0000000..6b2bfa5 --- /dev/null +++ b/gui-editors/gui-editor-apex/src/main/webapp/js/__test__/showhideElement.test.js @@ -0,0 +1,49 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2021-2022 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========================================================= + */ + +const mod = require('../showhideElement'); + +test('Test editTaskForm_activate CREATE', () => { + const mock_activate = jest.fn(mod.showHideElement_display); + let documentSpy = jest.spyOn(document, 'getElementById'); + mock_activate(documentSpy, 'element', 'style', 'hidestyle', 'buttonshowStyle','buttonhideStyle'); + expect(mock_activate).toBeCalled(); +}); + +test('Test editTaskForm_activate NO CHECKBOX', () => { + const mock_activate = jest.fn(mod.showHideElement_display); + spyOn($.fn, "find").and.returnValue(false); + const mockedElementDOM = { classList: { contains: jest.fn(), remove: jest.fn(), add: jest.fn() } }; + mockedElementDOM.classList.contains.mockReturnValueOnce(true); + mockedElementDOM.classList.remove.mockReturnValueOnce(true); + mock_activate(mockedElementDOM, 'element', 'style', 'hidestyle', 'buttonshowStyle','buttonhideStyle'); + expect(mock_activate).toBeCalled(); +}); + +test('Test editTaskForm', () => { + const mock_activate = jest.fn(mod.showHideElement); + var element = document.createElement("div"); + mock_activate("id_prefix", element, true, "showText", "hideText", "style", "hidestyle", "buttonshowStyle", "buttonhideStyle"); + expect(mock_activate).toBeCalled(); +}); + + + + diff --git a/gui-editors/gui-editor-apex/src/main/webapp/js/__test__/showhideTextArea.test.js b/gui-editors/gui-editor-apex/src/main/webapp/js/__test__/showhideTextArea.test.js new file mode 100644 index 0000000..3a72037 --- /dev/null +++ b/gui-editors/gui-editor-apex/src/main/webapp/js/__test__/showhideTextArea.test.js @@ -0,0 +1,48 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2021 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========================================================= + */ + +const mod = require('../showhideTextarea'); + +test('Test showHideTextarea_display_hide', () => { + let documentSpy = jest.spyOn(document, 'getElementById'); + let element_text = document.createElement("text_textarea"); + let element_showHide = document.createElement("text_showhide"); + documentSpy.mockReturnValue(element_text); + documentSpy.mockReturnValue(element_showHide); + const mock_activate = jest.fn(mod.showHideTextarea_display_hide); + mock_activate('text'); + expect(mock_activate).toBeCalled(); +}); + +test('Test showHideTextarea_display_show', () => { + let documentSpy = jest.spyOn(document, 'getElementById'); + let element_text = document.createElement("text_textarea"); + let element_showHide = document.createElement("text_showhide"); + documentSpy.mockReturnValue(element_text); + documentSpy.mockReturnValue(element_showHide); + const mock_activate = jest.fn(mod.showHideTextarea_display_show); + mock_activate('text'); + expect(mock_activate).toBeCalled(); +}); + + + + + diff --git a/gui-editors/gui-editor-apex/src/main/webapp/js/__test__/test-env.js b/gui-editors/gui-editor-apex/src/main/webapp/js/__test__/test-env.js new file mode 100644 index 0000000..8fd788a --- /dev/null +++ b/gui-editors/gui-editor-apex/src/main/webapp/js/__test__/test-env.js @@ -0,0 +1,2 @@ +import $ from 'jquery'; +global.$ = global.jQuery = $; \ No newline at end of file -- cgit 1.2.3-korg