Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
<template>
<sba-instance-section :error="error" :loading="isLoading">
<sba-panel :title="sbomId">
<sba-alert
v-if="!isLoading && !error && !normalizedData"
severity="WARN"
:error="t('instances.dependencies.no_data_provided')"
/>
<div ref="treeContainer" class="x-scroller"></div>
</sba-panel>

Expand Down Expand Up @@ -44,6 +49,7 @@ import { debounce } from 'lodash-es';
import { computed, onBeforeUnmount, onMounted, ref, watch } from 'vue';
import { useI18n } from 'vue-i18n';

import SbaAlert from '@/components/sba-alert.vue';
import SbaPanel from '@/components/sba-panel.vue';

import Instance from '@/services/instance';
Expand All @@ -68,11 +74,14 @@ const { t } = useI18n();

const treeContainer = ref<HTMLElement | null>(null);
const dependencies = ref<SbomDependency[]>([]);
const rootRef = ref<string>();
const rootNode = ref<D3DependencyTree | null>(null);
const error = ref<string | null>(null);
const isLoading = ref<boolean | null>(false);
const isLoading = ref<boolean | null>(true);

const normalizedData = computed(() => normalizeData(dependencies.value));
const normalizedData = computed(() =>
normalizeData(dependencies.value, rootRef.value),
);
const filteredData = computed(() =>
filterTree(normalizedData.value, props.filter || ''),
);
Expand All @@ -82,7 +91,8 @@ const fetchSbomDependencies = async (sbomId: string): Promise<void> => {
isLoading.value = true;
try {
const res = await props.instance.fetchSbom(sbomId);
dependencies.value = res.data.dependencies;
dependencies.value = res.data.dependencies ?? [];
rootRef.value = res.data.metadata?.component?.['bom-ref'];
await renderTree();
} catch (err) {
console.warn('Fetching sbom failed:', err);
Expand All @@ -109,8 +119,8 @@ const rerenderOrUpdateTree = async (
newVal: string,
oldVal: string,
): Promise<void> => {
if (dependencies.value.length > 0) {
if (!newVal.trim() || newVal === oldVal) {
if (normalizedData.value) {
if (!rootNode.value || !newVal.trim() || newVal === oldVal) {
await renderTree();
} else {
await updateTree();
Expand Down
Loading