Author: scottmf Date: 2009-12-11 10:31:06 -0800 (Fri, 11 Dec 2009) New Revision: 14075 URL: http://svn.hyperic.org/?view=rev&root=Hyperic+HQ&revision=14075 Added: trunk/unittest/data/escalationManagerTests.README trunk/unittest/data/escalationManagerTests.xml.gz trunk/unittest/src/org/hyperic/hq/escalation/server/session/EscalationManagerTest.java trunk/unittest/src/org/hyperic/hq/escalation/server/session/EscalationManagerTestEJBImpl.java Modified: trunk/build.xml trunk/src/org/hyperic/dao/DAOFactory.java trunk/src/org/hyperic/hq/dao/HibernateDAOFactory.java trunk/src/org/hyperic/hq/escalation/server/session/EscalationManagerEJBImpl.java Log: [HHQ-3499] added extra check to make sure the associated alertId is valid. added test to exercise that part of the code Modified: trunk/build.xml =================================================================== --- trunk/build.xml 2009-12-11 18:25:38 UTC (rev 14074) +++ trunk/build.xml 2009-12-11 18:31:06 UTC (rev 14075) @@ -1530,6 +1530,7 @@ <!-- Package the test jars and deploy into the EAR --> <jar file="${ear.dir}/hq-test.jar" basedir="${build.dir}/classes" > <include name="org/hyperic/**/*_test*"/> + <include name="org/hyperic/**/*Test*"/> </jar> <mkdir dir="${ear.dir}/hq-session-test.jar/META-INF" /> Modified: trunk/src/org/hyperic/dao/DAOFactory.java =================================================================== --- trunk/src/org/hyperic/dao/DAOFactory.java 2009-12-11 18:25:38 UTC (rev 14074) +++ trunk/src/org/hyperic/dao/DAOFactory.java 2009-12-11 18:31:06 UTC (rev 14075) @@ -53,6 +53,7 @@ import org.hyperic.hq.events.server.session.ActionDAO; import org.hyperic.hq.events.server.session.AlertActionLogDAO; import org.hyperic.hq.events.server.session.AlertConditionLogDAO; +import org.hyperic.hq.events.server.session.AlertDAO; import org.hyperic.hq.events.server.session.AlertDefinitionDAO; import org.hyperic.hq.events.server.session.TriggerDAO; @@ -81,6 +82,7 @@ // Event DAOs public abstract ActionDAO getActionDAO(); public abstract AlertDefinitionDAO getAlertDefDAO(); + public abstract AlertDAO getAlertDAO(); public abstract TriggerDAO getTriggerDAO(); public abstract AlertActionLogDAO getAlertActionLogDAO(); public abstract AlertConditionLogDAO getAlertConditionLogDAO(); Modified: trunk/src/org/hyperic/hq/dao/HibernateDAOFactory.java =================================================================== --- trunk/src/org/hyperic/hq/dao/HibernateDAOFactory.java 2009-12-11 18:25:38 UTC (rev 14074) +++ trunk/src/org/hyperic/hq/dao/HibernateDAOFactory.java 2009-12-11 18:31:06 UTC (rev 14075) @@ -49,6 +49,7 @@ import org.hyperic.hq.events.server.session.ActionDAO; import org.hyperic.hq.events.server.session.AlertActionLogDAO; import org.hyperic.hq.events.server.session.AlertConditionLogDAO; +import org.hyperic.hq.events.server.session.AlertDAO; import org.hyperic.hq.events.server.session.AlertDefinitionDAO; import org.hyperic.hq.events.server.session.TriggerDAO; import org.hyperic.hq.galerts.server.session.ExecutionStrategyTypeInfoDAO; @@ -195,4 +196,8 @@ public ExecutionStrategyTypeInfoDAO getExecutionStrategyTypeInfoDAO() { return new ExecutionStrategyTypeInfoDAO(this); } + + public AlertDAO getAlertDAO() { + return new AlertDAO(this); + } } Modified: trunk/src/org/hyperic/hq/escalation/server/session/EscalationManagerEJBImpl.java =================================================================== --- trunk/src/org/hyperic/hq/escalation/server/session/EscalationManagerEJBImpl.java 2009-12-11 18:25:38 UTC (rev 14074) +++ trunk/src/org/hyperic/hq/escalation/server/session/EscalationManagerEJBImpl.java 2009-12-11 18:31:06 UTC (rev 14075) @@ -40,7 +40,6 @@ import org.hyperic.dao.DAOFactory; import org.hyperic.hq.authz.server.session.AuthzSubject; import org.hyperic.hq.authz.server.session.AuthzSubjectManagerEJBImpl; -import org.hyperic.hq.authz.server.session.Resource; import org.hyperic.hq.authz.server.shared.ResourceDeletedException; import org.hyperic.hq.authz.shared.PermissionException; import org.hyperic.hq.common.ApplicationException; @@ -61,7 +60,8 @@ import org.hyperic.hq.events.Notify; import org.hyperic.hq.events.server.session.Action; import org.hyperic.hq.events.server.session.ActionManagerEJBImpl; -import org.hyperic.hq.events.server.session.AlertDefinitionManagerEJBImpl; +import org.hyperic.hq.events.server.session.Alert; +import org.hyperic.hq.events.server.session.AlertDAO; import org.hyperic.hq.events.server.session.AlertRegulator; import org.hyperic.hq.events.server.session.ClassicEscalationAlertType; import org.hyperic.hq.events.server.session.SessionBase; @@ -437,6 +437,11 @@ private void endEscalation(EscalationState state) { if (state != null) { + // make sure we have the updated state to avoid StaleStateExceptions + state = _stateDAO.findById(state.getId()); + if (state == null) { + return; + } _stateDAO.remove(state); EscalationRuntime.getInstance().unscheduleEscalation(state); } @@ -485,14 +490,15 @@ // XXX -- Need to make sure the application is running before // we allow this to proceed - _log.debug("Executing state[" + s.getId() + "]"); + final boolean debug = _log.isDebugEnabled(); + if (debug) _log.debug("Executing state[" + s.getId() + "]"); if (actionIdx >= e.getActions().size()) { if (e.isRepeat() && e.getActions().size() > 0) { actionIdx = 0; // Loop back } else { - _log.debug("Reached the end of the escalation state[" + - s.getId() + "]. Ending it"); + if (debug) _log.debug("Reached the end of the escalation state[" + + s.getId() + "]. Ending it"); endEscalation(s); return; } @@ -501,6 +507,14 @@ eAction = (EscalationAction)e.getActions().get(actionIdx); action = eAction.getAction(); + AlertDAO dao = DAOFactory.getDAOFactory().getAlertDAO(); + Alert alert = dao.getById(new Integer(s.getAlertId())); + // HHQ-3499, need to make sure that the alertId that is pointed to by + // the escalation still exists + if (alert == null) { + endEscalation(s); + return; + } Escalatable esc = getEscalatable(s); // HQ-1348: End escalation if alert is already fixed @@ -516,8 +530,8 @@ long nextTime = System.currentTimeMillis() + Math.max(offset, eAction.getWaitTime()); - _log.debug("Moving onto next state of escalation, but chillin' for " - + eAction.getWaitTime() + " ms"); + if (debug) _log.debug("Moving onto next state of escalation, but chillin' for " + + eAction.getWaitTime() + " ms"); s.setNextAction(actionIdx + 1); s.setNextActionTime(nextTime); s.setAcknowledgedBy(null); Added: trunk/unittest/data/escalationManagerTests.README =================================================================== --- trunk/unittest/data/escalationManagerTests.README (rev 0) +++ trunk/unittest/data/escalationManagerTests.README 2009-12-11 18:31:06 UTC (rev 14075) @@ -0,0 +1,39 @@ +bin/exportTable.sh -l jdbc:oracle:thin:@localhost:1522:hqdb -u hqadmin -p hqadmin \ +-t EAM_AGENT,\ +EAM_AUDIT,\ +EAM_CONFIG_RESPONSE,\ +EAM_CRISPO,\ +EAM_CRISPO_OPT,\ +EAM_CRITERIA,\ +EAM_DASH_CONFIG,\ +EAM_MEASUREMENT,\ +EAM_MEASUREMENT_CAT,\ +EAM_MEASUREMENT_TEMPL,\ +EAM_MONITORABLE_TYPE,\ +EAM_OPERATION,\ +EAM_PLATFORM,\ +EAM_PLATFORM_SERVER_TYPE_MAP,\ +EAM_PLATFORM_TYPE,\ +EAM_RES_GRP_RES_MAP,\ +EAM_RESOURCE,\ +EAM_RESOURCE_EDGE,\ +EAM_RESOURCE_GROUP,\ +EAM_RESOURCE_RELATION,\ +EAM_RESOURCE_TYPE,\ +EAM_ROLE,\ +EAM_ROLE_OPERATION_MAP,\ +EAM_ROLE_RESOURCE_GROUP_MAP,\ +EAM_SERVER,\ +EAM_SERVER_TYPE,\ +EAM_SERVICE,\ +EAM_SERVICE_TYPE,\ +EAM_SUBJECT,\ +EAM_SUBJECT_ROLE_MAP,\ +EAM_CONFIG_PROPS,\ +EAM_ESCALATION_STATE,\ +EAM_ESCALATION_ACTION,\ +EAM_ESCALATION,\ +EAM_ALERT_DEFINITION,\ +EAM_ALERT,\ +EAM_ALERT_CONDITION,\ +EAM_ACTION -d escalationManagerTest Added: trunk/unittest/data/escalationManagerTests.xml.gz =================================================================== (Binary files differ) Property changes on: trunk/unittest/data/escalationManagerTests.xml.gz ___________________________________________________________________ Name: svn:mime-type + application/octet-stream Added: trunk/unittest/src/org/hyperic/hq/escalation/server/session/EscalationManagerTest.java =================================================================== --- trunk/unittest/src/org/hyperic/hq/escalation/server/session/EscalationManagerTest.java (rev 0) +++ trunk/unittest/src/org/hyperic/hq/escalation/server/session/EscalationManagerTest.java 2009-12-11 18:31:06 UTC (rev 14075) @@ -0,0 +1,37 @@ +package org.hyperic.hq.escalation.server.session; + +import org.hyperic.hq.escalation.shared.EscalationManagerTestLocal; +import org.hyperic.util.unittest.server.BaseServerTestCase; +import org.hyperic.util.unittest.server.LocalInterfaceRegistry; + +public class EscalationManagerTest extends BaseServerTestCase { + + private static final String FILENAME = "escalationManagerTests.xml.gz"; + private LocalInterfaceRegistry _registry; + + public EscalationManagerTest(String name) { + super(name, true); + } + + public void setUp() throws Exception { + super.setUp(); + super.insertSchemaData(FILENAME); + _registry = deployHQ(); + } + + public void testExecuteStateWithInvalidAlertId() throws Exception { + EscalationManagerTestLocal eMan = (EscalationManagerTestLocal) + _registry.getLocalInterface( + EscalationManagerTestEJBImpl.class, + EscalationManagerTestLocal.class); + eMan.testExecuteStateWithInvalidAlertId(); + } + + public void tearDown() throws Exception { + super.tearDown(); + super.deleteSchemaData(FILENAME); + super.tearDown(); + undeployHQ(); + } + +} Added: trunk/unittest/src/org/hyperic/hq/escalation/server/session/EscalationManagerTestEJBImpl.java =================================================================== --- trunk/unittest/src/org/hyperic/hq/escalation/server/session/EscalationManagerTestEJBImpl.java (rev 0) +++ trunk/unittest/src/org/hyperic/hq/escalation/server/session/EscalationManagerTestEJBImpl.java 2009-12-11 18:31:06 UTC (rev 14075) @@ -0,0 +1,95 @@ +package org.hyperic.hq.escalation.server.session; + +import java.rmi.RemoteException; +import java.util.Collection; + +import javax.ejb.CreateException; +import javax.ejb.EJBException; +import javax.ejb.SessionBean; +import javax.ejb.SessionContext; + +import junit.framework.Assert; + +import org.hyperic.dao.DAOFactory; +import org.hyperic.hq.common.SystemException; +import org.hyperic.hq.escalation.shared.EscalationManagerLocal; +import org.hyperic.hq.escalation.shared.EscalationManagerTestLocal; +import org.hyperic.hq.escalation.shared.EscalationManagerTestUtil; +import org.hyperic.hq.events.server.session.AlertManagerEJBImpl; +import org.hyperic.hq.events.shared.AlertManagerLocal; +import org.hyperic.hq.measurement.MeasurementConstants; + +/** + * The session bean implementing the in-container unit tests for the + * AuthzSubjectManager. + * + * @ejb:bean name="EscalationManagerTest" + * jndi-name="ejb/authz/EscalationManagerTest" + * local-jndi-name="LocalEscalationManagerTest" + * view-type="local" + * type="Stateless" + * + * @ejb:util generate="physical" + * @ejb:transaction type="NotSupported" + */ +public class EscalationManagerTestEJBImpl implements SessionBean { + + public static EscalationManagerTestLocal getOne() { + try { + return EscalationManagerTestUtil.getLocalHome().create(); + } catch (Exception e) { + throw new SystemException(e); + } + } + + /** + * @ejb:interface-method + */ + public void testExecuteStateWithInvalidAlertId() throws Exception { + EscalationManagerLocal eMan = EscalationManagerEJBImpl.getOne(); + AlertManagerLocal aMan = AlertManagerEJBImpl.getOne(); + EscalationState state = new EscalationState(); + state.setAcknowledgedBy(null); + state.setNextAction(0); + state.setNextActionTime(System.currentTimeMillis()+MeasurementConstants.DAY); + int alertId = 10105; + Escalation esc = eMan.findById(new Integer(100)); + state.setEscalation(esc); + state.setAlertId(alertId); + state.setAlertDefinitionId(10001); + state.setAlertTypeEnum(-559038737); + getOne().runEscalation(state); + aMan.deleteAlerts(new Integer[] {new Integer(alertId)}); + Assert.assertNull( + "alert 10105 should not exist", aMan.getAlertById(new Integer(10105))); + try { + // should exit without any errors + eMan.executeState(state.getId()); + } catch (Exception e) { + Assert.assertTrue(e.getMessage(), false); + throw e; + } + Assert.assertNull( + "escalationStateId " + state.getId() + " should not exist", + getEscalationStateDAO().get(state.getId())); + } + + /** + * @ejb:transaction type="Required" + * @ejb:interface-method + */ + public void runEscalation(EscalationState state) { + getEscalationStateDAO().save(state); + EscalationRuntime.getInstance().scheduleEscalation(state); + } + + private EscalationStateDAO getEscalationStateDAO() { + return new EscalationStateDAO(DAOFactory.getDAOFactory()); + } + + public void ejbCreate() throws CreateException {} + public void ejbActivate() throws EJBException, RemoteException {} + public void ejbPassivate() throws EJBException, RemoteException {} + public void ejbRemove() throws EJBException, RemoteException {} + public void setSessionContext(SessionContext arg0) {} +} |