From 8a0a2c5c1e7b92ce7beb77b8312b77adfed04572 Mon Sep 17 00:00:00 2001 From: Andy Taylor Date: Tue, 30 Dec 2025 13:29:23 +0000 Subject: [PATCH] ARTEMIS-5836 - adding and enabling tests --- .github/workflows/test.yml | 25 ++++ .../src/artemis-service.test.ts | 141 +++++++++++++++++- 2 files changed, 162 insertions(+), 4 deletions(-) create mode 100644 .github/workflows/test.yml diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..63ada16 --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,25 @@ +name: "Test" + +on: [push, pull_request] + +jobs: + build: + + runs-on: ubuntu-latest + + strategy: + matrix: + node-version: [22.17.1] + + steps: + - uses: actions/checkout@v4 + - name: Use Node.js ${{ matrix.node-version }} + uses: actions/setup-node@v4 + with: + node-version: ${{ matrix.node-version }} + - name: install + run: yarn install + working-directory: ./artemis-console-extension/artemis-extension/packages/artemis-console-plugin + - name: test + run: yarn test + working-directory: ./artemis-console-extension/artemis-extension/packages/artemis-console-plugin \ No newline at end of file diff --git a/artemis-console-extension/artemis-extension/packages/artemis-console-plugin/src/artemis-service.test.ts b/artemis-console-extension/artemis-extension/packages/artemis-console-plugin/src/artemis-service.test.ts index d043919..9d83949 100644 --- a/artemis-console-extension/artemis-extension/packages/artemis-console-plugin/src/artemis-service.test.ts +++ b/artemis-console-extension/artemis-extension/packages/artemis-console-plugin/src/artemis-service.test.ts @@ -14,10 +14,11 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -import { beforeAll, describe, expect, test } from "@jest/globals" +import { beforeAll, beforeEach, describe, expect, jest, test } from "@jest/globals" import { artemisService, parseMBeanName } from "./artemis-service"; import { SortDirection } from './table/ArtemisTable' -import { userService } from '@hawtio/react' +import { jolokiaService, userService } from '@hawtio/react' +import { configManager } from './config-manager' beforeAll(async () => { // needed to determine Jolokia URL @@ -45,9 +46,141 @@ describe("Artemis Service basic tests", () => { const mbean = "org.apache.activemq.artemis:broker=\"0.0.0.0:61616\",component=acceptors,filter=\"x,y,z=a\",name=amqp" const parsed = parseMBeanName(mbean) expect(parsed.domain).toEqual("org.apache.activemq.artemis") - expect(parsed.properties["broker"]).toEqual("\"0.0.0.0:61616\"") - expect(parsed.properties["filter"]).toEqual("\"x,y,z=a\"") + expect(parsed.properties["broker"]).toEqual("0.0.0.0:61616") + expect(parsed.properties["filter"]).toEqual("x,y,z=a") expect(parsed.properties["name"]).toEqual("amqp") }) }) + +/** + * Tests for the initialize method in artemis-service.ts + */ +describe("ArtemisService.initialize()", () => { + + beforeEach(() => { + // Clear any mocks before each test + jest.clearAllMocks() + }) + + test("initialize should set up brokerObjectName promise", async () => { + // Create a new instance to test initialization + const testService = new (artemisService.constructor as any)() + + // Mock the config manager and jolokia service + const mockConfig = { jmx: { domain: 'org.apache.activemq.artemis' } } + jest.spyOn(configManager, 'getArtemisconfig').mockResolvedValue(mockConfig) + jest.spyOn(jolokiaService, 'search').mockResolvedValue(['org.apache.activemq.artemis:broker=test']) + + // Call initialize + testService.initialize() + + // Get the broker object name (which should now be initialized) + const brokerObjectName = await testService.getBrokerObjectName() + + expect(brokerObjectName).toBe('org.apache.activemq.artemis:broker=test') + expect(configManager.getArtemisconfig).toHaveBeenCalled() + expect(jolokiaService.search).toHaveBeenCalledWith('org.apache.activemq.artemis:broker=*') + }) + + test("initialize should handle empty search results", async () => { + const testService = new (artemisService.constructor as any)() + + const mockConfig = { jmx: { domain: 'org.apache.activemq.artemis' } } + jest.spyOn(configManager, 'getArtemisconfig').mockResolvedValue(mockConfig) + jest.spyOn(jolokiaService, 'search').mockResolvedValue([]) + + testService.initialize() + + const brokerObjectName = await testService.getBrokerObjectName() + + expect(brokerObjectName).toBe('') + }) + + test("initialize should handle null search results", async () => { + const testService = new (artemisService.constructor as any)() + + const mockConfig = { jmx: { domain: 'org.apache.activemq.artemis' } } + jest.spyOn(configManager, 'getArtemisconfig').mockResolvedValue(mockConfig) + jest.spyOn(jolokiaService, 'search').mockResolvedValue(null as any) + + testService.initialize() + + const brokerObjectName = await testService.getBrokerObjectName() + + expect(brokerObjectName).toBe('') + }) + + test("initialize should handle search errors gracefully", async () => { + const testService = new (artemisService.constructor as any)() + + const mockConfig = { jmx: { domain: 'org.apache.activemq.artemis' } } + jest.spyOn(configManager, 'getArtemisconfig').mockResolvedValue(mockConfig) + jest.spyOn(jolokiaService, 'search').mockRejectedValue(new Error('Connection failed')) + + testService.initialize() + + const brokerObjectName = await testService.getBrokerObjectName() + + expect(brokerObjectName).toBe('') + }) + + test("initialize should use first broker when multiple brokers found", async () => { + const testService = new (artemisService.constructor as any)() + + const mockConfig = { jmx: { domain: 'org.apache.activemq.artemis' } } + jest.spyOn(configManager, 'getArtemisconfig').mockResolvedValue(mockConfig) + jest.spyOn(jolokiaService, 'search').mockResolvedValue([ + 'org.apache.activemq.artemis:broker=broker1', + 'org.apache.activemq.artemis:broker=broker2' + ]) + + testService.initialize() + + const brokerObjectName = await testService.getBrokerObjectName() + + expect(brokerObjectName).toBe('org.apache.activemq.artemis:broker=broker1') + }) + + test("initialize should handle custom JMX domain from config", async () => { + const testService = new (artemisService.constructor as any)() + + const mockConfig = { jmx: { domain: 'custom.domain' } } + jest.spyOn(configManager, 'getArtemisconfig').mockResolvedValue(mockConfig) + jest.spyOn(jolokiaService, 'search').mockResolvedValue(['custom.domain:broker=test']) + + testService.initialize() + + await testService.getBrokerObjectName() + + expect(jolokiaService.search).toHaveBeenCalledWith('custom.domain:broker=*') + }) + + test("initialize can be called multiple times safely", async () => { + const testService = new (artemisService.constructor as any)() + + const mockConfig = { jmx: { domain: 'org.apache.activemq.artemis' } } + jest.spyOn(configManager, 'getArtemisconfig').mockResolvedValue(mockConfig) + jest.spyOn(jolokiaService, 'search').mockResolvedValue(['org.apache.activemq.artemis:broker=test']) + + // Call initialize multiple times + testService.initialize() + testService.initialize() + testService.initialize() + + const brokerObjectName = await testService.getBrokerObjectName() + + expect(brokerObjectName).toBe('org.apache.activemq.artemis:broker=test') + // Config should be fetched multiple times (once per initialize call) + expect(configManager.getArtemisconfig).toHaveBeenCalled() + }) + + test("getBrokerObjectName should return empty string before initialization", async () => { + const testService = new (artemisService.constructor as any)() + + // Don't call initialize + const brokerObjectName = await testService.getBrokerObjectName() + + expect(brokerObjectName).toBe('') + }) +})