Repository: ambari Updated Branches: refs/heads/branch-2.6 3a68e5140 -> 473abc324
AMBARI-22458 - SNMP Trap should contain sysuptime field filled (unit test fixes) (jonathanhurley) Project: http://git-wip-us.apache.org/repos/asf/ambari/repo Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/473abc32 Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/473abc32 Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/473abc32 Branch: refs/heads/branch-2.6 Commit: 473abc324ff04b2cda50ccd26f85a6fbfd7e2f51 Parents: 3a68e51 Author: Jonathan Hurley <[email protected]> Authored: Tue Nov 28 12:23:31 2017 -0500 Committer: Jonathan Hurley <[email protected]> Committed: Tue Nov 28 12:24:03 2017 -0500 ---------------------------------------------------------------------- .../dispatchers/AmbariSNMPDispatcherTest.java | 97 +++++++++++--------- .../AlertNoticeDispatchServiceTest.java | 63 ++++++++++--- 2 files changed, 106 insertions(+), 54 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ambari/blob/473abc32/ambari-server/src/test/java/org/apache/ambari/server/notifications/dispatchers/AmbariSNMPDispatcherTest.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/java/org/apache/ambari/server/notifications/dispatchers/AmbariSNMPDispatcherTest.java b/ambari-server/src/test/java/org/apache/ambari/server/notifications/dispatchers/AmbariSNMPDispatcherTest.java index b1fee2b..4ac1681 100644 --- a/ambari-server/src/test/java/org/apache/ambari/server/notifications/dispatchers/AmbariSNMPDispatcherTest.java +++ b/ambari-server/src/test/java/org/apache/ambari/server/notifications/dispatchers/AmbariSNMPDispatcherTest.java @@ -18,11 +18,32 @@ package org.apache.ambari.server.notifications.dispatchers; -import org.apache.ambari.server.notifications.TargetConfigurationResult; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.eq; +import static org.mockito.Mockito.doNothing; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.doThrow; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + import org.apache.ambari.server.notifications.DispatchCallback; import org.apache.ambari.server.notifications.Notification; import org.apache.ambari.server.notifications.NotificationDispatcher; import org.apache.ambari.server.notifications.Recipient; +import org.apache.ambari.server.notifications.TargetConfigurationResult; import org.apache.ambari.server.orm.entities.AlertDefinitionEntity; import org.apache.ambari.server.orm.entities.AlertHistoryEntity; import org.apache.ambari.server.state.AlertState; @@ -39,16 +60,6 @@ import org.snmp4j.smi.OID; import org.snmp4j.smi.OctetString; import org.snmp4j.smi.VariableBinding; -import java.io.IOException; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -import static org.junit.Assert.*; -import static org.mockito.Mockito.*; - public class AmbariSNMPDispatcherTest { private static final int DEFAULT_SNMP_PORT = 31444; @@ -81,7 +92,7 @@ public class AmbariSNMPDispatcherTest { Notification notification = mock(AlertNotification.class); notification.Callback = mock(DispatchCallback.class); notification.CallbackIds = mock(List.class); - Map<String, String> properties = new HashMap<String, String>(); + Map<String, String> properties = new HashMap<>(); properties.put(AmbariSNMPDispatcher.PORT_PROPERTY, "3"); properties.put(AmbariSNMPDispatcher.COMMUNITY_PROPERTY, "4"); properties.put(AmbariSNMPDispatcher.SNMP_VERSION_PROPERTY, "SNMPv1"); @@ -100,7 +111,7 @@ public class AmbariSNMPDispatcherTest { Notification notification = mock(AlertNotification.class); notification.Callback = mock(DispatchCallback.class); notification.CallbackIds = mock(List.class); - notification.DispatchProperties = new HashMap<String, String>(); + notification.DispatchProperties = new HashMap<>(); dispatcher.dispatch(notification); verify(notification.Callback).onFailure(notification.CallbackIds); verify(notification.Callback, never()).onSuccess(notification.CallbackIds); @@ -112,7 +123,7 @@ public class AmbariSNMPDispatcherTest { Notification notification = getAlertNotification(true); notification.Callback = mock(DispatchCallback.class); notification.CallbackIds = mock(List.class); - Map<String, String> properties = new HashMap<String, String>(); + Map<String, String> properties = new HashMap<>(); properties.put(AmbariSNMPDispatcher.PORT_PROPERTY, "3"); properties.put(AmbariSNMPDispatcher.COMMUNITY_PROPERTY, "4"); properties.put(AmbariSNMPDispatcher.SNMP_VERSION_PROPERTY, "SNMPv1"); @@ -128,12 +139,12 @@ public class AmbariSNMPDispatcherTest { Notification notification = getAlertNotification(true); notification.Callback = mock(DispatchCallback.class); notification.CallbackIds = mock(List.class); - Map<String, String> properties = new HashMap<String, String>(); + Map<String, String> properties = new HashMap<>(); properties.put(AmbariSNMPDispatcher.PORT_PROPERTY, "3"); properties.put(AmbariSNMPDispatcher.COMMUNITY_PROPERTY, "4"); properties.put(AmbariSNMPDispatcher.SNMP_VERSION_PROPERTY, "SNMPv1"); notification.DispatchProperties = properties; - notification.Recipients = new ArrayList<Recipient>(); + notification.Recipients = new ArrayList<>(); dispatcher.dispatch(notification); verify(notification.Callback).onFailure(notification.CallbackIds); verify(notification.Callback, never()).onSuccess(notification.CallbackIds); @@ -145,7 +156,7 @@ public class AmbariSNMPDispatcherTest { Notification notification = mock(AlertNotification.class); notification.Callback = mock(DispatchCallback.class); notification.CallbackIds = mock(List.class); - Map<String, String> properties = new HashMap<String, String>(); + Map<String, String> properties = new HashMap<>(); properties.put(AmbariSNMPDispatcher.PORT_PROPERTY, "3"); properties.put(AmbariSNMPDispatcher.COMMUNITY_PROPERTY, "4"); properties.put(AmbariSNMPDispatcher.SNMP_VERSION_PROPERTY, "SNMPv1"); @@ -163,7 +174,7 @@ public class AmbariSNMPDispatcherTest { Notification notification = mock(AlertNotification.class); notification.Callback = mock(DispatchCallback.class); notification.CallbackIds = mock(List.class); - Map<String, String> properties = new HashMap<String, String>(); + Map<String, String> properties = new HashMap<>(); properties.put(AmbariSNMPDispatcher.PORT_PROPERTY, "3"); properties.put(AmbariSNMPDispatcher.COMMUNITY_PROPERTY, "4"); properties.put(AmbariSNMPDispatcher.SNMP_VERSION_PROPERTY, "SNMPv11"); @@ -181,7 +192,7 @@ public class AmbariSNMPDispatcherTest { Notification notification = mock(AlertNotification.class); notification.Callback = mock(DispatchCallback.class); notification.CallbackIds = mock(List.class); - Map<String, String> properties = new HashMap<String, String>(); + Map<String, String> properties = new HashMap<>(); properties.put(AmbariSNMPDispatcher.PORT_PROPERTY, "3"); properties.put(AmbariSNMPDispatcher.COMMUNITY_PROPERTY, "4"); properties.put(AmbariSNMPDispatcher.SNMP_VERSION_PROPERTY, "SNMPv1"); @@ -200,7 +211,7 @@ public class AmbariSNMPDispatcherTest { Notification notification = mock(AlertNotification.class); notification.Callback = mock(DispatchCallback.class); notification.CallbackIds = mock(List.class); - Map<String, String> properties = new HashMap<String, String>(); + Map<String, String> properties = new HashMap<>(); properties.put(AmbariSNMPDispatcher.PORT_PROPERTY, "3"); properties.put(AmbariSNMPDispatcher.COMMUNITY_PROPERTY, "4"); properties.put(AmbariSNMPDispatcher.SNMP_VERSION_PROPERTY, "SNMPv2c"); @@ -218,7 +229,7 @@ public class AmbariSNMPDispatcherTest { Notification notification = getAlertNotification(true); notification.Callback = mock(DispatchCallback.class); notification.CallbackIds = mock(List.class); - Map<String, String> properties = new HashMap<String, String>(); + Map<String, String> properties = new HashMap<>(); properties.put(AmbariSNMPDispatcher.PORT_PROPERTY, "162"); properties.put(AmbariSNMPDispatcher.COMMUNITY_PROPERTY, "public"); properties.put(AmbariSNMPDispatcher.SNMP_VERSION_PROPERTY, "SNMPv3"); @@ -242,11 +253,11 @@ public class AmbariSNMPDispatcherTest { Notification notification = getAlertNotification(true); PDU pdu = dispatcher.prepareTrap(notification, snmpVersion); assertEquals(PDU.V1TRAP, pdu.getType()); - Map<String, VariableBinding> variableBindings = new HashMap<String, VariableBinding>(); + Map<String, VariableBinding> variableBindings = new HashMap<>(); for (VariableBinding variableBinding : pdu.toArray()) { variableBindings.put(variableBinding.getOid().toString(), variableBinding); } - assertEquals(10, variableBindings.size()); + assertEquals(11, variableBindings.size()); assertEquals(AmbariSNMPDispatcher.AMBARI_ALERT_TRAP_OID, variableBindings.get(SnmpConstants.snmpTrapOID.toString()).toValueString()); assertTrue(variableBindings.get(SnmpConstants.snmpTrapOID.toString()).getVariable() instanceof OID); assertEquals(String.valueOf(DEFINITION_ID), variableBindings.get(AmbariSNMPDispatcher.AMBARI_ALERT_DEFINITION_ID_OID).toValueString()); @@ -274,11 +285,11 @@ public class AmbariSNMPDispatcherTest { AlertNotification notification = (AlertNotification) getAlertNotification(false); PDU pdu = dispatcher.prepareTrap(notification, snmpVersion); assertEquals(PDU.V1TRAP, pdu.getType()); - Map<String, VariableBinding> variableBindings = new HashMap<String, VariableBinding>(); + Map<String, VariableBinding> variableBindings = new HashMap<>(); for (VariableBinding variableBinding : pdu.toArray()) { variableBindings.put(variableBinding.getOid().toString(), variableBinding); } - assertEquals(10, variableBindings.size()); + assertEquals(11, variableBindings.size()); assertEquals("null", variableBindings.get(AmbariSNMPDispatcher.AMBARI_ALERT_COMPONENT_NAME_OID).toValueString()); } @@ -290,12 +301,12 @@ public class AmbariSNMPDispatcherTest { Notification notification = getAlertNotification(true); PDU pdu = dispatcher.prepareTrap(notification, snmpVersion); assertEquals(PDU.TRAP, pdu.getType()); - Map<String, VariableBinding> variableBindings = new HashMap<String, VariableBinding>(); + Map<String, VariableBinding> variableBindings = new HashMap<>(); for (VariableBinding variableBinding : pdu.toArray()) { variableBindings.put(variableBinding.getOid().toString(), variableBinding); } - assertEquals(10, variableBindings.size()); + assertEquals(11, variableBindings.size()); assertEquals(AmbariSNMPDispatcher.AMBARI_ALERT_TRAP_OID, variableBindings.get(SnmpConstants.snmpTrapOID.toString()).toValueString()); assertEquals(String.valueOf(DEFINITION_ID), variableBindings.get(AmbariSNMPDispatcher.AMBARI_ALERT_DEFINITION_ID_OID).toValueString()); assertEquals(DEFINITION_NAME, variableBindings.get(AmbariSNMPDispatcher.AMBARI_ALERT_DEFINITION_NAME_OID).toValueString()); @@ -314,7 +325,7 @@ public class AmbariSNMPDispatcherTest { AmbariSNMPDispatcher dispatcher = spy(new AmbariSNMPDispatcher(snmp)); PDU trap = mock(PDU.class); Notification notification = new AlertNotification(); - Map<String, String> properties = new HashMap<String, String>(); + Map<String, String> properties = new HashMap<>(); properties.put(AmbariSNMPDispatcher.COMMUNITY_PROPERTY, "public"); properties.put(AmbariSNMPDispatcher.PORT_PROPERTY, "162"); notification.DispatchProperties = properties; @@ -336,7 +347,7 @@ public class AmbariSNMPDispatcherTest { AmbariSNMPDispatcher dispatcher = spy(new AmbariSNMPDispatcher(snmp)); PDU trap = mock(PDU.class); Notification notification = new AlertNotification(); - Map<String, String> properties = new HashMap<String, String>(); + Map<String, String> properties = new HashMap<>(); properties.put(AmbariSNMPDispatcher.COMMUNITY_PROPERTY, "public"); properties.put(AmbariSNMPDispatcher.PORT_PROPERTY, "162"); notification.DispatchProperties = properties; @@ -358,7 +369,7 @@ public class AmbariSNMPDispatcherTest { AmbariSNMPDispatcher dispatcher = spy(new AmbariSNMPDispatcher(snmp)); PDU trap = mock(PDU.class); Notification notification = new AlertNotification(); - Map<String, String> properties = new HashMap<String, String>(); + Map<String, String> properties = new HashMap<>(); properties.put(AmbariSNMPDispatcher.PORT_PROPERTY, "162"); properties.put(AmbariSNMPDispatcher.SNMP_VERSION_PROPERTY, "SNMPv3"); properties.put(AmbariSNMPDispatcher.SECURITY_USERNAME_PROPERTY, "USER"); @@ -384,7 +395,7 @@ public class AmbariSNMPDispatcherTest { AmbariSNMPDispatcher dispatcher = spy(new AmbariSNMPDispatcher(snmp)); PDU trap = mock(PDU.class); Notification notification = new AlertNotification(); - Map<String, String> properties = new HashMap<String, String>(); + Map<String, String> properties = new HashMap<>(); properties.put(AmbariSNMPDispatcher.PORT_PROPERTY, "162"); properties.put(AmbariSNMPDispatcher.SNMP_VERSION_PROPERTY, "SNMPv3"); properties.put(AmbariSNMPDispatcher.SECURITY_USERNAME_PROPERTY, "USER"); @@ -401,7 +412,7 @@ public class AmbariSNMPDispatcherTest { @Test public void testValidateAlertValidation_SNMPv1() throws Exception { - Map<String, Object> properties = new HashMap<String, Object>(); + Map<String, Object> properties = new HashMap<>(); properties.put(SNMPDispatcher.PORT_PROPERTY, "162"); properties.put(AmbariSNMPDispatcher.SNMP_VERSION_PROPERTY, "SNMPv1"); properties.put(AmbariSNMPDispatcher.COMMUNITY_PROPERTY, "public"); @@ -412,7 +423,7 @@ public class AmbariSNMPDispatcherTest { @Test public void testValidateAlertValidation_incorrectSNMPversion() throws Exception { - Map<String, Object> properties = new HashMap<String, Object>(); + Map<String, Object> properties = new HashMap<>(); properties.put(AmbariSNMPDispatcher.PORT_PROPERTY, "162"); properties.put(AmbariSNMPDispatcher.SNMP_VERSION_PROPERTY, "SNMPv4"); properties.put(AmbariSNMPDispatcher.COMMUNITY_PROPERTY, "public"); @@ -423,7 +434,7 @@ public class AmbariSNMPDispatcherTest { @Test public void testValidateAlertValidation_SNMPv1_invalid_noPort() throws Exception { - Map<String, Object> properties = new HashMap<String, Object>(); + Map<String, Object> properties = new HashMap<>(); properties.put(AmbariSNMPDispatcher.SNMP_VERSION_PROPERTY, "SNMPv1"); properties.put(AmbariSNMPDispatcher.COMMUNITY_PROPERTY, "public"); NotificationDispatcher dispatcher = new AmbariSNMPDispatcher(DEFAULT_SNMP_PORT); @@ -433,7 +444,7 @@ public class AmbariSNMPDispatcherTest { @Test public void testValidateAlertValidation_SNMPv2c() throws Exception { - Map<String, Object> properties = new HashMap<String, Object>(); + Map<String, Object> properties = new HashMap<>(); properties.put(AmbariSNMPDispatcher.PORT_PROPERTY, "162"); properties.put(AmbariSNMPDispatcher.SNMP_VERSION_PROPERTY, "SNMPv2c"); properties.put(AmbariSNMPDispatcher.COMMUNITY_PROPERTY, "public"); @@ -444,7 +455,7 @@ public class AmbariSNMPDispatcherTest { @Test public void testValidateAlertValidation_SNMPv2c_invalid() throws Exception { - Map<String, Object> properties = new HashMap<String, Object>(); + Map<String, Object> properties = new HashMap<>(); properties.put(AmbariSNMPDispatcher.PORT_PROPERTY, "162"); properties.put(AmbariSNMPDispatcher.SNMP_VERSION_PROPERTY, "SNMPv2c"); NotificationDispatcher dispatcher = new AmbariSNMPDispatcher(DEFAULT_SNMP_PORT); @@ -454,7 +465,7 @@ public class AmbariSNMPDispatcherTest { @Test public void testValidateAlertValidation_SNMPv3_incorrectSecurityLevel() throws Exception { - Map<String, Object> properties = new HashMap<String, Object>(); + Map<String, Object> properties = new HashMap<>(); properties.put(AmbariSNMPDispatcher.PORT_PROPERTY, "162"); properties.put(AmbariSNMPDispatcher.SNMP_VERSION_PROPERTY, "SNMPv3"); properties.put(AmbariSNMPDispatcher.SECURITY_USERNAME_PROPERTY, "USER"); @@ -468,7 +479,7 @@ public class AmbariSNMPDispatcherTest { @Test public void testValidateAlertValidation_SNMPv3_noAuthNoPriv() throws Exception { - Map<String, Object> properties = new HashMap<String, Object>(); + Map<String, Object> properties = new HashMap<>(); properties.put(AmbariSNMPDispatcher.PORT_PROPERTY, "162"); properties.put(AmbariSNMPDispatcher.SNMP_VERSION_PROPERTY, "SNMPv3"); properties.put(AmbariSNMPDispatcher.SECURITY_USERNAME_PROPERTY, "USER"); @@ -480,7 +491,7 @@ public class AmbariSNMPDispatcherTest { @Test public void testValidateAlertValidation_SNMPv3_AuthNoPriv_valid() throws Exception { - Map<String, Object> properties = new HashMap<String, Object>(); + Map<String, Object> properties = new HashMap<>(); properties.put(AmbariSNMPDispatcher.PORT_PROPERTY, "162"); properties.put(AmbariSNMPDispatcher.SNMP_VERSION_PROPERTY, "SNMPv3"); properties.put(AmbariSNMPDispatcher.SECURITY_USERNAME_PROPERTY, "USER"); @@ -493,7 +504,7 @@ public class AmbariSNMPDispatcherTest { @Test public void testValidateAlertValidation_SNMPv3_AuthNoPriv_invalid() throws Exception { - Map<String, Object> properties = new HashMap<String, Object>(); + Map<String, Object> properties = new HashMap<>(); properties.put(AmbariSNMPDispatcher.PORT_PROPERTY, "162"); properties.put(AmbariSNMPDispatcher.SNMP_VERSION_PROPERTY, "SNMPv3"); properties.put(AmbariSNMPDispatcher.SECURITY_USERNAME_PROPERTY, "USER"); @@ -505,7 +516,7 @@ public class AmbariSNMPDispatcherTest { @Test public void testValidateAlertValidation_SNMPv3_AuthPriv_valid() throws Exception { - Map<String, Object> properties = new HashMap<String, Object>(); + Map<String, Object> properties = new HashMap<>(); properties.put(AmbariSNMPDispatcher.PORT_PROPERTY, "162"); properties.put(AmbariSNMPDispatcher.SNMP_VERSION_PROPERTY, "SNMPv3"); properties.put(AmbariSNMPDispatcher.SECURITY_USERNAME_PROPERTY, "USER"); @@ -519,7 +530,7 @@ public class AmbariSNMPDispatcherTest { @Test public void testValidateAlertValidation_SNMPv3_AuthPriv_noPassphrases() throws Exception { - Map<String, Object> properties = new HashMap<String, Object>(); + Map<String, Object> properties = new HashMap<>(); properties.put(AmbariSNMPDispatcher.PORT_PROPERTY, "162"); properties.put(AmbariSNMPDispatcher.SNMP_VERSION_PROPERTY, "SNMPv3"); properties.put(AmbariSNMPDispatcher.SECURITY_USERNAME_PROPERTY, "USER"); @@ -531,7 +542,7 @@ public class AmbariSNMPDispatcherTest { @Test public void testValidateAlertValidation_SNMPv3_AuthPriv_onlyAuthPassphrase() throws Exception { - Map<String, Object> properties = new HashMap<String, Object>(); + Map<String, Object> properties = new HashMap<>(); properties.put(AmbariSNMPDispatcher.PORT_PROPERTY, "162"); properties.put(AmbariSNMPDispatcher.SNMP_VERSION_PROPERTY, "SNMPv3"); properties.put(AmbariSNMPDispatcher.SECURITY_USERNAME_PROPERTY, "USER"); http://git-wip-us.apache.org/repos/asf/ambari/blob/473abc32/ambari-server/src/test/java/org/apache/ambari/server/state/services/AlertNoticeDispatchServiceTest.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/java/org/apache/ambari/server/state/services/AlertNoticeDispatchServiceTest.java b/ambari-server/src/test/java/org/apache/ambari/server/state/services/AlertNoticeDispatchServiceTest.java index ee29762..31000a4 100644 --- a/ambari-server/src/test/java/org/apache/ambari/server/state/services/AlertNoticeDispatchServiceTest.java +++ b/ambari-server/src/test/java/org/apache/ambari/server/state/services/AlertNoticeDispatchServiceTest.java @@ -19,11 +19,15 @@ package org.apache.ambari.server.state.services; import static org.easymock.EasyMock.createNiceMock; import static org.easymock.EasyMock.createStrictMock; +import static org.easymock.EasyMock.expect; +import static org.easymock.EasyMock.replay; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; +import java.lang.management.ManagementFactory; +import java.lang.management.RuntimeMXBean; import java.math.BigDecimal; import java.util.ArrayList; import java.util.Calendar; @@ -36,27 +40,39 @@ import java.util.UUID; import java.util.Vector; import java.util.concurrent.Executor; +import javax.persistence.EntityManager; + import org.apache.ambari.server.api.services.AmbariMetaInfo; import org.apache.ambari.server.notifications.DispatchFactory; import org.apache.ambari.server.notifications.Notification; import org.apache.ambari.server.notifications.NotificationDispatcher; import org.apache.ambari.server.notifications.TargetConfigurationResult; import org.apache.ambari.server.notifications.dispatchers.AmbariSNMPDispatcher; -import org.apache.ambari.server.orm.InMemoryDefaultTestModule; +import org.apache.ambari.server.orm.DBAccessor; +import org.apache.ambari.server.orm.dao.AlertDefinitionDAO; import org.apache.ambari.server.orm.dao.AlertDispatchDAO; +import org.apache.ambari.server.orm.dao.AlertsDAO; import org.apache.ambari.server.orm.entities.AlertDefinitionEntity; import org.apache.ambari.server.orm.entities.AlertHistoryEntity; import org.apache.ambari.server.orm.entities.AlertNoticeEntity; import org.apache.ambari.server.orm.entities.AlertTargetEntity; +import org.apache.ambari.server.stack.StackManagerFactory; import org.apache.ambari.server.state.AlertState; +import org.apache.ambari.server.state.Cluster; +import org.apache.ambari.server.state.Clusters; import org.apache.ambari.server.state.NotificationState; import org.apache.ambari.server.state.alert.Scope; import org.apache.ambari.server.state.alert.SourceType; import org.apache.ambari.server.state.alert.TargetType; +import org.apache.ambari.server.state.stack.OsFamily; import org.easymock.EasyMock; import org.junit.Assert; import org.junit.Before; import org.junit.Test; +import org.junit.runner.RunWith; +import org.powermock.api.easymock.PowerMock; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; import org.snmp4j.CommandResponder; import org.snmp4j.CommandResponderEvent; import org.snmp4j.PDU; @@ -68,6 +84,7 @@ import org.snmp4j.smi.GenericAddress; import org.snmp4j.smi.Integer32; import org.snmp4j.smi.OID; import org.snmp4j.smi.OctetString; +import org.snmp4j.smi.TimeTicks; import org.snmp4j.smi.VariableBinding; import org.snmp4j.transport.DefaultUdpTransportMapping; @@ -75,11 +92,12 @@ import com.google.inject.Binder; import com.google.inject.Guice; import com.google.inject.Injector; import com.google.inject.Module; -import com.google.inject.util.Modules; /** * Tests the {@link AlertNoticeDispatchService}. */ +@RunWith(PowerMockRunner.class) +@PrepareForTest({ AmbariSNMPDispatcher.class, ManagementFactory.class }) public class AlertNoticeDispatchServiceTest extends AlertNoticeDispatchService { final static String ALERT_NOTICE_UUID_1 = UUID.randomUUID().toString(); @@ -92,9 +110,10 @@ public class AlertNoticeDispatchServiceTest extends AlertNoticeDispatchService { private DispatchFactory m_dispatchFactory = null; private AlertDispatchDAO m_dao = null; private Injector m_injector; + private RuntimeMXBean m_runtimeMXBean; - List<AlertDefinitionEntity> m_definitions = new ArrayList<AlertDefinitionEntity>(); - List<AlertHistoryEntity> m_histories = new ArrayList<AlertHistoryEntity>(); + List<AlertDefinitionEntity> m_definitions = new ArrayList<>(); + List<AlertHistoryEntity> m_histories = new ArrayList<>(); @Before public void before() { @@ -103,8 +122,7 @@ public class AlertNoticeDispatchServiceTest extends AlertNoticeDispatchService { m_metaInfo = createNiceMock(AmbariMetaInfo.class); // create an injector which will inject the mocks - m_injector = Guice.createInjector(Modules.override( - new InMemoryDefaultTestModule()).with(new MockModule())); + m_injector = Guice.createInjector(new MockModule()); Assert.assertNotNull(m_injector); @@ -150,7 +168,17 @@ public class AlertNoticeDispatchServiceTest extends AlertNoticeDispatchService { m_histories.add(history); } } - } + + // mock out the uptime to be a while (since most tests are not testing + // system uptime) + m_runtimeMXBean = EasyMock.createNiceMock(RuntimeMXBean.class); + PowerMock.mockStatic(ManagementFactory.class); + expect(ManagementFactory.getRuntimeMXBean()).andReturn(m_runtimeMXBean).atLeastOnce(); + PowerMock.replay(ManagementFactory.class); + expect(m_runtimeMXBean.getUptime()).andReturn(360000L).atLeastOnce(); + + replay( m_runtimeMXBean); + } /** * Tests the parsing of the {@link AlertHistoryEntity} list into @@ -381,7 +409,8 @@ public class AlertNoticeDispatchServiceTest extends AlertNoticeDispatchService { List<Vector> expectedTrapVectors = new LinkedList<>(); Vector firstVector = new Vector(); - firstVector.add(new VariableBinding(SnmpConstants.snmpTrapOID, new OID(AmbariSNMPDispatcher.AMBARI_ALERT_TRAP_OID))); + firstVector.add(new VariableBinding(SnmpConstants.sysUpTime, new TimeTicks(360000L))); + firstVector.add(new VariableBinding(SnmpConstants.snmpTrapOID, new OID(AmbariSNMPDispatcher.AMBARI_ALERT_TRAP_OID))); firstVector.add(new VariableBinding(new OID(AmbariSNMPDispatcher.AMBARI_ALERT_DEFINITION_ID_OID), new Integer32(new BigDecimal(1L).intValueExact()))); firstVector.add(new VariableBinding(new OID(AmbariSNMPDispatcher.AMBARI_ALERT_DEFINITION_NAME_OID), new OctetString("alert-definition-1"))); firstVector.add(new VariableBinding(new OID(AmbariSNMPDispatcher.AMBARI_ALERT_DEFINITION_HASH_OID), new OctetString("1"))); @@ -517,7 +546,7 @@ public class AlertNoticeDispatchServiceTest extends AlertNoticeDispatchService { notice.setAlertHistory(history); notice.setNotifyState(NotificationState.PENDING); - ArrayList<AlertNoticeEntity> notices = new ArrayList<AlertNoticeEntity>(); + ArrayList<AlertNoticeEntity> notices = new ArrayList<>(); notices.add(notice); return notices; @@ -575,7 +604,7 @@ public class AlertNoticeDispatchServiceTest extends AlertNoticeDispatchService { notice2.setAlertHistory(history2); notice2.setNotifyState(NotificationState.PENDING); - ArrayList<AlertNoticeEntity> notices = new ArrayList<AlertNoticeEntity>(); + ArrayList<AlertNoticeEntity> notices = new ArrayList<>(); notices.add(notice1); notices.add(notice2); @@ -637,7 +666,7 @@ public class AlertNoticeDispatchServiceTest extends AlertNoticeDispatchService { private static class MockSnmpDispatcher implements NotificationDispatcher { - private List<Notification> m_notifications = new ArrayList<Notification>(); + private List<Notification> m_notifications = new ArrayList<>(); /** * {@inheritDoc} @@ -762,9 +791,20 @@ public class AlertNoticeDispatchServiceTest extends AlertNoticeDispatchService { */ @Override public void configure(Binder binder) { + Cluster cluster = EasyMock.createNiceMock(Cluster.class); binder.bind(AlertDispatchDAO.class).toInstance(m_dao); binder.bind(DispatchFactory.class).toInstance(m_dispatchFactory); + binder.bind(StackManagerFactory.class).toInstance(createNiceMock(StackManagerFactory.class)); binder.bind(AmbariMetaInfo.class).toInstance(m_metaInfo); + binder.bind(Clusters.class).toInstance(createNiceMock(Clusters.class)); + binder.bind(OsFamily.class).toInstance(createNiceMock(OsFamily.class)); + binder.bind(DBAccessor.class).toInstance(createNiceMock(DBAccessor.class)); + binder.bind(Cluster.class).toInstance(cluster); + binder.bind(AlertDefinitionDAO.class).toInstance(createNiceMock(AlertDefinitionDAO.class)); + binder.bind(AlertsDAO.class).toInstance(createNiceMock(AlertsDAO.class)); + binder.bind(EntityManager.class).toInstance(createNiceMock(EntityManager.class)); + + binder.bind(AlertNoticeDispatchService.class).toInstance(new AlertNoticeDispatchService()); EasyMock.expect(m_metaInfo.getServerVersion()).andReturn("2.0.0").anyTimes(); EasyMock.replay(m_metaInfo); @@ -782,6 +822,7 @@ public class AlertNoticeDispatchServiceTest extends AlertNoticeDispatchService { receivedTrapsVectors = new LinkedList<>(); CommandResponder trapPrinter = new CommandResponder() { + @Override public synchronized void processPdu(CommandResponderEvent e){ PDU command = e.getPDU(); if (command != null) {
