Skip to content

Commit 8a0a2c5

Browse files
committed
ARTEMIS-5836 - adding and enabling tests
1 parent 6966520 commit 8a0a2c5

2 files changed

Lines changed: 162 additions & 4 deletions

File tree

.github/workflows/test.yml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
name: "Test"
2+
3+
on: [push, pull_request]
4+
5+
jobs:
6+
build:
7+
8+
runs-on: ubuntu-latest
9+
10+
strategy:
11+
matrix:
12+
node-version: [22.17.1]
13+
14+
steps:
15+
- uses: actions/checkout@v4
16+
- name: Use Node.js ${{ matrix.node-version }}
17+
uses: actions/setup-node@v4
18+
with:
19+
node-version: ${{ matrix.node-version }}
20+
- name: install
21+
run: yarn install
22+
working-directory: ./artemis-console-extension/artemis-extension/packages/artemis-console-plugin
23+
- name: test
24+
run: yarn test
25+
working-directory: ./artemis-console-extension/artemis-extension/packages/artemis-console-plugin

artemis-console-extension/artemis-extension/packages/artemis-console-plugin/src/artemis-service.test.ts

Lines changed: 137 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,11 @@
1414
* See the License for the specific language governing permissions and
1515
* limitations under the License.
1616
*/
17-
import { beforeAll, describe, expect, test } from "@jest/globals"
17+
import { beforeAll, beforeEach, describe, expect, jest, test } from "@jest/globals"
1818
import { artemisService, parseMBeanName } from "./artemis-service";
1919
import { SortDirection } from './table/ArtemisTable'
20-
import { userService } from '@hawtio/react'
20+
import { jolokiaService, userService } from '@hawtio/react'
21+
import { configManager } from './config-manager'
2122

