|
27 | 27 | import java.time.Duration; |
28 | 28 | import java.time.Instant; |
29 | 29 | import java.util.logging.Logger; |
| 30 | +import javax.management.AttributeList; |
30 | 31 | import javax.management.AttributeNotFoundException; |
31 | 32 | import javax.management.InstanceNotFoundException; |
32 | 33 | import javax.management.IntrospectionException; |
@@ -85,6 +86,9 @@ void shouldBeAbleToRegisterBaseServerConfig() { |
85 | 86 | MBeanAttributeInfo[] attributeInfoArray = info.getAttributes(); |
86 | 87 | assertThat(attributeInfoArray).hasSize(3); |
87 | 88 |
|
| 89 | + AttributeList attributeList = getAttributeList(name, attributeInfoArray); |
| 90 | + assertThat(attributeList).isNotNull().hasSize(3); |
| 91 | + |
88 | 92 | String uriValue = (String) beanServer.getAttribute(name, "Uri"); |
89 | 93 | assertThat(uriValue).isEqualTo(baseServerOptions.getExternalUri().toString()); |
90 | 94 |
|
@@ -130,23 +134,26 @@ id, nodeUri, new ImmutableCapabilities(), caps, Instant.now()))) |
130 | 134 | MBeanAttributeInfo[] attributeInfo = info.getAttributes(); |
131 | 135 | assertThat(attributeInfo).hasSize(9); |
132 | 136 |
|
133 | | - String currentSessions = (String) beanServer.getAttribute(name, "CurrentSessions"); |
134 | | - assertThat(Integer.parseInt(currentSessions)).isZero(); |
| 137 | + AttributeList attributeList = getAttributeList(name, attributeInfo); |
| 138 | + assertThat(attributeList).isNotNull().hasSize(9); |
| 139 | + |
| 140 | + Object currentSessions = beanServer.getAttribute(name, "CurrentSessions"); |
| 141 | + assertNumberAttribute(currentSessions, 0); |
135 | 142 |
|
136 | | - String maxSessions = (String) beanServer.getAttribute(name, "MaxSessions"); |
137 | | - assertThat(Integer.parseInt(maxSessions)).isEqualTo(1); |
| 143 | + Object maxSessions = beanServer.getAttribute(name, "MaxSessions"); |
| 144 | + assertNumberAttribute(maxSessions, 1); |
138 | 145 |
|
139 | 146 | String status = (String) beanServer.getAttribute(name, "Status"); |
140 | 147 | assertThat(status).isEqualTo("UP"); |
141 | 148 |
|
142 | | - String totalSlots = (String) beanServer.getAttribute(name, "TotalSlots"); |
143 | | - assertThat(Integer.parseInt(totalSlots)).isEqualTo(1); |
| 149 | + Object totalSlots = beanServer.getAttribute(name, "TotalSlots"); |
| 150 | + assertNumberAttribute(totalSlots, 1); |
144 | 151 |
|
145 | | - String usedSlots = (String) beanServer.getAttribute(name, "UsedSlots"); |
146 | | - assertThat(Integer.parseInt(usedSlots)).isZero(); |
| 152 | + Object usedSlots = beanServer.getAttribute(name, "UsedSlots"); |
| 153 | + assertNumberAttribute(usedSlots, 0); |
147 | 154 |
|
148 | | - String load = (String) beanServer.getAttribute(name, "Load"); |
149 | | - assertThat(Float.parseFloat(load)).isEqualTo(0.0f); |
| 155 | + Object load = beanServer.getAttribute(name, "Load"); |
| 156 | + assertNumberAttribute(load, 0.0f); |
150 | 157 |
|
151 | 158 | String remoteNodeUri = (String) beanServer.getAttribute(name, "RemoteNodeUri"); |
152 | 159 | assertThat(remoteNodeUri).isEqualTo(nodeUri.toString()); |
@@ -182,13 +189,14 @@ void shouldBeAbleToRegisterSessionQueueServerConfig() { |
182 | 189 | MBeanAttributeInfo[] attributeInfoArray = info.getAttributes(); |
183 | 190 | assertThat(attributeInfoArray).hasSize(2); |
184 | 191 |
|
185 | | - String requestTimeout = (String) beanServer.getAttribute(name, "RequestTimeoutSeconds"); |
186 | | - assertThat(Long.parseLong(requestTimeout)) |
187 | | - .isEqualTo(newSessionQueueOptions.getRequestTimeoutSeconds()); |
| 192 | + AttributeList attributeList = getAttributeList(name, attributeInfoArray); |
| 193 | + assertThat(attributeList).isNotNull().hasSize(2); |
| 194 | + |
| 195 | + Object requestTimeout = beanServer.getAttribute(name, "RequestTimeoutSeconds"); |
| 196 | + assertNumberAttribute(requestTimeout, newSessionQueueOptions.getRequestTimeoutSeconds()); |
188 | 197 |
|
189 | | - String retryInterval = (String) beanServer.getAttribute(name, "RetryIntervalMilliseconds"); |
190 | | - assertThat(Long.parseLong(retryInterval)) |
191 | | - .isEqualTo(newSessionQueueOptions.getRetryIntervalMilliseconds()); |
| 198 | + Object retryInterval = beanServer.getAttribute(name, "RetryIntervalMilliseconds"); |
| 199 | + assertNumberAttribute(retryInterval, newSessionQueueOptions.getRetryIntervalMilliseconds()); |
192 | 200 | } catch (InstanceNotFoundException |
193 | 201 | | IntrospectionException |
194 | 202 | | ReflectionException |
@@ -227,8 +235,11 @@ void shouldBeAbleToRegisterSessionQueue() { |
227 | 235 | MBeanAttributeInfo[] attributeInfoArray = info.getAttributes(); |
228 | 236 | assertThat(attributeInfoArray).hasSize(1); |
229 | 237 |
|
230 | | - String size = (String) beanServer.getAttribute(name, "NewSessionQueueSize"); |
231 | | - assertThat(Integer.parseInt(size)).isZero(); |
| 238 | + AttributeList attributeList = getAttributeList(name, attributeInfoArray); |
| 239 | + assertThat(attributeList).isNotNull().hasSize(1); |
| 240 | + |
| 241 | + Object size = beanServer.getAttribute(name, "NewSessionQueueSize"); |
| 242 | + assertNumberAttribute(size, 0); |
232 | 243 | } catch (InstanceNotFoundException |
233 | 244 | | IntrospectionException |
234 | 245 | | ReflectionException |
@@ -290,21 +301,57 @@ void shouldBeAbleToMonitorHub() throws Exception { |
290 | 301 | MBeanInfo info = beanServer.getMBeanInfo(name); |
291 | 302 | assertThat(info).isNotNull(); |
292 | 303 |
|
293 | | - String nodeUpCount = (String) beanServer.getAttribute(name, "NodeUpCount"); |
| 304 | + MBeanAttributeInfo[] attributeInfoArray = info.getAttributes(); |
| 305 | + assertThat(attributeInfoArray).hasSize(4); |
| 306 | + |
| 307 | + AttributeList attributeList = getAttributeList(name, attributeInfoArray); |
| 308 | + assertThat(attributeList).isNotNull().hasSize(4); |
| 309 | + |
| 310 | + Object nodeUpCount = beanServer.getAttribute(name, "NodeUpCount"); |
294 | 311 | LOG.info("Node up count=" + nodeUpCount); |
295 | | - assertThat(Integer.parseInt(nodeUpCount)).isEqualTo(1); |
| 312 | + assertNumberAttribute(nodeUpCount, 1); |
296 | 313 |
|
297 | | - String nodeDownCount = (String) beanServer.getAttribute(name, "NodeDownCount"); |
| 314 | + Object nodeDownCount = beanServer.getAttribute(name, "NodeDownCount"); |
298 | 315 | LOG.info("Node down count=" + nodeDownCount); |
299 | | - assertThat(Integer.parseInt(nodeDownCount)).isZero(); |
| 316 | + assertNumberAttribute(nodeDownCount, 0); |
300 | 317 |
|
301 | | - String activeSlots = (String) beanServer.getAttribute(name, "ActiveSlots"); |
| 318 | + Object activeSlots = beanServer.getAttribute(name, "ActiveSlots"); |
302 | 319 | LOG.info("Active slots count=" + activeSlots); |
303 | | - assertThat(Integer.parseInt(activeSlots)).isZero(); |
| 320 | + assertNumberAttribute(activeSlots, 0); |
304 | 321 |
|
305 | | - String idleSlots = (String) beanServer.getAttribute(name, "IdleSlots"); |
| 322 | + Object idleSlots = beanServer.getAttribute(name, "IdleSlots"); |
306 | 323 | LOG.info("Idle slots count=" + idleSlots); |
307 | | - assertThat(Integer.parseInt(idleSlots)).isEqualTo(1); |
| 324 | + assertNumberAttribute(idleSlots, 1); |
| 325 | + } |
| 326 | + } |
| 327 | + |
| 328 | + private AttributeList getAttributeList(ObjectName name, MBeanAttributeInfo[] attributeInfoArray) |
| 329 | + throws InstanceNotFoundException, ReflectionException { |
| 330 | + String[] attributeNames = new String[attributeInfoArray.length]; |
| 331 | + for (int i = 0; i < attributeInfoArray.length; i++) { |
| 332 | + attributeNames[i] = attributeInfoArray[i].getName(); |
308 | 333 | } |
| 334 | + |
| 335 | + return beanServer.getAttributes(name, attributeNames); |
| 336 | + } |
| 337 | + |
| 338 | + private void assertCommonNumberAttributes(Object attribute) { |
| 339 | + assertThat(attribute).isNotNull(); |
| 340 | + assertThat(attribute).isInstanceOf(Number.class); |
| 341 | + } |
| 342 | + |
| 343 | + private void assertNumberAttribute(Object attribute, int expectedValue) { |
| 344 | + assertCommonNumberAttributes(attribute); |
| 345 | + assertThat(Integer.parseInt(attribute.toString())).isEqualTo(expectedValue); |
| 346 | + } |
| 347 | + |
| 348 | + private void assertNumberAttribute(Object attribute, long expectedValue) { |
| 349 | + assertCommonNumberAttributes(attribute); |
| 350 | + assertThat(Long.parseLong(attribute.toString())).isEqualTo(expectedValue); |
| 351 | + } |
| 352 | + |
| 353 | + private void assertNumberAttribute(Object attribute, float expectedValue) { |
| 354 | + assertCommonNumberAttributes(attribute); |
| 355 | + assertThat(Float.parseFloat(attribute.toString())).isEqualTo(expectedValue); |
309 | 356 | } |
310 | 357 | } |
0 commit comments