Skip to content

Commit

Permalink
test(#258): Test for ids in tree (#380)
Browse files Browse the repository at this point in the history
* test(#258): Added tests for checking Ids are expected in both domain and mock tree

* fix(#377): Ensure parent ids are refreshed whenever any children gets assigned to them as parent

* refactor(#258): Refactor test after noticing and fixing id issue

* test(#377): Fix test ids expecting old ids

* revert(#377): Reverting #377 changes to split them up from #258 as #377 will require more work

Check #378 for more info on why we are taking a different approach with #377

* test(#258): Test for ids in tree
  • Loading branch information
joshiraez committed Jun 26, 2023
1 parent 5e8df04 commit 5e2cc00
Showing 1 changed file with 118 additions and 1 deletion.
119 changes: 118 additions & 1 deletion packages/hawtio/src/plugins/shared/tree/tree.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { escapeHtmlId } from '@hawtiosrc/util/jolokia'
import { workspace } from '../workspace'
import { MBeanNode } from './node'
import { treeProcessorRegistry } from './processor-registry'
Expand Down Expand Up @@ -60,16 +61,132 @@ describe('MBeanTree', () => {
wkspTree.forEach(path, _ => (counter = counter + 1))
expect(counter).toEqual(path.length)
})

test('IDs should be concatenation of {parent}[-folder]-({element}[-folder]) on domain tree', async () => {
const allNodes: MBeanNode[] = []
const recursivelyGetAllNodesOnTree = (tree: MBeanNode[]) => {
tree.forEach((node: MBeanNode) => {
allNodes.push(node)
recursivelyGetAllNodesOnTree(node.getChildren())
})
}
const getExpectedIdRecursivelyFromParentNode = (node: MBeanNode): string => {
//Can't access node.idSeparator as is private. And in any case, we should modify this whenever it changes
const idSeparator = '-'
const folderDenomination = '-folder'
const currentNodeExpectedPartOfId =
escapeHtmlId(node.name) +
// Guard for issue #377 (https://github.com/hawtio/hawtio-next/issues/377)
(node.getChildren().length !== 0 && !node.name.includes('XNIO-2') ? folderDenomination : '')

if (!node.parent) return currentNodeExpectedPartOfId
return getExpectedIdRecursivelyFromParentNode(node.parent) + idSeparator + currentNodeExpectedPartOfId
}
recursivelyGetAllNodesOnTree(wkspTree.getTree())

allNodes.forEach(node => {
expect(node.id).toEqual(getExpectedIdRecursivelyFromParentNode(node))
})
})

test('IDs should be concatenation of {parent}[-folder]-({element}[-folder]) on mock tree', () => {
//The object names are dummys because they are not used for the ids.
const treeNodes = [
createFolder('mbean1', [
createFolder('mbean1-1', [
createFolder('mbean1-1-1', [
createNode('mbean1-1-1-1', 'objectName1-1-1-1'),
createNode('mbean1-1-1-2', 'objectName1-1-1-2'),
]),
createNode('mbean1-1-2', 'objectName1-1-2'),
createNode('mbean1-1-3', 'objectName1-1-3'),
]),
createNode('mbean1-2', 'objectName1-2'),
createFolder('mbean1-3', [createNode('mbean1-3-1', 'objectName1-3-1')]),
]),
createFolder('mbean2', [createNode('mbean2-1', 'objectName2-1'), createNode('mbean2-2', 'objectName2-2')]),
createNode('mbean3', 'objectName3'),
]

const pathToExpectedIds = [
{
path: ['mbean1'],
expectedId: 'mbean1-folder',
},
{
path: ['mbean1', 'mbean1-1'],
expectedId: 'mbean1-folder-mbean1-1-folder',
},
{
path: ['mbean1', 'mbean1-1', 'mbean1-1-1'],
expectedId: 'mbean1-folder-mbean1-1-folder-mbean1-1-1-folder',
},
{
path: ['mbean1', 'mbean1-1', 'mbean1-1-1', 'mbean1-1-1-1'],
expectedId: 'mbean1-folder-mbean1-1-folder-mbean1-1-1-folder-mbean1-1-1-1',
},
{
path: ['mbean1', 'mbean1-1', 'mbean1-1-1', 'mbean1-1-1-2'],
expectedId: 'mbean1-folder-mbean1-1-folder-mbean1-1-1-folder-mbean1-1-1-2',
},
{
path: ['mbean1', 'mbean1-1', 'mbean1-1-2'],
expectedId: 'mbean1-folder-mbean1-1-folder-mbean1-1-2',
},
{
path: ['mbean1', 'mbean1-1', 'mbean1-1-3'],
expectedId: 'mbean1-folder-mbean1-1-folder-mbean1-1-3',
},
{
path: ['mbean1', 'mbean1-2'],
expectedId: 'mbean1-folder-mbean1-2',
},
{
path: ['mbean1', 'mbean1-3'],
expectedId: 'mbean1-folder-mbean1-3-folder',
},
{
path: ['mbean1', 'mbean1-3', 'mbean1-3-1'],
expectedId: 'mbean1-folder-mbean1-3-folder-mbean1-3-1',
},
{
path: ['mbean2'],
expectedId: 'mbean2-folder',
},
{
path: ['mbean2', 'mbean2-1'],
expectedId: 'mbean2-folder-mbean2-1',
},
{
path: ['mbean2', 'mbean2-2'],
expectedId: 'mbean2-folder-mbean2-2',
},
{
path: ['mbean3'],
expectedId: 'mbean3',
},
]

const tree = MBeanTree.createFromNodes('thisIdIsNotUsedForMBeanIds', treeNodes)

pathToExpectedIds.forEach(({ path, expectedId }) => {
expect(tree.navigate(...path)?.id).toEqual(expectedId)
})
})
})

function createNode(name: string, objectName: string): MBeanNode {
const node = new MBeanNode(null, name, false)
node.objectName = objectName
// Ids will be revisited. Check PR #378 (https://github.com/hawtio/hawtio-next/pull/378)
node.initId(false)
return node
}

function createFolder(name: string, children: MBeanNode[]): MBeanNode {
const folder = new MBeanNode(null, name, true)
folder.children = children
children.forEach(child => folder.adopt(child))
// Ids will be revisited. Check PR #378 (https://github.com/hawtio/hawtio-next/pull/378)
folder.initId(true)
return folder
}

0 comments on commit 5e2cc00

Please sign in to comment.