From 5032095d221292ad2909f0cb9c9c99d3d5111d0c Mon Sep 17 00:00:00 2001 From: drveerendra Date: Wed, 4 Mar 2020 20:30:44 -0500 Subject: Adding manage dictionary UI feature Adding manage dictionaries.js, its test file and respective changes in loopui, menu js files Issue-ID: CLAMP-589 Change-Id: Ib0440a7a966f3736682d2964e3329e08c91578d3 Signed-off-by: drveerendra --- .../ManageDictionaries/ManageDictionaries.js | 559 +++++++++++++++++++++ .../ManageDictionaries/ManageDictionaries.test.js | 202 ++++++++ .../__snapshots__/ManageDictionaries.test.js.snap | 195 +++++++ 3 files changed, 956 insertions(+) create mode 100644 ui-react/src/components/dialogs/ManageDictionaries/ManageDictionaries.js create mode 100644 ui-react/src/components/dialogs/ManageDictionaries/ManageDictionaries.test.js create mode 100644 ui-react/src/components/dialogs/ManageDictionaries/__snapshots__/ManageDictionaries.test.js.snap (limited to 'ui-react/src/components/dialogs/ManageDictionaries') diff --git a/ui-react/src/components/dialogs/ManageDictionaries/ManageDictionaries.js b/ui-react/src/components/dialogs/ManageDictionaries/ManageDictionaries.js new file mode 100644 index 000000000..189523765 --- /dev/null +++ b/ui-react/src/components/dialogs/ManageDictionaries/ManageDictionaries.js @@ -0,0 +1,559 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP CLAMP + * ================================================================================ + * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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. + * ============LICENSE_END============================================ + * =================================================================== + * + */ + + +import React from 'react'; +import Button from 'react-bootstrap/Button'; +import Modal from 'react-bootstrap/Modal'; +import styled from 'styled-components'; +import TemplateMenuService from '../../../api/TemplateService'; +import MaterialTable, {MTableToolbar} from "material-table"; +import IconButton from '@material-ui/core/IconButton'; +import Tooltip from '@material-ui/core/Tooltip'; +import Grid from '@material-ui/core/Grid'; +import { forwardRef } from 'react'; +import AddBox from '@material-ui/icons/AddBox'; +import ArrowUpward from '@material-ui/icons/ArrowUpward'; +import Check from '@material-ui/icons/Check'; +import ChevronLeft from '@material-ui/icons/ChevronLeft'; +import VerticalAlignTopIcon from '@material-ui/icons/VerticalAlignTop'; +import VerticalAlignBottomIcon from '@material-ui/icons/VerticalAlignBottom'; +import ChevronRight from '@material-ui/icons/ChevronRight'; +import Clear from '@material-ui/icons/Clear'; +import DeleteOutline from '@material-ui/icons/DeleteOutline'; +import Edit from '@material-ui/icons/Edit'; +import FilterList from '@material-ui/icons/FilterList'; +import FirstPage from '@material-ui/icons/FirstPage'; +import LastPage from '@material-ui/icons/LastPage'; +import Remove from '@material-ui/icons/Remove'; +import Search from '@material-ui/icons/Search'; +import ViewColumn from '@material-ui/icons/ViewColumn'; + + +const ModalStyled = styled(Modal)` + background-color: transparent; +` +const cellStyle = { border: '1px solid black' }; +const headerStyle = { backgroundColor: '#ddd', border: '2px solid black' }; +const rowHeaderStyle = {backgroundColor:'#ddd', fontSize: '15pt', text: 'bold', border: '1px solid black'}; +var dictList = []; + +function SelectSubDictType(props) { + const {onChange} = props; + const selectedValues = (e) => { + var options = e.target.options; + var SelectedDictTypes = ''; + for (var dictType = 0, values = options.length; dictType < values; dictType++) { + if (options[dictType].selected) { + SelectedDictTypes = SelectedDictTypes.concat(options[dictType].value); + SelectedDictTypes = SelectedDictTypes.concat('|'); + } + } + SelectedDictTypes = SelectedDictTypes.slice(0,-1); + onChange(SelectedDictTypes); + } + return( +
+ +
+ ) +} + +function SubDict(props) { + const {onChange} = props; + const subDicts = []; + subDicts.push('Default'); + for(var item in dictList) { + if(dictList[item].secondLevelDictionary === 1) { + subDicts.push(dictList[item].name); + } + }; + subDicts.push(''); + var optionItems = subDicts.map( + (item) => + ); + function selectedValue (e) { + onChange(e.target.value); + } + return( + + ) +} + +export default class ManageDictionaries extends React.Component { + constructor(props, context) { + super(props, context); + this.handleClose = this.handleClose.bind(this); + this.getDictionary = this.getDictionary.bind(this); + this.getDictionaryElements = this.getDictionaryElements.bind(this); + this.clickHandler = this.clickHandler.bind(this); + this.addDictionary = this.addDictionary.bind(this); + this.deleteDictionary = this.deleteDictionary.bind(this); + this.fileSelectedHandler = this.fileSelectedHandler.bind(this); + this.state = { + show: true, + selectedFile: '', + dictNameFlag: false, + exportFilename: '', + content: null, + newDict: '', + newDictItem: '', + delDictItem: '', + addDict: false, + delData: '', + delDict: false, + validImport: false, + dictionaryNames: [], + dictionaryElements: [], + tableIcons: { + Add: forwardRef((props, ref) => ), + Check: forwardRef((props, ref) => ), + Clear: forwardRef((props, ref) => ), + Delete: forwardRef((props, ref) => ), + DetailPanel: forwardRef((props, ref) => ), + Edit: forwardRef((props, ref) => ), + Export: forwardRef((props, ref) => ), + Filter: forwardRef((props, ref) => ), + FirstPage: forwardRef((props, ref) => ), + LastPage: forwardRef((props, ref) => ), + NextPage: forwardRef((props, ref) => ), + PreviousPage: forwardRef((props, ref) => ), + ResetSearch: forwardRef((props, ref) => ), + Search: forwardRef((props, ref) => ), + SortArrow: forwardRef((props, ref) => ), + ThirdStateCheck: forwardRef((props, ref) => ), + ViewColumn: forwardRef((props, ref) => ) + }, + dictColumns: [ + { + title: "Dictionary Name", field: "name",editable: 'onAdd', + cellStyle: cellStyle, + headerStyle: headerStyle + }, + { + title: "Sub Dictionary ?", field: "secondLevelDictionary", lookup: {0: 'No', 1: 'Yes'}, + cellStyle: cellStyle, + headerStyle: headerStyle + }, + { + title: "Dictionary Type", field: "subDictionaryType",lookup: {string: 'string', number: 'number'}, + cellStyle: cellStyle, + headerStyle: headerStyle + }, + { + title: "Updated By", field: "updatedBy", editable: 'never', + cellStyle: cellStyle, + headerStyle: headerStyle + }, + { + title: "Last Updated Date", field: "updatedDate", editable: 'never', + cellStyle: cellStyle, + headerStyle: headerStyle + } + ], + dictElementColumns: [ + { + title: "Element Short Name", field: "shortName",editable: 'onAdd', + cellStyle: cellStyle, + headerStyle: headerStyle + }, + { + title: "Element Name", field: "name", + cellStyle: cellStyle, + headerStyle: headerStyle + }, + { + title: "Element Description", field: "description", + cellStyle: cellStyle, + headerStyle: headerStyle + }, + { + title: "Element Type", field: "type", + editComponent: props => ( +
+ +
+ ), + cellStyle: cellStyle, + headerStyle: headerStyle + }, + { + title: "Sub-Dictionary", field: "subDictionary", + editComponent: props => ( +
+ +
+ ), + cellStyle: cellStyle, + headerStyle: headerStyle + }, + { + title: "Updated By", field: "updatedBy", editable: 'never', + cellStyle: cellStyle, + headerStyle: headerStyle + }, + { + title: "Updated Date", field: "updatedDate", editable: 'never', + cellStyle: cellStyle, + headerStyle: headerStyle + } + ] + } + } + + componentWillMount() { + this.getDictionary(); + } + + getDictionary() { + TemplateMenuService.getDictionary().then(dictionaryNames => { + this.setState({ dictionaryNames: dictionaryNames }) + }); + var dictNamesingetDict = this.state.dictionaryNames; + } + + getDictionaryElements(dictionaryName) { + TemplateMenuService.getDictionaryElements(dictionaryName).then(dictionaryElements => { + dictList = this.state.dictionaryNames; + this.setState({ dictionaryElements: dictionaryElements.dictionaryElements}); + }); + } + + clickHandler(rowData) { + this.setState({ + dictNameFlag: false, + addDict: false, + }); + } + + handleClose() { + this.setState({ show: false }); + this.props.history.push('/'); + } + + addDictionary() { + var modifiedData = []; + if(this.state.newDict !== '') { + modifiedData = this.state.newDict; + } else { + modifiedData = {"name": this.state.dictionaryName, 'dictionaryElements': this.state.newDictItem}; + } + if(this.state.newDictItem === '') { + TemplateMenuService.insDictionary(modifiedData).then(resp => { + }); + } else { + TemplateMenuService.insDictionaryElements(modifiedData).then(resp => { + }); + } + } + + deleteDictionary() { + var modifiedData = []; + if(this.state.delData !== '') { + modifiedData = this.state.delData.name; + } else { + modifiedData = {"name": this.state.dictionaryName, "shortName": this.state.delDictItem.shortName}; + } + if(this.state.delDictItem === '') { + TemplateMenuService.deleteDictionary(modifiedData).then(resp => { + }); + } else { + TemplateMenuService.deleteDictionaryElements(modifiedData).then(resp => { + }); + } + } + + fileSelectedHandler = (event) => { + const text = this; + var dictionaryElements = []; + if (event.target.files[0].type === 'text/csv' ) { + if (event.target.files && event.target.files[0]) { + let reader = new FileReader(); + reader.onload = function(e) { + var dictElems = reader.result.split('\n'); + var jsonObj = []; + var headers = dictElems[0].split(','); + for(var i = 0; i < dictElems.length; i++) { + var data = dictElems[i].split(','); + var obj = {}; + for(var j = 0; j < data.length; j++) { + obj[headers[j].trim()] = data[j].trim(); + } + jsonObj.push(obj); + } + JSON.stringify(jsonObj); + const dictKeys = ['Element Short Name','Element Name','Element Description','Element Type','Sub-Dictionary']; + const mandatoryKeys = [ 'Element Short Name', 'Element Name', 'Element Type' ]; + const validTypes = ['string','number','datetime','json','map']; + if (!dictElems){ + + text.setState({validData: false}); + } else if (headers.length !== dictKeys.length){ + text.setState({validImport: false}); + } else { + var subDictionaries = []; + for(var item in dictList) { + if(dictList[item].secondLevelDictionary === 1) { + subDictionaries.push(dictList[item].name); + } + }; + subDictionaries = subDictionaries.toString(); + var row = 0; + for (var dictElem of jsonObj){ + ++row; + for (var itemKey in dictElem){ + var value = dictElem[itemKey].trim(); + if (dictKeys.indexOf(itemKey) < 0){ + var errorMessage = 'unknown field name of, ' + itemKey + ', found in CSV header'; + text.setState({validImport: false}); + alert(errorMessage); + break; + } else if (value === "" && mandatoryKeys.indexOf(itemKey) >= 0){ + errorMessage = 'value for ' + itemKey + ', at row #, ' + row + ', is empty but required'; + text.setState({validImport: false}); + alert(errorMessage); + break; + } else if (itemKey === 'Element Type' && validTypes.indexOf(value) < 0 && row > 1) { + errorMessage = 'invalid dictElemenType of ' + value + ' at row #' + row; + text.setState({validImport: false}); + alert(errorMessage); + break; + } else if (value !== "" && itemKey === 'Sub-Dictionary' && subDictionaries.indexOf(value) < 0 && row > 1) { + errorMessage = 'invalid subDictionary of ' + value + ' at row #' + row; + text.setState({validImport: false}); + alert(errorMessage); + } + } + } + } + const headerKeys = ['shortName','name','description','type','subDictionary']; + + for(i = 1; i < dictElems.length; i++) { + data = dictElems[i].split(','); + obj = {}; + for(j = 0; j < data.length; j++) { + obj[headerKeys[j].trim()] = data[j].trim(); + } + dictionaryElements.push(obj); + } + text.setState({newDictItem: dictionaryElements, addDict: true}); + } + reader.readAsText(event.target.files[0]); + } + this.setState({selectedFile: event.target.files[0]}) + } else { + text.setState({validImport: false}); + alert('Please upload .csv extention files only.'); + } + + } + + render() { + return ( + + + Manage Dictionaries + + + {!this.state.dictNameFlag? {this.getDictionaryElements(rowData.name);this.setState({dictNameFlag: true, exportFilename: rowData.name, dictionaryName: rowData.name})}} + options={{ + headerStyle: rowHeaderStyle, + }} + editable={{ + onRowAdd: newData => + new Promise((resolve, reject) => { + setTimeout(() => { + { + const dictionaryNames = this.state.dictionaryNames; + var validData = true; + if(/[^a-zA-Z0-9-_.]/.test(newData.name)) { + validData = false; + alert('Please enter alphanumberic input. Only allowed special characters are:(period, hyphen, underscore)'); + } + for (var i = 0; i < this.state.dictionaryNames.length; i++) { + if (this.state.dictionaryNames[i].name === newData.name) { + validData = false; + alert(newData.name + ' dictionary name already exists') + } + } + if(validData){ + dictionaryNames.push(newData); + this.setState({ dictionaryNames }, () => resolve()); + this.setState({addDict: true, newDict: newData}); + } + } + resolve(); + }, 1000); + }), + onRowUpdate: (newData, oldData) => + new Promise((resolve, reject) => { + setTimeout(() => { + { + const dictionaryNames = this.state.dictionaryNames; + var validData = true; + if(/[^a-zA-Z0-9-_.]/.test(newData.name)) { + validData = false; + alert('Please enter alphanumberic input. Only allowed special characters are:(period, hyphen, underscore)'); + } + if(validData){ + const index = dictionaryNames.indexOf(oldData); + dictionaryNames[index] = newData; + this.setState({ dictionaryNames }, () => resolve()); + this.setState({addDict: true, newDict: newData}); + } + } + resolve(); + }, 1000); + }), + onRowDelete: oldData => + new Promise((resolve, reject) => { + setTimeout(() => { + { + let data = this.state.dictionaryNames; + const index = data.indexOf(oldData); + data.splice(index, 1); + this.setState({ data }, () => resolve()); + this.setState({delDict: true, delData: oldData}) + } + resolve() + }, 1000) + }) + }} + />:"" + } + {this.state.dictNameFlag? ( +
+ +
+ + + this.fileUpload.click()}> + + + + +
+ {this.fileUpload = fileUpload;}} style={{ visibility: 'hidden'}} onChange={this.fileSelectedHandler} /> +
+ ) + }} + editable={{ + onRowAdd: newData => + new Promise((resolve, reject) => { + setTimeout(() => { + { + const dictionaryElements = this.state.dictionaryElements; + var validData = true; + for (var i = 0; i < this.state.dictionaryElements.length; i++) { + if (this.state.dictionaryElements[i].shortName === newData.shortName) { + validData = false; + alert(newData.shortname + 'short name already exists') + } + } + if(/[^a-zA-Z0-9-_.]/.test(newData.shortName)) { + validData = false; + alert('Please enter alphanumberic input. Only allowed special characters are:(period, hyphen, underscore)'); + } + if(!newData.type){ + validData = false; + alert('Element Type cannot be null'); + } + if(validData){ + dictionaryElements.push(newData); + this.setState({ dictionaryElements }, () => resolve()); + this.setState({addDict: true, newDictItem: [newData]}); + } + } + resolve(); + }, 1000); + }), + onRowUpdate: (newData, oldData) => + new Promise((resolve, reject) => { + setTimeout(() => { + { + const dictionaryElements = this.state.dictionaryElements; + var validData = true; + if(!newData.type){ + validData = false; + alert('Element Type cannot be null'); + } + if(validData){ + const index = dictionaryElements.indexOf(oldData); + dictionaryElements[index] = newData; + this.setState({ dictionaryElements }, () => resolve()); + this.setState({addDict: true, newDictItem: [newData]}); + } + } + resolve(); + }, 1000); + }), + onRowDelete: oldData => + new Promise((resolve, reject) => { + setTimeout(() => { + { + let data = this.state.dictionaryElements; + const index = data.indexOf(oldData); + data.splice(index, 1); + this.setState({ data }, () => resolve()); + this.setState({delDict: true, delDictItem: oldData}) + } + resolve() + }, 1000) + }) + }} + />:"" + } + {this.state.dictNameFlag?:""} + {this.state.addDict && this.addDictionary()} + {this.state.delDict && this.deleteDictionary()} +
+ + + +
+ ); + } +} diff --git a/ui-react/src/components/dialogs/ManageDictionaries/ManageDictionaries.test.js b/ui-react/src/components/dialogs/ManageDictionaries/ManageDictionaries.test.js new file mode 100644 index 000000000..4363da923 --- /dev/null +++ b/ui-react/src/components/dialogs/ManageDictionaries/ManageDictionaries.test.js @@ -0,0 +1,202 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP CLAMP + * ================================================================================ + * Copyright (C) 2019 AT&T Intellectual Property. All rights + * reserved. + * ================================================================================ + * 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. + * ============LICENSE_END============================================ + * =================================================================== + * + */ +import React from 'react'; +import { shallow } from 'enzyme'; +import { mount } from 'enzyme'; +import { render } from 'enzyme'; +import ManageDictionaries from './ManageDictionaries'; +import TemplateMenuService from '../../../api/TemplateService' + +describe('Verify ManageDictionaries', () => { + beforeEach(() => { + fetch.resetMocks(); + }); + + it('Test API Successful', () => { + fetch.mockImplementationOnce(() => { + return Promise.resolve({ + ok: true, + status: 200, + json: () => { + return Promise.resolve({ + "name": "vtest", + "secondLevelDictionary": "1", + "subDictionaryType": "string", + "updatedBy": "test", + "updatedDate": "05-07-2019 19:09:42" + }); + } + }); + }); + const component = shallow(); + expect(component).toMatchSnapshot(); + }); + + it('Test API Exception', () => { + fetch.mockImplementationOnce(() => { + return Promise.resolve({ + ok: false, + status: 500, + json: () => { + return Promise.resolve({ + "name": "vtest", + "secondLevelDictionary": "1", + "subDictionaryType": "string", + "updatedBy": "test", + "updatedDate": "05-07-2019 19:09:42" + }); + } + }); + }); + const component = shallow(); + }); + + it('Test API Rejection', () => { + const myMockFunc = fetch.mockImplementationOnce(() => Promise.reject('error')); + setTimeout( () => myMockFunc().catch(e => { + console.log(e); + }), + 100 + ); + new Promise(resolve => setTimeout(resolve, 200)); + const component = shallow(); + expect(myMockFunc.mock.calls.length).toBe(1); + }); + + it('Test Table icons', () => { + fetch.mockImplementationOnce(() => { + return Promise.resolve({ + ok: true, + status: 200, + json: () => { + return Promise.resolve({ + "name": "vtest", + "secondLevelDictionary": "1", + "subDictionaryType": "string", + "updatedBy": "test", + "updatedDate": "05-07-2019 19:09:42" + }); + } + }); + }); + const component = mount(); + expect(component.find('[className="MuiSelect-icon MuiTablePagination-selectIcon"]')).toBeTruthy(); + }); + + test('Test get dictionaryNames/dictionaryElements, add/delete dictionary functions', async () => { + const historyMock = { push: jest.fn() }; + TemplateMenuService.getDictionary = jest.fn().mockImplementation(() => { + return Promise.resolve("test"); + }); + TemplateMenuService.getDictionaryElements = jest.fn().mockImplementation(() => { + return Promise.resolve({dictionaryElements:"testitem"}); + }); + TemplateMenuService.insDictionary = jest.fn().mockImplementation(() => { + return Promise.resolve(200); + }); + TemplateMenuService.deleteDictionary = jest.fn().mockImplementation(() => { + return Promise.resolve(200); + }); + const flushPromises = () => new Promise(setImmediate); + const component = shallow() + component.setState({ newDict: { + "name": "test", + "secondLevelDictionary": "0", + "subDictionaryType": "string" + } + }); + component.setState({ delData: { + "name": "test", + "secondLevelDictionary": "0", + "subDictionaryType": "string" + } + }); + const instance = component.instance(); + instance.getDictionaryElements("test"); + instance.clickHandler(); + instance.addDictionary(); + instance.deleteDictionary(); + await flushPromises(); + expect(component.state('dictionaryNames')).toEqual("test"); + expect(component.state('dictionaryElements')).toEqual("testitem"); + expect(component.state('dictNameFlag')).toEqual(false); + }); + + test('Test adding and deleting dictionaryelements', async () => { + const historyMock = { push: jest.fn() }; + TemplateMenuService.getDictionary = jest.fn().mockImplementation(() => { + return Promise.resolve("test"); + }); + TemplateMenuService.insDictionaryElements = jest.fn().mockImplementation(() => { + return Promise.resolve(200); + }); + TemplateMenuService.deleteDictionaryElements = jest.fn().mockImplementation(() => { + return Promise.resolve(200); + }); + const flushPromises = () => new Promise(setImmediate); + const component = shallow() + component.setState({ newDictItem: { + "name": "test", + "dictionaryElements" : { + "shortName": "shorttest", + } + }}); + component.setState({ delDictItem: { + "name": "test", + "dictionaryElements" : { + "shortName": "shortTest", + } + }}); + const instance = component.instance(); + instance.addDictionary(); + instance.deleteDictionary(); + await flushPromises(); + expect(component.state('dictionaryNames')).toEqual("test"); + }); + + it('Test handleClose', () => { + fetch.mockImplementationOnce(() => { + return Promise.resolve({ + ok: true, + status: 200, + json: () => { + return Promise.resolve({ + "name": "vtest", + "secondLevelDictionary": "1", + "subDictionaryType": "string", + "updatedBy": "test", + "updatedDate": "05-07-2019 19:09:42" + }); + } + }); + }); + const historyMock = { push: jest.fn() }; + const handleClose = jest.spyOn(ManageDictionaries.prototype,'handleClose'); + const component = shallow() + component.find('[variant="secondary"]').prop('onClick')(); + expect(handleClose).toHaveBeenCalledTimes(1); + expect(component.state('show')).toEqual(false); + expect(historyMock.push.mock.calls[0]).toEqual([ '/']); + handleClose.mockClear(); + }); +}); diff --git a/ui-react/src/components/dialogs/ManageDictionaries/__snapshots__/ManageDictionaries.test.js.snap b/ui-react/src/components/dialogs/ManageDictionaries/__snapshots__/ManageDictionaries.test.js.snap new file mode 100644 index 000000000..e78292216 --- /dev/null +++ b/ui-react/src/components/dialogs/ManageDictionaries/__snapshots__/ManageDictionaries.test.js.snap @@ -0,0 +1,195 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`Verify ManageDictionaries Test API Successful 1`] = ` + + + + Manage Dictionaries + + + + + + + + + +`; -- cgit 1.2.3-korg From 82775724cf35060294388f84d2e7d2b0671ee838 Mon Sep 17 00:00:00 2001 From: sebdet Date: Mon, 16 Mar 2020 07:07:18 -0700 Subject: Fix the legacy Guard UI Fix the legacy policies UI because guard content can't be set in PolicyModal component. It was due to Jsoneditor bugged parameter Issue-ID: CLAMP-780 Signed-off-by: sebdet Change-Id: I8070d8c57e48c9a386e19475f1d377f91104aef1 Signed-off-by: sebdet --- .../ManageDictionaries/ManageDictionaries.js | 1 - .../src/components/dialogs/Policy/PolicyModal.js | 22 ++++++++++++++++++---- 2 files changed, 18 insertions(+), 5 deletions(-) (limited to 'ui-react/src/components/dialogs/ManageDictionaries') diff --git a/ui-react/src/components/dialogs/ManageDictionaries/ManageDictionaries.js b/ui-react/src/components/dialogs/ManageDictionaries/ManageDictionaries.js index 189523765..d8065ede6 100644 --- a/ui-react/src/components/dialogs/ManageDictionaries/ManageDictionaries.js +++ b/ui-react/src/components/dialogs/ManageDictionaries/ManageDictionaries.js @@ -236,7 +236,6 @@ export default class ManageDictionaries extends React.Component { TemplateMenuService.getDictionary().then(dictionaryNames => { this.setState({ dictionaryNames: dictionaryNames }) }); - var dictNamesingetDict = this.state.dictionaryNames; } getDictionaryElements(dictionaryName) { diff --git a/ui-react/src/components/dialogs/Policy/PolicyModal.js b/ui-react/src/components/dialogs/Policy/PolicyModal.js index 7151725c3..0f41beb09 100644 --- a/ui-react/src/components/dialogs/Policy/PolicyModal.js +++ b/ui-react/src/components/dialogs/Policy/PolicyModal.js @@ -102,10 +102,25 @@ export default class PolicyModal extends React.Component { } createJsonEditor(toscaModel, editorData) { + JSONEditor.defaults.themes.myBootstrap4 = JSONEditor.defaults.themes.bootstrap4.extend({ + getTab: function(text,tabId) { + var liel = document.createElement('li'); + liel.classList.add('nav-item'); + var ael = document.createElement("a"); + ael.classList.add("nav-link"); + ael.setAttribute("style",'padding:10px;max-width:160px;'); + ael.setAttribute("href", "#" + tabId); + ael.setAttribute('data-toggle', 'tab'); + text.setAttribute("style",'word-wrap:break-word;'); + ael.appendChild(text); + liel.appendChild(ael); + return liel; + } + }); return new JSONEditor(document.getElementById("editor"), { schema: toscaModel, startval: editorData, - theme: 'bootstrap4', + theme: 'myBootstrap4', object_layout: 'grid', disable_properties: true, disable_edit_json: false, @@ -117,10 +132,9 @@ export default class PolicyModal extends React.Component { collapsed:true, show_errors: 'always', display_required_only: false, - show_opt_in: true, + show_opt_in: false, prompt_before_delete: true, - required_by_default: false, - array_controls_top: true + required_by_default: false }) } -- cgit 1.2.3-korg From 9e82c5a3d192c9557c7549becf172e1f37e2dec9 Mon Sep 17 00:00:00 2001 From: JulienBe Date: Wed, 1 Apr 2020 17:40:42 +0200 Subject: Remove unused objects reported by Sonar Issue-ID: CLAMP-806 Signed-off-by: JulienBe Change-Id: Ia3d6939e1f946280551583a777032efe63e12e01 --- .../src/components/dialogs/ManageDictionaries/ManageDictionaries.test.js | 1 - ui-react/src/components/dialogs/Tosca/ViewLoopTemplatesModal.test.js | 1 - ui-react/src/components/dialogs/Tosca/ViewToscaPolicyModal.test.js | 1 - 3 files changed, 3 deletions(-) (limited to 'ui-react/src/components/dialogs/ManageDictionaries') diff --git a/ui-react/src/components/dialogs/ManageDictionaries/ManageDictionaries.test.js b/ui-react/src/components/dialogs/ManageDictionaries/ManageDictionaries.test.js index 4363da923..3a45153e9 100644 --- a/ui-react/src/components/dialogs/ManageDictionaries/ManageDictionaries.test.js +++ b/ui-react/src/components/dialogs/ManageDictionaries/ManageDictionaries.test.js @@ -78,7 +78,6 @@ describe('Verify ManageDictionaries', () => { }), 100 ); - new Promise(resolve => setTimeout(resolve, 200)); const component = shallow(); expect(myMockFunc.mock.calls.length).toBe(1); }); diff --git a/ui-react/src/components/dialogs/Tosca/ViewLoopTemplatesModal.test.js b/ui-react/src/components/dialogs/Tosca/ViewLoopTemplatesModal.test.js index ddfb2a70c..e11d86f53 100644 --- a/ui-react/src/components/dialogs/Tosca/ViewLoopTemplatesModal.test.js +++ b/ui-react/src/components/dialogs/Tosca/ViewLoopTemplatesModal.test.js @@ -77,7 +77,6 @@ describe('Verify ViewLoopTemplatesModal', () => { }), 100 ); - new Promise(resolve => setTimeout(resolve, 200)); const component = shallow(); expect(myMockFunc.mock.calls.length).toBe(1); }); diff --git a/ui-react/src/components/dialogs/Tosca/ViewToscaPolicyModal.test.js b/ui-react/src/components/dialogs/Tosca/ViewToscaPolicyModal.test.js index 952e88867..29127ed55 100644 --- a/ui-react/src/components/dialogs/Tosca/ViewToscaPolicyModal.test.js +++ b/ui-react/src/components/dialogs/Tosca/ViewToscaPolicyModal.test.js @@ -82,7 +82,6 @@ describe('Verify ViewToscaPolicyModal', () => { }), 100 ); - new Promise(resolve => setTimeout(resolve, 200)); const component = shallow(); expect(myMockFunc.mock.calls.length).toBe(1); }); -- cgit 1.2.3-korg From 0cde2b5f4b7d769dec74667d963138e858588ef3 Mon Sep 17 00:00:00 2001 From: JulienBe Date: Fri, 3 Apr 2020 13:21:13 +0200 Subject: Move to console.info in tests to please Sonar Issue-ID: CLAMP-806 Signed-off-by: JulienBe Change-Id: Ia09874acfa7237f0a920f0327e6e2d76eb61fb2a --- .../components/dialogs/ManageDictionaries/ManageDictionaries.test.js | 2 +- ui-react/src/components/dialogs/Tosca/ViewLoopTemplatesModal.test.js | 4 ++-- ui-react/src/components/dialogs/Tosca/ViewToscaPolicyModal.test.js | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) (limited to 'ui-react/src/components/dialogs/ManageDictionaries') diff --git a/ui-react/src/components/dialogs/ManageDictionaries/ManageDictionaries.test.js b/ui-react/src/components/dialogs/ManageDictionaries/ManageDictionaries.test.js index 3a45153e9..13a6035a3 100644 --- a/ui-react/src/components/dialogs/ManageDictionaries/ManageDictionaries.test.js +++ b/ui-react/src/components/dialogs/ManageDictionaries/ManageDictionaries.test.js @@ -74,7 +74,7 @@ describe('Verify ManageDictionaries', () => { it('Test API Rejection', () => { const myMockFunc = fetch.mockImplementationOnce(() => Promise.reject('error')); setTimeout( () => myMockFunc().catch(e => { - console.log(e); + console.info(e); }), 100 ); diff --git a/ui-react/src/components/dialogs/Tosca/ViewLoopTemplatesModal.test.js b/ui-react/src/components/dialogs/Tosca/ViewLoopTemplatesModal.test.js index e11d86f53..1a6cc1959 100644 --- a/ui-react/src/components/dialogs/Tosca/ViewLoopTemplatesModal.test.js +++ b/ui-react/src/components/dialogs/Tosca/ViewLoopTemplatesModal.test.js @@ -73,7 +73,7 @@ describe('Verify ViewLoopTemplatesModal', () => { it('Test API Rejection', () => { const myMockFunc = fetch.mockImplementationOnce(() => Promise.reject('error')); setTimeout( () => myMockFunc().catch(e => { - console.log(e); + console.info(e); }), 100 ); @@ -158,4 +158,4 @@ describe('Verify ViewLoopTemplatesModal', () => { expect(historyMock.push.mock.calls[0]).toEqual([ '/']); handleClose.mockClear(); }); - }); \ No newline at end of file + }); diff --git a/ui-react/src/components/dialogs/Tosca/ViewToscaPolicyModal.test.js b/ui-react/src/components/dialogs/Tosca/ViewToscaPolicyModal.test.js index 29127ed55..a599cec04 100644 --- a/ui-react/src/components/dialogs/Tosca/ViewToscaPolicyModal.test.js +++ b/ui-react/src/components/dialogs/Tosca/ViewToscaPolicyModal.test.js @@ -78,7 +78,7 @@ describe('Verify ViewToscaPolicyModal', () => { setTimeout( () => myMockFunc().catch(e => { - console.log(e); + console.info(e); }), 100 ); @@ -192,4 +192,4 @@ describe('Verify ViewToscaPolicyModal', () => { expect(historyMock.push.mock.calls[0]).toEqual([ '/']); handleClose.mockClear(); }); -}); \ No newline at end of file +}); -- cgit 1.2.3-korg From edaf4f9e6d0f8e1b95115cc39492c84d730ba1a2 Mon Sep 17 00:00:00 2001 From: sebdet Date: Thu, 16 Apr 2020 00:43:48 +0200 Subject: Add template name to UI Add template name to the UI loop viewer + change color of logs panel Issue-ID: CLAMP-826 Signed-off-by: sebdet Change-Id: I6776729cefc0597067ae8ef722f7d6e488a12cad --- ui-react/src/LoopUI.js | 2 +- ui-react/src/__snapshots__/LoopUI.test.js.snap | 2 ++ ui-react/src/__snapshots__/OnapClamp.test.js.snap | 4 ++++ ui-react/src/api/LoopCache.js | 7 +++++++ ui-react/src/components/dialogs/Loop/CreateLoopModal.js | 2 +- ui-react/src/components/dialogs/Loop/DeployLoopModal.js | 2 +- ui-react/src/components/dialogs/Loop/LoopPropertiesModal.js | 2 +- ui-react/src/components/dialogs/Loop/ModifyLoopModal.js | 2 +- ui-react/src/components/dialogs/Loop/OpenLoopModal.js | 2 +- .../dialogs/Loop/__snapshots__/DeployLoopModal.test.js.snap | 1 + .../dialogs/Loop/__snapshots__/LoopPropertiesModal.test.js.snap | 1 + .../dialogs/Loop/__snapshots__/OpenLoopModal.test.js.snap | 1 + .../components/dialogs/ManageDictionaries/ManageDictionaries.js | 2 +- .../__snapshots__/ManageDictionaries.test.js.snap | 1 + ui-react/src/components/dialogs/Policy/PolicyModal.js | 2 +- ui-react/src/components/dialogs/Tosca/UploadToscaPolicyModal.js | 2 +- ui-react/src/components/dialogs/Tosca/ViewLoopTemplatesModal.js | 2 +- ui-react/src/components/dialogs/Tosca/ViewToscaPolicyModal.js | 2 +- .../Tosca/__snapshots__/ViewLoopTemplatesModal.test.js.snap | 1 + .../dialogs/Tosca/__snapshots__/ViewToscaPolicyModal.test.js.snap | 1 + ui-react/src/components/loop_viewer/logs/LoopLogs.js | 4 ++-- ui-react/src/theme/globalStyle.js | 3 +++ 22 files changed, 35 insertions(+), 13 deletions(-) (limited to 'ui-react/src/components/dialogs/ManageDictionaries') diff --git a/ui-react/src/LoopUI.js b/ui-react/src/LoopUI.js index 5e2da79c6..efd02b41f 100644 --- a/ui-react/src/LoopUI.js +++ b/ui-react/src/LoopUI.js @@ -195,7 +195,7 @@ export default class LoopUI extends React.Component { renderLoopViewHeader() { return ( - Loop Viewer - {this.state.loopName} + Loop Viewer - {this.state.loopName} - ({this.state.loopCache.getTemplateName()}) ); } diff --git a/ui-react/src/__snapshots__/LoopUI.test.js.snap b/ui-react/src/__snapshots__/LoopUI.test.js.snap index e523de948..ff08f7afb 100644 --- a/ui-react/src/__snapshots__/LoopUI.test.js.snap +++ b/ui-react/src/__snapshots__/LoopUI.test.js.snap @@ -176,6 +176,8 @@ exports[`Verify LoopUI Test the render method 1`] = ` Loop Viewer - testLoopName + - ( + ) Loop Viewer - Empty (NO loop loaded yet) + - ( + ) + Create Model diff --git a/ui-react/src/components/dialogs/Loop/DeployLoopModal.js b/ui-react/src/components/dialogs/Loop/DeployLoopModal.js index 6e7437038..853df1c89 100644 --- a/ui-react/src/components/dialogs/Loop/DeployLoopModal.js +++ b/ui-react/src/components/dialogs/Loop/DeployLoopModal.js @@ -161,7 +161,7 @@ export default class DeployLoopModal extends React.Component { } render() { return ( - + Deployment parameters diff --git a/ui-react/src/components/dialogs/Loop/LoopPropertiesModal.js b/ui-react/src/components/dialogs/Loop/LoopPropertiesModal.js index 73946f45d..97b782246 100644 --- a/ui-react/src/components/dialogs/Loop/LoopPropertiesModal.js +++ b/ui-react/src/components/dialogs/Loop/LoopPropertiesModal.js @@ -102,7 +102,7 @@ export default class LoopPropertiesModal extends React.Component { render() { return ( - + Model Properties diff --git a/ui-react/src/components/dialogs/Loop/ModifyLoopModal.js b/ui-react/src/components/dialogs/Loop/ModifyLoopModal.js index 42c03daca..1802e43de 100644 --- a/ui-react/src/components/dialogs/Loop/ModifyLoopModal.js +++ b/ui-react/src/components/dialogs/Loop/ModifyLoopModal.js @@ -174,7 +174,7 @@ export default class ModifyLoopModal extends React.Component { render() { return ( - + Modify Loop Operational Policies diff --git a/ui-react/src/components/dialogs/Loop/OpenLoopModal.js b/ui-react/src/components/dialogs/Loop/OpenLoopModal.js index 7c98fab4d..15a718885 100644 --- a/ui-react/src/components/dialogs/Loop/OpenLoopModal.js +++ b/ui-react/src/components/dialogs/Loop/OpenLoopModal.js @@ -101,7 +101,7 @@ export default class OpenLoopModal extends React.Component { render() { return ( - + Open Model diff --git a/ui-react/src/components/dialogs/Loop/__snapshots__/DeployLoopModal.test.js.snap b/ui-react/src/components/dialogs/Loop/__snapshots__/DeployLoopModal.test.js.snap index d7beecd02..c9acb9e12 100644 --- a/ui-react/src/components/dialogs/Loop/__snapshots__/DeployLoopModal.test.js.snap +++ b/ui-react/src/components/dialogs/Loop/__snapshots__/DeployLoopModal.test.js.snap @@ -2,6 +2,7 @@ exports[`Verify DeployLoopModal Test the render method 1`] = ` + Manage Dictionaries diff --git a/ui-react/src/components/dialogs/ManageDictionaries/__snapshots__/ManageDictionaries.test.js.snap b/ui-react/src/components/dialogs/ManageDictionaries/__snapshots__/ManageDictionaries.test.js.snap index e78292216..a73513e47 100644 --- a/ui-react/src/components/dialogs/ManageDictionaries/__snapshots__/ManageDictionaries.test.js.snap +++ b/ui-react/src/components/dialogs/ManageDictionaries/__snapshots__/ManageDictionaries.test.js.snap @@ -2,6 +2,7 @@ exports[`Verify ManageDictionaries Test API Successful 1`] = ` + Edit the policy diff --git a/ui-react/src/components/dialogs/Tosca/UploadToscaPolicyModal.js b/ui-react/src/components/dialogs/Tosca/UploadToscaPolicyModal.js index 1937485b0..ec080589f 100644 --- a/ui-react/src/components/dialogs/Tosca/UploadToscaPolicyModal.js +++ b/ui-react/src/components/dialogs/Tosca/UploadToscaPolicyModal.js @@ -86,7 +86,7 @@ export default class UploadToscaPolicyModal extends React.Component { render() { return ( - + Upload Tosca Model diff --git a/ui-react/src/components/dialogs/Tosca/ViewLoopTemplatesModal.js b/ui-react/src/components/dialogs/Tosca/ViewLoopTemplatesModal.js index cec6722d0..c5a91ea26 100644 --- a/ui-react/src/components/dialogs/Tosca/ViewLoopTemplatesModal.js +++ b/ui-react/src/components/dialogs/Tosca/ViewLoopTemplatesModal.js @@ -133,7 +133,7 @@ export default class ViewLoopTemplatesModal extends React.Component { render() { return ( - + diff --git a/ui-react/src/components/dialogs/Tosca/ViewToscaPolicyModal.js b/ui-react/src/components/dialogs/Tosca/ViewToscaPolicyModal.js index 650080520..71f371a6d 100644 --- a/ui-react/src/components/dialogs/Tosca/ViewToscaPolicyModal.js +++ b/ui-react/src/components/dialogs/Tosca/ViewToscaPolicyModal.js @@ -139,7 +139,7 @@ export default class ViewToscalPolicyModal extends React.Component { render() { return ( - + diff --git a/ui-react/src/components/dialogs/Tosca/__snapshots__/ViewLoopTemplatesModal.test.js.snap b/ui-react/src/components/dialogs/Tosca/__snapshots__/ViewLoopTemplatesModal.test.js.snap index 253820f86..2926e066e 100644 --- a/ui-react/src/components/dialogs/Tosca/__snapshots__/ViewLoopTemplatesModal.test.js.snap +++ b/ui-react/src/components/dialogs/Tosca/__snapshots__/ViewLoopTemplatesModal.test.js.snap @@ -2,6 +2,7 @@ exports[`Verify ViewLoopTemplatesModal Test the tosca model view render method 1`] = ` props.theme.loopViewerHeaderBackgroundColor}; + background-color: ${props => props.theme.loopLogsHeaderBackgroundColor}; padding: 10px 10px; - color: ${props => props.theme.loopViewerHeaderFontColor}; + color: ${props => props.theme.loopLogsHeaderFontColor}; ` const TableStyled = styled(Table)` diff --git a/ui-react/src/theme/globalStyle.js b/ui-react/src/theme/globalStyle.js index 3656f9e98..64fd7c5d9 100644 --- a/ui-react/src/theme/globalStyle.js +++ b/ui-react/src/theme/globalStyle.js @@ -87,6 +87,9 @@ export const DefaultClampTheme = { loopViewerHeaderBackgroundColor: '#337ab7', loopViewerHeaderFontColor: 'white', + loopLogsHeaderBackgroundColor: 'white', + loopLogsHeaderFontColor: 'black', + menuBackgroundColor: 'white', menuFontColor: 'black', menuHighlightedBackgroundColor: '#337ab7', -- cgit 1.2.3-korg From df68db271f23561671a59b1678c39b8ee54cd8e4 Mon Sep 17 00:00:00 2001 From: Ted Humphrey Date: Thu, 16 Apr 2020 17:36:17 +0000 Subject: Minor UI fixes for dialogues and Tosca upload feature Issue-ID: CLAMP-587 Change-Id: I683b62fbd0b15ab5db18c7e1e941ff8cb58cb0f3 Signed-off-by: Ted Humphrey --- ui-react-lib/libIndex.js | 6 +++ .../ConfigurationPolicyModal.js | 2 +- .../src/components/dialogs/Loop/CreateLoopModal.js | 33 ++++++++---- .../src/components/dialogs/Loop/DeployLoopModal.js | 2 +- .../components/dialogs/Loop/LoopPropertiesModal.js | 2 +- .../src/components/dialogs/Loop/ModifyLoopModal.js | 2 +- .../src/components/dialogs/Loop/OpenLoopModal.js | 40 ++++++++++----- .../__snapshots__/DeployLoopModal.test.js.snap | 1 + .../__snapshots__/LoopPropertiesModal.test.js.snap | 1 + .../Loop/__snapshots__/OpenLoopModal.test.js.snap | 60 ++++++++++++++++++---- .../ManageDictionaries/ManageDictionaries.js | 2 +- .../__snapshots__/ManageDictionaries.test.js.snap | 1 + .../OperationalPolicy/OperationalPolicyModal.js | 2 +- .../dialogs/Tosca/UploadToscaPolicyModal.js | 4 +- .../dialogs/Tosca/ViewLoopTemplatesModal.js | 18 +++++-- .../dialogs/Tosca/ViewToscaPolicyModal.js | 2 +- .../ViewLoopTemplatesModal.test.js.snap | 3 +- .../ViewToscaPolicyModal.test.js.snap | 1 + ui-react/src/components/loop_viewer/svg/LoopSvg.js | 14 +++-- .../svg/__snapshots__/LoopSvg.test.js.snap | 6 +-- 20 files changed, 146 insertions(+), 56 deletions(-) (limited to 'ui-react/src/components/dialogs/ManageDictionaries') diff --git a/ui-react-lib/libIndex.js b/ui-react-lib/libIndex.js index fae47e7fd..da98df43c 100755 --- a/ui-react-lib/libIndex.js +++ b/ui-react-lib/libIndex.js @@ -33,13 +33,19 @@ export { default as LoopService } from './src/api/LoopService'; export { default as LoopStatus } from './src/components/loop_viewer/status/LoopStatus'; export { default as LoopSvg } from './src/components/loop_viewer/svg/LoopSvg'; export { default as LoopUI } from './src/LoopUI'; +export { default as ManageDictionaries } from './src/components/dialogs/ManageDictionaries/ManageDictionaries'; export { default as MenuBar } from './src/components/menu/MenuBar'; +export { default as ModifyLoopModal } from './src/components/dialogs/Loop/ModifyLoopModal'; export { default as NotFound } from './src/NotFound'; export { default as OpenLoopModal } from './src/components/dialogs/Loop/OpenLoopModal'; export { default as CreateLoopModal } from './src/components/dialogs/Loop/CreateLoopModal'; export { default as OperationalPolicyModal } from './src/components/dialogs/OperationalPolicy/OperationalPolicyModal'; export { default as PerformActions } from './src/components/dialogs/PerformActions'; +export { default as PolicyToscaService } from './src/api/PolicyToscaService'; export { default as RefreshStatus } from './src/components/dialogs/RefreshStatus'; +export { default as TemplateService } from './src/api/TemplateService'; export { default as UserInfoModal } from './src/components/dialogs/UserInfoModal'; export { default as UserService } from './src/api/UserService'; +export { default as UploadToscaPolicyModal } from './src/components/dialogs/Tosca/UploadToscaPolicyModal'; +export { default as ViewLoopTemplatesModal } from './src/components/dialogs/Tosca/ViewLoopTemplatesModal'; export { default as ViewToscaPolicyModal } from './src/components/dialogs/Tosca/ViewToscaPolicyModal'; diff --git a/ui-react/src/components/dialogs/ConfigurationPolicy/ConfigurationPolicyModal.js b/ui-react/src/components/dialogs/ConfigurationPolicy/ConfigurationPolicyModal.js index da65ac9f1..3ff1ebec7 100644 --- a/ui-react/src/components/dialogs/ConfigurationPolicy/ConfigurationPolicyModal.js +++ b/ui-react/src/components/dialogs/ConfigurationPolicy/ConfigurationPolicyModal.js @@ -104,7 +104,7 @@ export default class ConfigurationPolicyModal extends React.Component { render() { return ( - + Configuration policies diff --git a/ui-react/src/components/dialogs/Loop/CreateLoopModal.js b/ui-react/src/components/dialogs/Loop/CreateLoopModal.js index 321304a6a..e98b59566 100644 --- a/ui-react/src/components/dialogs/Loop/CreateLoopModal.js +++ b/ui-react/src/components/dialogs/Loop/CreateLoopModal.js @@ -34,13 +34,20 @@ import TemplateService from '../../../api/TemplateService'; const ModalStyled = styled(Modal)` background-color: transparent; ` -const LoopViewSvgDivStyled = styled.div` - overflow: hidden; +const LoopViewSvgDivStyled = styled.svg` + display: flex; + flex-direction: row; + overflow-x: scroll; background-color: ${props => (props.theme.loopViewerBackgroundColor)}; border-color: ${props => (props.theme.loopViewerHeaderColor)}; + margin-top: 3em; margin-left: auto; margin-right:auto; + margin-bottom: -1em; text-align: center; + align-items: center; + height: 100%; + width: 100%; ` export default class CreateLoopModal extends React.Component { @@ -118,24 +125,28 @@ export default class CreateLoopModal extends React.Component { render() { return ( - + Create Model - Template Name + Template Name: @@ -148,4 +159,4 @@ export default class CreateLoopModal extends React.Component { ); } -} \ No newline at end of file +} diff --git a/ui-react/src/components/dialogs/Loop/DeployLoopModal.js b/ui-react/src/components/dialogs/Loop/DeployLoopModal.js index 853df1c89..2155977f6 100644 --- a/ui-react/src/components/dialogs/Loop/DeployLoopModal.js +++ b/ui-react/src/components/dialogs/Loop/DeployLoopModal.js @@ -161,7 +161,7 @@ export default class DeployLoopModal extends React.Component { } render() { return ( - + Deployment parameters diff --git a/ui-react/src/components/dialogs/Loop/LoopPropertiesModal.js b/ui-react/src/components/dialogs/Loop/LoopPropertiesModal.js index 97b782246..c0a2084f5 100644 --- a/ui-react/src/components/dialogs/Loop/LoopPropertiesModal.js +++ b/ui-react/src/components/dialogs/Loop/LoopPropertiesModal.js @@ -102,7 +102,7 @@ export default class LoopPropertiesModal extends React.Component { render() { return ( - + Model Properties diff --git a/ui-react/src/components/dialogs/Loop/ModifyLoopModal.js b/ui-react/src/components/dialogs/Loop/ModifyLoopModal.js index 8886dbfdf..8c6740564 100644 --- a/ui-react/src/components/dialogs/Loop/ModifyLoopModal.js +++ b/ui-react/src/components/dialogs/Loop/ModifyLoopModal.js @@ -180,7 +180,7 @@ export default class ModifyLoopModal extends React.Component { render() { return ( - + Modify Loop Operational Policies diff --git a/ui-react/src/components/dialogs/Loop/OpenLoopModal.js b/ui-react/src/components/dialogs/Loop/OpenLoopModal.js index 15a718885..c04883443 100644 --- a/ui-react/src/components/dialogs/Loop/OpenLoopModal.js +++ b/ui-react/src/components/dialogs/Loop/OpenLoopModal.js @@ -37,13 +37,20 @@ const ModalStyled = styled(Modal)` const CheckBoxStyled = styled(FormCheck.Input)` margin-left:3rem; ` -const LoopViewSvgDivStyled = styled.div` - overflow: hidden; +const LoopViewSvgDivStyled = styled.svg` + overflow-x: scroll; + display: flex; + flex-direction: row; background-color: ${props => (props.theme.loopViewerBackgroundColor)}; border-color: ${props => (props.theme.loopViewerHeaderColor)}; + margin-top: 2em; margin-left: auto; margin-right:auto; + margin-bottom: -3em; text-align: center; + align-items: center; + height: 100%; + width: 100%; ` export default class OpenLoopModal extends React.Component { @@ -54,6 +61,7 @@ export default class OpenLoopModal extends React.Component { this.handleOpen = this.handleOpen.bind(this); this.handleClose = this.handleClose.bind(this); this.handleDropdownListChange = this.handleDropdownListChange.bind(this); + this.showReadOnly = props.showReadOnly ? props.showReadOnly : true; this.state = { show: true, chosenLoopName: '', @@ -101,28 +109,34 @@ export default class OpenLoopModal extends React.Component { render() { return ( - + Open Model - Model Name + Model Name: this.fileInput = fileInput}/> diff --git a/ui-react/src/components/dialogs/Tosca/ViewLoopTemplatesModal.js b/ui-react/src/components/dialogs/Tosca/ViewLoopTemplatesModal.js index c5a91ea26..7cf02f711 100644 --- a/ui-react/src/components/dialogs/Tosca/ViewLoopTemplatesModal.js +++ b/ui-react/src/components/dialogs/Tosca/ViewLoopTemplatesModal.js @@ -38,16 +38,24 @@ import MaterialTable from "material-table"; const ModalStyled = styled(Modal)` background-color: transparent; ` -const LoopViewSvgDivStyled = styled.div` - overflow: hidden; +const LoopViewSvgDivStyled = styled.svg` + overflow-x: scroll; background-color: ${props => (props.theme.loopViewerBackgroundColor)}; border-color: ${props => (props.theme.loopViewerHeaderColor)}; - margin-left: auto; + margin-top: 3em; + margin-left: 2em; margin-right:auto; text-align: center; - margin-top: 20px; + height: 100%; + width: 100%; + display: flex; + flex-direction: row; + align-items: center; + ` const SvgContainerDivStyled = styled.div` + display: flex; + align-items: center; border: 1px solid; ` @@ -133,7 +141,7 @@ export default class ViewLoopTemplatesModal extends React.Component { render() { return ( - + diff --git a/ui-react/src/components/dialogs/Tosca/ViewToscaPolicyModal.js b/ui-react/src/components/dialogs/Tosca/ViewToscaPolicyModal.js index 71f371a6d..d49232f2d 100644 --- a/ui-react/src/components/dialogs/Tosca/ViewToscaPolicyModal.js +++ b/ui-react/src/components/dialogs/Tosca/ViewToscaPolicyModal.js @@ -139,7 +139,7 @@ export default class ViewToscalPolicyModal extends React.Component { render() { return ( - + diff --git a/ui-react/src/components/dialogs/Tosca/__snapshots__/ViewLoopTemplatesModal.test.js.snap b/ui-react/src/components/dialogs/Tosca/__snapshots__/ViewLoopTemplatesModal.test.js.snap index 2926e066e..3f6dc9482 100644 --- a/ui-react/src/components/dialogs/Tosca/__snapshots__/ViewLoopTemplatesModal.test.js.snap +++ b/ui-react/src/components/dialogs/Tosca/__snapshots__/ViewLoopTemplatesModal.test.js.snap @@ -3,6 +3,7 @@ exports[`Verify ViewLoopTemplatesModal Test the tosca model view render method 1`] = ` - (props.theme.loopViewerBackgroundColor)}; border: 1px solid; border-color: ${props => (props.theme.loopViewerHeaderColor)}; + margin-top: 1em; margin-left: auto; margin-right:auto; - text-align: center; + margin-bottom: -3em; + align-items: center; + height: 100%; + width: 100%; ` @@ -101,4 +107,4 @@ class LoopViewSvg extends React.Component { } } -export default withRouter(LoopViewSvg); \ No newline at end of file +export default withRouter(LoopViewSvg); diff --git a/ui-react/src/components/loop_viewer/svg/__snapshots__/LoopSvg.test.js.snap b/ui-react/src/components/loop_viewer/svg/__snapshots__/LoopSvg.test.js.snap index cecfb425a..e05f1c794 100644 --- a/ui-react/src/components/loop_viewer/svg/__snapshots__/LoopSvg.test.js.snap +++ b/ui-react/src/components/loop_viewer/svg/__snapshots__/LoopSvg.test.js.snap @@ -1,7 +1,7 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP exports[`Verify LoopSvg Test the render method 1`] = ` -No LOOP (SVG)", @@ -12,7 +12,7 @@ exports[`Verify LoopSvg Test the render method 1`] = ` `; exports[`Verify LoopSvg Test the render method no loopName 1`] = ` -No LOOP (SVG)", @@ -23,7 +23,7 @@ exports[`Verify LoopSvg Test the render method no loopName 1`] = ` `; exports[`Verify LoopSvg Test the render method svg not empty 1`] = ` -", -- cgit 1.2.3-korg From f1692af8bcc02c6a7335078160463025b85eba68 Mon Sep 17 00:00:00 2001 From: xuegao Date: Wed, 22 Apr 2020 13:54:24 +0200 Subject: Remove bugs and vulnerabilities Remove bugs and vulnerabilities based on the sonar report Issue-ID: CLAMP-841 Change-Id: I49d0eeeedf69a0cbe3adf49cf115ab25aec7adfd Signed-off-by: xuegao --- .../onap/clamp/loop/template/LoopElementModel.java | 4 +- .../OperationalPolicyRepresentationBuilder.java | 98 ++++++++++++---------- .../ManageDictionaries/ManageDictionaries.js | 14 ++-- 3 files changed, 62 insertions(+), 54 deletions(-) (limited to 'ui-react/src/components/dialogs/ManageDictionaries') diff --git a/src/main/java/org/onap/clamp/loop/template/LoopElementModel.java b/src/main/java/org/onap/clamp/loop/template/LoopElementModel.java index 4a46a9544..35fdf43cd 100644 --- a/src/main/java/org/onap/clamp/loop/template/LoopElementModel.java +++ b/src/main/java/org/onap/clamp/loop/template/LoopElementModel.java @@ -75,8 +75,8 @@ public class LoopElementModel extends AuditEntity implements Serializable { @Column(columnDefinition = "MEDIUMTEXT", name = "blueprint_yaml") private String blueprint; - public static String MICRO_SERVICE_TYPE = "MICRO_SERVICE_TYPE"; - public static String OPERATIONAL_POLICY_TYPE = "OPERATIONAL_POLICY_TYPE"; + public static final String MICRO_SERVICE_TYPE = "MICRO_SERVICE_TYPE"; + public static final String OPERATIONAL_POLICY_TYPE = "OPERATIONAL_POLICY_TYPE"; /** * The type of element. */ diff --git a/src/main/java/org/onap/clamp/policy/operational/OperationalPolicyRepresentationBuilder.java b/src/main/java/org/onap/clamp/policy/operational/OperationalPolicyRepresentationBuilder.java index 57d13ef17..674db2295 100644 --- a/src/main/java/org/onap/clamp/policy/operational/OperationalPolicyRepresentationBuilder.java +++ b/src/main/java/org/onap/clamp/policy/operational/OperationalPolicyRepresentationBuilder.java @@ -45,9 +45,19 @@ public class OperationalPolicyRepresentationBuilder { EELFManager.getInstance().getLogger(OperationalPolicyRepresentationBuilder.class); public static final String PROPERTIES = "properties"; + public static final String ITEMS = "items"; + public static final String ANY_OF = "anyOf"; + public static final String TITLE = "title"; + public static final String RECIPE = "recipe"; + public static final String DEFAULT = "default"; + public static final String STRING = "string"; public static final String TYPE = "type"; public static final String TYPE_LIST = "list"; + private OperationalPolicyRepresentationBuilder() { + throw new IllegalStateException("This is Utility class, not supposed to be initiated."); + } + /** * This method generates the operational policy json representation that will be * used by ui for rendering. It uses the model (VF and VFModule) defined in the @@ -65,21 +75,21 @@ public class OperationalPolicyRepresentationBuilder { ResourceFileUtil .getResourceAsString("clds/json-schema/operational_policies/operational_policy.json"), JsonObject.class); - jsonSchema.get("properties").getAsJsonObject() - .get("operational_policy").getAsJsonObject().get("properties").getAsJsonObject().get("policies") - .getAsJsonObject().get("items").getAsJsonObject().get("properties").getAsJsonObject().get("target") - .getAsJsonObject().get("anyOf").getAsJsonArray().addAll(createAnyOfArray(modelJson, true)); + jsonSchema.get(PROPERTIES).getAsJsonObject() + .get("operational_policy").getAsJsonObject().get(PROPERTIES).getAsJsonObject().get("policies") + .getAsJsonObject().get(ITEMS).getAsJsonObject().get(PROPERTIES).getAsJsonObject().get("target") + .getAsJsonObject().get(ANY_OF).getAsJsonArray().addAll(createAnyOfArray(modelJson, true)); // update CDS recipe and payload information to schema - JsonArray actors = jsonSchema.get("properties").getAsJsonObject() - .get("operational_policy").getAsJsonObject().get("properties").getAsJsonObject().get("policies") - .getAsJsonObject().get("items").getAsJsonObject().get("properties").getAsJsonObject().get("actor") - .getAsJsonObject().get("anyOf").getAsJsonArray(); + JsonArray actors = jsonSchema.get(PROPERTIES).getAsJsonObject() + .get("operational_policy").getAsJsonObject().get(PROPERTIES).getAsJsonObject().get("policies") + .getAsJsonObject().get(ITEMS).getAsJsonObject().get(PROPERTIES).getAsJsonObject().get("actor") + .getAsJsonObject().get(ANY_OF).getAsJsonArray(); for (JsonElement actor : actors) { - if ("CDS".equalsIgnoreCase(actor.getAsJsonObject().get("title").getAsString())) { - actor.getAsJsonObject().get("properties").getAsJsonObject().get("recipe").getAsJsonObject() - .get("anyOf").getAsJsonArray() + if ("CDS".equalsIgnoreCase(actor.getAsJsonObject().get(TITLE).getAsString())) { + actor.getAsJsonObject().get(PROPERTIES).getAsJsonObject().get(RECIPE).getAsJsonObject() + .get(ANY_OF).getAsJsonArray() .addAll(createAnyOfArrayForCdsRecipe(modelJson)); } } @@ -93,9 +103,9 @@ public class OperationalPolicyRepresentationBuilder { private static JsonObject createSchemaProperty(String title, String type, String defaultValue, String readOnlyFlag, String[] enumArray) { JsonObject property = new JsonObject(); - property.addProperty("title", title); - property.addProperty("type", type); - property.addProperty("default", defaultValue); + property.addProperty(TITLE, title); + property.addProperty(TYPE, type); + property.addProperty(DEFAULT, defaultValue); property.addProperty("readOnly", readOnlyFlag); if (enumArray != null) { @@ -114,15 +124,15 @@ public class OperationalPolicyRepresentationBuilder { for (Entry entry : modelVnfs.entrySet()) { JsonObject vnfOneOfSchema = new JsonObject(); - vnfOneOfSchema.addProperty("title", "VNF" + "-" + entry.getKey()); + vnfOneOfSchema.addProperty(TITLE, "VNF" + "-" + entry.getKey()); JsonObject properties = new JsonObject(); if (generateType) { - properties.add("type", createSchemaProperty("Type", "string", "VNF", "True", null)); + properties.add(TYPE, createSchemaProperty("Type", STRING, "VNF", "True", null)); } - properties.add("resourceID", createSchemaProperty("Resource ID", "string", + properties.add("resourceID", createSchemaProperty("Resource ID", STRING, modelVnfs.get(entry.getKey()).getAsJsonObject().get("name").getAsString(), "True", null)); - vnfOneOfSchema.add("properties", properties); + vnfOneOfSchema.add(PROPERTIES, properties); vnfSchemaArray.add(vnfOneOfSchema); } return vnfSchemaArray; @@ -134,39 +144,39 @@ public class OperationalPolicyRepresentationBuilder { for (Entry entry : modelVfModules.entrySet()) { JsonObject vfModuleOneOfSchema = new JsonObject(); - vfModuleOneOfSchema.addProperty("title", "VFMODULE" + "-" + entry.getKey()); + vfModuleOneOfSchema.addProperty(TITLE, "VFMODULE" + "-" + entry.getKey()); JsonObject properties = new JsonObject(); if (generateType) { - properties.add("type", createSchemaProperty("Type", "string", "VFMODULE", "True", null)); + properties.add(TYPE, createSchemaProperty("Type", STRING, "VFMODULE", "True", null)); } properties.add("resourceID", - createSchemaProperty("Resource ID", "string", + createSchemaProperty("Resource ID", STRING, modelVfModules.get(entry.getKey()).getAsJsonObject().get("vfModuleModelName").getAsString(), "True", null)); properties.add("modelInvariantId", - createSchemaProperty("Model Invariant Id (ModelInvariantUUID)", "string", + createSchemaProperty("Model Invariant Id (ModelInvariantUUID)", STRING, modelVfModules.get(entry.getKey()).getAsJsonObject().get("vfModuleModelInvariantUUID") .getAsString(), "True", null)); properties.add("modelVersionId", - createSchemaProperty("Model Version Id (ModelUUID)", "string", + createSchemaProperty("Model Version Id (ModelUUID)", STRING, modelVfModules.get(entry.getKey()).getAsJsonObject().get("vfModuleModelUUID").getAsString(), "True", null)); properties.add("modelName", - createSchemaProperty("Model Name", "string", + createSchemaProperty("Model Name", STRING, modelVfModules.get(entry.getKey()).getAsJsonObject().get("vfModuleModelName").getAsString(), "True", null)); - properties.add("modelVersion", createSchemaProperty("Model Version", "string", + properties.add("modelVersion", createSchemaProperty("Model Version", STRING, modelVfModules.get(entry.getKey()).getAsJsonObject().get("vfModuleModelVersion").getAsString(), "True", null)); properties .add("modelCustomizationId", - createSchemaProperty("Customization ID", "string", + createSchemaProperty("Customization ID", STRING, modelVfModules.get(entry.getKey()).getAsJsonObject() .get("vfModuleModelCustomizationUUID").getAsString(), "True", null)); - vfModuleOneOfSchema.add("properties", properties); + vfModuleOneOfSchema.add(PROPERTIES, properties); vfModuleOneOfSchemaArray.add(vfModuleOneOfSchema); } return vfModuleOneOfSchemaArray; @@ -202,9 +212,9 @@ public class OperationalPolicyRepresentationBuilder { JsonObject workflows = controllerProperties.getAsJsonObject("workflows"); for (Entry workflowsEntry : workflows.entrySet()) { JsonObject obj = new JsonObject(); - obj.addProperty("title", workflowsEntry.getKey()); - obj.addProperty("type", "object"); - obj.add("properties", createPayloadProperty(workflowsEntry.getValue().getAsJsonObject(), + obj.addProperty(TITLE, workflowsEntry.getKey()); + obj.addProperty(TYPE, "object"); + obj.add(PROPERTIES, createPayloadProperty(workflowsEntry.getValue().getAsJsonObject(), controllerProperties, workflowsEntry.getKey())); schemaArray.add(obj); } @@ -217,21 +227,21 @@ public class OperationalPolicyRepresentationBuilder { private static JsonObject createPayloadProperty(JsonObject workFlow, JsonObject controllerProperties, String workFlowName) { JsonObject payload = new JsonObject(); - payload.addProperty("title", "Payload"); - payload.addProperty("type", "object"); - payload.add("properties", createInputPropertiesForPayload(workFlow, + payload.addProperty(TITLE, "Payload"); + payload.addProperty(TYPE, "object"); + payload.add(PROPERTIES, createInputPropertiesForPayload(workFlow, controllerProperties)); JsonObject properties = new JsonObject(); - properties.add("recipe", createRecipeForCdsWorkflow(workFlowName)); + properties.add(RECIPE, createRecipeForCdsWorkflow(workFlowName)); properties.add("payload", payload); return properties; } private static JsonObject createRecipeForCdsWorkflow(String workflow) { JsonObject recipe = new JsonObject(); - recipe.addProperty("title", "recipe"); - recipe.addProperty("type", "string"); - recipe.addProperty("default", workflow); + recipe.addProperty(TITLE, RECIPE); + recipe.addProperty(TYPE, STRING); + recipe.addProperty(DEFAULT, workflow); JsonObject options = new JsonObject(); options.addProperty("hidden", true); recipe.add("options", options); @@ -253,18 +263,18 @@ public class OperationalPolicyRepresentationBuilder { JsonObject inputs = workFlow.getAsJsonObject("inputs"); JsonObject jsonObject = new JsonObject(); jsonObject.add("artifact_name", createSchemaProperty( - "artifact name", "string", artifactName, "True", null)); + "artifact name", STRING, artifactName, "True", null)); jsonObject.add("artifact_version", createSchemaProperty( - "artifact version", "string", artifactVersion, "True", null)); + "artifact version", STRING, artifactVersion, "True", null)); jsonObject.add("mode", createCdsInputProperty( - "mode", "string", "async" ,null)); + "mode", STRING, "async" ,null)); jsonObject.add("data", createDataProperty(inputs)); return jsonObject; } private static JsonObject createDataProperty(JsonObject inputs) { JsonObject data = new JsonObject(); - data.addProperty("title", "data"); + data.addProperty(TITLE, "data"); JsonObject dataObj = new JsonObject(); addDataFields(inputs, dataObj); data.add(PROPERTIES, dataObj); @@ -294,7 +304,7 @@ public class OperationalPolicyRepresentationBuilder { String defaultValue, JsonObject cdsProperty) { JsonObject property = new JsonObject(); - property.addProperty("title", title); + property.addProperty(TITLE, title); if (TYPE_LIST.equalsIgnoreCase(type)) { property.addProperty(TYPE, "array"); @@ -304,14 +314,14 @@ public class OperationalPolicyRepresentationBuilder { dataObject); JsonObject listProperties = new JsonObject(); listProperties.add(PROPERTIES, dataObject); - property.add("items", listProperties); + property.add(ITEMS, listProperties); } } else { property.addProperty(TYPE, type); } if (defaultValue != null) { - property.addProperty("default", defaultValue); + property.addProperty(DEFAULT, defaultValue); } return property; } diff --git a/ui-react/src/components/dialogs/ManageDictionaries/ManageDictionaries.js b/ui-react/src/components/dialogs/ManageDictionaries/ManageDictionaries.js index 2af1b7ce4..538688dac 100644 --- a/ui-react/src/components/dialogs/ManageDictionaries/ManageDictionaries.js +++ b/ui-react/src/components/dialogs/ManageDictionaries/ManageDictionaries.js @@ -21,7 +21,7 @@ */ -import React from 'react'; +import React, { forwardRef } from 'react'; import Button from 'react-bootstrap/Button'; import Modal from 'react-bootstrap/Modal'; import styled from 'styled-components'; @@ -30,7 +30,6 @@ import MaterialTable, {MTableToolbar} from "material-table"; import IconButton from '@material-ui/core/IconButton'; import Tooltip from '@material-ui/core/Tooltip'; import Grid from '@material-ui/core/Grid'; -import { forwardRef } from 'react'; import AddBox from '@material-ui/icons/AddBox'; import ArrowUpward from '@material-ui/icons/ArrowUpward'; import Check from '@material-ui/icons/Check'; @@ -204,7 +203,7 @@ export default class ManageDictionaries extends React.Component { cellStyle: cellStyle, headerStyle: headerStyle }, - { + { title: "Sub-Dictionary", field: "subDictionary", editComponent: props => (
@@ -214,7 +213,7 @@ export default class ManageDictionaries extends React.Component { cellStyle: cellStyle, headerStyle: headerStyle }, - { + { title: "Updated By", field: "updatedBy", editable: 'never', cellStyle: cellStyle, headerStyle: headerStyle @@ -294,7 +293,7 @@ export default class ManageDictionaries extends React.Component { var dictionaryElements = []; if (event.target.files[0].type === 'text/csv' ) { if (event.target.files && event.target.files[0]) { - let reader = new FileReader(); + const reader = new FileReader(); reader.onload = function(e) { var dictElems = reader.result.split('\n'); var jsonObj = []; @@ -312,7 +311,6 @@ export default class ManageDictionaries extends React.Component { const mandatoryKeys = [ 'Element Short Name', 'Element Name', 'Element Type' ]; const validTypes = ['string','number','datetime','json','map']; if (!dictElems){ - text.setState({validData: false}); } else if (headers.length !== dictKeys.length){ text.setState({validImport: false}); @@ -373,7 +371,7 @@ export default class ManageDictionaries extends React.Component { } } - + render() { return ( @@ -440,7 +438,7 @@ export default class ManageDictionaries extends React.Component { new Promise((resolve, reject) => { setTimeout(() => { { - let data = this.state.dictionaryNames; + const data = this.state.dictionaryNames; const index = data.indexOf(oldData); data.splice(index, 1); this.setState({ data }, () => resolve()); -- cgit 1.2.3-korg From e7c9326c63077634c5aa68940f5c24677177c9d6 Mon Sep 17 00:00:00 2001 From: sebdet Date: Thu, 23 Apr 2020 15:17:59 +0200 Subject: Fix bugs reported by sonar Fix bugs reported by sonar as critical, major, ... Issue-ID: CLAMP-841 Signed-off-by: sebdet Change-Id: Id525d532d4f405216da44f95503e06e172458e70 --- .../dialogs/ManageDictionaries/ManageDictionaries.js | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) (limited to 'ui-react/src/components/dialogs/ManageDictionaries') diff --git a/ui-react/src/components/dialogs/ManageDictionaries/ManageDictionaries.js b/ui-react/src/components/dialogs/ManageDictionaries/ManageDictionaries.js index 2af1b7ce4..71477ae0c 100644 --- a/ui-react/src/components/dialogs/ManageDictionaries/ManageDictionaries.js +++ b/ui-react/src/components/dialogs/ManageDictionaries/ManageDictionaries.js @@ -88,11 +88,13 @@ function SubDict(props) { const {onChange} = props; const subDicts = []; subDicts.push('Default'); - for(var item in dictList) { - if(dictList[item].secondLevelDictionary === 1) { - subDicts.push(dictList[item].name); - } - }; + if (dictList != "undefined" && dictList.length > 0) { + for(var item in dictList) { + if(dictList[item].secondLevelDictionary === 1) { + subDicts.push(dictList[item].name); + } + }; + } subDicts.push(''); var optionItems = subDicts.map( (item) => -- cgit 1.2.3-korg From b56cb11bb745c34455b7159980be38b81d8c115f Mon Sep 17 00:00:00 2001 From: Ted Humphrey Date: Fri, 12 Jun 2020 03:17:35 -0400 Subject: Address Manage Dictionary issues A variety of bugs are addressed. See JIRA. Issue-ID: CLAMP-849 Change-Id: I620c1ec774fdcec484f40fc638551960a98973c8 Signed-off-by: Ted Humphrey --- ui-react-lib/libIndex.js | 1 + .../ManageDictionaries/ManageDictionaries.js | 694 +++++++++++---------- .../ManageDictionaries/ManageDictionaries.test.js | 106 ++-- .../__snapshots__/ManageDictionaries.test.js.snap | 1 - ui-react/src/utils/CsvToJson.js | 204 ++++++ ui-react/src/utils/CsvToJson.test.js | 268 ++++++++ 6 files changed, 891 insertions(+), 383 deletions(-) create mode 100644 ui-react/src/utils/CsvToJson.js create mode 100644 ui-react/src/utils/CsvToJson.test.js (limited to 'ui-react/src/components/dialogs/ManageDictionaries') diff --git a/ui-react-lib/libIndex.js b/ui-react-lib/libIndex.js index 16728ef8d..f090b6145 100755 --- a/ui-react-lib/libIndex.js +++ b/ui-react-lib/libIndex.js @@ -21,6 +21,7 @@ * */ +export { default as CsvToJson } from './src/utils/CsvToJson'; export { default as CreateLoopModal } from './src/components/dialogs/Loop/CreateLoopModal'; export { default as DeployLoopModal } from './src/components/dialogs/Loop/DeployLoopModal'; export { default as LoopActionService } from './src/api/LoopActionService'; diff --git a/ui-react/src/components/dialogs/ManageDictionaries/ManageDictionaries.js b/ui-react/src/components/dialogs/ManageDictionaries/ManageDictionaries.js index 54ac6411b..58cb9c6c3 100644 --- a/ui-react/src/components/dialogs/ManageDictionaries/ManageDictionaries.js +++ b/ui-react/src/components/dialogs/ManageDictionaries/ManageDictionaries.js @@ -24,12 +24,14 @@ import React, { forwardRef } from 'react'; import Button from 'react-bootstrap/Button'; import Modal from 'react-bootstrap/Modal'; +import Row from 'react-bootstrap/Row'; +import Col from 'react-bootstrap/Col'; import styled from 'styled-components'; import TemplateMenuService from '../../../api/TemplateService'; +import CsvToJson from '../../../utils/CsvToJson'; import MaterialTable, {MTableToolbar} from "material-table"; import IconButton from '@material-ui/core/IconButton'; import Tooltip from '@material-ui/core/Tooltip'; -import Grid from '@material-ui/core/Grid'; import AddBox from '@material-ui/icons/AddBox'; import ArrowUpward from '@material-ui/icons/ArrowUpward'; import Check from '@material-ui/icons/Check'; @@ -49,19 +51,37 @@ import ViewColumn from '@material-ui/icons/ViewColumn'; const ModalStyled = styled(Modal)` + @media (min-width: 1200px) { + .modal-xl { + max-width: 96%; + } + } background-color: transparent; ` + +const MTableToolbarStyled = styled(MTableToolbar)` + display: flex; + flex-direction: row; + align-items: center; +` +const ColPullLeftStyled = styled(Col)` + display: flex; + flex-direction: row; + align-items: center; + margin-left: -40px; +` + const cellStyle = { border: '1px solid black' }; const headerStyle = { backgroundColor: '#ddd', border: '2px solid black' }; const rowHeaderStyle = {backgroundColor:'#ddd', fontSize: '15pt', text: 'bold', border: '1px solid black'}; -var dictList = []; +let dictList = []; function SelectSubDictType(props) { const {onChange} = props; const selectedValues = (e) => { - var options = e.target.options; - var SelectedDictTypes = ''; - for (var dictType = 0, values = options.length; dictType < values; dictType++) { + let options = e.target.options; + let SelectedDictTypes = ''; + for (let dictType = 0, values = options.length; dictType < values; dictType++) { if (options[dictType].selected) { SelectedDictTypes = SelectedDictTypes.concat(options[dictType].value); SelectedDictTypes = SelectedDictTypes.concat('|'); @@ -87,15 +107,16 @@ function SubDict(props) { const {onChange} = props; const subDicts = []; subDicts.push('Default'); - if (dictList != "undefined" && dictList.length > 0) { - for(var item in dictList) { + if (dictList !== undefined && dictList.length > 0) { + let item; + for(item in dictList) { if(dictList[item].secondLevelDictionary === 1) { subDicts.push(dictList[item].name); } }; } subDicts.push(''); - var optionItems = subDicts.map( + let optionItems = subDicts.map( (item) => ); function selectedValue (e) { @@ -112,46 +133,45 @@ export default class ManageDictionaries extends React.Component { constructor(props, context) { super(props, context); this.handleClose = this.handleClose.bind(this); - this.getDictionary = this.getDictionary.bind(this); - this.getDictionaryElements = this.getDictionaryElements.bind(this); this.clickHandler = this.clickHandler.bind(this); - this.addDictionary = this.addDictionary.bind(this); - this.deleteDictionary = this.deleteDictionary.bind(this); + this.getDictionaries = this.getDictionaries.bind(this); + this.getDictionaryElements = this.getDictionaryElements.bind(this); + this.addReplaceDictionaryRequest = this.addReplaceDictionaryRequest.bind(this); + this.deleteDictionaryRequest = this.deleteDictionaryRequest.bind(this); + this.updateDictionaryElementsRequest = this.updateDictionaryElementsRequest.bind(this); + this.addDictionaryRow = this.addDictionaryRow.bind(this); + this.updateDictionaryRow = this.updateDictionaryRow.bind(this); + this.deleteDictionaryRow = this.deleteDictionaryRow.bind(this); + this.addDictionaryElementRow = this.addDictionaryElementRow.bind(this); + this.deleteDictionaryElementRow = this.deleteDictionaryElementRow.bind(this); + this.updateDictionaryElementRow = this.updateDictionaryElementRow.bind(this); this.fileSelectedHandler = this.fileSelectedHandler.bind(this); this.state = { show: true, selectedFile: '', - dictNameFlag: false, + currentSelectedDictionary: null, exportFilename: '', content: null, - newDict: '', - newDictItem: '', - delDictItem: '', - addDict: false, - delData: '', - delDict: false, - validImport: false, - dictionaryNames: [], dictionaryElements: [], - tableIcons: { - Add: forwardRef((props, ref) => ), - Check: forwardRef((props, ref) => ), - Clear: forwardRef((props, ref) => ), - Delete: forwardRef((props, ref) => ), - DetailPanel: forwardRef((props, ref) => ), - Edit: forwardRef((props, ref) => ), - Export: forwardRef((props, ref) => ), - Filter: forwardRef((props, ref) => ), - FirstPage: forwardRef((props, ref) => ), - LastPage: forwardRef((props, ref) => ), - NextPage: forwardRef((props, ref) => ), - PreviousPage: forwardRef((props, ref) => ), - ResetSearch: forwardRef((props, ref) => ), - Search: forwardRef((props, ref) => ), - SortArrow: forwardRef((props, ref) => ), - ThirdStateCheck: forwardRef((props, ref) => ), - ViewColumn: forwardRef((props, ref) => ) - }, + tableIcons: { + Add: forwardRef((props, ref) => ), + Check: forwardRef((props, ref) => ), + Clear: forwardRef((props, ref) => ), + Delete: forwardRef((props, ref) => ), + DetailPanel: forwardRef((props, ref) => ), + Edit: forwardRef((props, ref) => ), + Export: forwardRef((props, ref) => ), + Filter: forwardRef((props, ref) => ), + FirstPage: forwardRef((props, ref) => ), + LastPage: forwardRef((props, ref) => ), + NextPage: forwardRef((props, ref) => ), + PreviousPage: forwardRef((props, ref) => ), + ResetSearch: forwardRef((props, ref) => ), + Search: forwardRef((props, ref) => ), + SortArrow: forwardRef((props, ref) => ), + ThirdStateCheck: forwardRef((props, ref) => ), + ViewColumn: forwardRef((props, ref) => ) + }, dictColumns: [ { title: "Dictionary Name", field: "name",editable: 'onAdd', @@ -185,7 +205,7 @@ export default class ManageDictionaries extends React.Component { cellStyle: cellStyle, headerStyle: headerStyle }, - { + { title: "Element Name", field: "name", cellStyle: cellStyle, headerStyle: headerStyle @@ -194,8 +214,8 @@ export default class ManageDictionaries extends React.Component { title: "Element Description", field: "description", cellStyle: cellStyle, headerStyle: headerStyle - }, - { + }, + { title: "Element Type", field: "type", editComponent: props => (
@@ -204,8 +224,8 @@ export default class ManageDictionaries extends React.Component { ), cellStyle: cellStyle, headerStyle: headerStyle - }, - { + }, + { title: "Sub-Dictionary", field: "subDictionary", editComponent: props => (
@@ -214,8 +234,8 @@ export default class ManageDictionaries extends React.Component { ), cellStyle: cellStyle, headerStyle: headerStyle - }, - { + }, + { title: "Updated By", field: "updatedBy", editable: 'never', cellStyle: cellStyle, headerStyle: headerStyle @@ -229,325 +249,325 @@ export default class ManageDictionaries extends React.Component { } } - componentWillMount() { - this.getDictionary(); - } + componentDidMount() { + this.getDictionaries(); + } - getDictionary() { - TemplateMenuService.getDictionary().then(dictionaryNames => { - this.setState({ dictionaryNames: dictionaryNames }) - }); - } + getDictionaries() { + TemplateMenuService.getDictionary().then(arrayOfdictionaries => { + this.setState({ dictionaries: arrayOfdictionaries, currentSelectedDictionary: null }) + }); + } - getDictionaryElements(dictionaryName) { - TemplateMenuService.getDictionaryElements(dictionaryName).then(dictionaryElements => { - dictList = this.state.dictionaryNames; - this.setState({ dictionaryElements: dictionaryElements.dictionaryElements}); - }); - } + getDictionaryElements(dictionaryName) { + TemplateMenuService.getDictionaryElements(dictionaryName).then(dictionaryElements => { + dictList = this.state.dictionaries; + this.setState({ dictionaryElements: dictionaryElements.dictionaryElements} ); + }); + } - clickHandler(rowData) { - this.setState({ - dictNameFlag: false, - addDict: false, - }); - } + clickHandler(rowData) { + this.getDictionaries(); + } - handleClose() { - this.setState({ show: false }); - this.props.history.push('/'); - } + handleClose() { + this.setState({ show: false }); + this.props.history.push('/'); + } - addDictionary() { - var modifiedData = []; - if(this.state.newDict !== '') { - modifiedData = this.state.newDict; - } else { - modifiedData = {"name": this.state.dictionaryName, 'dictionaryElements': this.state.newDictItem}; - } - if(this.state.newDictItem === '') { - TemplateMenuService.insDictionary(modifiedData).then(resp => { - }); - } else { - TemplateMenuService.insDictionaryElements(modifiedData).then(resp => { - }); - } - } + addReplaceDictionaryRequest(dictionaryEntry) { + TemplateMenuService.insDictionary(dictionaryEntry) + .then(resp => {}) + .then(() => {this.getDictionaries()}); + } - deleteDictionary() { - var modifiedData = []; - if(this.state.delData !== '') { - modifiedData = this.state.delData.name; - } else { - modifiedData = {"name": this.state.dictionaryName, "shortName": this.state.delDictItem.shortName}; - } - if(this.state.delDictItem === '') { - TemplateMenuService.deleteDictionary(modifiedData).then(resp => { - }); - } else { - TemplateMenuService.deleteDictionaryElements(modifiedData).then(resp => { - }); - } - } + updateDictionaryElementsRequest(dictElements) { + let reqData = { "name": this.state.currentSelectedDictionary, 'dictionaryElements': dictElements }; + TemplateMenuService.insDictionaryElements(reqData) + .then(resp => {}) + .then(() => { this.getDictionaryElements(this.state.currentSelectedDictionary) }); + } - fileSelectedHandler = (event) => { - const text = this; - var dictionaryElements = []; - if (event.target.files[0].type === 'text/csv' ) { - if (event.target.files && event.target.files[0]) { - const reader = new FileReader(); - reader.onload = function(e) { - var dictElems = reader.result.split('\n'); - var jsonObj = []; - var headers = dictElems[0].split(','); - for(var i = 0; i < dictElems.length; i++) { - var data = dictElems[i].split(','); - var obj = {}; - for(var j = 0; j < data.length; j++) { - obj[headers[j].trim()] = data[j].trim(); - } - jsonObj.push(obj); - } - JSON.stringify(jsonObj); - const dictKeys = ['Element Short Name','Element Name','Element Description','Element Type','Sub-Dictionary']; - const mandatoryKeys = [ 'Element Short Name', 'Element Name', 'Element Type' ]; - const validTypes = ['string','number','datetime','json','map']; - if (!dictElems){ - text.setState({validData: false}); - } else if (headers.length !== dictKeys.length){ - text.setState({validImport: false}); - } else { - var subDictionaries = []; - for(var item in dictList) { - if(dictList[item].secondLevelDictionary === 1) { - subDictionaries.push(dictList[item].name); - } - }; - subDictionaries = subDictionaries.toString(); - var row = 0; - for (var dictElem of jsonObj){ - ++row; - for (var itemKey in dictElem){ - var value = dictElem[itemKey].trim(); - if (dictKeys.indexOf(itemKey) < 0){ - var errorMessage = 'unknown field name of, ' + itemKey + ', found in CSV header'; - text.setState({validImport: false}); - alert(errorMessage); - break; - } else if (value === "" && mandatoryKeys.indexOf(itemKey) >= 0){ - errorMessage = 'value for ' + itemKey + ', at row #, ' + row + ', is empty but required'; - text.setState({validImport: false}); - alert(errorMessage); - break; - } else if (itemKey === 'Element Type' && validTypes.indexOf(value) < 0 && row > 1) { - errorMessage = 'invalid dictElemenType of ' + value + ' at row #' + row; - text.setState({validImport: false}); - alert(errorMessage); - break; - } else if (value !== "" && itemKey === 'Sub-Dictionary' && subDictionaries.indexOf(value) < 0 && row > 1) { - errorMessage = 'invalid subDictionary of ' + value + ' at row #' + row; - text.setState({validImport: false}); - alert(errorMessage); - } + deleteDictionaryRequest(dictionaryName) { + TemplateMenuService.deleteDictionary(dictionaryName) + .then(resp => { this.getDictionaries() }); + } + + deleteDictionaryElementRequest(dictionaryName, elemenetShortName) { + TemplateMenuService.deleteDictionaryElements({ 'name': dictionaryName, 'shortName': elemenetShortName }) + .then(resp => { + this.getDictionaryElements(dictionaryName); + }); + } + + fileSelectedHandler = (event) => { + + if (event.target.files[0].type === 'text/csv' || event.target.files[0].type === 'application/vnd.ms-excel') { + if (event.target.files && event.target.files[0]) { + const reader = new FileReader(); + reader.onload = (e) => { + + const jsonKeyNames = [ 'shortName', 'name', 'description', 'type', 'subDictionary' ]; + const userHeaderNames = [ 'Element Short Name', 'Element Name', 'Element Description', 'Element Type', 'Sub-Dictionary' ]; + const mandatory = [ true, true, true, true, false ]; + const validTypes = ['string','number','datetime','json','map']; + + let result = CsvToJson(reader.result, ',', '||||', userHeaderNames, jsonKeyNames, mandatory); + + let errorMessages = result.errorMessages; + let jsonObjArray = result.jsonObjArray; + + let validTypesErrorMesg = ''; + + for (let i=0; i < validTypes.length; ++i) { + if (i === 0) { + validTypesErrorMesg = validTypes[i]; + } else { + validTypesErrorMesg += ',' + validTypes[i]; + } + } + + if (errorMessages !== '') { + alert(errorMessages); + return; + } + + // Perform further checks on data that is now in JSON form + let subDictionaries = []; + + // NOTE: dictList is a global variable maintained faithfully + // by the getDictionaries() method outside this import + // functionality. + let item; + for (item in dictList) { + if (dictList[item].secondLevelDictionary === 1) { + subDictionaries.push(dictList[item].name); + } + }; + + // Check for valid Sub-Dictionary and Element Type values + subDictionaries = subDictionaries.toString(); + let row = 2; + let dictElem; + for (dictElem of jsonObjArray) { + let itemKey; + for (itemKey in dictElem){ + let value = dictElem[itemKey].trim(); + let keyIndex = jsonKeyNames.indexOf(itemKey); + if (itemKey === 'shortName' && /[^a-zA-Z0-9-_.]/.test(value)) { + errorMessages += '\n' + userHeaderNames[keyIndex] + + ' at row #' + row + + ' can only contain alphanumeric characters and periods, hyphens or underscores'; + } + if (itemKey === 'type' && validTypes.indexOf(value) < 0) { + errorMessages += '\nInvalid value of "' + value + '" for "' + userHeaderNames[keyIndex] + '" at row #' + row; + errorMessages += '\nValid types are: ' + validTypesErrorMesg; + } + if (value !== "" && itemKey === 'subDictionary' && subDictionaries.indexOf(value) < 0) { + errorMessages += '\nInvalid Sub-Dictionary value of "' + value + '" at row #' + row; } } + ++row; } - const headerKeys = ['shortName','name','description','type','subDictionary']; + if (errorMessages) { + alert(errorMessages); + return; + } - for(i = 1; i < dictElems.length; i++) { - data = dictElems[i].split(','); - obj = {}; - for(j = 0; j < data.length; j++) { - obj[headerKeys[j].trim()] = data[j].trim(); - } - dictionaryElements.push(obj); - } - text.setState({newDictItem: dictionaryElements, addDict: true}); - } - reader.readAsText(event.target.files[0]); - } - this.setState({selectedFile: event.target.files[0]}) - } else { - text.setState({validImport: false}); - alert('Please upload .csv extention files only.'); - } + // We made it through all the checks. Send it to back end + this.updateDictionaryElementsRequest(jsonObjArray); + } + reader.readAsText(event.target.files[0]); + } + this.setState({selectedFile: event.target.files[0]}) + } else { + alert('Please upload .csv extention files only.'); + } + } - } - - render() { - return ( - - - Manage Dictionaries - - - {!this.state.dictNameFlag? {this.getDictionaryElements(rowData.name);this.setState({dictNameFlag: true, exportFilename: rowData.name, dictionaryName: rowData.name})}} - options={{ - headerStyle: rowHeaderStyle, - }} - editable={{ - onRowAdd: newData => - new Promise((resolve, reject) => { - setTimeout(() => { - { - const dictionaryNames = this.state.dictionaryNames; - var validData = true; - if(/[^a-zA-Z0-9-_.]/.test(newData.name)) { - validData = false; - alert('Please enter alphanumberic input. Only allowed special characters are:(period, hyphen, underscore)'); - } - for (var i = 0; i < this.state.dictionaryNames.length; i++) { - if (this.state.dictionaryNames[i].name === newData.name) { - validData = false; - alert(newData.name + ' dictionary name already exists') - } - } - if(validData){ - dictionaryNames.push(newData); - this.setState({ dictionaryNames }, () => resolve()); - this.setState({addDict: true, newDict: newData}); - } - } - resolve(); - }, 1000); - }), - onRowUpdate: (newData, oldData) => - new Promise((resolve, reject) => { - setTimeout(() => { - { - const dictionaryNames = this.state.dictionaryNames; - var validData = true; - if(/[^a-zA-Z0-9-_.]/.test(newData.name)) { - validData = false; - alert('Please enter alphanumberic input. Only allowed special characters are:(period, hyphen, underscore)'); - } - if(validData){ - const index = dictionaryNames.indexOf(oldData); - dictionaryNames[index] = newData; - this.setState({ dictionaryNames }, () => resolve()); - this.setState({addDict: true, newDict: newData}); - } - } - resolve(); - }, 1000); - }), - onRowDelete: oldData => - new Promise((resolve, reject) => { - setTimeout(() => { - { - const data = this.state.dictionaryNames; - const index = data.indexOf(oldData); - data.splice(index, 1); - this.setState({ data }, () => resolve()); - this.setState({delDict: true, delData: oldData}) - } - resolve() - }, 1000) - }) - }} - />:"" + addDictionaryRow(newData) { + let validData = true; + return new Promise((resolve, reject) => { + setTimeout(() => { + if (/[^a-zA-Z0-9-_.]/.test(newData.name)) { + validData = false; + alert('Please enter alphanumeric input. Only allowed special characters are:(period, hyphen, underscore)'); + reject(() => {}); + } + for (let i = 0; i < this.state.dictionaries.length; i++) { + if (this.state.dictionaries[i].name === newData.name) { + validData = false; + alert(newData.name + ' dictionary name already exists') + reject(() => {}); + } + } + if (validData) { + this.addReplaceDictionaryRequest(newData); + } + resolve(); + }, 1000); + }); + } + + + updateDictionaryRow(oldData, newData) { + let validData = true; + return new Promise((resolve) => { + setTimeout(() => { + if (/[^a-zA-Z0-9-_.]/.test(newData.name)) { + validData = false; + alert('Please enter alphanumberic input. Only allowed special characters are:(period, hyphen, underscore)'); + } + if (validData) { + this.addReplaceDictionaryRequest(newData); + } + resolve(); + }, 1000); + }); + } + + deleteDictionaryRow(oldData) { + return new Promise((resolve) => { + setTimeout(() => { + this.deleteDictionaryRequest(oldData.name); + resolve(); + }, 1000); + }); + } + + addDictionaryElementRow(newData) { + return new Promise((resolve, reject) => { + setTimeout(() => { + let dictionaryElements = this.state.dictionaryElements; + let errorMessage = ''; + for (let i = 0; i < this.state.dictionaryElements.length; i++) { + if (this.state.dictionaryElements[i].shortName === newData.shortName) { + alert('Short Name "' + newData.shortName + '" already exists'); + reject(() => {}); + } + } + if (newData.shortName !== '' && /[^a-zA-Z0-9-_.]/.test(newData.shortName)) { + errorMessage += '\nShort Name is limited to alphanumeric characters and also period, hyphen, and underscore'; + } + if (!newData.shortName){ + errorMessage += '\nShort Name must be specified'; + } + if (!newData.name){ + errorMessage += '\nElement Name must be specified'; + } + if (!newData.type){ + errorMessage += '\nElement Type must be specified'; + } + if (!newData.description){ + errorMessage += '\nElement Description must be specified'; + } + if (errorMessage === '') { + dictionaryElements.push(newData); + this.updateDictionaryElementsRequest(dictionaryElements); + resolve(); + } else { + alert(errorMessage); + reject(() => {}); + } + }, 1000); + }); + } + + updateDictionaryElementRow(newData, oldData) { + return new Promise((resolve) => { + setTimeout(() => { + let dictionaryElements = this.state.dictionaryElements; + let validData = true; + if (!newData.type) { + validData = false; + alert('Element Type cannot be null'); + } + if (validData) { + const index = dictionaryElements.indexOf(oldData); + dictionaryElements[index] = newData; + this.updateDictionaryElementsRequest(dictionaryElements); + } + resolve(); + }, 1000); + }); + } + + + deleteDictionaryElementRow(oldData) { + return new Promise((resolve) => { + setTimeout(() => { + this.deleteDictionaryElementRequest(this.state.currentSelectedDictionary, oldData.shortName); + resolve(); + }, 1000); + }); + } + + render() { + return ( + + + Manage Dictionaries + + + {this.state.currentSelectedDictionary === null ? { + this.setState({ + currentSelectedDictionary : rowData.name, + exportFilename: rowData.name + }) + this.getDictionaryElements(rowData.name); + }} + options={{ + headerStyle: rowHeaderStyle, + }} + editable={{ + onRowAdd: this.addDictionaryRow, + onRowUpdate: this.updateDictionaryRow, + onRowDelete: this.deleteDictionaryRow + }} + /> : null } - {this.state.dictNameFlag? ( -
- -
- + + + + + - this.fileUpload.click()}> - - + this.fileUpload.click()}> + + - -
- {this.fileUpload = fileUpload;}} style={{ visibility: 'hidden'}} onChange={this.fileSelectedHandler} /> -
+ {this.fileUpload = fileUpload;}} + style={{ visibility: 'hidden', width: '1px' }} onChange={this.fileSelectedHandler} /> + + ) }} editable={{ - onRowAdd: newData => - new Promise((resolve, reject) => { - setTimeout(() => { - { - const dictionaryElements = this.state.dictionaryElements; - var validData = true; - for (var i = 0; i < this.state.dictionaryElements.length; i++) { - if (this.state.dictionaryElements[i].shortName === newData.shortName) { - validData = false; - alert(newData.shortname + 'short name already exists') - } - } - if(/[^a-zA-Z0-9-_.]/.test(newData.shortName)) { - validData = false; - alert('Please enter alphanumberic input. Only allowed special characters are:(period, hyphen, underscore)'); - } - if(!newData.type){ - validData = false; - alert('Element Type cannot be null'); - } - if(validData){ - dictionaryElements.push(newData); - this.setState({ dictionaryElements }, () => resolve()); - this.setState({addDict: true, newDictItem: [newData]}); - } - } - resolve(); - }, 1000); - }), - onRowUpdate: (newData, oldData) => - new Promise((resolve, reject) => { - setTimeout(() => { - { - const dictionaryElements = this.state.dictionaryElements; - var validData = true; - if(!newData.type){ - validData = false; - alert('Element Type cannot be null'); - } - if(validData){ - const index = dictionaryElements.indexOf(oldData); - dictionaryElements[index] = newData; - this.setState({ dictionaryElements }, () => resolve()); - this.setState({addDict: true, newDictItem: [newData]}); - } - } - resolve(); - }, 1000); - }), - onRowDelete: oldData => - new Promise((resolve, reject) => { - setTimeout(() => { - { - let data = this.state.dictionaryElements; - const index = data.indexOf(oldData); - data.splice(index, 1); - this.setState({ data }, () => resolve()); - this.setState({delDict: true, delDictItem: oldData}) - } - resolve() - }, 1000) - }) + onRowAdd: this.addDictionaryElementRow, + onRowUpdate: this.updateDictionaryElementRow, + onRowDelete: this.deleteDictionaryElementRow }} - />:"" + /> : null } - {this.state.dictNameFlag?:""} - {this.state.addDict && this.addDictionary()} - {this.state.delDict && this.deleteDictionary()} + {this.state.currentSelectedDictionary !== null ? :""}
@@ -555,4 +575,4 @@ export default class ManageDictionaries extends React.Component {
); } -} +} diff --git a/ui-react/src/components/dialogs/ManageDictionaries/ManageDictionaries.test.js b/ui-react/src/components/dialogs/ManageDictionaries/ManageDictionaries.test.js index 13a6035a3..d1d4aa662 100644 --- a/ui-react/src/components/dialogs/ManageDictionaries/ManageDictionaries.test.js +++ b/ui-react/src/components/dialogs/ManageDictionaries/ManageDictionaries.test.js @@ -60,10 +60,10 @@ describe('Verify ManageDictionaries', () => { json: () => { return Promise.resolve({ "name": "vtest", - "secondLevelDictionary": "1", + "secondLevelDictionary": "1", "subDictionaryType": "string", - "updatedBy": "test", - "updatedDate": "05-07-2019 19:09:42" + "updatedBy": "test", + "updatedDate": "05-07-2019 19:09:42" }); } }); @@ -90,10 +90,10 @@ describe('Verify ManageDictionaries', () => { json: () => { return Promise.resolve({ "name": "vtest", - "secondLevelDictionary": "1", + "secondLevelDictionary": "1", "subDictionaryType": "string", - "updatedBy": "test", - "updatedDate": "05-07-2019 19:09:42" + "updatedBy": "test", + "updatedDate": "05-07-2019 19:09:42" }); } }); @@ -103,12 +103,33 @@ describe('Verify ManageDictionaries', () => { }); test('Test get dictionaryNames/dictionaryElements, add/delete dictionary functions', async () => { + const dictionaries = [ + { + name: "DefaultActors", + secondLevelDictionary: 0, + subDictionaryType: "", + dictionaryElements: [ + { + "shortName": "SDNR", + "name": "SDNR Change", + "description": "SDNR component", + "type": "string", + "createdDate": "2020-06-07T18:57:18.130858Z", + "updatedDate": "2020-06-11T13:10:52.239282Z", + "updatedBy": "admin" + } + ], + createdDate: "2020-06-07T22:21:08.428742Z", + updatedDate: "2020-06-10T00:41:49.122908Z", + updatedBy: "Not found" + } + ]; const historyMock = { push: jest.fn() }; TemplateMenuService.getDictionary = jest.fn().mockImplementation(() => { - return Promise.resolve("test"); + return Promise.resolve(dictionaries); }); TemplateMenuService.getDictionaryElements = jest.fn().mockImplementation(() => { - return Promise.resolve({dictionaryElements:"testitem"}); + return Promise.resolve(dictionaries[0]); }); TemplateMenuService.insDictionary = jest.fn().mockImplementation(() => { return Promise.resolve(200); @@ -118,33 +139,40 @@ describe('Verify ManageDictionaries', () => { }); const flushPromises = () => new Promise(setImmediate); const component = shallow() - component.setState({ newDict: { - "name": "test", - "secondLevelDictionary": "0", - "subDictionaryType": "string" - } - }); - component.setState({ delData: { - "name": "test", - "secondLevelDictionary": "0", - "subDictionaryType": "string" - } - }); const instance = component.instance(); - instance.getDictionaryElements("test"); + instance.getDictionaryElements("DefaultActors"); instance.clickHandler(); - instance.addDictionary(); - instance.deleteDictionary(); + instance.addReplaceDictionaryRequest(); + instance.deleteDictionaryRequest(); await flushPromises(); - expect(component.state('dictionaryNames')).toEqual("test"); - expect(component.state('dictionaryElements')).toEqual("testitem"); - expect(component.state('dictNameFlag')).toEqual(false); + expect(component.state('dictionaries')).toEqual(dictionaries); }); test('Test adding and deleting dictionaryelements', async () => { const historyMock = { push: jest.fn() }; + const dictionaries = [ + { + name: "DefaultActors", + secondLevelDictionary: 0, + subDictionaryType: "", + dictionaryElements: [ + { + "shortName": "SDNR", + "name": "SDNR Change", + "description": "SDNR component", + "type": "string", + "createdDate": "2020-06-07T18:57:18.130858Z", + "updatedDate": "2020-06-11T13:10:52.239282Z", + "updatedBy": "admin" + } + ], + createdDate: "2020-06-07T22:21:08.428742Z", + updatedDate: "2020-06-10T00:41:49.122908Z", + updatedBy: "Not found" + } + ]; TemplateMenuService.getDictionary = jest.fn().mockImplementation(() => { - return Promise.resolve("test"); + return Promise.resolve(dictionaries); }); TemplateMenuService.insDictionaryElements = jest.fn().mockImplementation(() => { return Promise.resolve(200); @@ -154,23 +182,11 @@ describe('Verify ManageDictionaries', () => { }); const flushPromises = () => new Promise(setImmediate); const component = shallow() - component.setState({ newDictItem: { - "name": "test", - "dictionaryElements" : { - "shortName": "shorttest", - } - }}); - component.setState({ delDictItem: { - "name": "test", - "dictionaryElements" : { - "shortName": "shortTest", - } - }}); const instance = component.instance(); - instance.addDictionary(); - instance.deleteDictionary(); + instance.addReplaceDictionaryRequest({ name: "EventDictionary", secondLevelDictionary: "0", subDictionaryType: "string"} ); + instance.deleteDictionaryRequest('EventDictionary'); await flushPromises(); - expect(component.state('dictionaryNames')).toEqual("test"); + expect(component.state('currentSelectedDictionary')).toEqual(null); }); it('Test handleClose', () => { @@ -181,10 +197,10 @@ describe('Verify ManageDictionaries', () => { json: () => { return Promise.resolve({ "name": "vtest", - "secondLevelDictionary": "1", + "secondLevelDictionary": "1", "subDictionaryType": "string", - "updatedBy": "test", - "updatedDate": "05-07-2019 19:09:42" + "updatedBy": "test", + "updatedDate": "05-07-2019 19:09:42" }); } }); diff --git a/ui-react/src/components/dialogs/ManageDictionaries/__snapshots__/ManageDictionaries.test.js.snap b/ui-react/src/components/dialogs/ManageDictionaries/__snapshots__/ManageDictionaries.test.js.snap index 71cc393b8..40914aee6 100644 --- a/ui-react/src/components/dialogs/ManageDictionaries/__snapshots__/ManageDictionaries.test.js.snap +++ b/ui-react/src/components/dialogs/ManageDictionaries/__snapshots__/ManageDictionaries.test.js.snap @@ -88,7 +88,6 @@ exports[`Verify ManageDictionaries Test API Successful 1`] = ` }, ] } - data={Array []} editable={ Object { "onRowAdd": [Function], diff --git a/ui-react/src/utils/CsvToJson.js b/ui-react/src/utils/CsvToJson.js new file mode 100644 index 000000000..5ec19c9e2 --- /dev/null +++ b/ui-react/src/utils/CsvToJson.js @@ -0,0 +1,204 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP CLAMP + * ================================================================================ + * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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. + * ============LICENSE_END============================================ + * =================================================================== + * + */ + +export default function CsvToJson(rawCsvData, delimiter, internalDelimiter, csvHeaderNames, jsonKeyNames, mandatory) { + + let printDictKeys = ''; + let result = { jsonObjArray: [], errorMessages: '' }; + + // Validate that all parallel arrays passed in have same number of elements; + // this would be a developer error. + + let checkLength = csvHeaderNames.length; + + if (checkLength !== jsonKeyNames.length || checkLength !== mandatory.length) { + result.errorMessages = 'interanl error: csvHeaderNames, jsonKeyNames, and mandatory arrays parameters are not the same length'; + return result; + } + + if (checkLength < 1) { + result.errorMessages = 'interanl error: csvHeaderNames, jsonKeyNames, and mandatory arrays have no entries'; + return result; + } + + // Make a nice string to print in the error case to tell user what is the + // required heaer row format + + for (let i=0; i < csvHeaderNames.length; ++i) { + if (i === 0) { + printDictKeys = csvHeaderNames[i]; + } else { + printDictKeys += ',' + csvHeaderNames[i]; + } + } + + let dictElems = rawCsvData.split('\n'); + let numColumns = 0; + let filteredDictElems = []; + + // The task of the following loop is to convert raw CSV rows into easily parseable + // and streamlined versions of the rows with an internalDelimiter replacing the standard + // comma; it is presumed (and checked) that the internalDelimiter cannot exist as a valid + // sequence of characters in the user's data. + + // This conversion process also strips leading and trailing whitespace from each row, + // discards empty rows, correctly interprets and removes all double quotes that programs like + // Excel use to support user columns that contain special characters, most notably, the comma + // delimiter. A double-quote that is contained within a double-quoted column value + // must appear in this raw data as a sequence of two double quotes. Furthermore, any column + // value in the raw CSV data that does not contain a delimiter may or may not be enclosed in + // double quotes. It is the Excel convention to not use double qoutes unless necessary, and + // there is no reasonable way to tell Excel to surround every column value with double quotes. + // Any files that were directly "exported" by CLAMP itself from the Managing Dictionaries + // capability, surround all columns with double quotes. + + for (let i = 0; i < dictElems.length; i++) { + + let oneRow = dictElems[i].trim(); + let j = 0; + let inQuote = false + let nextChar = undefined; + let prevChar = null; + + + if (oneRow === '') { + continue; // Skip blank rows + } else if (oneRow.indexOf(internalDelimiter) !== -1) { + result.errorMessages += '\nRow #' + i + ' contains illegal sequence of characters (' + internalDelimiter + ')'; + break; + } else { + nextChar = oneRow[1]; + } + + let newStr = ''; + numColumns = 1; + + // This "while loop" performs the very meticulous task of removing double quotes that + // are used by Excel to encase special characters as user string value data, + // and manages to correctly identify columns that are defined with or without + // double quotes and to process the comma delimiter correctly when encountered + // as a user value within a column. Such a column would have to be encased in + // double quotes; a comma found outside double quotes IS a delimiter. + + while (j < oneRow.length) { + if (oneRow[j] === '"') { + if (inQuote === false) { + if (prevChar !== delimiter && prevChar !== null) { + result.errorMessages += '\nMismatched double quotes or illegal whitespace around delimiter at row #' + (i + 1) + ' near column #' + numColumns; + break; + } else { + inQuote = true; + } + } else { + if (nextChar === '"') { + newStr += '"'; + ++j; + } else if ((nextChar !== delimiter) && (nextChar !== undefined)) { + result.errorMessages += '\nRow #' + (i + 1) + ' is badly formatted at column #' + numColumns + '. Perhaps an unescaped double quote.'; + break; + } else if (nextChar === delimiter) { + ++numColumns; + inQuote = false; + newStr += internalDelimiter; + prevChar = delimiter; + j += 2; + nextChar = oneRow[j+1]; + continue; + } else { + ++numColumns; + inQuote = false; + break; + } + } + } else { + if (oneRow[j] === delimiter && inQuote === false) { + newStr += internalDelimiter; + ++numColumns; + } else { + newStr += oneRow[j]; + } + } + prevChar = oneRow[j]; + ++j; + nextChar = oneRow[j+1]; // can result in undefined at the end + } + + if (result.errorMessages === '' && inQuote !== false) { + result.errorMessages += '\nMismatched double quotes at row #' + (i + 1); + break; + } else if (result.errorMessages === '' && numColumns < jsonKeyNames.length) { + result.errorMessages += '\nNot enough columns (' + jsonKeyNames.length + ') at row #' + (i + 1); + break; + } + + filteredDictElems.push(newStr); + } + + if (result.errorMessages !== '') { + return result; + } + + // Perform further checks on data that is now in JSON form + if (filteredDictElems.length < 2) { + result.errorMessages += '\nNot enough row data found in import file. Need at least a header row and one row of data'; + return result; + } + + // Now that we have something reliably parsed into sanitized columns lets run some checks + // and convert it all into an array of JSON objects to push to the back end if all the + // checks pass. + + let headers = filteredDictElems[0].split(internalDelimiter); + + // check that headers are included in proper order + for (let i=0; i < jsonKeyNames.length; ++i) { + if (csvHeaderNames[i] !== headers[i]) { + result.errorMessages += 'Row 1 header key at column #' + (i + 1) + ' is a mismatch. Expected row header must contain at least:\n' + printDictKeys; + return result; + } + } + + // Convert the ASCII rows of data into an array of JSON obects that omit the header + // row which is not sent to the back end. + + for (let i = 1; i < filteredDictElems.length; i++) { + let data = filteredDictElems[i].split(internalDelimiter); + let obj = {}; + for (let j = 0; j < data.length && j < jsonKeyNames.length; j++) { + let value = data[j].trim(); + if (mandatory[j] === true && value === '') { + result.errorMessages += '\n' + csvHeaderNames[j] + ' at row #' + (i+1) + ' is empty but requires a value.'; + } + obj[jsonKeyNames[j]] = value; + } + result.jsonObjArray.push(obj); + } + + if (result.errorMessages !== '') { + // If we have errors, return empty parse result even though some things + // may have parsed properly. We do not want to encourage the caller + // to think the data is good for use. + result.jsonObjArray = []; + } + + return result; +} diff --git a/ui-react/src/utils/CsvToJson.test.js b/ui-react/src/utils/CsvToJson.test.js new file mode 100644 index 000000000..88fa7a472 --- /dev/null +++ b/ui-react/src/utils/CsvToJson.test.js @@ -0,0 +1,268 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP CLAMP + * ================================================================================ + * Copyright (C) 2019 AT&T Intellectual Property. All rights + * reserved. + * ================================================================================ + * 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. + * ============LICENSE_END============================================ + * =================================================================== + * + */ + +import CsvToJson from './CsvToJson' + +describe('Verify CsvToJson', () => { + + const hdrNames= [ + "Element Short Name", + "Element Name", + "Element Description", + "Element Type", + "Sub-Dictionary" + ]; + + const jsonKeyNames = [ + "shortName", + "name", + "description", + "type", + "subDictionary" + ]; + + const mandatory = [ true, true, true, true, false ]; + + it('Test CsvToJson No Error Case, Quoted Columns', () => { + + let rawCsv = '"Element Short Name","Element Name","Element Description","Element Type","Sub-Dictionary"\n'; + rawCsv += '"alertType","Alert Type","Type of Alert","string","","admin","2020-06-11T13:56:14.927437Z"'; + + let expectedResult = { + errorMessages: '', + jsonObjArray: [ + { + description: "Type of Alert", + name: "Alert Type", + shortName: "alertType", + subDictionary: "", + type: "string" + } + ] + }; + + expect(CsvToJson(rawCsv, ',', '|', hdrNames, jsonKeyNames, mandatory)).toEqual(expectedResult); + }); + + it('Test CsvToJson No Error Case, Unquoted Columns', () => { + + let rawCsv = 'Element Short Name,Element Name,Element Description,Element Type,Sub-Dictionary\n'; + rawCsv += 'alertType,Alert Type,Type of Alert,string,,admin,2020-06-11T13:56:14.927437Z'; + + let expectedResult = { + errorMessages: '', + jsonObjArray: [ + { + description: "Type of Alert", + name: "Alert Type", + shortName: "alertType", + subDictionary: "", + type: "string" + } + ] + }; + + expect(CsvToJson(rawCsv, ',', '|', hdrNames, jsonKeyNames, mandatory)).toEqual(expectedResult); + }); + + it('Test CsvToJson Properly Escaped Double Quote and Delimiter', () => { + + let rawCsv = '"Element Short Name","Element Name","Element Description","Element Type","Sub-Dictionary"\n'; + rawCsv += '"alertType","Alert ""Type""","Type of Alert, Varies","string","","admin","2020-06-11T13:56:14.927437Z"'; + + let errorMessage = ''; + + let expectedResult = { + errorMessages: errorMessage, + jsonObjArray: [ + { + description: "Type of Alert, Varies", + name: 'Alert "Type"', + shortName: 'alertType', + subDictionary: "", + type: "string", + } + + ] + }; + + expect(CsvToJson(rawCsv, ',', '|', hdrNames, jsonKeyNames, mandatory)).toEqual(expectedResult); + }); + + + it('Test CsvToJson Error Header Mismatch Error Case', () => { + + let rawCsv = '"Element Short Names","Element Name","Element Description","Element Type","Sub-Dictionary"\n'; + rawCsv += '"alertType","Alert Type","Type of Alert","string","","admin","2020-06-11T13:56:14.927437Z"'; + + let errorMessage = 'Row 1 header key at column #1 is a mismatch. Expected row header must contain at least:\n'; + errorMessage += 'Element Short Name,Element Name,Element Description,Element Type,Sub-Dictionary'; + + let expectedResult = { + errorMessages: errorMessage, + jsonObjArray: [] + }; + + expect(CsvToJson(rawCsv, ',', '|', hdrNames, jsonKeyNames, mandatory)).toEqual(expectedResult); + }); + + it('Test CsvToJson Error Mismatched Double Quotes in Column', () => { + + let rawCsv = '"Element Short Name","Element Name","Element Description","Element Type","Sub-Dictionary"\n'; + rawCsv += '"alert"Type","Alert Type","Type of Alert","string","","admin","2020-06-11T13:56:14.927437Z"'; + + let errorMessage = '\nRow #2 is badly formatted at column #1. Perhaps an unescaped double quote.' + + let expectedResult = { + errorMessages: errorMessage, + jsonObjArray: [] + }; + + expect(CsvToJson(rawCsv, ',', '|', hdrNames, jsonKeyNames, mandatory)).toEqual(expectedResult); + }); + + it('Test CsvToJson Error Illegal Whitespace', () => { + + let rawCsv = '"Element Short Name","Element Name","Element Description","Element Type","Sub-Dictionary"\n'; + rawCsv += 'alertType , "Alert Type","Type of Alert","string","","admin","2020-06-11T13:56:14.927437Z"'; + + let errorMessage = '\nMismatched double quotes or illegal whitespace around delimiter at row #2 near column #2'; + + let expectedResult = { + errorMessages: errorMessage, + jsonObjArray: [] + }; + + expect(CsvToJson(rawCsv, ',', '|', hdrNames, jsonKeyNames, mandatory)).toEqual(expectedResult); + }); + + it('Test CsvToJson Error Too Few Data Columns', () => { + + let rawCsv = '"Element Short Name","Element Name","Element Description","Element Type","Sub-Dictionary"\n'; + rawCsv += '"alertType","Alert Type","Type of Alert"'; + + let errorMessage = '\nNot enough columns (5) at row #2'; + + let expectedResult = { + errorMessages: errorMessage, + jsonObjArray: [] + }; + + expect(CsvToJson(rawCsv, ',', '|', hdrNames, jsonKeyNames, mandatory)).toEqual(expectedResult); + }); + + it('Test CsvToJson Error Wrong Header Column Order', () => { + + let rawCsv = '"Element Name","Element Short Name","Element Description","Element Type","Sub-Dictionary"\n'; + rawCsv += '"alertType","Alert Type","Type of Alert","string","","admin","2020-06-11T13:56:14.927437Z"'; + + let errorMessage = 'Row 1 header key at column #1 is a mismatch. Expected row header must contain at least:\n'; + errorMessage += 'Element Short Name,Element Name,Element Description,Element Type,Sub-Dictionary'; + + let expectedResult = { + errorMessages: errorMessage, + jsonObjArray: [] + }; + + expect(CsvToJson(rawCsv, ',', '|', hdrNames, jsonKeyNames, mandatory)).toEqual(expectedResult); + }); + + it('Test CsvToJson Error Not Enough Rows', () => { + + let rawCsv = '"Element Short Name","Element Name","Element Description","Element Type","Sub-Dictionary"\n'; + + let errorMessage = '\nNot enough row data found in import file. Need at least a header row and one row of data'; + + let expectedResult = { + errorMessages: errorMessage, + jsonObjArray: [] + }; + + expect(CsvToJson(rawCsv, ',', '|', hdrNames, jsonKeyNames, mandatory)).toEqual(expectedResult); + }); + + it('Test CsvToJson Error Mandatory Field Is Empty', () => { + + let rawCsv = '"Element Short Name","Element Name","Element Description","Element Type","Sub-Dictionary"\n'; + rawCsv += '"","Alert Type","Type of Alert","string","","admin","2020-06-11T13:56:14.927437Z"'; + + let expectedResult = { + errorMessages: '\nElement Short Name at row #2 is empty but requires a value.', + jsonObjArray: [] + }; + + expect(CsvToJson(rawCsv, ',', '|', hdrNames, jsonKeyNames, mandatory)).toEqual(expectedResult); + }); + + it('Test CsvToJson Error Mismatched Double Quotes At End', () => { + + let rawCsv = '"Element Short Name","Element Name","Element Description","Element Type","Sub-Dictionary"\n'; + rawCsv += '"alertType","Alert Type","Alert Type Description","string","admin","2020-06-11T13:56:14.927437Z'; + + let expectedResult = { + errorMessages: '\nMismatched double quotes at row #2', + jsonObjArray: [] + }; + + expect(CsvToJson(rawCsv, ',', '||', hdrNames, jsonKeyNames, mandatory)).toEqual(expectedResult); + }); + + it('Test CsvToJson Error Mismatched Mandatory Array Parameters', () => { + + let rawCsv = '"Element Short Name","Element Name","Element Description","Element Type","Sub-Dictionary"\n'; + rawCsv += '"alertType","Alert Type","Alert Type Description","string","admin","2020-06-11T13:56:14.927437Z'; + + let expectedResult = { + errorMessages: 'interanl error: csvHeaderNames, jsonKeyNames, and mandatory arrays parameters are not the same length', + jsonObjArray: [] + }; + + expect(CsvToJson(rawCsv, ',', '||', hdrNames, jsonKeyNames, [ true ])).toEqual(expectedResult); + }); + + it('Test CsvToJson Error Empty Mandatory Array Parameters', () => { + + let rawCsv = '"Element Short Name","Element Name","Element Description","Element Type","Sub-Dictionary"\n'; + rawCsv += '"alertType","Alert Type","Alert Type Description","string","admin","2020-06-11T13:56:14.927437Z'; + + let expectedResult = { + errorMessages: 'interanl error: csvHeaderNames, jsonKeyNames, and mandatory arrays have no entries', + jsonObjArray: [] + }; + + expect(CsvToJson(rawCsv, ',', '||', [], [], [])).toEqual(expectedResult); + }); + + it('Test CsvToJson Error Illegal Data Contains Internal Delimiter', () => { + + let rawCsv = '"Element Short Name","Element Name","Element Description","Element Type","Sub-Dictionary"\n'; + rawCsv += '"alertType","Alert Type","Alert Type||Description","string","admin","2020-06-11T13:56:14.927437Z'; + + let expectedResult = { + errorMessages: '\nRow #1 contains illegal sequence of characters (||)', + jsonObjArray: [] + }; + + expect(CsvToJson(rawCsv, ',', '||', hdrNames, jsonKeyNames, mandatory)).toEqual(expectedResult); + }); +}) -- cgit 1.2.3-korg From b80ded057a71f33f5d3365ba5393880a4296a229 Mon Sep 17 00:00:00 2001 From: Ted Humphrey Date: Wed, 17 Jun 2020 21:40:35 -0400 Subject: Add many jests tests and implement block on element type and subdictionary fields for subdictionary elements Issue-ID: CLAMP-849 Change-Id: Ic6c4c06ee3b41e2e4dfe1913b7ecf57a951d9993 Signed-off-by: Ted Humphrey --- .../ManageDictionaries/ManageDictionaries.js | 437 ++++++++++--------- .../ManageDictionaries/ManageDictionaries.test.js | 463 ++++++++++++++++----- 2 files changed, 599 insertions(+), 301 deletions(-) (limited to 'ui-react/src/components/dialogs/ManageDictionaries') diff --git a/ui-react/src/components/dialogs/ManageDictionaries/ManageDictionaries.js b/ui-react/src/components/dialogs/ManageDictionaries/ManageDictionaries.js index 58cb9c6c3..90bbc887c 100644 --- a/ui-react/src/components/dialogs/ManageDictionaries/ManageDictionaries.js +++ b/ui-react/src/components/dialogs/ManageDictionaries/ManageDictionaries.js @@ -74,7 +74,9 @@ const ColPullLeftStyled = styled(Col)` const cellStyle = { border: '1px solid black' }; const headerStyle = { backgroundColor: '#ddd', border: '2px solid black' }; const rowHeaderStyle = {backgroundColor:'#ddd', fontSize: '15pt', text: 'bold', border: '1px solid black'}; + let dictList = []; +let subDictFlag = false; function SelectSubDictType(props) { const {onChange} = props; @@ -90,9 +92,10 @@ function SelectSubDictType(props) { SelectedDictTypes = SelectedDictTypes.slice(0,-1); onChange(SelectedDictTypes); } + // When the subDictFlag is true, we need to disable selection of element "type" return(
- @@ -100,66 +103,75 @@ function SelectSubDictType(props) {
- ) + ); } function SubDict(props) { const {onChange} = props; const subDicts = []; - subDicts.push('Default'); + subDicts.push('none'); if (dictList !== undefined && dictList.length > 0) { - let item; - for(item in dictList) { - if(dictList[item].secondLevelDictionary === 1) { - subDicts.push(dictList[item].name); - } - }; + let item; + for(item in dictList) { + if(dictList[item].secondLevelDictionary === 1) { + subDicts.push(dictList[item].name); + } + } + } + let optionItems = []; + for (let i=0; i{subDicts[i]}); + } else { + optionItems.push(); + } } - subDicts.push(''); - let optionItems = subDicts.map( - (item) => - ); + function selectedValue (e) { onChange(e.target.value); } + // When the subDictFlag is true, we need to disable selection of + // the sub-dictionary flag return( - - ) + + ); } export default class ManageDictionaries extends React.Component { constructor(props, context) { super(props, context); - this.handleClose = this.handleClose.bind(this); + this.addDictionaryElementRow = this.addDictionaryElementRow.bind(this); + this.addDictionaryRow = this.addDictionaryRow.bind(this); + this.addReplaceDictionaryRequest = this.addReplaceDictionaryRequest.bind(this); this.clickHandler = this.clickHandler.bind(this); + this.deleteDictionaryElementRow = this.deleteDictionaryElementRow.bind(this); + this.deleteDictionaryRequest = this.deleteDictionaryRequest.bind(this); + this.deleteDictionaryRow = this.deleteDictionaryRow.bind(this); + this.fileSelectedHandler = this.fileSelectedHandler.bind(this); this.getDictionaries = this.getDictionaries.bind(this); this.getDictionaryElements = this.getDictionaryElements.bind(this); - this.addReplaceDictionaryRequest = this.addReplaceDictionaryRequest.bind(this); - this.deleteDictionaryRequest = this.deleteDictionaryRequest.bind(this); + this.handleClose = this.handleClose.bind(this); + this.handleDictionaryRowClick = this.handleDictionaryRowClick.bind(this); + this.importCsvData = this.importCsvData.bind(this); + this.updateDictionaryElementRow = this.updateDictionaryElementRow.bind(this); this.updateDictionaryElementsRequest = this.updateDictionaryElementsRequest.bind(this); - this.addDictionaryRow = this.addDictionaryRow.bind(this); this.updateDictionaryRow = this.updateDictionaryRow.bind(this); - this.deleteDictionaryRow = this.deleteDictionaryRow.bind(this); - this.addDictionaryElementRow = this.addDictionaryElementRow.bind(this); - this.deleteDictionaryElementRow = this.deleteDictionaryElementRow.bind(this); - this.updateDictionaryElementRow = this.updateDictionaryElementRow.bind(this); - this.fileSelectedHandler = this.fileSelectedHandler.bind(this); + this.readOnly = props.readOnly !== undefined ? props.readOnly : false; this.state = { show: true, - selectedFile: '', currentSelectedDictionary: null, exportFilename: '', content: null, dictionaryElements: [], tableIcons: { Add: forwardRef((props, ref) => ), - Check: forwardRef((props, ref) => ), - Clear: forwardRef((props, ref) => ), Delete: forwardRef((props, ref) => ), DetailPanel: forwardRef((props, ref) => ), Edit: forwardRef((props, ref) => ), + Check: forwardRef((props, ref) => ), + Clear: forwardRef((props, ref) => ), Export: forwardRef((props, ref) => ), Filter: forwardRef((props, ref) => ), FirstPage: forwardRef((props, ref) => ), @@ -226,10 +238,10 @@ export default class ManageDictionaries extends React.Component { headerStyle: headerStyle }, { - title: "Sub-Dictionary", field: "subDictionary", - editComponent: props => ( + title: "Sub-Dictionary", field: "subDictionary", + editComponent: props => (
- +
), cellStyle: cellStyle, @@ -256,14 +268,19 @@ export default class ManageDictionaries extends React.Component { getDictionaries() { TemplateMenuService.getDictionary().then(arrayOfdictionaries => { this.setState({ dictionaries: arrayOfdictionaries, currentSelectedDictionary: null }) + // global variable setting used functional components in this file + dictList = arrayOfdictionaries; + }).catch(() => { + console.error('Failed to retrieve dictionaries'); + this.setState({ dictionaries: [], currentSelectedDictionary: null }) }); } getDictionaryElements(dictionaryName) { TemplateMenuService.getDictionaryElements(dictionaryName).then(dictionaryElements => { - dictList = this.state.dictionaries; this.setState({ dictionaryElements: dictionaryElements.dictionaryElements} ); - }); + this.setState({ currentSelectDictionary: dictionaryName }); + }).catch(() => console.error('Failed to retrieve dictionary elements')) } clickHandler(rowData) { @@ -277,27 +294,33 @@ export default class ManageDictionaries extends React.Component { addReplaceDictionaryRequest(dictionaryEntry) { TemplateMenuService.insDictionary(dictionaryEntry) - .then(resp => {}) - .then(() => {this.getDictionaries()}); + .then(resp => { + this.getDictionaries(); + }) + .catch(() => console.error('Failed to insert new dictionary elements')); } updateDictionaryElementsRequest(dictElements) { let reqData = { "name": this.state.currentSelectedDictionary, 'dictionaryElements': dictElements }; TemplateMenuService.insDictionaryElements(reqData) - .then(resp => {}) - .then(() => { this.getDictionaryElements(this.state.currentSelectedDictionary) }); + .then(resp => { this.getDictionaryElements(this.state.currentSelectedDictionary) }) + .catch(() => console.error('Failed to update dictionary elements')); } deleteDictionaryRequest(dictionaryName) { TemplateMenuService.deleteDictionary(dictionaryName) - .then(resp => { this.getDictionaries() }); + .then(resp => { + this.getDictionaries(); + }) + .catch(() => console.error('Failed to delete dictionary')); } deleteDictionaryElementRequest(dictionaryName, elemenetShortName) { TemplateMenuService.deleteDictionaryElements({ 'name': dictionaryName, 'shortName': elemenetShortName }) .then(resp => { this.getDictionaryElements(dictionaryName); - }); + }) + .catch(() => console.error('Failed to delete dictionary elements')); } fileSelectedHandler = (event) => { @@ -306,83 +329,94 @@ export default class ManageDictionaries extends React.Component { if (event.target.files && event.target.files[0]) { const reader = new FileReader(); reader.onload = (e) => { + let errorMessages = this.importCsvData(reader.result); + if (errorMessages !== '') { + alert(errorMessages); + } + } + reader.readAsText(event.target.files[0]); + } + } else { + alert('Please upload .csv extention files only.'); + } + } - const jsonKeyNames = [ 'shortName', 'name', 'description', 'type', 'subDictionary' ]; - const userHeaderNames = [ 'Element Short Name', 'Element Name', 'Element Description', 'Element Type', 'Sub-Dictionary' ]; - const mandatory = [ true, true, true, true, false ]; - const validTypes = ['string','number','datetime','json','map']; + importCsvData(rawCsvData) { - let result = CsvToJson(reader.result, ',', '||||', userHeaderNames, jsonKeyNames, mandatory); + const jsonKeyNames = [ 'shortName', 'name', 'description', 'type', 'subDictionary' ]; + const userHeaderNames = [ 'Element Short Name', 'Element Name', 'Element Description', 'Element Type', 'Sub-Dictionary' ]; + const validTypes = ['string','number','datetime','json','map']; - let errorMessages = result.errorMessages; - let jsonObjArray = result.jsonObjArray; + let mandatory; - let validTypesErrorMesg = ''; + if (subDictFlag) { + mandatory = [ true, true, true, false, false ]; + } else { + mandatory = [ true, true, true, true, false ]; + } - for (let i=0; i < validTypes.length; ++i) { - if (i === 0) { - validTypesErrorMesg = validTypes[i]; - } else { - validTypesErrorMesg += ',' + validTypes[i]; - } - } + let result = CsvToJson(rawCsvData, ',', '||||', userHeaderNames, jsonKeyNames, mandatory); - if (errorMessages !== '') { - alert(errorMessages); - return; - } + let errorMessages = result.errorMessages; + let jsonObjArray = result.jsonObjArray; - // Perform further checks on data that is now in JSON form - let subDictionaries = []; + let validTypesErrorMesg = ''; - // NOTE: dictList is a global variable maintained faithfully - // by the getDictionaries() method outside this import - // functionality. - let item; - for (item in dictList) { - if (dictList[item].secondLevelDictionary === 1) { - subDictionaries.push(dictList[item].name); - } - }; - - // Check for valid Sub-Dictionary and Element Type values - subDictionaries = subDictionaries.toString(); - let row = 2; - let dictElem; - for (dictElem of jsonObjArray) { - let itemKey; - for (itemKey in dictElem){ - let value = dictElem[itemKey].trim(); - let keyIndex = jsonKeyNames.indexOf(itemKey); - if (itemKey === 'shortName' && /[^a-zA-Z0-9-_.]/.test(value)) { - errorMessages += '\n' + userHeaderNames[keyIndex] + - ' at row #' + row + - ' can only contain alphanumeric characters and periods, hyphens or underscores'; - } - if (itemKey === 'type' && validTypes.indexOf(value) < 0) { - errorMessages += '\nInvalid value of "' + value + '" for "' + userHeaderNames[keyIndex] + '" at row #' + row; - errorMessages += '\nValid types are: ' + validTypesErrorMesg; - } - if (value !== "" && itemKey === 'subDictionary' && subDictionaries.indexOf(value) < 0) { - errorMessages += '\nInvalid Sub-Dictionary value of "' + value + '" at row #' + row; - } - } - ++row; - } - if (errorMessages) { - alert(errorMessages); - return; - } + for (let i=0; i < validTypes.length; ++i) { + if (i === 0) { + validTypesErrorMesg = validTypes[i]; + } else { + validTypesErrorMesg += ',' + validTypes[i]; + } + } + + if (errorMessages !== '') { + return errorMessages; + } + + // Perform further checks on data that is now in JSON form + let subDictionaries = []; - // We made it through all the checks. Send it to back end - this.updateDictionaryElementsRequest(jsonObjArray); + // NOTE: dictList is a global variable maintained faithfully + // by the getDictionaries() method outside this import + // functionality. + let item; + for (item in dictList) { + if (dictList[item].secondLevelDictionary === 1) { + subDictionaries.push(dictList[item].name); + } + }; + + // Check for valid Sub-Dictionary and Element Type values + subDictionaries = subDictionaries.toString(); + let row = 2; + let dictElem; + for (dictElem of jsonObjArray) { + let itemKey; + for (itemKey in dictElem){ + let value = dictElem[itemKey].trim(); + let keyIndex = jsonKeyNames.indexOf(itemKey); + if (itemKey === 'shortName' && /[^a-zA-Z0-9-_.]/.test(value)) { + errorMessages += '\n' + userHeaderNames[keyIndex] + + ' at row #' + row + + ' can only contain alphanumeric characters and periods, hyphens or underscores'; + } + if (itemKey === 'type' && validTypes.indexOf(value) < 0) { + errorMessages += '\nInvalid value of "' + value + '" for "' + userHeaderNames[keyIndex] + '" at row #' + row; + errorMessages += '\nValid types are: ' + validTypesErrorMesg; + } + if (value !== "" && itemKey === 'subDictionary' && subDictionaries.indexOf(value) < 0) { + errorMessages += '\nInvalid Sub-Dictionary value of "' + value + '" at row #' + row; } - reader.readAsText(event.target.files[0]); } - this.setState({selectedFile: event.target.files[0]}) - } else { - alert('Please upload .csv extention files only.'); + ++row; + } + if (errorMessages === '') { + // We made it through all the checks. Send it to back end + this.updateDictionaryElementsRequest(jsonObjArray); } + + return errorMessages; } addDictionaryRow(newData) { @@ -392,13 +426,13 @@ export default class ManageDictionaries extends React.Component { if (/[^a-zA-Z0-9-_.]/.test(newData.name)) { validData = false; alert('Please enter alphanumeric input. Only allowed special characters are:(period, hyphen, underscore)'); - reject(() => {}); + reject(); } for (let i = 0; i < this.state.dictionaries.length; i++) { if (this.state.dictionaries[i].name === newData.name) { validData = false; alert(newData.name + ' dictionary name already exists') - reject(() => {}); + reject(); } } if (validData) { @@ -410,13 +444,14 @@ export default class ManageDictionaries extends React.Component { } - updateDictionaryRow(oldData, newData) { + updateDictionaryRow(newData, oldData) { let validData = true; - return new Promise((resolve) => { + return new Promise((resolve, reject) => { setTimeout(() => { if (/[^a-zA-Z0-9-_.]/.test(newData.name)) { validData = false; alert('Please enter alphanumberic input. Only allowed special characters are:(period, hyphen, underscore)'); + reject(); } if (validData) { this.addReplaceDictionaryRequest(newData); @@ -427,7 +462,7 @@ export default class ManageDictionaries extends React.Component { } deleteDictionaryRow(oldData) { - return new Promise((resolve) => { + return new Promise((resolve, reject) => { setTimeout(() => { this.deleteDictionaryRequest(oldData.name); resolve(); @@ -439,53 +474,63 @@ export default class ManageDictionaries extends React.Component { return new Promise((resolve, reject) => { setTimeout(() => { let dictionaryElements = this.state.dictionaryElements; - let errorMessage = ''; + let errorMessages = ''; for (let i = 0; i < this.state.dictionaryElements.length; i++) { if (this.state.dictionaryElements[i].shortName === newData.shortName) { alert('Short Name "' + newData.shortName + '" already exists'); - reject(() => {}); + reject(""); } } - if (newData.shortName !== '' && /[^a-zA-Z0-9-_.]/.test(newData.shortName)) { - errorMessage += '\nShort Name is limited to alphanumeric characters and also period, hyphen, and underscore'; + // MaterialTable returns no property at all if the user has not touched a + // new column, so we want to add the property with an emptry string + // for several cases if that is the case to simplify other checks. + if (newData.description === undefined) { + newData.description = ""; + } + if (newData.subDictionary === undefined) { + newData.subDictionary = null; + } + if (newData.type === undefined) { + newData.type = ""; + } + if (!newData.shortName && /[^a-zA-Z0-9-_.]/.test(newData.shortName)) { + errorMessages += '\nShort Name is limited to alphanumeric characters and also period, hyphen, and underscore'; } if (!newData.shortName){ - errorMessage += '\nShort Name must be specified'; + errorMessages += '\nShort Name must be specified'; } if (!newData.name){ - errorMessage += '\nElement Name must be specified'; + errorMessages += '\nElement Name must be specified'; } - if (!newData.type){ - errorMessage += '\nElement Type must be specified'; + if (!newData.type && !subDictFlag){ + errorMessages += '\nElement Type must be specified'; } - if (!newData.description){ - errorMessage += '\nElement Description must be specified'; - } - if (errorMessage === '') { + if (errorMessages === '') { dictionaryElements.push(newData); - this.updateDictionaryElementsRequest(dictionaryElements); + this.updateDictionaryElementsRequest([newData]); resolve(); } else { - alert(errorMessage); - reject(() => {}); + alert(errorMessages); + reject(""); } }, 1000); }); } updateDictionaryElementRow(newData, oldData) { - return new Promise((resolve) => { + return new Promise((resolve, reject) => { setTimeout(() => { let dictionaryElements = this.state.dictionaryElements; let validData = true; if (!newData.type) { validData = false; alert('Element Type cannot be null'); + reject(); } if (validData) { const index = dictionaryElements.indexOf(oldData); dictionaryElements[index] = newData; - this.updateDictionaryElementsRequest(dictionaryElements); + this.updateDictionaryElementsRequest([newData]); } resolve(); }, 1000); @@ -502,6 +547,15 @@ export default class ManageDictionaries extends React.Component { }); } + handleDictionaryRowClick(event, rowData) { + subDictFlag = rowData.secondLevelDictionary === 1 ? true : false; + this.setState({ + currentSelectedDictionary : rowData.name, + exportFilename: rowData.name + }) + this.getDictionaryElements(rowData.name); + } + render() { return ( @@ -509,70 +563,69 @@ export default class ManageDictionaries extends React.Component { Manage Dictionaries - {this.state.currentSelectedDictionary === null ? { - this.setState({ - currentSelectedDictionary : rowData.name, - exportFilename: rowData.name - }) - this.getDictionaryElements(rowData.name); - }} - options={{ - headerStyle: rowHeaderStyle, - }} - editable={{ - onRowAdd: this.addDictionaryRow, - onRowUpdate: this.updateDictionaryRow, - onRowDelete: this.deleteDictionaryRow - }} + {this.state.currentSelectedDictionary === null ? + : null + } + {this.state.currentSelectedDictionary !== null ? + ( + + + + + + + this.fileUpload.click()}> + + + + {this.fileUpload = fileUpload;}} + style={{ visibility: 'hidden', width: '1px' }} onChange={this.fileSelectedHandler} /> + + + ) + }} + editable={!this.readOnly ? + { + onRowAdd: this.addDictionaryElementRow, + onRowUpdate: this.updateDictionaryElementRow, + onRowDelete: this.deleteDictionaryElementRow + } : undefined + } /> : null - } - {this.state.currentSelectedDictionary !== null ? ( - - - - - - - this.fileUpload.click()}> - - - - {this.fileUpload = fileUpload;}} - style={{ visibility: 'hidden', width: '1px' }} onChange={this.fileSelectedHandler} /> - - - ) - }} - editable={{ - onRowAdd: this.addDictionaryElementRow, - onRowUpdate: this.updateDictionaryElementRow, - onRowDelete: this.deleteDictionaryElementRow - }} - /> : null - } - {this.state.currentSelectedDictionary !== null ? :""} - - - - - - ); - } + } + {this.state.currentSelectedDictionary !== null ? :""} +
+ + + +
+ ); + } } diff --git a/ui-react/src/components/dialogs/ManageDictionaries/ManageDictionaries.test.js b/ui-react/src/components/dialogs/ManageDictionaries/ManageDictionaries.test.js index d1d4aa662..a4c1335d8 100644 --- a/ui-react/src/components/dialogs/ManageDictionaries/ManageDictionaries.test.js +++ b/ui-react/src/components/dialogs/ManageDictionaries/ManageDictionaries.test.js @@ -27,7 +27,94 @@ import { render } from 'enzyme'; import ManageDictionaries from './ManageDictionaries'; import TemplateMenuService from '../../../api/TemplateService' +const TestDictionaryElements = { + name: "test", + secondLevelDictionary: 0, + subDictionaryType: "", + dictionaryElements: [ + { + shortName: "alertType", + name: "Alert Type", + description: "Type of Alert", + type: "string", + subDictionary: "", + createdDate: "2020-06-12T13:58:51.443931Z", + updatedDate: "2020-06-13T16:27:57.084870Z", + updatedBy: "admin", + createdBy: "admin" + } + ] +}; + +const TestDictionaries = +[ + { + name: "test", + secondLevelDictionary: 0, + subDictionaryType: "string", + dictionaryElements: [ TestDictionaryElements ], + createdDate: "2020-06-14T21:00:33.231166Z", + updatedDate: "2020-06-14T21:00:33.231166Z", + updatedBy: "admin", + createdBy: "admin" + }, + { + name: "testSub1", + secondLevelDictionary: 1, + subDictionaryType: "string", + dictionaryElements: [ + { + shortName: "subElem", + name: "Sub Element", + description: "Sub Element Description", + type: "string", + createdDate: "2020-06-14T21:04:44.402287Z", + updatedDate: "2020-06-14T21:04:44.402287Z", + updatedBy: "admin", + createdBy: "admin" + } + ], + createdDate: "2020-06-14T21:01:16.390250Z", + updatedDate: "2020-06-14T21:01:16.390250Z", + updatedBy: "admin", + createdBy: "admin" + } +]; + + +const historyMock = { push: jest.fn() }; + +let errorMessage = ''; + +window.alert = jest.fn().mockImplementation((mesg) => { errorMessage = mesg ; return }); + +TemplateMenuService.getDictionary = jest.fn().mockImplementation(() => { + return Promise.resolve(TestDictionaries); +}); + +TemplateMenuService.insDictionary = jest.fn().mockImplementation(() => { + return Promise.resolve({ ok: true, status: 200 }); +}); + +TemplateMenuService.deleteDictionary = jest.fn().mockImplementation(() => { + return Promise.resolve("200"); +}); + +TemplateMenuService.getDictionaryElements = jest.fn().mockImplementation(() => { + return Promise.resolve(TestDictionaryElements); +}); + +TemplateMenuService.deleteDictionaryElements = jest.fn().mockImplementation(() => { + return Promise.resolve("200"); +}); + +TemplateMenuService.insDictionaryElements = jest.fn().mockImplementation(() => { + return Promise.resolve("200"); +}); + + describe('Verify ManageDictionaries', () => { + beforeEach(() => { fetch.resetMocks(); }); @@ -40,7 +127,7 @@ describe('Verify ManageDictionaries', () => { json: () => { return Promise.resolve({ "name": "vtest", - "secondLevelDictionary": "1", + "secondLevelDictionary": 1, "subDictionaryType": "string", "updatedBy": "test", "updatedDate": "05-07-2019 19:09:42" @@ -60,7 +147,7 @@ describe('Verify ManageDictionaries', () => { json: () => { return Promise.resolve({ "name": "vtest", - "secondLevelDictionary": "1", + "secondLevelDictionary": 1, "subDictionaryType": "string", "updatedBy": "test", "updatedDate": "05-07-2019 19:09:42" @@ -71,124 +158,283 @@ describe('Verify ManageDictionaries', () => { const component = shallow(); }); - it('Test API Rejection', () => { - const myMockFunc = fetch.mockImplementationOnce(() => Promise.reject('error')); - setTimeout( () => myMockFunc().catch(e => { - console.info(e); - }), - 100 - ); - const component = shallow(); - expect(myMockFunc.mock.calls.length).toBe(1); - }); - it('Test Table icons', () => { - fetch.mockImplementationOnce(() => { - return Promise.resolve({ - ok: true, - status: 200, - json: () => { - return Promise.resolve({ - "name": "vtest", - "secondLevelDictionary": "1", - "subDictionaryType": "string", - "updatedBy": "test", - "updatedDate": "05-07-2019 19:09:42" - }); - } - }); - }); + const component = mount(); expect(component.find('[className="MuiSelect-icon MuiTablePagination-selectIcon"]')).toBeTruthy(); }); - test('Test get dictionaryNames/dictionaryElements, add/delete dictionary functions', async () => { - const dictionaries = [ - { - name: "DefaultActors", - secondLevelDictionary: 0, - subDictionaryType: "", - dictionaryElements: [ - { - "shortName": "SDNR", - "name": "SDNR Change", - "description": "SDNR component", - "type": "string", - "createdDate": "2020-06-07T18:57:18.130858Z", - "updatedDate": "2020-06-11T13:10:52.239282Z", - "updatedBy": "admin" - } - ], - createdDate: "2020-06-07T22:21:08.428742Z", - updatedDate: "2020-06-10T00:41:49.122908Z", - updatedBy: "Not found" - } - ]; - const historyMock = { push: jest.fn() }; - TemplateMenuService.getDictionary = jest.fn().mockImplementation(() => { - return Promise.resolve(dictionaries); - }); - TemplateMenuService.getDictionaryElements = jest.fn().mockImplementation(() => { - return Promise.resolve(dictionaries[0]); - }); - TemplateMenuService.insDictionary = jest.fn().mockImplementation(() => { - return Promise.resolve(200); - }); - TemplateMenuService.deleteDictionary = jest.fn().mockImplementation(() => { - return Promise.resolve(200); - }); - const flushPromises = () => new Promise(setImmediate); - const component = shallow() + test('Test add/replace and delete dictionary requests', async () => { + + const component = shallow() const instance = component.instance(); - instance.getDictionaryElements("DefaultActors"); - instance.clickHandler(); - instance.addReplaceDictionaryRequest(); - instance.deleteDictionaryRequest(); + + const flushPromises = () => new Promise(setImmediate); + + instance.addReplaceDictionaryRequest({name: "newdict", secondLevelDictionary: 0, subDictionaryType: "string"}); + instance.deleteDictionaryRequest("test"); + await flushPromises(); - expect(component.state('dictionaries')).toEqual(dictionaries); + + expect(component.state('currentSelectedDictionary')).toEqual(null); + expect(component.state('dictionaries')).toEqual(TestDictionaries); }); - test('Test adding and deleting dictionaryelements', async () => { - const historyMock = { push: jest.fn() }; - const dictionaries = [ - { - name: "DefaultActors", - secondLevelDictionary: 0, - subDictionaryType: "", - dictionaryElements: [ - { - "shortName": "SDNR", - "name": "SDNR Change", - "description": "SDNR component", - "type": "string", - "createdDate": "2020-06-07T18:57:18.130858Z", - "updatedDate": "2020-06-11T13:10:52.239282Z", - "updatedBy": "admin" - } - ], - createdDate: "2020-06-07T22:21:08.428742Z", - updatedDate: "2020-06-10T00:41:49.122908Z", - updatedBy: "Not found" - } - ]; - TemplateMenuService.getDictionary = jest.fn().mockImplementation(() => { - return Promise.resolve(dictionaries); - }); - TemplateMenuService.insDictionaryElements = jest.fn().mockImplementation(() => { - return Promise.resolve(200); - }); - TemplateMenuService.deleteDictionaryElements = jest.fn().mockImplementation(() => { - return Promise.resolve(200); - }); - const flushPromises = () => new Promise(setImmediate); + test('Test update dictionary row', async () => { + const component = shallow() const instance = component.instance(); - instance.addReplaceDictionaryRequest({ name: "EventDictionary", secondLevelDictionary: "0", subDictionaryType: "string"} ); - instance.deleteDictionaryRequest('EventDictionary'); - await flushPromises(); - expect(component.state('currentSelectedDictionary')).toEqual(null); + const rowData = { name: "newdict", secondLevelDictionary: 0, subDictionaryType: "string" }; + + await expect(instance.updateDictionaryRow(rowData, rowData)).resolves.toEqual(undefined); + + }, 2000); + + test('Test add dictionary row', async () => { + + const addReplaceRequest = jest.spyOn(ManageDictionaries.prototype,'addReplaceDictionaryRequest'); + const component = shallow() + const instance = component.instance(); + const rowData = { name: "newdict", secondLevelDictionary: 0, subDictionaryType: "string" }; + + await instance.addDictionaryRow(rowData); + expect(addReplaceRequest).toHaveBeenCalledWith(rowData); + + }, 2000); + + test('Test add dictionary row with errors name already exists', async () => { + + const component = shallow() + const instance = component.instance(); + let rowData = { name: "test", secondLevelDictionary: 0, subDictionaryType: "" }; + + await expect(instance.addDictionaryRow(rowData)).rejects.toEqual(undefined); + + }, 2000); + + test('Test add dictionary row with errors illegal chars in name', async () => { + + const component = shallow() + const instance = component.instance(); + let rowData = { name: "test@@", secondLevelDictionary: 0, subDictionaryType: "" }; + + await expect(instance.addDictionaryRow(rowData)).rejects.toEqual(undefined); + + }, 2000); + + test('Test update dictionary row with errors illegal chars in name', async () => { + + const component = shallow() + const instance = component.instance(); + let rowData = { name: "test@@", secondLevelDictionary: 0, subDictionaryType: "" }; + + await expect(instance.updateDictionaryRow(rowData)).rejects.toEqual(undefined); + }); + + + test('Test add dictionary row with errors (illegal chars)', async () => { + + const addReplaceRequest = jest.spyOn(ManageDictionaries.prototype,'addReplaceDictionaryRequest'); + const component = shallow() + const instance = component.instance(); + let rowData = { name: "test@@", secondLevelDictionary: 0, subDictionaryType: "" }; + + await expect(instance.addDictionaryRow(rowData)).rejects.toEqual(undefined); + + }, 2000); + + + test('Test delete dictionary row', async () => { + + const deleteRequest = jest.spyOn(ManageDictionaries.prototype,'deleteDictionaryRequest'); + const component = shallow() + const instance = component.instance(); + const rowData = { name: "newdict", secondLevelDictionary: 0, subDictionaryType: "string" }; + + await instance.deleteDictionaryRow(rowData); + expect(deleteRequest).toHaveBeenCalledWith("newdict"); + + }, 2000); + + test('Test handle select dictionary row click', async () => { + + const component = shallow() + const instance = component.instance(); + const rowData = { name: "newdict", secondLevelDictionary: 0, subDictionaryType: "string" }; + + instance.handleDictionaryRowClick("event", rowData); + expect(component.state('currentSelectedDictionary')).toEqual("newdict"); + }, 2000); + + test('Test dictionary element row add, update, delete', async () => { + + const rowData = { + createdBy: "admin", + createdDate: "2020-06-15T13:59:20.467381Z", + description: "Description", + name: "Some Elem", + shortName: "someElem", + type: "string", + updatedBy: "admin", + updatedDate: "2020-06-15T13:59:20.467381Z" + }; + + const component = shallow() + const instance = component.instance(); + + const badRowData = { + description: "Description", + name: "Some Elem", + shortName: "someElem", + type: "string" + }; + + await instance.clickHandler(); + await instance.getDictionaryElements("test"); + + await expect(instance.addDictionaryElementRow(rowData)).resolves.toEqual(undefined); + await expect(instance.updateDictionaryElementRow(rowData, rowData)).resolves.toEqual(undefined); + await expect(instance.deleteDictionaryElementRow(rowData)).resolves.toEqual(undefined); + }); + + test('Test dictionary element row add with errors', async () => { + + const badRowData = { + description: "", + name: "", + shortName: "some#Elem", + type: "" + }; + + const component = shallow() + const instance = component.instance(); + + await expect(instance.addDictionaryElementRow(badRowData)).rejects.toEqual(""); + }); + + test('Test dictionary element update with error illegal name', async () => { + + const badRowData = { + description: "", + name: "test@@", + shortName: "some#Elem", + type: "" + }; + + const component = shallow() + const instance = component.instance(); + + await expect(instance.updateDictionaryElementRow(badRowData)).rejects.toEqual(undefined); + }); + + test('Test dictionary element addition with duplicate name error', async () => { + + const badRowData = { + description: "description", + name: "Alert Type", + shortName: "alertType", + type: "string" + }; + + const component = shallow() + const instance = component.instance(); + + component.setState({ currentSelectedDictionary: 'test' }); + + await instance.getDictionaryElements(); + await expect(instance.addDictionaryElementRow(badRowData)).rejects.toEqual(""); + }); + + test('Test dictionary element addition with empty name error', async () => { + + const badRowData = { + description: "description", + name: "Alert Type", + shortName: "", + type: "string" + }; + + const component = shallow() + const instance = component.instance(); + + component.setState({ currentSelectedDictionary: 'test' }); + + await instance.getDictionaryElements(); + await expect(instance.addDictionaryElementRow(badRowData)).rejects.toEqual(""); + }); + + + it('Test Import CSV Sunny Day', async () => { + + TemplateMenuService.insDictionaryElements = jest.fn().mockImplementation(() => { + return Promise.resolve({ ok: true, status: 200 }); + }); + + let rawCsvData = '"Element Short Name","Element Name","Element Description","Element Type","Sub-Dictionary"\n'; + rawCsvData += '"alertType","Alert Type","Alert Type Description","string","","admin","2020-06-11T13:56:14.927437Z"'; + + let expectedResult = [ + { + description: "Alert Type Description", + name: "Alert Type", + shortName: "alertType", + subDictionary: "", + type: "string" + } + ]; + + const updateDictionaryElementsRequest = jest.spyOn(ManageDictionaries.prototype,'updateDictionaryElementsRequest'); + + const component = shallow() + const instance = component.instance(); + + await expect(instance.importCsvData(rawCsvData)).toEqual(''); + expect(updateDictionaryElementsRequest).toHaveBeenCalledWith(expectedResult); + }); + + it('Test Import CSV Mandatory Field Check Errors', () => { + + let rawCsvData = '"Element Short Name","Element Name","Element Description","Element Type","Sub-Dictionary"\n'; + rawCsvData += '"","","","","","",""'; + + // The empty values for all the fields in row 1 of the rawCsvData will trigger a bunch of errors. + // Getting Enzyme to properly match them with embedded newlines turned out to be impossible + // and maybe not desirable anyway; so our test for "success" here is simply that the + // routine returns a non-empty error string. + + const component = shallow() + const instance = component.instance(); + expect(instance.importCsvData(rawCsvData)).not.toEqual(''); + }); + + it('Test Import CSV Errors in Row Data', async () => { + + TemplateMenuService.insDictionaryElements = jest.fn().mockImplementation(() => { + return Promise.resolve({ ok: true, status: 200 }); + }); + + let rawCsvData = '"Element Short Name","Element Name","Element Description","Element Type","Sub-Dictionary"\n'; + rawCsvData += '"alert@Type","Alert Type","Alert Type Description","strin","subby","admin","2020-06-11T13:56:14.927437Z"'; + + let expectedResult = [ + { + description: "Alert Type Description", + name: "Alert Type", + shortName: "alertType", + subDictionary: "", + type: "string" + } + ]; + + const updateDictionaryElementsRequest = jest.spyOn(ManageDictionaries.prototype,'updateDictionaryElementsRequest'); + + const component = shallow() + const instance = component.instance(); + + await expect(instance.importCsvData(rawCsvData)).not.toEqual(''); }); + it('Test handleClose', () => { fetch.mockImplementationOnce(() => { return Promise.resolve({ @@ -197,7 +443,7 @@ describe('Verify ManageDictionaries', () => { json: () => { return Promise.resolve({ "name": "vtest", - "secondLevelDictionary": "1", + "secondLevelDictionary": 1, "subDictionaryType": "string", "updatedBy": "test", "updatedDate": "05-07-2019 19:09:42" @@ -205,7 +451,6 @@ describe('Verify ManageDictionaries', () => { } }); }); - const historyMock = { push: jest.fn() }; const handleClose = jest.spyOn(ManageDictionaries.prototype,'handleClose'); const component = shallow() component.find('[variant="secondary"]').prop('onClick')(); -- cgit 1.2.3-korg