Skip to content
Open
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
52 changes: 46 additions & 6 deletions spring-boot-admin-server-ui/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions spring-boot-admin-server-ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
"@vue/eslint-config-typescript": "^14.0.0",
"@vue/test-utils": "2.4.6",
"autoprefixer": "10.4.24",
"axios-mock-adapter": "^2.1.0",
"babel-loader": "10.0.0",
"eslint": "^10.0.0",
"eslint-config-prettier": "^10.0.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// __mocks__/@stekoe/vue-toast-notificationcenter.js
import { vi } from 'vitest';

// Ensure errorSpy is available
if (!globalThis.errorSpy) {
globalThis.errorSpy = vi.fn();
}

export const useNotificationCenter = () => ({ error: globalThis.errorSpy });

export default {
install(app) {
app.config.globalProperties.$nc = { error: globalThis.errorSpy };
},
};
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { AxiosError } from 'axios';
import { describe, expect, test, vi } from 'vitest';

import Instance from '@/services/instance';
Expand Down Expand Up @@ -40,4 +41,128 @@ describe('Instance', () => {
expect(instance.showUrl()).toEqual(expectUrlToBeShownOnUI);
},
);

describe('fetchMetric', () => {
const instance = new Instance({
id: 'test-id',
registration: { name: 'test' },
availableMetrics: ['test.metric', 'cache.size', 'cache.gets'],
});
test('should pass suppressToast option to axios config', async () => {
// Spy on axios.get
const axiosGetSpy = vi.spyOn(instance.axios, 'get');

// Mock the axios request
axiosGetSpy.mockResolvedValue({
data: {
measurements: [{ value: 42 }],
},
});

await instance.fetchMetric(
'test.metric',
{ tag: 'value' },
{
suppressToast: true,
},
);

// Verify suppressToast was passed in config
expect(axiosGetSpy).toHaveBeenCalledWith(
expect.stringContaining('actuator/metrics/test.metric'),
expect.objectContaining({
suppressToast: true,
}),
);
});

test('should work without options parameter for backward compatibility', async () => {
const axiosGetSpy = vi.spyOn(instance.axios, 'get');

axiosGetSpy.mockResolvedValue({
data: {
measurements: [{ value: 42 }],
},
});

await instance.fetchMetric('test.metric', { tag: 'value' });

// Verify it was called without suppressToast
expect(axiosGetSpy).toHaveBeenCalledWith(
expect.stringContaining('actuator/metrics/test.metric'),
expect.objectContaining({
suppressToast: undefined,
}),
);
});

test('should pass suppressToast=false when explicitly set to false', async () => {
const axiosGetSpy = vi.spyOn(instance.axios, 'get');

axiosGetSpy.mockResolvedValue({
data: {
measurements: [{ value: 42 }],
},
});

await instance.fetchMetric(
'test.metric',
{ tag: 'value' },
{
suppressToast: false,
},
);

expect(axiosGetSpy).toHaveBeenCalledWith(
expect.stringContaining('actuator/metrics/test.metric'),
expect.objectContaining({
suppressToast: false,
}),
);
});

test('should include tags in request parameters', async () => {
const axiosGetSpy = vi.spyOn(instance.axios, 'get');

axiosGetSpy.mockResolvedValue({
data: {
measurements: [{ value: 42 }],
},
});

await instance.fetchMetric('cache.gets', {
name: 'my-cache',
result: 'hit',
});

const callArgs = axiosGetSpy.mock.calls[0];
const params = callArgs[1]?.params as URLSearchParams;

expect(params).toBeInstanceOf(URLSearchParams);
expect(params.getAll('tag')).toContain('name:my-cache');
expect(params.getAll('tag')).toContain('result:hit');
});

test('should pass suppressToast function to axios config', async () => {
const axiosGetSpy = vi.spyOn(instance.axios, 'get');

axiosGetSpy.mockResolvedValue({
data: {
measurements: [{ value: 42 }],
},
});

const suppressFn = (err: AxiosError) => err.response?.status === 404;
await instance.fetchMetric(
'cache.size',
{},
{ suppressToast: suppressFn },
);

expect(axiosGetSpy).toHaveBeenCalledWith(
expect.any(String),
expect.objectContaining({ suppressToast: suppressFn }),
);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { AxiosInstance } from 'axios';
import { AxiosError, AxiosInstance } from 'axios';
import saveAs from 'file-saver';
import { Observable, concat, from, ignoreElements } from 'rxjs';

Expand All @@ -29,6 +29,17 @@ import { useSbaConfig } from '@/sba-config';
import { actuatorMimeTypes } from '@/services/spring-mime-types';
import { transformToJSON } from '@/utils/transformToJSON';

// Extend AxiosRequestConfig to allow suppressToast
declare module 'axios' {
interface AxiosRequestConfig {
suppressToast?: boolean | ((error: AxiosError) => boolean);
}
}

export type FetchMetricOptions = {
suppressToast?: boolean | ((error: AxiosError) => boolean);
};

const isInstanceActuatorRequest = (url: string) =>
url.match(/^instances[/][^/]+[/]actuator([/].*)?$/);

Expand Down Expand Up @@ -167,7 +178,11 @@ class Instance {
return response;
}

async fetchMetric(metric: string, tags: Record<string, any>) {
async fetchMetric(
metric: string,
tags?: Record<string, string>,
options?: FetchMetricOptions,
) {
if (this.availableMetrics.length === 0) {
try {
await this.fetchMetrics();
Expand Down Expand Up @@ -203,6 +218,7 @@ class Instance {
}
return this.axios.get(uri`actuator/metrics/${metric}`, {
params,
suppressToast: options?.suppressToast,
});
}

Expand Down
11 changes: 11 additions & 0 deletions spring-boot-admin-server-ui/src/main/frontend/tests/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,17 @@ global.EventSource = class {

global.SBA = sbaConfig;

// Mock localStorage globally for all tests
Object.defineProperty(global, 'localStorage', {
value: {
getItem: vi.fn(),
setItem: vi.fn(),
removeItem: vi.fn(),
clear: vi.fn(),
},
writable: true,
});

beforeAll(() => server.listen({ onUnhandledRequest: 'error' }));
afterAll(() => server.close());
afterEach(() => server.resetHandlers());
Expand Down
Loading
Loading