ThresholdEvaluatorStateHighLow item = new ThresholdEvaluatorStateHighLow(wrapper); Event event=item.getEventForState(Status.TRIGGERED, new Date(), 100.0, null); assertEquals("UEI should be the highThresholdExceededUEI", EventConstants.HIGH_THRESHOLD_EVENT_UEI, event.getUei()); parmPresentAndValueNonNull(event, "instance"); event=item.getEventForState(Status.RE_ARMED, new Date(), 94.0, null); assertEquals("UEI should be the highThresholdRearmedUEI", EventConstants.HIGH_THRESHOLD_REARM_EVENT_UEI, event.getUei()); parmPresentAndValueNonNull(event, "instance"); event=item.getEventForState(Status.TRIGGERED, new Date(), 100.0, new MockCollectionResourceWrapper("testInstance")); assertEquals("UEI should be the highThresholdExceededUEI", EventConstants.HIGH_THRESHOLD_EVENT_UEI, event.getUei()); parmPresentWithValue(event, "instance", "testInstance"); event=item.getEventForState(Status.RE_ARMED, new Date(), 94.0, new MockCollectionResourceWrapper("testInstance")); assertEquals("UEI should be the highThresholdRearmedUEI", EventConstants.HIGH_THRESHOLD_REARM_EVENT_UEI, event.getUei()); parmPresentWithValue(event, "instance", "testInstance"); event=item.getEventForState(Status.TRIGGERED, new Date(), 94.0, null); assertEquals("UEI should be the lowThresholdExceededUEI", EventConstants.LOW_THRESHOLD_EVENT_UEI, event.getUei()); parmPresentAndValueNonNull(event, "instance"); event=item.getEventForState(Status.RE_ARMED, new Date(), 100.0, null); assertEquals("UEI should be the lowThresholdRearmedUEI", EventConstants.LOW_THRESHOLD_REARM_EVENT_UEI, event.getUei()); parmPresentAndValueNonNull(event, "instance"); event=item.getEventForState(Status.TRIGGERED, new Date(), 94.0, new MockCollectionResourceWrapper("testInstance")); assertEquals("UEI should be the lowThresholdExceededUEI", EventConstants.LOW_THRESHOLD_EVENT_UEI, event.getUei()); parmPresentWithValue(event, "instance", "testInstance");
@Override public Status evaluate(double dsValue) { if (isThresholdExceeded(dsValue)) { if (isArmed()) { setExceededCount(getExceededCount() + 1); LOG.debug("evaluate: {} threshold exceeded, count={}", getType(), getExceededCount()); if (isTriggerCountExceeded()) { LOG.debug("evaluate: {} threshold triggered", getType()); setExceededCount(1); setArmed(false); return Status.TRIGGERED; } } } else if (isRearmExceeded(dsValue)) { if (!isArmed()) { LOG.debug("evaluate: {} threshold rearmed", getType()); setArmed(true); setExceededCount(0); return Status.RE_ARMED; } if (getExceededCount() > 0) { LOG.debug("evaluate: resetting {} threshold count to 0, because the current value indicates that the in-progress threshold has been rearmed, but it doesn't triggered yet.", getType()); setExceededCount(0); } } else { LOG.debug("evaluate: resetting {} threshold count to 0", getType()); setExceededCount(0); } return Status.NO_CHANGE; }
switch (status) { case TRIGGERED: uei=getThresholdConfig().getTriggeredUEI().orElse(null); if (ThresholdType.LOW.equals(getThresholdConfig().getType())) { if(uei==null || "".equals(uei)) { uei=EventConstants.LOW_THRESHOLD_EVENT_UEI; return createBasicEvent(uei, date, dsValue, resource); } else if (ThresholdType.HIGH.equals(getThresholdConfig().getType())) { if(uei==null || "".equals(uei)) { uei=EventConstants.HIGH_THRESHOLD_EVENT_UEI; return createBasicEvent(uei, date, dsValue, resource); } else { throw new IllegalArgumentException("Threshold type " + getThresholdConfig().getType() + " is not supported"); uei=getThresholdConfig().getRearmedUEI().orElse(null); if (ThresholdType.LOW.equals(getThresholdConfig().getType())) { if(uei==null || "".equals(uei)) { uei=EventConstants.LOW_THRESHOLD_REARM_EVENT_UEI; return createBasicEvent(uei, date, dsValue, resource); } else if (ThresholdType.HIGH.equals(getThresholdConfig().getType())) { if(uei==null || "".equals(uei)) { uei=EventConstants.HIGH_THRESHOLD_REARM_EVENT_UEI; return createBasicEvent(uei, date, dsValue, resource); } else { throw new IllegalArgumentException("Threshold type " + getThresholdConfig().getType() + " is not supported");
@Test public void testGetEventForStateCustomUEIS() { String triggeredUEI="uei.opennms.org/custom/thresholdTriggered"; String rearmedUEI="uei.opennms.org/custom/thresholdRearmed"; Threshold threshold = new Threshold(); threshold.setType(ThresholdType.HIGH); threshold.setDsName("ds-name"); threshold.setDsType("node"); threshold.setValue(99.0); threshold.setRearm(95.0); threshold.setTrigger(1); threshold.setTriggeredUEI(triggeredUEI); threshold.setRearmedUEI(rearmedUEI); ThresholdConfigWrapper wrapper=new ThresholdConfigWrapper(threshold); ThresholdEvaluatorStateHighLow item = new ThresholdEvaluatorStateHighLow(wrapper); Event event=item.getEventForState(Status.TRIGGERED, new Date(), 100.0, null); assertEquals("UEI should be the uei.opennms.org/custom/thresholdTriggered", triggeredUEI, event.getUei()); parmPresentAndValueNonNull(event, "instance"); event=item.getEventForState(Status.RE_ARMED, new Date(), 94.0, null); assertEquals("UEI should be the uei.opennms.org/custom/thresholdRearmed", rearmedUEI, event.getUei()); parmPresentAndValueNonNull(event, "instance"); } }
@Test public void testEvaluateHighTriggerTwiceNoRetrigger() { Threshold threshold = new Threshold(); threshold.setType(ThresholdType.HIGH); threshold.setDsName("ds-name"); threshold.setDsType("node"); threshold.setValue(99.0); threshold.setRearm(0.5); threshold.setTrigger(2); ThresholdConfigWrapper wrapper=new ThresholdConfigWrapper(threshold); ThresholdEvaluatorState item = new ThresholdEvaluatorStateHighLow(wrapper); Status status = item.evaluate(100.0); assertEquals("first threshold evaluation status", Status.NO_CHANGE, status); status = item.evaluate(100.0); assertEquals("second threshold evaluation status", Status.TRIGGERED, status); status = item.evaluate(100.0); assertEquals("third threshold evaluation status", Status.NO_CHANGE, status); status = item.evaluate(100.0); assertEquals("fourth threshold evaluation status", Status.NO_CHANGE, status); }
@Test public void testIsThresholdExceededBogusType() { Threshold threshold = new Threshold(); threshold.setType(ThresholdType.RELATIVE_CHANGE); threshold.setDsName("ds-name"); threshold.setDsType("node"); threshold.setValue(99.0); threshold.setRearm(0.5); threshold.setTrigger(1); ThresholdConfigWrapper wrapper=new ThresholdConfigWrapper(threshold); ThrowableAnticipator ta = new ThrowableAnticipator(); ta.anticipate(new IllegalStateException("This thresholding strategy can only be used for thresholding types of 'high' and 'low'.")); try { ThresholdEvaluatorStateHighLow item = new ThresholdEvaluatorStateHighLow(wrapper); item.isThresholdExceeded(98.0); } catch (Throwable t) { ta.throwableReceived(t); } ta.verifyAnticipated(); }
@Test public void testIsRearmExceededBogusType() { Threshold threshold = new Threshold(); threshold.setType(ThresholdType.RELATIVE_CHANGE); threshold.setDsName("ds-name"); threshold.setDsType("node"); threshold.setValue(99.0); threshold.setRearm(0.5); threshold.setTrigger(1); ThresholdConfigWrapper wrapper=new ThresholdConfigWrapper(threshold); ThrowableAnticipator ta = new ThrowableAnticipator(); ta.anticipate(new IllegalStateException("This thresholding strategy can only be used for thresholding types of 'high' and 'low'.")); try { ThresholdEvaluatorStateHighLow item = new ThresholdEvaluatorStateHighLow(wrapper); item.isThresholdExceeded(0.0); } catch (Throwable t) { ta.throwableReceived(t); } ta.verifyAnticipated(); }
@Test public void testConstructorThresholdNoValue() { ThrowableAnticipator ta = new ThrowableAnticipator(); ta.anticipate(new IllegalArgumentException("threshold must have a 'value' value set")); Threshold threshold = new Threshold(); threshold.setType(ThresholdType.HIGH); threshold.setDsName("ds-name"); threshold.setDsType("node"); threshold.setRearm(0.5); threshold.setTrigger(3); ThresholdConfigWrapper wrapper=new ThresholdConfigWrapper(threshold); try { new ThresholdEvaluatorStateHighLow(wrapper); } catch (Throwable t) { ta.throwableReceived(t); } ta.verifyAnticipated(); }
@Test public void testConstructorThresholdNoDsType() { ThrowableAnticipator ta = new ThrowableAnticipator(); ta.anticipate(new IllegalArgumentException("threshold must have a 'ds-type' value set")); Threshold threshold = new Threshold(); threshold.setType(ThresholdType.HIGH); threshold.setDsName("ds-name"); threshold.setValue(1.0); threshold.setRearm(0.5); threshold.setTrigger(3); ThresholdConfigWrapper wrapper=new ThresholdConfigWrapper(threshold); try { new ThresholdEvaluatorStateHighLow(wrapper); } catch (Throwable t) { ta.throwableReceived(t); } ta.verifyAnticipated(); }
@Test public void testConstructorThresholdNoDsName() { ThrowableAnticipator ta = new ThrowableAnticipator(); ta.anticipate(new IllegalArgumentException("threshold must have a 'ds-name' value set")); Threshold threshold = new Threshold(); threshold.setType(ThresholdType.HIGH); threshold.setDsType("node"); threshold.setValue(1.0); threshold.setRearm(0.5); threshold.setTrigger(3); ThresholdConfigWrapper wrapper=new ThresholdConfigWrapper(threshold); try { new ThresholdEvaluatorStateHighLow(wrapper); } catch (Throwable t) { ta.throwableReceived(t); } ta.verifyAnticipated(); }
@Test public void testConstructorThresholdNoTrigger() { ThrowableAnticipator ta = new ThrowableAnticipator(); ta.anticipate(new IllegalArgumentException("threshold must have a 'trigger' value set")); Threshold threshold = new Threshold(); threshold.setType(ThresholdType.HIGH); threshold.setDsName("ds-name"); threshold.setDsType("node"); threshold.setValue(1.0); threshold.setRearm(0.5); ThresholdConfigWrapper wrapper=new ThresholdConfigWrapper(threshold); try { new ThresholdEvaluatorStateHighLow(wrapper); } catch (Throwable t) { ta.throwableReceived(t); } ta.verifyAnticipated(); }
@Test public void testIsTriggerCountExceededNotAfterReArm() { Threshold threshold = new Threshold(); threshold.setType(ThresholdType.HIGH); threshold.setDsName("ds-name"); threshold.setDsType("node"); threshold.setValue(99.0); threshold.setRearm(0.5); threshold.setTrigger(1); ThresholdConfigWrapper wrapper=new ThresholdConfigWrapper(threshold); ThresholdEvaluatorStateHighLow item = new ThresholdEvaluatorStateHighLow(wrapper); assertFalse("trigger count should not be exeeded before exceeding value", item.isTriggerCountExceeded()); Status status = item.evaluate(100.0); assertEquals("first threshold evaluation status", Status.TRIGGERED, status); assertTrue("trigger count should be exeeded after exceeding value", item.isTriggerCountExceeded()); status = item.evaluate(0.0); assertEquals("first threshold evaluation status", Status.RE_ARMED, status); assertFalse("trigger count should be reset after being rearmed", item.isTriggerCountExceeded()); }
@Test public void testConstructorThresholdNoType() { ThrowableAnticipator ta = new ThrowableAnticipator(); ta.anticipate(new IllegalArgumentException("threshold must have a 'type' value set")); Threshold threshold = new Threshold(); threshold.setDsName("ds-name"); threshold.setDsType("node"); threshold.setValue(1.0); threshold.setRearm(0.5); threshold.setTrigger(3); ThresholdConfigWrapper wrapper=new ThresholdConfigWrapper(threshold); try { new ThresholdEvaluatorStateHighLow(wrapper); } catch (Throwable t) { ta.throwableReceived(t); } ta.verifyAnticipated(); }
@Test public void testEvaluateHighTriggerRearm() { Threshold threshold = new Threshold(); threshold.setType(ThresholdType.HIGH); threshold.setDsName("ds-name"); threshold.setDsType("node"); threshold.setValue(99.0); threshold.setRearm(0.5); threshold.setTrigger(1); ThresholdConfigWrapper wrapper=new ThresholdConfigWrapper(threshold); ThresholdEvaluatorState item = new ThresholdEvaluatorStateHighLow(wrapper); Status status = item.evaluate(100.0); assertEquals("first threshold evaluation status", Status.TRIGGERED, status); status = item.evaluate(0.0); assertEquals("second threshold evaluation status", Status.RE_ARMED, status); }
@Test public void testEvaluateHighTriggerRearmTriggerAgain() { Threshold threshold = new Threshold(); threshold.setType(ThresholdType.HIGH); threshold.setDsName("ds-name"); threshold.setDsType("node"); threshold.setValue(99.0); threshold.setRearm(0.5); threshold.setTrigger(1); ThresholdConfigWrapper wrapper=new ThresholdConfigWrapper(threshold); ThresholdEvaluatorState item = new ThresholdEvaluatorStateHighLow(wrapper); Status status = item.evaluate(100.0); assertEquals("first threshold evaluation status", Status.TRIGGERED, status); status = item.evaluate(0.0); assertEquals("second threshold evaluation status", Status.RE_ARMED, status); status = item.evaluate(100.0); assertEquals("third threshold evaluation status", Status.TRIGGERED, status); }
@Test public void testConstructorThresholdNoRearm() { ThrowableAnticipator ta = new ThrowableAnticipator(); ta.anticipate(new IllegalArgumentException("threshold must have a 'rearm' value set")); Threshold threshold = new Threshold(); threshold.setType(ThresholdType.HIGH); threshold.setDsName("ds-name"); threshold.setDsType("node"); threshold.setValue(1.0); threshold.setTrigger(3); ThresholdConfigWrapper wrapper=new ThresholdConfigWrapper(threshold); try { new ThresholdEvaluatorStateHighLow(wrapper); } catch (Throwable t) { ta.throwableReceived(t); } ta.verifyAnticipated(); }
@Test public void testEvaluateHighTriggerTwice() { Threshold threshold = new Threshold(); threshold.setType(ThresholdType.HIGH); threshold.setDsName("ds-name"); threshold.setDsType("node"); threshold.setValue(99.0); threshold.setRearm(0.5); threshold.setTrigger(2); ThresholdConfigWrapper wrapper=new ThresholdConfigWrapper(threshold); ThresholdEvaluatorState item = new ThresholdEvaluatorStateHighLow(wrapper); Status status = item.evaluate(100.0); assertEquals("first threshold evaluation status", Status.NO_CHANGE, status); status = item.evaluate(100.0); assertEquals("second threshold evaluation status", Status.TRIGGERED, status); }
@Test public void testIsTriggerCountExceededNotAtTriggerValueTwo() { Threshold threshold = new Threshold(); threshold.setType(ThresholdType.HIGH); threshold.setDsName("ds-name"); threshold.setDsType("node"); threshold.setValue(99.0); threshold.setRearm(0.5); threshold.setTrigger(2); ThresholdConfigWrapper wrapper=new ThresholdConfigWrapper(threshold); ThresholdEvaluatorStateHighLow item = new ThresholdEvaluatorStateHighLow(wrapper); assertFalse("trigger count should not be exeeded before exceeding value", item.isTriggerCountExceeded()); Status status = item.evaluate(100.0); assertEquals("first threshold evaluation status", Status.NO_CHANGE, status); assertFalse("trigger count should not be exeeded after only exceeding value once", item.isTriggerCountExceeded()); }
@Test public void testIsTriggerCountExceededAtTriggerValueOne() { Threshold threshold = new Threshold(); threshold.setType(ThresholdType.HIGH); threshold.setDsName("ds-name"); threshold.setDsType("node"); threshold.setValue(99.0); threshold.setRearm(0.5); threshold.setTrigger(1); ThresholdConfigWrapper wrapper=new ThresholdConfigWrapper(threshold); ThresholdEvaluatorStateHighLow item = new ThresholdEvaluatorStateHighLow(wrapper); assertFalse("trigger count should not be exeeded before exceeding value", item.isTriggerCountExceeded()); Status status = item.evaluate(100.0); assertEquals("first threshold evaluation status", Status.TRIGGERED, status); assertTrue("trigger count should be exeeded after exceeding value (and being triggered)", item.isTriggerCountExceeded()); }
@Test public void testEvaluateHighNoTrigger() { Threshold threshold = new Threshold(); threshold.setType(ThresholdType.HIGH); threshold.setDsName("ds-name"); threshold.setDsType("node"); threshold.setValue(101.0); threshold.setRearm(0.5); threshold.setTrigger(1); ThresholdConfigWrapper wrapper=new ThresholdConfigWrapper(threshold); ThresholdEvaluatorState item = new ThresholdEvaluatorStateHighLow(wrapper); Status status = item.evaluate(100.0); assertEquals("threshold evaluation status", Status.NO_CHANGE, status); }