|
21 | 21 |
|
22 | 22 | import javax.mail.MessagingException; |
23 | 23 |
|
| 24 | +import org.apache.cloudstack.alert.AlertService; |
| 25 | +import org.apache.cloudstack.framework.config.ConfigKey; |
| 26 | +import org.apache.cloudstack.framework.messagebus.MessageBus; |
| 27 | +import org.apache.cloudstack.framework.messagebus.MessageSubscriber; |
24 | 28 | import org.apache.cloudstack.storage.datastore.db.PrimaryDataStoreDao; |
25 | 29 | import org.apache.cloudstack.storage.datastore.db.StoragePoolVO; |
26 | 30 | import org.apache.cloudstack.utils.mailing.SMTPMailSender; |
|
35 | 39 | import org.mockito.Mockito; |
36 | 40 | import org.mockito.Spy; |
37 | 41 | import org.mockito.junit.MockitoJUnitRunner; |
| 42 | +import org.springframework.test.util.ReflectionTestUtils; |
38 | 43 |
|
39 | 44 | import com.cloud.alert.dao.AlertDao; |
40 | 45 | import com.cloud.capacity.Capacity; |
|
45 | 50 | import com.cloud.dc.dao.ClusterDao; |
46 | 51 | import com.cloud.dc.dao.DataCenterDao; |
47 | 52 | import com.cloud.dc.dao.HostPodDao; |
| 53 | +import com.cloud.event.EventTypes; |
48 | 54 | import com.cloud.host.Host; |
49 | 55 | import com.cloud.host.HostVO; |
50 | 56 | import com.cloud.host.dao.HostDao; |
51 | 57 | import com.cloud.storage.StorageManager; |
| 58 | +import com.cloud.utils.Ternary; |
52 | 59 |
|
53 | 60 | import static org.junit.Assert.assertEquals; |
54 | 61 | import static org.junit.Assert.assertNotNull; |
@@ -219,4 +226,79 @@ public void testRecalculateStorageCapacities() { |
219 | 226 | Mockito.verify(storageManager, Mockito.times(2)).createCapacityEntry(sharedPool, Capacity.CAPACITY_TYPE_STORAGE_ALLOCATED, 10L); |
220 | 227 | Mockito.verify(storageManager, Mockito.times(1)).createCapacityEntry(nonSharedPool, Capacity.CAPACITY_TYPE_LOCAL_STORAGE, 20L); |
221 | 228 | } |
| 229 | + |
| 230 | + @Test |
| 231 | + public void initMessageBusListenerSubscribesToConfigurationEditEvent() { |
| 232 | + MessageBus messageBusMock = Mockito.mock(MessageBus.class); |
| 233 | + alertManagerImplMock.messageBus = messageBusMock; |
| 234 | + alertManagerImplMock.initMessageBusListener(); |
| 235 | + Mockito.verify(messageBusMock).subscribe(Mockito.eq(EventTypes.EVENT_CONFIGURATION_VALUE_EDIT), Mockito.any()); |
| 236 | + } |
| 237 | + |
| 238 | + @Test |
| 239 | + public void initMessageBusListenerTriggersSetupRepetitiveAlertTypesOnAllowedKeyEdit() { |
| 240 | + MessageBus messageBusMock = Mockito.mock(MessageBus.class); |
| 241 | + alertManagerImplMock.messageBus = messageBusMock; |
| 242 | + alertManagerImplMock.initMessageBusListener(); |
| 243 | + ArgumentCaptor<MessageSubscriber> captor = ArgumentCaptor.forClass(MessageSubscriber.class); |
| 244 | + Mockito.verify(messageBusMock).subscribe(Mockito.eq(EventTypes.EVENT_CONFIGURATION_VALUE_EDIT), captor.capture()); |
| 245 | + Ternary<String, ConfigKey.Scope, Long> args = new Ternary<>(AlertManager.AllowedRepetitiveAlertTypes.key(), ConfigKey.Scope.Global, 1L); |
| 246 | + captor.getValue().onPublishMessage(null, null, args); |
| 247 | + Mockito.verify(alertManagerImplMock).setupRepetitiveAlertTypes(); |
| 248 | + } |
| 249 | + |
| 250 | + @Test |
| 251 | + public void initMessageBusListenerDoesNotTriggerSetupRepetitiveAlertTypesOnOtherKeyEdit() { |
| 252 | + MessageBus messageBusMock = Mockito.mock(MessageBus.class); |
| 253 | + alertManagerImplMock.messageBus = messageBusMock; |
| 254 | + alertManagerImplMock.initMessageBusListener(); |
| 255 | + ArgumentCaptor<MessageSubscriber> captor = ArgumentCaptor.forClass(MessageSubscriber.class); |
| 256 | + Mockito.verify(messageBusMock).subscribe(Mockito.eq(EventTypes.EVENT_CONFIGURATION_VALUE_EDIT), captor.capture()); |
| 257 | + Ternary<String, ConfigKey.Scope, Long> args = new Ternary<>("some.other.key", ConfigKey.Scope.Global, 1L); |
| 258 | + captor.getValue().onPublishMessage(null, null, args); |
| 259 | + Mockito.verify(alertManagerImplMock, Mockito.never()).setupRepetitiveAlertTypes(); |
| 260 | + } |
| 261 | + |
| 262 | + private void mockAllowedRepetitiveAlertTypesConfigKey(String value) { |
| 263 | + ReflectionTestUtils.setField(AlertManager.AllowedRepetitiveAlertTypes, "_defaultValue", value); |
| 264 | + } |
| 265 | + |
| 266 | + @Test |
| 267 | + public void setupRepetitiveAlertTypesParsesValidAlertTypesCorrectly() { |
| 268 | + mockAllowedRepetitiveAlertTypesConfigKey("ALERT.CPU,ALERT.MEMORY"); |
| 269 | + alertManagerImplMock.setupRepetitiveAlertTypes(); |
| 270 | + List<AlertService.AlertType> expectedTypes = (List<AlertService.AlertType>)ReflectionTestUtils.getField(alertManagerImplMock, "allowedRepetitiveAlertTypes"); |
| 271 | + Assert.assertNotNull(expectedTypes); |
| 272 | + Assert.assertEquals(2, expectedTypes.size()); |
| 273 | + Assert.assertTrue(expectedTypes.contains(AlertManager.AlertType.ALERT_TYPE_CPU)); |
| 274 | + Assert.assertTrue(expectedTypes.contains(AlertManager.AlertType.ALERT_TYPE_MEMORY)); |
| 275 | + } |
| 276 | + |
| 277 | + @Test |
| 278 | + public void setupRepetitiveAlertTypesHandlesEmptyConfigValue() { |
| 279 | + mockAllowedRepetitiveAlertTypesConfigKey(""); |
| 280 | + alertManagerImplMock.setupRepetitiveAlertTypes(); |
| 281 | + List<AlertService.AlertType> expectedTypes = (List<AlertService.AlertType>)ReflectionTestUtils.getField(alertManagerImplMock, "allowedRepetitiveAlertTypes"); |
| 282 | + Assert.assertNotNull(expectedTypes); |
| 283 | + Assert.assertTrue(expectedTypes.isEmpty()); |
| 284 | + } |
| 285 | + |
| 286 | + @Test |
| 287 | + public void setupRepetitiveAlertTypesIgnoresUnknownAlertTypes() { |
| 288 | + mockAllowedRepetitiveAlertTypesConfigKey("ALERT.CPU,UNKNOWN_ALERT_TYPE"); |
| 289 | + alertManagerImplMock.setupRepetitiveAlertTypes(); |
| 290 | + List<AlertService.AlertType> expectedTypes = (List<AlertService.AlertType>)ReflectionTestUtils.getField(alertManagerImplMock, "allowedRepetitiveAlertTypes"); |
| 291 | + Assert.assertNotNull(expectedTypes); |
| 292 | + Assert.assertEquals(1, expectedTypes.size()); |
| 293 | + Assert.assertTrue(expectedTypes.contains(AlertManager.AlertType.ALERT_TYPE_CPU)); |
| 294 | + } |
| 295 | + |
| 296 | + @Test |
| 297 | + public void setupRepetitiveAlertTypesHandlesNullConfigValue() { |
| 298 | + mockAllowedRepetitiveAlertTypesConfigKey(null); |
| 299 | + alertManagerImplMock.setupRepetitiveAlertTypes(); |
| 300 | + List<AlertService.AlertType> expectedTypes = (List<AlertService.AlertType>)ReflectionTestUtils.getField(alertManagerImplMock, "allowedRepetitiveAlertTypes"); |
| 301 | + Assert.assertNotNull(expectedTypes); |
| 302 | + Assert.assertTrue(expectedTypes.isEmpty()); |
| 303 | + } |
222 | 304 | } |
0 commit comments