From ad6a4573ef18da3395eee1c9f922a26b8ac1bbcf Mon Sep 17 00:00:00 2001 From: brunomilitzer Date: Thu, 19 Aug 2021 17:20:13 +0100 Subject: ADD State Change CL Instantiation Will Apply Unit Tests on POLICY-3537, since focus is to get the functionalities completed. Issue-ID: POLICY-3425 Change-Id: I926bb1a213e71dee56c0fc5df4779a4f1b633383 Signed-off-by: brunomilitzer --- gui-clamp/ui-react-lib/libIndex.js | 2 + gui-clamp/ui-react/src/LoopUI.js | 2 + .../ui-react/src/__snapshots__/LoopUI.test.js.snap | 4 + .../src/__snapshots__/OnapClamp.test.js.snap | 4 + gui-clamp/ui-react/src/api/ControlLoopService.js | 21 +++ .../ControlLoop/InstantiationManagementModal.js | 170 +++++++++++++++++++++ .../InstantiationOrderStateChangeItem.js | 113 ++++++++++++++ .../dialogs/InstantiationManagementModal.test.js | 69 +++++++++ .../InstantiationManagementModal.test.js.snap | 114 ++++++++++++++ gui-clamp/ui-react/src/components/menu/MenuBar.js | 3 +- .../menu/__snapshots__/MenuBar.test.js.snap | 65 ++++++++ 11 files changed, 566 insertions(+), 1 deletion(-) create mode 100644 gui-clamp/ui-react/src/components/dialogs/ControlLoop/InstantiationManagementModal.js create mode 100644 gui-clamp/ui-react/src/components/dialogs/ControlLoop/InstantiationOrderStateChangeItem.js create mode 100644 gui-clamp/ui-react/src/components/dialogs/InstantiationManagementModal.test.js create mode 100644 gui-clamp/ui-react/src/components/dialogs/__snapshots__/InstantiationManagementModal.test.js.snap diff --git a/gui-clamp/ui-react-lib/libIndex.js b/gui-clamp/ui-react-lib/libIndex.js index 2af68db..779e363 100644 --- a/gui-clamp/ui-react-lib/libIndex.js +++ b/gui-clamp/ui-react-lib/libIndex.js @@ -60,6 +60,8 @@ export { default as InstantiationItem } from '../ui-react/src/components/dialogs export { default as InstantiationElements } from '../ui-react/src/components/dialogs/ControlLoop/InstantiationElements'; export { default as InstantiationElementItem } from '../ui-react/src/components/dialogs/ControlLoop/InstantiationElementItem'; export { default as InstanceModal } from '../ui-react/src/components/dialogs/ControlLoop/InstanceModal'; +export { default as InstantiationManagementModal } from '../ui-react/src/components/dialogs/ControlLoop/InstantiationManagementModal'; +export { default as InstantiationOrderStateChangeItem } from '../ui-react/src/components/dialogs/ControlLoop/InstantiationOrderStateChangeItem'; export { default as ControlLoopService } from '../ui-react/src/api/ControlLoopService'; export { default as GetLocalToscaFileForUpload } from '../ui-react/src/components/dialogs/ControlLoop/GetLocalToscaFileForUpload'; export { default as ReadAndConvertYaml } from '../ui-react/src/components/dialogs/ControlLoop/ReadAndConvertYaml'; diff --git a/gui-clamp/ui-react/src/LoopUI.js b/gui-clamp/ui-react/src/LoopUI.js index bfdd7dd..79943bf 100644 --- a/gui-clamp/ui-react/src/LoopUI.js +++ b/gui-clamp/ui-react/src/LoopUI.js @@ -56,6 +56,7 @@ import MonitorInstantiation from "./components/dialogs/ControlLoop/MonitorInstan import GetLocalToscaFileForUpload from "./components/dialogs/ControlLoop/GetLocalToscaFileForUpload"; import CommissioningModal from "./components/dialogs/ControlLoop/CommissioningModal"; import InstanceModal from "./components/dialogs/ControlLoop/InstanceModal"; +import InstantiationManagementModal from "./components/dialogs/ControlLoop/InstantiationManagementModal"; const StyledMainDiv = styled.div` background-color: ${ props => props.theme.backgroundColor }; @@ -386,6 +387,7 @@ export default class LoopUI extends React.Component { showFailAlert={ this.showFailAlert }/>) } /> () }/> + () }/> () }/> ); diff --git a/gui-clamp/ui-react/src/__snapshots__/LoopUI.test.js.snap b/gui-clamp/ui-react/src/__snapshots__/LoopUI.test.js.snap index 2123f37..abfcb8d 100644 --- a/gui-clamp/ui-react/src/__snapshots__/LoopUI.test.js.snap +++ b/gui-clamp/ui-react/src/__snapshots__/LoopUI.test.js.snap @@ -89,6 +89,10 @@ exports[`Verify LoopUI Test the render method 1`] = ` path="/monitorInstantiation" render={[Function]} /> + + { + const [show, setShow] = useState(true); + const [windowLocationPathnameGet, setWindowLocationPathnameGet] = useState(''); + const [windowLocationPathNameSave, setWindowLocationPathNameSave] = useState(''); + const [controlLoopIdentifierList, setControlLoopIdentifierList] = useState([]); + const [orderedState, setOrderedState] = useState(''); + const [toscaOrderStateObject, setToscaOrderStateObject] = useState({}); + const [instantiationOrderStateOk, setInstantiationOrderStateOk] = useState(true); + const [instantiationOrderStateError, setInstantiationOrderStateError] = useState({}); + const [alertMessage, setAlertMessage] = useState(null); + + useEffect(async () => { + setWindowLocationPathnameGet(window.location.pathname); + + const instantiationOrderState = await ControlLoopService.getInstanceOrderState(windowLocationPathnameGet) + .catch(error => error.message); + + const orderStateJson = await instantiationOrderState.json(); + + if (!instantiationOrderState.ok || orderStateJson['controlLoopIdentifierList'].length === 0) { + setInstantiationOrderStateOk(true); + setInstantiationOrderStateError(orderStateJson); + } else { + setControlLoopIdentifierList(orderStateJson['controlLoopIdentifierList']); + setOrderedState(orderStateJson['orderedState']); + } + }, []); + + const handleDropSelect = (event) => { + console.log("handleDropDownChange called"); + + const stateChangeObject = { + orderedState: event, + controlLoopIdentifierList: controlLoopIdentifierList + } + setToscaOrderStateObject(stateChangeObject); + orderStateContext.orderState = stateChangeObject; + } + + const handleSave = async () => { + console.log("handleSave called"); + setWindowLocationPathNameSave(window.location.pathname); + + const response = await ControlLoopService.changeInstanceOrderState(toscaOrderStateObject, windowLocationPathNameSave).catch(error => error.message); + + if (response.ok) { + successAlert(); + } else { + await errorAlert(response); + } + } + + const handleClose = () => { + console.log('handleClose called'); + setShow(false); + props.history.push('/'); + } + + const successAlert = () => { + console.log("successAlert called"); + setAlertMessage( + Order State Changed Success +

