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
25 changes: 25 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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('')
})
})
Loading