|
14 | 14 | * See the License for the specific language governing permissions and |
15 | 15 | * limitations under the License. |
16 | 16 | */ |
17 | | -import { beforeAll, describe, expect, test } from "@jest/globals" |
| 17 | +import { beforeAll, beforeEach, describe, expect, jest, test } from "@jest/globals" |
18 | 18 | import { artemisService, parseMBeanName } from "./artemis-service"; |
19 | 19 | import { SortDirection } from './table/ArtemisTable' |
20 | | -import { userService } from '@hawtio/react' |
| 20 | +import { jolokiaService, userService } from '@hawtio/react' |
| 21 | +import { configManager } from './config-manager' |
21 | 22 |
|
22 | 23 | beforeAll(async () => { |
23 | 24 | // needed to determine Jolokia URL |
@@ -45,9 +46,141 @@ describe("Artemis Service basic tests", () => { |
45 | 46 | const mbean = "org.apache.activemq.artemis:broker=\"0.0.0.0:61616\",component=acceptors,filter=\"x,y,z=a\",name=amqp" |
46 | 47 | const parsed = parseMBeanName(mbean) |
47 | 48 | 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") |
50 | 51 | expect(parsed.properties["name"]).toEqual("amqp") |
51 | 52 | }) |
52 | 53 |
|
53 | 54 | }) |
| 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