diff --git a/spring-boot-admin-server-ui/src/main/frontend/views/instances/sbomdependencytrees/sbomUtils.spec.ts b/spring-boot-admin-server-ui/src/main/frontend/views/instances/sbomdependencytrees/sbomUtils.spec.ts index 39a49ee122f..c1a6cae9365 100644 --- a/spring-boot-admin-server-ui/src/main/frontend/views/instances/sbomdependencytrees/sbomUtils.spec.ts +++ b/spring-boot-admin-server-ui/src/main/frontend/views/instances/sbomdependencytrees/sbomUtils.spec.ts @@ -3,12 +3,78 @@ import { describe, expect, it } from 'vitest'; import { SbomDependency, filterTree, + normalizeData, normalizeNodeName, retrieveChildren, } from './sbomUtils'; import { DependencyTreeData } from '@/views/instances/sbomdependencytrees/dependencyTree'; +describe('normalizeData', () => { + const root = { ref: 'app', dependsOn: ['a', 'b'] }; + const libraries = [ + { ref: 'a', dependsOn: ['c'] }, + { ref: 'b', dependsOn: [] }, + { ref: 'c', dependsOn: [] }, + ]; + + it.each([0, 1, 3])('uses the metadata root at index %s', (index) => { + const dependencies = [...libraries]; + dependencies.splice(index, 0, root); + const original = structuredClone(dependencies); + + expect(normalizeData(dependencies, 'app')).toEqual({ + name: 'app', + children: [ + { name: 'a', children: [{ name: 'c', children: undefined }] }, + { name: 'b', children: undefined }, + ], + }); + expect(dependencies).toEqual(original); + }); + + it('matches the full reference before normalizing its label', () => { + const jar = 'pkg:maven/example/app@1.0?type=jar'; + const pom = 'pkg:maven/example/app@1.0?type=pom'; + expect( + normalizeData( + [ + { ref: pom, dependsOn: ['wrong'] }, + { ref: jar, dependsOn: ['a'] }, + ], + jar, + ), + ).toEqual({ + name: 'example/app@1.0', + children: [{ name: 'a', children: undefined }], + }); + }); + + it.each([undefined, '', 'missing'])('does not guess a root for %s', (ref) => { + expect(normalizeData([root, ...libraries], ref)).toBeNull(); + }); + + it('handles an empty dependency graph', () => { + expect(normalizeData([], 'app')).toBeNull(); + }); + + it.each([[], undefined])('handles root dependencies %s', (dependsOn) => { + expect(normalizeData([{ ref: 'app', dependsOn }], 'app')).toEqual({ + name: 'app', + children: undefined, + }); + }); + + it('handles a child whose dependency list is absent', () => { + expect( + normalizeData([{ ref: 'app', dependsOn: ['a'] }, { ref: 'a' }], 'app'), + ).toEqual({ + name: 'app', + children: [{ name: 'a', children: undefined }], + }); + }); +}); + describe('normalizeNodeName', () => { it('should remove the path before the first forward slash', () => { const input = 'path/to/resource'; diff --git a/spring-boot-admin-server-ui/src/main/frontend/views/instances/sbomdependencytrees/sbomUtils.ts b/spring-boot-admin-server-ui/src/main/frontend/views/instances/sbomdependencytrees/sbomUtils.ts index c2cce177fbd..f581848abe6 100644 --- a/spring-boot-admin-server-ui/src/main/frontend/views/instances/sbomdependencytrees/sbomUtils.ts +++ b/spring-boot-admin-server-ui/src/main/frontend/views/instances/sbomdependencytrees/sbomUtils.ts @@ -12,7 +12,7 @@ export const normalizeNodeName = (name: string): string => function getChildren(sbomDependencies: SbomDependency[], item: string) { return retrieveChildren( sbomDependencies - .filter((node) => node.ref === item && node.dependsOn.length) + .filter((node) => node.ref === item && node.dependsOn?.length) .flatMap((node) => node.dependsOn), sbomDependencies, ); @@ -32,22 +32,25 @@ export const retrieveChildren = ( export const normalizeData = ( sbomDependencies: SbomDependency[], -): DependencyTreeData => { - const children = sbomDependencies[0].dependsOn.map((item) => ({ - name: normalizeNodeName(item), - children: getChildren(sbomDependencies, item), - })); + rootRef: string | undefined, +): DependencyTreeData | null => { + if (!rootRef) return null; + + const root = sbomDependencies.find((node) => node.ref === rootRef); + if (!root) return null; return { - name: normalizeNodeName(sbomDependencies[0].ref), - children, + name: normalizeNodeName(root.ref), + children: retrieveChildren(root.dependsOn ?? [], sbomDependencies), }; }; export const filterTree = ( - treeData: DependencyTreeData, + treeData: DependencyTreeData | null, filter: string, ): DependencyTreeData | null => { + if (!treeData) return null; + if (!filter || filter.trim() === '') { return treeData; } diff --git a/spring-boot-admin-server-ui/src/main/frontend/views/instances/sbomdependencytrees/tree.spec.ts b/spring-boot-admin-server-ui/src/main/frontend/views/instances/sbomdependencytrees/tree.spec.ts index 0f161035134..40cd8a5c320 100644 --- a/spring-boot-admin-server-ui/src/main/frontend/views/instances/sbomdependencytrees/tree.spec.ts +++ b/spring-boot-admin-server-ui/src/main/frontend/views/instances/sbomdependencytrees/tree.spec.ts @@ -1,7 +1,9 @@ import { RenderResult, screen, waitFor } from '@testing-library/vue'; +import { HttpResponse, http } from 'msw'; import { beforeEach, describe, expect, it, vi } from 'vitest'; import { applications } from '@/mocks/applications/data'; +import { server } from '@/mocks/server'; import Application from '@/services/application'; import Instance from '@/services/instance'; import { render } from '@/test-utils'; @@ -29,6 +31,89 @@ const setUnknownFilter = async ( }); }; +describe('TreeGraph root selection', () => { + beforeEach(() => vi.useFakeTimers({ ignoreMissingTimers: true })); + afterEach(() => vi.useRealTimers()); + + const application = new Application(applications[0]); + const instance: Instance = application.instances[0]; + const dependencies = [ + { ref: 'library-a', dependsOn: ['library-c'] }, + { ref: 'root-app', dependsOn: ['library-a', 'library-b'] }, + { ref: 'library-b', dependsOn: [] }, + { ref: 'library-c', dependsOn: [] }, + ]; + + it('uses the metadata root from the fetched SBOM', async () => { + server.use( + http.get('/instances/:instanceId/actuator/sbom/application', () => + HttpResponse.json({ + metadata: { component: { 'bom-ref': 'root-app' } }, + dependencies, + }), + ), + ); + render(TreeGraph, { props: { instance, sbomId: 'application' } }); + + expect(await screen.findAllByText('root-app')).not.toHaveLength(0); + expect(screen.getAllByText('library-a')).not.toHaveLength(0); + expect(screen.getAllByText('library-b')).not.toHaveLength(0); + }); + + it('renders the resolved root after clearing an initially unmatched filter', async () => { + server.use( + http.get('/instances/:instanceId/actuator/sbom/application', () => + HttpResponse.json({ + metadata: { component: { 'bom-ref': 'root-app' } }, + dependencies, + }), + ), + ); + const component = render(TreeGraph, { + props: { instance, sbomId: 'application', filter: 'no-match' }, + }); + await waitFor(() => + expect( + screen.queryByTestId('instance-section-loading-spinner'), + ).not.toBeInTheDocument(), + ); + expect(screen.queryByTestId('treecontainer-svg')).not.toBeInTheDocument(); + expect( + screen.queryByText('instances.dependencies.no_data_provided'), + ).not.toBeInTheDocument(); + + await component.rerender({ filter: '' }); + vi.advanceTimersByTime(2000); + + expect(await screen.findAllByText('root-app')).not.toHaveLength(0); + expect(screen.getAllByText('library-b')).not.toHaveLength(0); + }); + + it.each([ + { dependencies }, + { metadata: { component: {} }, dependencies }, + { metadata: { component: { 'bom-ref': 'missing' } }, dependencies }, + { metadata: { component: { 'bom-ref': 'root-app' } } }, + { metadata: { component: { 'bom-ref': 'root-app' } }, dependencies: [] }, + ])( + 'shows an empty state when the root cannot be resolved: %j', + async (sbom) => { + server.use( + http.get('/instances/:instanceId/actuator/sbom/application', () => + HttpResponse.json(sbom), + ), + ); + render(TreeGraph, { props: { instance, sbomId: 'application' } }); + + expect( + await screen.findByText('instances.dependencies.no_data_provided'), + ).toBeInTheDocument(); + expect(screen.queryByTestId('treecontainer-svg')).not.toBeInTheDocument(); + expect(screen.queryByText('library-a')).not.toBeInTheDocument(); + }, + ); +}); + describe('TreeGraph', () => { const application = new Application(applications[0]); const instance: Instance = application.instances[0]; diff --git a/spring-boot-admin-server-ui/src/main/frontend/views/instances/sbomdependencytrees/tree.vue b/spring-boot-admin-server-ui/src/main/frontend/views/instances/sbomdependencytrees/tree.vue index debaaa2a417..bc8fd4ea713 100644 --- a/spring-boot-admin-server-ui/src/main/frontend/views/instances/sbomdependencytrees/tree.vue +++ b/spring-boot-admin-server-ui/src/main/frontend/views/instances/sbomdependencytrees/tree.vue @@ -17,6 +17,11 @@