2223
beforeAll(async () => {
2324
// needed to determine Jolokia URL
@@ -45,9 +46,141 @@ describe("Artemis Service basic tests", () => {
4546
const mbean = "org.apache.activemq.artemis:broker=\"0.0.0.0:61616\",component=acceptors,filter=\"x,y,z=a\",name=amqp"
4647
const parsed = parseMBeanName(mbean)
4748
expect(parsed.domain).toEqual("org.apache.activemq.artemis")
48-
expect(parsed.properties["broker"]).toEqual("\"0.0.0.0:61616\"")
49-
expect(parsed.properties["filter"]).toEqual("\"x,y,z=a\"")
49+
expect(parsed.properties["broker"]).toEqual("0.0.0.0:61616")
50+
expect(parsed.properties["filter"]).toEqual("x,y,z=a")
5051
expect(parsed.properties["name"]).toEqual("amqp")
5152
})
5253

5354
})
55+
56+
/**
57+
* Tests for the initialize method in artemis-service.ts
58+
*/
59+
describe("ArtemisService.initialize()", () => {
60+
61+
beforeEach(() => {
62+
// Clear any mocks before each test
63+
jest.clearAllMocks()
64+
})
65+
66+
test("initialize should set up brokerObjectName promise", async () => {
67+
// Create a new instance to test initialization
68+
const testService = new (artemisService.constructor as any)()
69+
70+
// Mock the config manager and jolokia service
71+
const mockConfig = { jmx: { domain: 'org.apache.activemq.artemis' } }
72+
jest.spyOn(configManager, 'getArtemisconfig').mockResolvedValue(mockConfig)
73+
jest.spyOn(jolokiaService, 'search').mockResolvedValue(['org.apache.activemq.artemis:broker=test'])
74+
75+
// Call initialize
76+
testService.initialize()
77+
78+
// Get the broker object name (which should now be initialized)
79+
const brokerObjectName = await testService.getBrokerObjectName()
80+
81+
expect(brokerObjectName).toBe('org.apache.activemq.artemis:broker=test')
82+
expect(configManager.getArtemisconfig).toHaveBeenCalled()
83+
expect(jolokiaService.search).toHaveBeenCalledWith('org.apache.activemq.artemis:broker=*')
84+
})
85+
86+
test("initialize should handle empty search results", async () => {
87+
const testService = new (artemisService.constructor as any)()
88+
89+
const mockConfig = { jmx: { domain: 'org.apache.activemq.artemis' } }
90+
jest.spyOn(configManager, 'getArtemisconfig').mockResolvedValue(mockConfig)
91+
jest.spyOn(jolokiaService, 'search').mockResolvedValue([])
92+
93+
testService.initialize()
94+
95+
const brokerObjectName = await testService.getBrokerObjectName()
96+
97+
expect(brokerObjectName).toBe('')
98+
})
99+
100+
test("initialize should handle null search results", async () => {
101+
const testService = new (artemisService.constructor as any)()
102+
103+
const mockConfig = { jmx: { domain: 'org.apache.activemq.artemis' } }
104+
jest.spyOn(configManager, 'getArtemisconfig').mockResolvedValue(mockConfig)
105+
jest.spyOn(jolokiaService, 'search').mockResolvedValue(null as any)
106+
107+
testService.initialize()
108+
109+
const brokerObjectName = await testService.getBrokerObjectName()
110+
111+
expect(brokerObjectName).toBe('')
112+
})
113+
114+
test("initialize should handle search errors gracefully", async () => {
115+
const testService = new (artemisService.constructor as any)()
116+
117+
const mockConfig = { jmx: { domain: 'org.apache.activemq.artemis' } }
118+
jest.spyOn(configManager, 'getArtemisconfig').mockResolvedValue(mockConfig)
119+
jest.spyOn(jolokiaService, 'search').mockRejectedValue(new Error('Connection failed'))
120+
121+
testService.initialize()
122+
123+
const brokerObjectName = await testService.getBrokerObjectName()
124+
125+
expect(brokerObjectName).toBe('')
126+
})
127+
128+
test("initialize should use first broker when multiple brokers found", async () => {
129+
const testService = new (artemisService.constructor as any)()
130+
131+
const mockConfig = { jmx: { domain: 'org.apache.activemq.artemis' } }
132+
jest.spyOn(configManager, 'getArtemisconfig').mockResolvedValue(mockConfig)
133+
jest.spyOn(jolokiaService, 'search').mockResolvedValue([
134+
'org.apache.activemq.artemis:broker=broker1',
135+
'org.apache.activemq.artemis:broker=broker2'
136+
])
137+
138+
testService.initialize()
139+
140+
const brokerObjectName = await testService.getBrokerObjectName()
141+
142+
expect(brokerObjectName).toBe('org.apache.activemq.artemis:broker=broker1')
143+
})
144+
145+
test("initialize should handle custom JMX domain from config", async () => {
146+
const testService = new (artemisService.constructor as any)()
147+
148+
const mockConfig = { jmx: { domain: 'custom.domain' } }
149+
jest.spyOn(configManager, 'getArtemisconfig').mockResolvedValue(mockConfig)
150+
jest.spyOn(jolokiaService, 'search').mockResolvedValue(['custom.domain:broker=test'])
151+
152+
testService.initialize()
153+
154+
await testService.getBrokerObjectName()
155+
156+
expect(jolokiaService.search).toHaveBeenCalledWith('custom.domain:broker=*')
157+
})
158+
159+
test("initialize can be called multiple times safely", async () => {
160+
const testService = new (artemisService.constructor as any)()
161+
162+
const mockConfig = { jmx: { domain: 'org.apache.activemq.artemis' } }
163+
jest.spyOn(configManager, 'getArtemisconfig').mockResolvedValue(mockConfig)
164+
jest.spyOn(jolokiaService, 'search').mockResolvedValue(['org.apache.activemq.artemis:broker=test'])
165+
166+
// Call initialize multiple times
167+
testService.initialize()
168+
testService.initialize()
169+
testService.initialize()
170+
171+
const brokerObjectName = await testService.getBrokerObjectName()
172+
173+
expect(brokerObjectName).toBe('org.apache.activemq.artemis:broker=test')
174+
// Config should be fetched multiple times (once per initialize call)
175+
expect(configManager.getArtemisconfig).toHaveBeenCalled()
176+
})
177+
178+
test("getBrokerObjectName should return empty string before initialization", async () => {
179+
const testService = new (artemisService.constructor as any)()
180+
181+
// Don't call initialize
182+
const brokerObjectName = await testService.getBrokerObjectName()
183+
184+
expect(brokerObjectName).toBe('')
185+
})
186+
})

0 commit comments

Comments
 (0)