From 6687b48b58bf1248532de48f3e375b1f663f4015 Mon Sep 17 00:00:00 2001 From: "saul.gill" Date: Fri, 2 Jul 2021 17:10:54 +0100 Subject: Changed components to be service-centric Moved endpoints to ControlLoopService.js Refactored components and tests Issue-ID: POLICY-3424 Change-Id: I70d48750250eecd651b845ef0c726617983f75f5 Signed-off-by: saul.gill --- .../ControlLoop/GetLocalToscaFileForUpload.js | 150 ++++++++++++++++++++ .../ControlLoop/GetLocalToscaFileForUpload.test.js | 122 ++++++++++++++++ .../dialogs/ControlLoop/GetToscaTemplate.js | 50 +++++++ .../dialogs/ControlLoop/GetToscaTemplate.test.js | 53 +++++++ .../dialogs/ControlLoop/ReadAndConvertYaml.js | 96 +++++++++++++ .../dialogs/ControlLoop/ReadAndConvertYaml.test.js | 99 +++++++++++++ .../dialogs/ControlLoop/UploadToscaFile.js | 54 ++++++++ .../dialogs/ControlLoop/UploadToscaFile.test.js | 61 ++++++++ .../GetLocalToscaFileForUpload.test.js.snap | 103 ++++++++++++++ .../__snapshots__/GetToscaTemplate.test.js.snap | 15 ++ .../__snapshots__/ReadAndConvertYaml.test.js.snap | 46 +++++++ .../__snapshots__/UploadToscaFile.test.js.snap | 16 +++ .../dialogs/GetLocalToscaFileForUpload.js | 153 --------------------- .../dialogs/GetLocalToscaFileForUpload.test.js | 124 ----------------- .../src/components/dialogs/ReadAndConvertYaml.js | 87 ------------ .../components/dialogs/ReadAndConvertYaml.test.js | 101 -------------- .../GetLocalToscaFileForUpload.test.js.snap | 103 -------------- .../__snapshots__/ReadAndConvertYaml.test.js.snap | 39 ------ 18 files changed, 865 insertions(+), 607 deletions(-) create mode 100644 gui-clamp/ui-react/src/components/dialogs/ControlLoop/GetLocalToscaFileForUpload.js create mode 100644 gui-clamp/ui-react/src/components/dialogs/ControlLoop/GetLocalToscaFileForUpload.test.js create mode 100644 gui-clamp/ui-react/src/components/dialogs/ControlLoop/GetToscaTemplate.js create mode 100644 gui-clamp/ui-react/src/components/dialogs/ControlLoop/GetToscaTemplate.test.js create mode 100644 gui-clamp/ui-react/src/components/dialogs/ControlLoop/ReadAndConvertYaml.js create mode 100644 gui-clamp/ui-react/src/components/dialogs/ControlLoop/ReadAndConvertYaml.test.js create mode 100644 gui-clamp/ui-react/src/components/dialogs/ControlLoop/UploadToscaFile.js create mode 100644 gui-clamp/ui-react/src/components/dialogs/ControlLoop/UploadToscaFile.test.js create mode 100644 gui-clamp/ui-react/src/components/dialogs/ControlLoop/__snapshots__/GetLocalToscaFileForUpload.test.js.snap create mode 100644 gui-clamp/ui-react/src/components/dialogs/ControlLoop/__snapshots__/GetToscaTemplate.test.js.snap create mode 100644 gui-clamp/ui-react/src/components/dialogs/ControlLoop/__snapshots__/ReadAndConvertYaml.test.js.snap create mode 100644 gui-clamp/ui-react/src/components/dialogs/ControlLoop/__snapshots__/UploadToscaFile.test.js.snap delete mode 100644 gui-clamp/ui-react/src/components/dialogs/GetLocalToscaFileForUpload.js delete mode 100644 gui-clamp/ui-react/src/components/dialogs/GetLocalToscaFileForUpload.test.js delete mode 100644 gui-clamp/ui-react/src/components/dialogs/ReadAndConvertYaml.js delete mode 100644 gui-clamp/ui-react/src/components/dialogs/ReadAndConvertYaml.test.js delete mode 100644 gui-clamp/ui-react/src/components/dialogs/__snapshots__/GetLocalToscaFileForUpload.test.js.snap delete mode 100644 gui-clamp/ui-react/src/components/dialogs/__snapshots__/ReadAndConvertYaml.test.js.snap (limited to 'gui-clamp/ui-react/src/components/dialogs') diff --git a/gui-clamp/ui-react/src/components/dialogs/ControlLoop/GetLocalToscaFileForUpload.js b/gui-clamp/ui-react/src/components/dialogs/ControlLoop/GetLocalToscaFileForUpload.js new file mode 100644 index 0000000..013dd89 --- /dev/null +++ b/gui-clamp/ui-react/src/components/dialogs/ControlLoop/GetLocalToscaFileForUpload.js @@ -0,0 +1,150 @@ +/* + * ============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========================================================= + */ + +import React, { useState } from 'react'; +import Button from "react-bootstrap/Button"; +import Modal from 'react-bootstrap/Modal'; +import Form from 'react-bootstrap/Form'; +import Row from 'react-bootstrap/Row'; +import styled from 'styled-components'; +import Alert from 'react-bootstrap/Alert'; +import * as yaml from "js-yaml"; +import UploadToscaFile from "./UploadToscaFile"; + +const ModalStyled = styled(Modal)` + background-color: transparent; +` + +const StyledMessagesDiv = styled.div` + overflow: auto; + min-width: 100%; + max-height: 300px; + padding: 5px 5px 0px 5px; + text-align: center; +` + +const GetLocalToscaFileForUpload = (props) => { + const [selectedFile, setSelectedFile] = useState(); + const [fileIsSelected, setFileIsSelected] = useState(false); + const [toscaJsonObject, setToscaJsonObject] = useState({}); + const [show, setShow] = useState(true); + const [alertMessages, setAlertMessages] = useState(); + + const handleClose = () => { + console.log('handleClose called'); + setShow(false); + props.history.push('/'); + } + + const fileChangeHandler = (event) => { + event.preventDefault(); + console.log('fileChangeHandler called'); + + if (event.currentTarget.files[0] !== undefined) { + console.log('file defined'); + setSelectedFile(event.currentTarget.files[0]); + setFileIsSelected(true); + + const file = event.currentTarget.files[0]; + + setAlertMessages([]); + + const fileReader = new FileReader(); + + fileReader.onload = () => { + const jsonObj = yaml.load(fileReader.result, 'utf8'); + setToscaJsonObject(jsonObj); + } + + fileReader.readAsText(file); + + } else { + return; + } + }; + + const receiveResponseFromUpload = async (response) => { + + if (await response.ok) { + setAlertMessages( + Upload Success +

Tosca Service Template from { selectedFile.name } was Successfully Uploaded

+
+

Type: { selectedFile.type }

Size: { +selectedFile.size / 1000 }Kb

+
); + } + else { + setAlertMessages( + Upload Failure +

Tosca Service Template from { selectedFile.name } failed to upload

+

Status code: { await response.status }: { response.statusText }

+

Response Text: { await response.text() }

+
+

Type: { selectedFile.type }

Size: { +selectedFile.size / 1000 }Kb

+
); + } + }; + + return ( + + + Upload Tosca to Commissioning API + +
+
+ +
+ + + + Only .yaml, .yml and .json files are supported + + + + + + + { alertMessages } + + +
+
+
+ + + +
+ ); +} + +export default GetLocalToscaFileForUpload; diff --git a/gui-clamp/ui-react/src/components/dialogs/ControlLoop/GetLocalToscaFileForUpload.test.js b/gui-clamp/ui-react/src/components/dialogs/ControlLoop/GetLocalToscaFileForUpload.test.js new file mode 100644 index 0000000..9b885c6 --- /dev/null +++ b/gui-clamp/ui-react/src/components/dialogs/ControlLoop/GetLocalToscaFileForUpload.test.js @@ -0,0 +1,122 @@ +/* + * ============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========================================================= + */ + +import React from 'react'; +import { mount, shallow } from 'enzyme'; +import toJson from 'enzyme-to-json'; +import { act } from "react-dom/test-utils"; +import GetLocalToscaFileForUpload from './GetLocalToscaFileForUpload'; +import { createMemoryHistory } from 'history'; + + +describe('Verify GetLocalToscaFileForUpload', () => { + const fs = require('fs'); + let testFile = fs.readFileSync('src/components/dialogs/Policy/toscaData.test.json'); + const file = new Blob([testFile], { type: 'file' }); + + beforeEach(() => { + fetch.resetMocks(); + fetch.mockImplementation(() => { + return Promise.resolve({ + ok: true, + status: 200, + json: () => { + return Promise.resolve({ + "tosca_definitions_version": "tosca_simple_yaml_1_1_0", + "data_types": {}, + "policy_types": {}, + "topology_template": {}, + "name": "ToscaServiceTemplateSimple", + "version": "1.0.0", + "metadata": {}, + "id": "0.19518677404255147" + }); + } + }); + }); + }) + + it("renders without crashing", () => { + shallow(); + }); + + it("renders correctly", () => { + const tree = shallow(); + expect(toJson(tree)).toMatchSnapshot(); + }); + + it('should have a UploadToscaFile element', () => { + const container = shallow() + expect(container.find('UploadToscaFile').length).toEqual(1); + }); + + it('handleClose called when bottom button clicked', () => { + const history = createMemoryHistory(); + const component = mount() + const logSpy = jest.spyOn(console, 'log'); + + + act(() => { + component.find('[variant="secondary"]').simulate('click'); + expect(logSpy).toHaveBeenCalledWith('handleClose called'); + }); + }); + + it('handleClose called when top-right button clicked', () => { + const history = createMemoryHistory(); + const component = mount() + const logSpy = jest.spyOn(console, 'log'); + + + act(() => { + component.find('[size="lg"]').get(0).props.onHide(); + expect(logSpy).toHaveBeenCalledWith('handleClose called'); + }); + }); + + it('should call fileChangeHandler on change, file undefined', () => { + const component = mount(); + const logSpy = jest.spyOn(console, 'log'); + const event = { + preventDefault() { + }, + currentTarget: { files: [] } + }; + + act(() => { + component.find('[type="file"]').get(0).props.onChange(event); + expect(logSpy).toHaveBeenCalledWith('fileChangeHandler called'); + }); + }); + + it('should call fileChangeHandler on change, file defined', async () => { + const component = mount(); + const logSpy = jest.spyOn(console, 'log'); + const event = { + preventDefault() { + }, + currentTarget: { files: [file] } + }; + + act(async () => { + component.find('[type="file"]').get(0).props.onChange(event); + expect(logSpy).toHaveBeenCalledWith('file defined'); + }); + }); +}); diff --git a/gui-clamp/ui-react/src/components/dialogs/ControlLoop/GetToscaTemplate.js b/gui-clamp/ui-react/src/components/dialogs/ControlLoop/GetToscaTemplate.js new file mode 100644 index 0000000..7da8c13 --- /dev/null +++ b/gui-clamp/ui-react/src/components/dialogs/ControlLoop/GetToscaTemplate.js @@ -0,0 +1,50 @@ +/* + * ============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========================================================= + */ + +import React, { useState } from "react"; +import Button from "react-bootstrap/Button"; +import ControlLoopService from "../../../api/ControlLoopService"; + +const GetToscaTemplate = (props) => { + + const [windowLocationPathName, setWindowLocationPathname] = useState(''); + + const getTemplateHandler = async () => { + console.log('getTemplateHandler called') + setWindowLocationPathname(window.location.pathname); + + const response = await ControlLoopService.getToscaTemplate(props.templateName, props.templateVersion, windowLocationPathName) + .catch(error => error.message); + + props.onGetToscaServiceTemplate(response); + + } + + return ( + + + + ); + + +} + +export default GetToscaTemplate; diff --git a/gui-clamp/ui-react/src/components/dialogs/ControlLoop/GetToscaTemplate.test.js b/gui-clamp/ui-react/src/components/dialogs/ControlLoop/GetToscaTemplate.test.js new file mode 100644 index 0000000..ce2a398 --- /dev/null +++ b/gui-clamp/ui-react/src/components/dialogs/ControlLoop/GetToscaTemplate.test.js @@ -0,0 +1,53 @@ +/* + * ============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========================================================= + */ + +import React from 'react'; +import { mount, shallow } from 'enzyme'; +import GetToscaTemplate from './GetToscaTemplate'; +import toJson from "enzyme-to-json"; + +describe('Verify GetToscaTemplate', () => { + + it("renders without crashing", () => { + shallow(); + }); + + it("renders correctly", () => { + const tree = shallow(); + expect(toJson(tree)).toMatchSnapshot(); + }); + + it('should have a Button element', () => { + const container = shallow() + expect(container.find('Button').length).toEqual(1); + }); + + it('button should call getTemplateHandler when clicked', async () => { + const component = mount() + const logSpy = jest.spyOn(console, 'log'); + + component.find('[variant="primary"]').simulate('click'); + expect(logSpy).toHaveBeenCalledWith('getTemplateHandler called'); + }); + + it('should have a Button element with specified text', () => { + const container = shallow() + expect(container.find('Button').text()).toBe('Get Tosca Service Template'); + }); +}); diff --git a/gui-clamp/ui-react/src/components/dialogs/ControlLoop/ReadAndConvertYaml.js b/gui-clamp/ui-react/src/components/dialogs/ControlLoop/ReadAndConvertYaml.js new file mode 100644 index 0000000..53b541c --- /dev/null +++ b/gui-clamp/ui-react/src/components/dialogs/ControlLoop/ReadAndConvertYaml.js @@ -0,0 +1,96 @@ +/* + * ============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========================================================= + */ + +import React, { useState } from "react"; +import GetToscaTemplate from "./GetToscaTemplate"; +import Modal from "react-bootstrap/Modal"; +import Button from "react-bootstrap/Button"; +import { Alert } from "react-bootstrap"; + +import styled from 'styled-components'; + +const ModalStyled = styled(Modal)` + background-color: transparent; +` + +const AlertStyled = styled(Alert)` + margin-top: 10px; +` + +const PreStyled = styled.pre` + color: #7F0055; + overflow: auto; + max-height: 70vh; + margin-top: 10px; +` + +const ReadAndConvertYaml = (props) => { + const [show, setShow] = useState(true); + const [toscaTemplateData, setToscaTemplateData] = useState(); + const [responeOk, setResponseOk] = useState(true); + const name = 'ToscaServiceTemplateSimple'; + const version = '1.0.0'; + + const handleClose = () => { + console.log('handleClose called'); + setShow(false); + props.history.push('/'); + } + + const getToscaServiceTemplateHandler = async (toscaServiceTemplateResponse) => { + // console.log('getToscaServiceTemplateHandler called: ' + toscaServiceTemplate); + + if (!toscaServiceTemplateResponse.ok) { + setResponseOk(false); + const toscaData = await toscaServiceTemplateResponse; + setToscaTemplateData(toscaData); + } else { + setResponseOk(true); + const toscaData = await toscaServiceTemplateResponse.json(); + setToscaTemplateData(toscaData); + } + } + + return ( + + + View Tosca Template + + + + { responeOk && { JSON.stringify(toscaTemplateData, null, 2) } } + { toscaTemplateData } + + + + + + ); +}; + +export default ReadAndConvertYaml; diff --git a/gui-clamp/ui-react/src/components/dialogs/ControlLoop/ReadAndConvertYaml.test.js b/gui-clamp/ui-react/src/components/dialogs/ControlLoop/ReadAndConvertYaml.test.js new file mode 100644 index 0000000..90d7185 --- /dev/null +++ b/gui-clamp/ui-react/src/components/dialogs/ControlLoop/ReadAndConvertYaml.test.js @@ -0,0 +1,99 @@ +/* + * ============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========================================================= + */ + +import React from 'react'; +import { mount, shallow } from 'enzyme'; +import ReadAndConvertYaml from './ReadAndConvertYaml'; +import toJson from "enzyme-to-json"; +import { act } from "react-dom/test-utils"; +import { createMemoryHistory } from "history"; + +describe('Verify ReadAndConvertYaml', () => { + + beforeEach(() => { + fetch.resetMocks(); + fetch.mockImplementation(() => { + return Promise.resolve({ + ok: true, + status: 200, + json: () => { + return Promise.resolve({ + "tosca_definitions_version": "tosca_simple_yaml_1_1_0", + "data_types": {}, + "policy_types": {}, + "topology_template": {}, + "name": "ToscaServiceTemplateSimple", + "version": "1.0.0", + "metadata": {}, + "id": "0.19518677404255147" + }); + } + }); + }); + }) + + it("renders without crashing", () => { + shallow(); + }); + + it("renders correctly", () => { + const tree = shallow(); + expect(toJson(tree)).toMatchSnapshot(); + }); + + it('should have a GetToscaTemplate element', () => { + const container = shallow() + expect(container.find('GetToscaTemplate').length).toEqual(1); + }); + + it('should call getToscaServiceTemplateHandler on click', async () => { + const component = mount(); + const logSpy = jest.spyOn(console, 'log'); + + act(async () => { + component.find('GetToscaTemplate').simulate('click'); + expect(logSpy).toHaveBeenCalledWith('getToscaServiceTemplateHandler called'); + }); + }); + + it('handleClose called when bottom button clicked', () => { + const history = createMemoryHistory(); + const component = mount() + const logSpy = jest.spyOn(console, 'log'); + + + act(() => { + component.find('[variant="secondary"]').simulate('click'); + expect(logSpy).toHaveBeenCalledWith('handleClose called'); + }); + }); + + it('handleClose called when top-right button clicked', () => { + const history = createMemoryHistory(); + const component = mount() + const logSpy = jest.spyOn(console, 'log'); + + + act(() => { + component.find('[size="xl"]').get(0).props.onHide(); + expect(logSpy).toHaveBeenCalledWith('handleClose called'); + }); + }); + +}); diff --git a/gui-clamp/ui-react/src/components/dialogs/ControlLoop/UploadToscaFile.js b/gui-clamp/ui-react/src/components/dialogs/ControlLoop/UploadToscaFile.js new file mode 100644 index 0000000..6ee6a43 --- /dev/null +++ b/gui-clamp/ui-react/src/components/dialogs/ControlLoop/UploadToscaFile.js @@ -0,0 +1,54 @@ +/* + * ============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========================================================= + */ + +import Button from "react-bootstrap/Button"; +import React, { useState } from "react"; +import ControlLoopService from "../../../api/ControlLoopService"; + +const UploadToscaFile = (props) => { + const [windowLocationPathName, setWindowLocationPathname] = useState(''); + + const postServiceTemplateHandler = async (event) => { + event.preventDefault(); + console.log('postServiceTemplateHandler called'); + setWindowLocationPathname(window.location.pathname); + + const response = await ControlLoopService.uploadToscaFile(props.toscaObject, windowLocationPathName) + .catch(error => error.message); + + // const responseMessage = await response.text(); + + props.onResponseReceived(response); + + } + + return ( + + + + ); + +}; + +export default UploadToscaFile; diff --git a/gui-clamp/ui-react/src/components/dialogs/ControlLoop/UploadToscaFile.test.js b/gui-clamp/ui-react/src/components/dialogs/ControlLoop/UploadToscaFile.test.js new file mode 100644 index 0000000..94d9038 --- /dev/null +++ b/gui-clamp/ui-react/src/components/dialogs/ControlLoop/UploadToscaFile.test.js @@ -0,0 +1,61 @@ +/* + * ============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========================================================= + */ + +import React from 'react'; +import { mount, shallow } from 'enzyme'; +import UploadToscaFile from './UploadToscaFile'; +import toJson from "enzyme-to-json"; +import { act } from "react-dom/test-utils"; + +describe('Verify UploadToscaFile', () => { + + it("renders without crashing", () => { + shallow(); + }); + + it("renders correctly", () => { + const tree = shallow(); + expect(toJson(tree)).toMatchSnapshot(); + }); + + it('should have a Button element', () => { + const container = shallow() + expect(container.find('Button').length).toEqual(1); + }); + + it('button should call postServiceTemplateHandler when clicked', async () => { + const component = mount() + const logSpy = jest.spyOn(console, 'log'); + const event = { + preventDefault() { + } + }; + + act(async () => { + component.find('[variant="primary"]').get(0).props.onClick(event); + expect(logSpy).toHaveBeenCalledWith('postServiceTemplateHandler called'); + }) + + }); + + it('should have a Button element with specified text', () => { + const container = shallow() + expect(container.find('Button').text()).toBe('Upload Tosca Service Template'); + }); +}); diff --git a/gui-clamp/ui-react/src/components/dialogs/ControlLoop/__snapshots__/GetLocalToscaFileForUpload.test.js.snap b/gui-clamp/ui-react/src/components/dialogs/ControlLoop/__snapshots__/GetLocalToscaFileForUpload.test.js.snap new file mode 100644 index 0000000..84168fd --- /dev/null +++ b/gui-clamp/ui-react/src/components/dialogs/ControlLoop/__snapshots__/GetLocalToscaFileForUpload.test.js.snap @@ -0,0 +1,103 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`Verify GetLocalToscaFileForUpload renders correctly 1`] = ` + + + + Upload Tosca to Commissioning API + + +
+
+ +
+ + + + Only .yaml, .yml and .json files are supported + + + + + + + + +
+
+
+ + + +
+`; diff --git a/gui-clamp/ui-react/src/components/dialogs/ControlLoop/__snapshots__/GetToscaTemplate.test.js.snap b/gui-clamp/ui-react/src/components/dialogs/ControlLoop/__snapshots__/GetToscaTemplate.test.js.snap new file mode 100644 index 0000000..2c591d9 --- /dev/null +++ b/gui-clamp/ui-react/src/components/dialogs/ControlLoop/__snapshots__/GetToscaTemplate.test.js.snap @@ -0,0 +1,15 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`Verify GetToscaTemplate renders correctly 1`] = ` + + + +`; diff --git a/gui-clamp/ui-react/src/components/dialogs/ControlLoop/__snapshots__/ReadAndConvertYaml.test.js.snap b/gui-clamp/ui-react/src/components/dialogs/ControlLoop/__snapshots__/ReadAndConvertYaml.test.js.snap new file mode 100644 index 0000000..fab7801 --- /dev/null +++ b/gui-clamp/ui-react/src/components/dialogs/ControlLoop/__snapshots__/ReadAndConvertYaml.test.js.snap @@ -0,0 +1,46 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`Verify ReadAndConvertYaml renders correctly 1`] = ` + + + + View Tosca Template + + + + + + + + + + + + + + +`; diff --git a/gui-clamp/ui-react/src/components/dialogs/ControlLoop/__snapshots__/UploadToscaFile.test.js.snap b/gui-clamp/ui-react/src/components/dialogs/ControlLoop/__snapshots__/UploadToscaFile.test.js.snap new file mode 100644 index 0000000..a1ae439 --- /dev/null +++ b/gui-clamp/ui-react/src/components/dialogs/ControlLoop/__snapshots__/UploadToscaFile.test.js.snap @@ -0,0 +1,16 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`Verify UploadToscaFile renders correctly 1`] = ` + + + +`; diff --git a/gui-clamp/ui-react/src/components/dialogs/GetLocalToscaFileForUpload.js b/gui-clamp/ui-react/src/components/dialogs/GetLocalToscaFileForUpload.js deleted file mode 100644 index bac9953..0000000 --- a/gui-clamp/ui-react/src/components/dialogs/GetLocalToscaFileForUpload.js +++ /dev/null @@ -1,153 +0,0 @@ -/* - * - - * * ============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========================================================= - * - */ - -import React, { useState } from 'react'; -import Button from "react-bootstrap/Button"; -import Modal from 'react-bootstrap/Modal'; -import Form from 'react-bootstrap/Form'; -import Row from 'react-bootstrap/Row'; -import styled from 'styled-components'; -import Alert from 'react-bootstrap/Alert'; -import * as yaml from "js-yaml"; -import UploadToscaFile from "../../api/UploadToscaFile"; - -const ModalStyled = styled(Modal)` - background-color: transparent; -` - -const StyledMessagesDiv = styled.div` - overflow: auto; - min-width: 100%; - max-height: 300px; - padding: 5px 5px 0px 5px; - text-align: center; -` - -const GetLocalToscaFileForUpload = (props) => { - const [selectedFile, setSelectedFile] = useState(); - const [fileIsSelected, setFileIsSelected] = useState(false); - const [toscaJsonObject, setToscaJsonObject] = useState({}); - const [show, setShow] = useState(true); - const [alertMessages, setAlertMessages] = useState(); - - const handleClose = () => { - console.log('handleClose called'); - setShow(false); - props.history.push('/'); - } - - const fileChangeHandler = (event) => { - event.preventDefault(); - console.log('fileChangeHandler called'); - - if (event.currentTarget.files[0] !== undefined) { - console.log('file defined'); - setSelectedFile(event.currentTarget.files[0]); - setFileIsSelected(true); - - const file = event.currentTarget.files[0]; - - setAlertMessages([]); - - const fileReader = new FileReader(); - - fileReader.onload = () => { - const jsonObj = yaml.load(fileReader.result, 'utf8'); - setToscaJsonObject(jsonObj); - } - - fileReader.readAsText(file); - - } else { - return; - } - }; - - const receiveResponseFromUpload = (response, responseMessage) => { - - if (response.ok) { - setAlertMessages( - Upload Success -

Tosca Service Template from { selectedFile.name } was Successfully Uploaded

-
-

Type: { selectedFile.type }

Size: { +selectedFile.size / 1000 }Kb

-
); - } - else { - setAlertMessages( - Upload Failure -

Tosca Service Template from { selectedFile.name } failed to upload

-

Status code: { response.status }: { response.statusText }

-

Response from CLAMP: { responseMessage }

-
-

Type: { selectedFile.type }

Size: { +selectedFile.size / 1000 }Kb

-
); - } - }; - - return ( - - - Upload Tosca to Commissioning API - -
-
- -
- - - - Only .yaml, .yml and .json files are supported - - - - - - - { alertMessages } - - -
-
-
- - - -
- ); -} - -export default GetLocalToscaFileForUpload; diff --git a/gui-clamp/ui-react/src/components/dialogs/GetLocalToscaFileForUpload.test.js b/gui-clamp/ui-react/src/components/dialogs/GetLocalToscaFileForUpload.test.js deleted file mode 100644 index dfd3b9e..0000000 --- a/gui-clamp/ui-react/src/components/dialogs/GetLocalToscaFileForUpload.test.js +++ /dev/null @@ -1,124 +0,0 @@ -/* - * - - * * ============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========================================================= - * - */ -import React from 'react'; -import { mount, shallow } from 'enzyme'; -import toJson from 'enzyme-to-json'; -import { act } from "react-dom/test-utils"; -import GetLocalToscaFileForUpload from './GetLocalToscaFileForUpload'; -import { createMemoryHistory } from 'history'; - - -describe('Verify GetLocalToscaFileForUpload', () => { - const fs = require('fs'); - let testFile = fs.readFileSync('src/components/dialogs/Policy/toscaData.test.json'); - const file = new Blob([testFile], { type: 'file' }); - - beforeEach(() => { - fetch.resetMocks(); - fetch.mockImplementation(() => { - return Promise.resolve({ - ok: true, - status: 200, - json: () => { - return Promise.resolve({ - "tosca_definitions_version": "tosca_simple_yaml_1_1_0", - "data_types": {}, - "policy_types": {}, - "topology_template": {}, - "name": "ToscaServiceTemplateSimple", - "version": "1.0.0", - "metadata": {}, - "id": "0.19518677404255147" - }); - } - }); - }); - }) - - it("renders without crashing", () => { - shallow(); - }); - - it("renders correctly", () => { - const tree = shallow(); - expect(toJson(tree)).toMatchSnapshot(); - }); - - it('should have a UploadToscaFile element', () => { - const container = shallow() - expect(container.find('UploadToscaFile').length).toEqual(1); - }); - - it('handleClose called when bottom button clicked', () => { - const history = createMemoryHistory(); - const component = mount() - const logSpy = jest.spyOn(console, 'log'); - - - act(() => { - component.find('[variant="secondary"]').simulate('click'); - expect(logSpy).toHaveBeenCalledWith('handleClose called'); - }); - }); - - it('handleClose called when top-right button clicked', () => { - const history = createMemoryHistory(); - const component = mount() - const logSpy = jest.spyOn(console, 'log'); - - - act(() => { - component.find('[size="lg"]').get(0).props.onHide(); - expect(logSpy).toHaveBeenCalledWith('handleClose called'); - }); - }); - - it('should call fileChangeHandler on change, file undefined', () => { - const component = mount(); - const logSpy = jest.spyOn(console, 'log'); - const event = { - preventDefault() { - }, - currentTarget: { files: [] } - }; - - act(() => { - component.find('[type="file"]').get(0).props.onChange(event); - expect(logSpy).toHaveBeenCalledWith('fileChangeHandler called'); - }); - }); - - it('should call fileChangeHandler on change, file defined', async () => { - const component = mount(); - const logSpy = jest.spyOn(console, 'log'); - const event = { - preventDefault() { - }, - currentTarget: { files: [file] } - }; - - act(async () => { - component.find('[type="file"]').get(0).props.onChange(event); - expect(logSpy).toHaveBeenCalledWith('file defined'); - }); - }); -}); diff --git a/gui-clamp/ui-react/src/components/dialogs/ReadAndConvertYaml.js b/gui-clamp/ui-react/src/components/dialogs/ReadAndConvertYaml.js deleted file mode 100644 index 3a02588..0000000 --- a/gui-clamp/ui-react/src/components/dialogs/ReadAndConvertYaml.js +++ /dev/null @@ -1,87 +0,0 @@ -/*- - * ============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========================================================= - */ -import React, { useState } from "react"; -import GetToscaTemplate from "../../api/GetToscaTemplate"; -import Modal from "react-bootstrap/Modal"; -import Button from "react-bootstrap/Button"; - -import styled from 'styled-components'; - -const ModalStyled = styled(Modal)` - background-color: transparent; -` - -const ErrMsgStyled = styled.div` - color: red; -` - -const PreStyled = styled.pre` - color: #7F0055; - overflow: auto; - max-height: 70vh; -` - -const ReadAndConvertYaml = (props) => { - const [show, setShow] = useState(true); - const [toscaTemplateData, setToscaTemplateData] = useState(); - const name = 'ToscaServiceTemplateSimple'; - const version = '1.0.0'; - - const handleClose = () => { - console.log('handleClose called'); - setShow(false); - props.history.push('/'); - } - - const getToscaServiceTemplateHandler = async (toscaServiceTemplate) => { - console.log('getToscaServiceTemplateHandler called'); - const toscaData = { - ...toscaServiceTemplate, - id: Math.random().toString() - }; - // console.log(toscaData); - setToscaTemplateData(toscaData); - } - - return ( - - - View Tosca Template - - - - { JSON.stringify(toscaTemplateData, null, 2) } - - - - - - ); -} - -export default ReadAndConvertYaml; diff --git a/gui-clamp/ui-react/src/components/dialogs/ReadAndConvertYaml.test.js b/gui-clamp/ui-react/src/components/dialogs/ReadAndConvertYaml.test.js deleted file mode 100644 index daec619..0000000 --- a/gui-clamp/ui-react/src/components/dialogs/ReadAndConvertYaml.test.js +++ /dev/null @@ -1,101 +0,0 @@ -/* - * - - * * ============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========================================================= - * - */ -import React from 'react'; -import { mount, shallow } from 'enzyme'; -import ReadAndConvertYaml from './ReadAndConvertYaml'; -import toJson from "enzyme-to-json"; -import { act } from "react-dom/test-utils"; -import { createMemoryHistory } from "history"; - -describe('Verify ReadAndConvertYaml', () => { - - beforeEach(() => { - fetch.resetMocks(); - fetch.mockImplementation(() => { - return Promise.resolve({ - ok: true, - status: 200, - json: () => { - return Promise.resolve({ - "tosca_definitions_version": "tosca_simple_yaml_1_1_0", - "data_types": {}, - "policy_types": {}, - "topology_template": {}, - "name": "ToscaServiceTemplateSimple", - "version": "1.0.0", - "metadata": {}, - "id": "0.19518677404255147" - }); - } - }); - }); - }) - - it("renders without crashing", () => { - shallow(); - }); - - it("renders correctly", () => { - const tree = shallow(); - expect(toJson(tree)).toMatchSnapshot(); - }); - - it('should have a GetToscaTemplate element', () => { - const container = shallow() - expect(container.find('GetToscaTemplate').length).toEqual(1); - }); - - it('should call getToscaServiceTemplateHandler on click', async () => { - const component = mount(); - const logSpy = jest.spyOn(console, 'log'); - - act(async () => { - component.find('GetToscaTemplate').simulate('click'); - expect(logSpy).toHaveBeenCalledWith('getToscaServiceTemplateHandler called'); - }); - }); - - it('handleClose called when bottom button clicked', () => { - const history = createMemoryHistory(); - const component = mount() - const logSpy = jest.spyOn(console, 'log'); - - - act(() => { - component.find('[variant="secondary"]').simulate('click'); - expect(logSpy).toHaveBeenCalledWith('handleClose called'); - }); - }); - - it('handleClose called when top-right button clicked', () => { - const history = createMemoryHistory(); - const component = mount() - const logSpy = jest.spyOn(console, 'log'); - - - act(() => { - component.find('[size="xl"]').get(0).props.onHide(); - expect(logSpy).toHaveBeenCalledWith('handleClose called'); - }); - }); - -}); diff --git a/gui-clamp/ui-react/src/components/dialogs/__snapshots__/GetLocalToscaFileForUpload.test.js.snap b/gui-clamp/ui-react/src/components/dialogs/__snapshots__/GetLocalToscaFileForUpload.test.js.snap deleted file mode 100644 index 84168fd..0000000 --- a/gui-clamp/ui-react/src/components/dialogs/__snapshots__/GetLocalToscaFileForUpload.test.js.snap +++ /dev/null @@ -1,103 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`Verify GetLocalToscaFileForUpload renders correctly 1`] = ` - - - - Upload Tosca to Commissioning API - - -
-
- -
- - - - Only .yaml, .yml and .json files are supported - - - - - - - - -
-
-
- - - -
-`; diff --git a/gui-clamp/ui-react/src/components/dialogs/__snapshots__/ReadAndConvertYaml.test.js.snap b/gui-clamp/ui-react/src/components/dialogs/__snapshots__/ReadAndConvertYaml.test.js.snap deleted file mode 100644 index cbead03..0000000 --- a/gui-clamp/ui-react/src/components/dialogs/__snapshots__/ReadAndConvertYaml.test.js.snap +++ /dev/null @@ -1,39 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`Verify ReadAndConvertYaml renders correctly 1`] = ` - - - - View Tosca Template - - - - - - - - - - -`; -- cgit 1.2.3-korg