Order State Changed was successfully changed

+
+
); + } + + const errorAlert = async (response) => { + console.log("errorAlert called"); + setAlertMessage( + Order State Changed Failure +

An error occurred while trying to change order state

+

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

+

Status Text: { await response.text() }

+
+
); + } + + return ( + + + Manage Instantiation + +
+ + + + + Select Order State + + + UNINITIALISED + PASSIVE + RUNNING + + + { + controlLoopIdentifierList.map((clIdList, index) => ( + + )) + } + + Can't get instantiation ordered state:
{ JSON.stringify(instantiationOrderStateError, null, 2) }
+
+ + { alertMessage } + +
+ + + + +
+ ); +} + +export default InstantiationManagementModal; diff --git a/gui-clamp/ui-react/src/components/dialogs/ControlLoop/InstantiationOrderStateChangeItem.js b/gui-clamp/ui-react/src/components/dialogs/ControlLoop/InstantiationOrderStateChangeItem.js new file mode 100644 index 0000000..8545943 --- /dev/null +++ b/gui-clamp/ui-react/src/components/dialogs/ControlLoop/InstantiationOrderStateChangeItem.js @@ -0,0 +1,113 @@ +/* + * ============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 styled from "styled-components"; + +const UninitialisedBox = styled.div` + margin: 2px -15px; + padding: 8px; + outline: none; + font-size: 16px; + font-weight: normal; + background: #cccccc; + border-radius: 8px; + border: 1px solid #7f7f7f; + + &:focus, &:active, &:after { + outline: none; + border-radius: 8px; + } +` + +const PassiveBox = styled.div` + margin: 2px -15px; + padding: 8px; + outline: none; + font-size: 16px; + font-weight: normal; + background: #ffe87c; + border-radius: 8px; + border: 1px solid #7f7f7f; + + &:focus, &:active, &:after { + outline: none; + border-radius: 8px; + } +` + +const RunningBox = styled.div` + margin: 2px -15px; + padding: 8px; + outline: none; + font-size: 16px; + font-weight: normal; + background: #7ec699; + border-radius: 8px; + border: 1px solid #7f7f7f; + + &:focus, &:active, &:after { + outline: none; + border-radius: 8px; + } +` + +const InstantiationOrderStateChangeItem = (props) => { + + const renderOrderStateItem = () => { + console.log("renderOrderStateItem called"); + switch (props.orderState) { + case 'UNINITIALISED': + console.log("called UNINITIALISED"); + return renderUninitialisedOrderedState(); + case 'PASSIVE': + console.log("called PASSIVE"); + return renderPassiveOrderedState(); + case 'RUNNING': + console.log("called RUNNING"); + return renderRunningOrderedState(); + } + } + + const renderUninitialisedOrderedState = () => { + return ( + { props.title } + ) + } + + const renderPassiveOrderedState = () => { + return ( + { props.title } + ) + } + + const renderRunningOrderedState = () => { + return ( + { props.title } + ) + } + + return ( + + { renderOrderStateItem() } + + ); +} + +export default InstantiationOrderStateChangeItem; diff --git a/gui-clamp/ui-react/src/components/dialogs/InstantiationManagementModal.test.js b/gui-clamp/ui-react/src/components/dialogs/InstantiationManagementModal.test.js new file mode 100644 index 0000000..61f8c1a --- /dev/null +++ b/gui-clamp/ui-react/src/components/dialogs/InstantiationManagementModal.test.js @@ -0,0 +1,69 @@ +/* + * ============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 { mount, shallow } from "enzyme"; +import React from "react"; +import toJson from "enzyme-to-json"; +import { createMemoryHistory } from "history"; +import { act } from "react-dom/test-utils"; +import InstantiationManagementModal from "./ControlLoop/InstantiationManagementModal"; + +describe('Verify InstantiationManagementModal', () => { + + it("renders without crashing", () => { + shallow(); + }); + + it("renders correctly", () => { + const tree = shallow(); + expect(toJson(tree)).toMatchSnapshot(); + }); + + it('should have save button element', () => { + const container = shallow() + expect(container.find('[variant="primary"]').length).toEqual(1); + }); + + it('handleSave called when save button clicked', () => { + const history = createMemoryHistory(); + const component = mount() + const logSpy = jest.spyOn(console, 'log'); + + act(() => { + component.find('[variant="primary"]').simulate('click'); + expect(logSpy).toHaveBeenCalledWith('handleSave called'); + }); + }); + + it('should have close button element', () => { + const container = shallow() + expect(container.find('[variant="secondary"]').length).toEqual(1); + }); + + it('handleClose called when close 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'); + }); + }); +}); diff --git a/gui-clamp/ui-react/src/components/dialogs/__snapshots__/InstantiationManagementModal.test.js.snap b/gui-clamp/ui-react/src/components/dialogs/__snapshots__/InstantiationManagementModal.test.js.snap new file mode 100644 index 0000000..fde9ba1 --- /dev/null +++ b/gui-clamp/ui-react/src/components/dialogs/__snapshots__/InstantiationManagementModal.test.js.snap @@ -0,0 +1,114 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`Verify InstantiationManagementModal renders correctly 1`] = ` + + + + Manage Instantiation + + +
+ + + + + Select Order State + + + + UNINITIALISED + + + PASSIVE + + + RUNNING + + + + + + Can't get instantiation ordered state: +
+ {} +
+
+ +
+ + + + +
+`; diff --git a/gui-clamp/ui-react/src/components/menu/MenuBar.js b/gui-clamp/ui-react/src/components/menu/MenuBar.js index 7ee7ffd..99480a9 100644 --- a/gui-clamp/ui-react/src/components/menu/MenuBar.js +++ b/gui-clamp/ui-react/src/components/menu/MenuBar.js @@ -116,8 +116,9 @@ export default class MenuBar extends React.Component { Instantiation Monitor Instantiation + Manage Instantiation Edit Control Loop Instance Properties - + Commissioning Manage Commissioned Tosca Template Upload Tosca to Commissioning diff --git a/gui-clamp/ui-react/src/components/menu/__snapshots__/MenuBar.test.js.snap b/gui-clamp/ui-react/src/components/menu/__snapshots__/MenuBar.test.js.snap index edc0624..36132ff 100644 --- a/gui-clamp/ui-react/src/components/menu/__snapshots__/MenuBar.test.js.snap +++ b/gui-clamp/ui-react/src/components/menu/__snapshots__/MenuBar.test.js.snap @@ -1102,6 +1102,71 @@ exports[`Verify MenuBar Test the render method 1`] = ` white-space: nowrap; border: 0; + :hover { + text-decoration: none; + background-color: ", + [Function], + "; + color: ", + [Function], + "; + } +", + ], + "staticRulesId": "", + }, + "foldedComponentIds": Array [], + "render": [Function], + "shouldForwardProp": undefined, + "styledComponentId": "sc-bdnxRM", + "target": Object { + "$$typeof": Symbol(react.forward_ref), + "propTypes": Object { + "innerRef": [Function], + "onClick": [Function], + "replace": [Function], + "target": [Function], + "to": [Function], + }, + "render": [Function], + }, + "toString": [Function], + "warnTooManyClasses": [Function], + "withComponent": [Function], + } + } + disabled={false} + to="/manageInstantiation" + > + Manage Instantiation + +