drveerendra | 5032095 | 2020-03-04 20:30:44 -0500 | [diff] [blame] | 1 | /*- |
| 2 | * ============LICENSE_START======================================================= |
| 3 | * ONAP CLAMP |
| 4 | * ================================================================================ |
| 5 | * Copyright (C) 2019 AT&T Intellectual Property. All rights |
| 6 | * reserved. |
| 7 | * ================================================================================ |
| 8 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 9 | * you may not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
| 11 | * |
| 12 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | * |
| 14 | * Unless required by applicable law or agreed to in writing, software |
| 15 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | * See the License for the specific language governing permissions and |
| 18 | * limitations under the License. |
| 19 | * ============LICENSE_END============================================ |
| 20 | * =================================================================== |
| 21 | * |
| 22 | */ |
| 23 | import React from 'react'; |
| 24 | import { shallow } from 'enzyme'; |
| 25 | import { mount } from 'enzyme'; |
| 26 | import { render } from 'enzyme'; |
| 27 | import ManageDictionaries from './ManageDictionaries'; |
| 28 | import TemplateMenuService from '../../../api/TemplateService' |
| 29 | |
Ted Humphrey | b80ded0 | 2020-06-17 21:40:35 -0400 | [diff] [blame] | 30 | const TestDictionaryElements = { |
| 31 | name: "test", |
| 32 | secondLevelDictionary: 0, |
| 33 | subDictionaryType: "", |
| 34 | dictionaryElements: [ |
| 35 | { |
| 36 | shortName: "alertType", |
| 37 | name: "Alert Type", |
| 38 | description: "Type of Alert", |
| 39 | type: "string", |
| 40 | subDictionary: "", |
| 41 | createdDate: "2020-06-12T13:58:51.443931Z", |
| 42 | updatedDate: "2020-06-13T16:27:57.084870Z", |
| 43 | updatedBy: "admin", |
| 44 | createdBy: "admin" |
| 45 | } |
| 46 | ] |
| 47 | }; |
| 48 | |
| 49 | const TestDictionaries = |
| 50 | [ |
| 51 | { |
| 52 | name: "test", |
| 53 | secondLevelDictionary: 0, |
| 54 | subDictionaryType: "string", |
| 55 | dictionaryElements: [ TestDictionaryElements ], |
| 56 | createdDate: "2020-06-14T21:00:33.231166Z", |
| 57 | updatedDate: "2020-06-14T21:00:33.231166Z", |
| 58 | updatedBy: "admin", |
| 59 | createdBy: "admin" |
| 60 | }, |
| 61 | { |
| 62 | name: "testSub1", |
| 63 | secondLevelDictionary: 1, |
| 64 | subDictionaryType: "string", |
| 65 | dictionaryElements: [ |
| 66 | { |
| 67 | shortName: "subElem", |
| 68 | name: "Sub Element", |
| 69 | description: "Sub Element Description", |
| 70 | type: "string", |
| 71 | createdDate: "2020-06-14T21:04:44.402287Z", |
| 72 | updatedDate: "2020-06-14T21:04:44.402287Z", |
| 73 | updatedBy: "admin", |
| 74 | createdBy: "admin" |
| 75 | } |
| 76 | ], |
| 77 | createdDate: "2020-06-14T21:01:16.390250Z", |
| 78 | updatedDate: "2020-06-14T21:01:16.390250Z", |
| 79 | updatedBy: "admin", |
| 80 | createdBy: "admin" |
| 81 | } |
| 82 | ]; |
| 83 | |
| 84 | |
| 85 | const historyMock = { push: jest.fn() }; |
| 86 | |
| 87 | let errorMessage = ''; |
| 88 | |
| 89 | window.alert = jest.fn().mockImplementation((mesg) => { errorMessage = mesg ; return }); |
| 90 | |
| 91 | TemplateMenuService.getDictionary = jest.fn().mockImplementation(() => { |
| 92 | return Promise.resolve(TestDictionaries); |
| 93 | }); |
| 94 | |
| 95 | TemplateMenuService.insDictionary = jest.fn().mockImplementation(() => { |
| 96 | return Promise.resolve({ ok: true, status: 200 }); |
| 97 | }); |
| 98 | |
| 99 | TemplateMenuService.deleteDictionary = jest.fn().mockImplementation(() => { |
| 100 | return Promise.resolve("200"); |
| 101 | }); |
| 102 | |
| 103 | TemplateMenuService.getDictionaryElements = jest.fn().mockImplementation(() => { |
| 104 | return Promise.resolve(TestDictionaryElements); |
| 105 | }); |
| 106 | |
| 107 | TemplateMenuService.deleteDictionaryElements = jest.fn().mockImplementation(() => { |
| 108 | return Promise.resolve("200"); |
| 109 | }); |
| 110 | |
| 111 | TemplateMenuService.insDictionaryElements = jest.fn().mockImplementation(() => { |
| 112 | return Promise.resolve("200"); |
| 113 | }); |
| 114 | |
| 115 | |
drveerendra | 5032095 | 2020-03-04 20:30:44 -0500 | [diff] [blame] | 116 | describe('Verify ManageDictionaries', () => { |
Ted Humphrey | b80ded0 | 2020-06-17 21:40:35 -0400 | [diff] [blame] | 117 | |
drveerendra | 5032095 | 2020-03-04 20:30:44 -0500 | [diff] [blame] | 118 | beforeEach(() => { |
| 119 | fetch.resetMocks(); |
| 120 | }); |
| 121 | |
| 122 | it('Test API Successful', () => { |
| 123 | fetch.mockImplementationOnce(() => { |
| 124 | return Promise.resolve({ |
| 125 | ok: true, |
| 126 | status: 200, |
| 127 | json: () => { |
| 128 | return Promise.resolve({ |
| 129 | "name": "vtest", |
Ted Humphrey | b80ded0 | 2020-06-17 21:40:35 -0400 | [diff] [blame] | 130 | "secondLevelDictionary": 1, |
drveerendra | 5032095 | 2020-03-04 20:30:44 -0500 | [diff] [blame] | 131 | "subDictionaryType": "string", |
| 132 | "updatedBy": "test", |
| 133 | "updatedDate": "05-07-2019 19:09:42" |
| 134 | }); |
| 135 | } |
| 136 | }); |
| 137 | }); |
| 138 | const component = shallow(<ManageDictionaries />); |
| 139 | expect(component).toMatchSnapshot(); |
| 140 | }); |
| 141 | |
| 142 | it('Test API Exception', () => { |
| 143 | fetch.mockImplementationOnce(() => { |
| 144 | return Promise.resolve({ |
| 145 | ok: false, |
| 146 | status: 500, |
| 147 | json: () => { |
| 148 | return Promise.resolve({ |
| 149 | "name": "vtest", |
Ted Humphrey | b80ded0 | 2020-06-17 21:40:35 -0400 | [diff] [blame] | 150 | "secondLevelDictionary": 1, |
drveerendra | 5032095 | 2020-03-04 20:30:44 -0500 | [diff] [blame] | 151 | "subDictionaryType": "string", |
Ted Humphrey | b56cb11 | 2020-06-12 03:17:35 -0400 | [diff] [blame] | 152 | "updatedBy": "test", |
| 153 | "updatedDate": "05-07-2019 19:09:42" |
drveerendra | 5032095 | 2020-03-04 20:30:44 -0500 | [diff] [blame] | 154 | }); |
| 155 | } |
| 156 | }); |
| 157 | }); |
| 158 | const component = shallow(<ManageDictionaries />); |
| 159 | }); |
| 160 | |
drveerendra | 5032095 | 2020-03-04 20:30:44 -0500 | [diff] [blame] | 161 | it('Test Table icons', () => { |
Ted Humphrey | b80ded0 | 2020-06-17 21:40:35 -0400 | [diff] [blame] | 162 | |
drveerendra | 5032095 | 2020-03-04 20:30:44 -0500 | [diff] [blame] | 163 | const component = mount(<ManageDictionaries />); |
| 164 | expect(component.find('[className="MuiSelect-icon MuiTablePagination-selectIcon"]')).toBeTruthy(); |
| 165 | }); |
| 166 | |
Ted Humphrey | b80ded0 | 2020-06-17 21:40:35 -0400 | [diff] [blame] | 167 | test('Test add/replace and delete dictionary requests', async () => { |
drveerendra | 5032095 | 2020-03-04 20:30:44 -0500 | [diff] [blame] | 168 | |
drveerendra | 5032095 | 2020-03-04 20:30:44 -0500 | [diff] [blame] | 169 | const component = shallow(<ManageDictionaries history={historyMock}/>) |
drveerendra | 5032095 | 2020-03-04 20:30:44 -0500 | [diff] [blame] | 170 | const instance = component.instance(); |
Ted Humphrey | b80ded0 | 2020-06-17 21:40:35 -0400 | [diff] [blame] | 171 | |
| 172 | const flushPromises = () => new Promise(setImmediate); |
| 173 | |
| 174 | instance.addReplaceDictionaryRequest({name: "newdict", secondLevelDictionary: 0, subDictionaryType: "string"}); |
| 175 | instance.deleteDictionaryRequest("test"); |
| 176 | |
drveerendra | 5032095 | 2020-03-04 20:30:44 -0500 | [diff] [blame] | 177 | await flushPromises(); |
Ted Humphrey | b80ded0 | 2020-06-17 21:40:35 -0400 | [diff] [blame] | 178 | |
Ted Humphrey | b56cb11 | 2020-06-12 03:17:35 -0400 | [diff] [blame] | 179 | expect(component.state('currentSelectedDictionary')).toEqual(null); |
Ted Humphrey | b80ded0 | 2020-06-17 21:40:35 -0400 | [diff] [blame] | 180 | expect(component.state('dictionaries')).toEqual(TestDictionaries); |
drveerendra | 5032095 | 2020-03-04 20:30:44 -0500 | [diff] [blame] | 181 | }); |
| 182 | |
Ted Humphrey | b80ded0 | 2020-06-17 21:40:35 -0400 | [diff] [blame] | 183 | test('Test update dictionary row', async () => { |
| 184 | |
| 185 | const component = shallow(<ManageDictionaries history={historyMock}/>) |
| 186 | const instance = component.instance(); |
| 187 | const rowData = { name: "newdict", secondLevelDictionary: 0, subDictionaryType: "string" }; |
| 188 | |
| 189 | await expect(instance.updateDictionaryRow(rowData, rowData)).resolves.toEqual(undefined); |
| 190 | |
| 191 | }, 2000); |
| 192 | |
| 193 | test('Test add dictionary row', async () => { |
| 194 | |
| 195 | const addReplaceRequest = jest.spyOn(ManageDictionaries.prototype,'addReplaceDictionaryRequest'); |
| 196 | const component = shallow(<ManageDictionaries />) |
| 197 | const instance = component.instance(); |
| 198 | const rowData = { name: "newdict", secondLevelDictionary: 0, subDictionaryType: "string" }; |
| 199 | |
| 200 | await instance.addDictionaryRow(rowData); |
| 201 | expect(addReplaceRequest).toHaveBeenCalledWith(rowData); |
| 202 | |
| 203 | }, 2000); |
| 204 | |
| 205 | test('Test add dictionary row with errors name already exists', async () => { |
| 206 | |
| 207 | const component = shallow(<ManageDictionaries />) |
| 208 | const instance = component.instance(); |
| 209 | let rowData = { name: "test", secondLevelDictionary: 0, subDictionaryType: "" }; |
| 210 | |
| 211 | await expect(instance.addDictionaryRow(rowData)).rejects.toEqual(undefined); |
| 212 | |
| 213 | }, 2000); |
| 214 | |
| 215 | test('Test add dictionary row with errors illegal chars in name', async () => { |
| 216 | |
| 217 | const component = shallow(<ManageDictionaries />) |
| 218 | const instance = component.instance(); |
| 219 | let rowData = { name: "test@@", secondLevelDictionary: 0, subDictionaryType: "" }; |
| 220 | |
| 221 | await expect(instance.addDictionaryRow(rowData)).rejects.toEqual(undefined); |
| 222 | |
| 223 | }, 2000); |
| 224 | |
| 225 | test('Test update dictionary row with errors illegal chars in name', async () => { |
| 226 | |
| 227 | const component = shallow(<ManageDictionaries />) |
| 228 | const instance = component.instance(); |
| 229 | let rowData = { name: "test@@", secondLevelDictionary: 0, subDictionaryType: "" }; |
| 230 | |
| 231 | await expect(instance.updateDictionaryRow(rowData)).rejects.toEqual(undefined); |
| 232 | }); |
| 233 | |
| 234 | |
| 235 | test('Test add dictionary row with errors (illegal chars)', async () => { |
| 236 | |
| 237 | const addReplaceRequest = jest.spyOn(ManageDictionaries.prototype,'addReplaceDictionaryRequest'); |
| 238 | const component = shallow(<ManageDictionaries />) |
| 239 | const instance = component.instance(); |
| 240 | let rowData = { name: "test@@", secondLevelDictionary: 0, subDictionaryType: "" }; |
| 241 | |
| 242 | await expect(instance.addDictionaryRow(rowData)).rejects.toEqual(undefined); |
| 243 | |
| 244 | }, 2000); |
| 245 | |
| 246 | |
| 247 | test('Test delete dictionary row', async () => { |
| 248 | |
| 249 | const deleteRequest = jest.spyOn(ManageDictionaries.prototype,'deleteDictionaryRequest'); |
| 250 | const component = shallow(<ManageDictionaries />) |
| 251 | const instance = component.instance(); |
| 252 | const rowData = { name: "newdict", secondLevelDictionary: 0, subDictionaryType: "string" }; |
| 253 | |
| 254 | await instance.deleteDictionaryRow(rowData); |
| 255 | expect(deleteRequest).toHaveBeenCalledWith("newdict"); |
| 256 | |
| 257 | }, 2000); |
| 258 | |
| 259 | test('Test handle select dictionary row click', async () => { |
| 260 | |
| 261 | const component = shallow(<ManageDictionaries />) |
| 262 | const instance = component.instance(); |
| 263 | const rowData = { name: "newdict", secondLevelDictionary: 0, subDictionaryType: "string" }; |
| 264 | |
| 265 | instance.handleDictionaryRowClick("event", rowData); |
| 266 | expect(component.state('currentSelectedDictionary')).toEqual("newdict"); |
| 267 | }, 2000); |
| 268 | |
| 269 | test('Test dictionary element row add, update, delete', async () => { |
| 270 | |
| 271 | const rowData = { |
| 272 | createdBy: "admin", |
| 273 | createdDate: "2020-06-15T13:59:20.467381Z", |
| 274 | description: "Description", |
| 275 | name: "Some Elem", |
| 276 | shortName: "someElem", |
| 277 | type: "string", |
| 278 | updatedBy: "admin", |
| 279 | updatedDate: "2020-06-15T13:59:20.467381Z" |
| 280 | }; |
| 281 | |
| 282 | const component = shallow(<ManageDictionaries/>) |
| 283 | const instance = component.instance(); |
| 284 | |
| 285 | const badRowData = { |
| 286 | description: "Description", |
| 287 | name: "Some Elem", |
| 288 | shortName: "someElem", |
| 289 | type: "string" |
| 290 | }; |
| 291 | |
| 292 | await instance.clickHandler(); |
| 293 | await instance.getDictionaryElements("test"); |
| 294 | |
| 295 | await expect(instance.addDictionaryElementRow(rowData)).resolves.toEqual(undefined); |
| 296 | await expect(instance.updateDictionaryElementRow(rowData, rowData)).resolves.toEqual(undefined); |
| 297 | await expect(instance.deleteDictionaryElementRow(rowData)).resolves.toEqual(undefined); |
| 298 | }); |
| 299 | |
| 300 | test('Test dictionary element row add with errors', async () => { |
| 301 | |
| 302 | const badRowData = { |
| 303 | description: "", |
| 304 | name: "", |
| 305 | shortName: "some#Elem", |
| 306 | type: "" |
| 307 | }; |
| 308 | |
| 309 | const component = shallow(<ManageDictionaries/>) |
| 310 | const instance = component.instance(); |
| 311 | |
| 312 | await expect(instance.addDictionaryElementRow(badRowData)).rejects.toEqual(""); |
| 313 | }); |
| 314 | |
| 315 | test('Test dictionary element update with error illegal name', async () => { |
| 316 | |
| 317 | const badRowData = { |
| 318 | description: "", |
| 319 | name: "test@@", |
| 320 | shortName: "some#Elem", |
| 321 | type: "" |
| 322 | }; |
| 323 | |
| 324 | const component = shallow(<ManageDictionaries/>) |
| 325 | const instance = component.instance(); |
| 326 | |
| 327 | await expect(instance.updateDictionaryElementRow(badRowData)).rejects.toEqual(undefined); |
| 328 | }); |
| 329 | |
| 330 | test('Test dictionary element addition with duplicate name error', async () => { |
| 331 | |
| 332 | const badRowData = { |
| 333 | description: "description", |
| 334 | name: "Alert Type", |
| 335 | shortName: "alertType", |
| 336 | type: "string" |
| 337 | }; |
| 338 | |
| 339 | const component = shallow(<ManageDictionaries/>) |
| 340 | const instance = component.instance(); |
| 341 | |
| 342 | component.setState({ currentSelectedDictionary: 'test' }); |
| 343 | |
| 344 | await instance.getDictionaryElements(); |
| 345 | await expect(instance.addDictionaryElementRow(badRowData)).rejects.toEqual(""); |
| 346 | }); |
| 347 | |
| 348 | test('Test dictionary element addition with empty name error', async () => { |
| 349 | |
| 350 | const badRowData = { |
| 351 | description: "description", |
| 352 | name: "Alert Type", |
| 353 | shortName: "", |
| 354 | type: "string" |
| 355 | }; |
| 356 | |
| 357 | const component = shallow(<ManageDictionaries/>) |
| 358 | const instance = component.instance(); |
| 359 | |
| 360 | component.setState({ currentSelectedDictionary: 'test' }); |
| 361 | |
| 362 | await instance.getDictionaryElements(); |
| 363 | await expect(instance.addDictionaryElementRow(badRowData)).rejects.toEqual(""); |
| 364 | }); |
| 365 | |
| 366 | |
| 367 | it('Test Import CSV Sunny Day', async () => { |
| 368 | |
| 369 | TemplateMenuService.insDictionaryElements = jest.fn().mockImplementation(() => { |
| 370 | return Promise.resolve({ ok: true, status: 200 }); |
| 371 | }); |
| 372 | |
| 373 | let rawCsvData = '"Element Short Name","Element Name","Element Description","Element Type","Sub-Dictionary"\n'; |
| 374 | rawCsvData += '"alertType","Alert Type","Alert Type Description","string","","admin","2020-06-11T13:56:14.927437Z"'; |
| 375 | |
| 376 | let expectedResult = [ |
| 377 | { |
| 378 | description: "Alert Type Description", |
| 379 | name: "Alert Type", |
| 380 | shortName: "alertType", |
| 381 | subDictionary: "", |
| 382 | type: "string" |
| 383 | } |
| 384 | ]; |
| 385 | |
| 386 | const updateDictionaryElementsRequest = jest.spyOn(ManageDictionaries.prototype,'updateDictionaryElementsRequest'); |
| 387 | |
| 388 | const component = shallow(<ManageDictionaries />) |
| 389 | const instance = component.instance(); |
| 390 | |
| 391 | await expect(instance.importCsvData(rawCsvData)).toEqual(''); |
| 392 | expect(updateDictionaryElementsRequest).toHaveBeenCalledWith(expectedResult); |
| 393 | }); |
| 394 | |
| 395 | it('Test Import CSV Mandatory Field Check Errors', () => { |
| 396 | |
| 397 | let rawCsvData = '"Element Short Name","Element Name","Element Description","Element Type","Sub-Dictionary"\n'; |
| 398 | rawCsvData += '"","","","","","",""'; |
| 399 | |
| 400 | // The empty values for all the fields in row 1 of the rawCsvData will trigger a bunch of errors. |
| 401 | // Getting Enzyme to properly match them with embedded newlines turned out to be impossible |
| 402 | // and maybe not desirable anyway; so our test for "success" here is simply that the |
| 403 | // routine returns a non-empty error string. |
| 404 | |
| 405 | const component = shallow(<ManageDictionaries />) |
| 406 | const instance = component.instance(); |
| 407 | expect(instance.importCsvData(rawCsvData)).not.toEqual(''); |
| 408 | }); |
| 409 | |
| 410 | it('Test Import CSV Errors in Row Data', async () => { |
| 411 | |
| 412 | TemplateMenuService.insDictionaryElements = jest.fn().mockImplementation(() => { |
| 413 | return Promise.resolve({ ok: true, status: 200 }); |
| 414 | }); |
| 415 | |
| 416 | let rawCsvData = '"Element Short Name","Element Name","Element Description","Element Type","Sub-Dictionary"\n'; |
| 417 | rawCsvData += '"alert@Type","Alert Type","Alert Type Description","strin","subby","admin","2020-06-11T13:56:14.927437Z"'; |
| 418 | |
| 419 | let expectedResult = [ |
| 420 | { |
| 421 | description: "Alert Type Description", |
| 422 | name: "Alert Type", |
| 423 | shortName: "alertType", |
| 424 | subDictionary: "", |
| 425 | type: "string" |
| 426 | } |
| 427 | ]; |
| 428 | |
| 429 | const updateDictionaryElementsRequest = jest.spyOn(ManageDictionaries.prototype,'updateDictionaryElementsRequest'); |
| 430 | |
| 431 | const component = shallow(<ManageDictionaries />) |
| 432 | const instance = component.instance(); |
| 433 | |
| 434 | await expect(instance.importCsvData(rawCsvData)).not.toEqual(''); |
| 435 | }); |
| 436 | |
| 437 | |
drveerendra | 5032095 | 2020-03-04 20:30:44 -0500 | [diff] [blame] | 438 | it('Test handleClose', () => { |
| 439 | fetch.mockImplementationOnce(() => { |
| 440 | return Promise.resolve({ |
| 441 | ok: true, |
| 442 | status: 200, |
| 443 | json: () => { |
| 444 | return Promise.resolve({ |
| 445 | "name": "vtest", |
Ted Humphrey | b80ded0 | 2020-06-17 21:40:35 -0400 | [diff] [blame] | 446 | "secondLevelDictionary": 1, |
drveerendra | 5032095 | 2020-03-04 20:30:44 -0500 | [diff] [blame] | 447 | "subDictionaryType": "string", |
Ted Humphrey | b56cb11 | 2020-06-12 03:17:35 -0400 | [diff] [blame] | 448 | "updatedBy": "test", |
| 449 | "updatedDate": "05-07-2019 19:09:42" |
drveerendra | 5032095 | 2020-03-04 20:30:44 -0500 | [diff] [blame] | 450 | }); |
| 451 | } |
| 452 | }); |
| 453 | }); |
drveerendra | 5032095 | 2020-03-04 20:30:44 -0500 | [diff] [blame] | 454 | const handleClose = jest.spyOn(ManageDictionaries.prototype,'handleClose'); |
| 455 | const component = shallow(<ManageDictionaries history={historyMock} />) |
| 456 | component.find('[variant="secondary"]').prop('onClick')(); |
| 457 | expect(handleClose).toHaveBeenCalledTimes(1); |
| 458 | expect(component.state('show')).toEqual(false); |
| 459 | expect(historyMock.push.mock.calls[0]).toEqual([ '/']); |
| 460 | handleClose.mockClear(); |
| 461 | }); |
| 462 | }); |