@Test public void testConstructorThresholdNull() { ThrowableAnticipator ta = new ThrowableAnticipator(); ta.anticipate(new IllegalArgumentException("threshold argument cannot be null")); try { new ThresholdEvaluatorStateRelativeChange(null); } catch (Throwable t) { ta.throwableReceived(t); } ta.verifyAnticipated(); }
@Override public Event getEventForState(Status status, Date date, double dsValue, CollectionResourceWrapper resource) { if (status == Status.TRIGGERED) { final String uei=getThresholdConfig().getTriggeredUEI().orElse(EventConstants.RELATIVE_CHANGE_THRESHOLD_EVENT_UEI); return createBasicEvent(uei, date, dsValue, resource); } else { return null; } }
private Event createBasicEvent(String uei, Date date, double dsValue, CollectionResourceWrapper resource) { Map<String,String> params = new HashMap<String,String>(); params.put("previousValue", formatValue(getPreviousTriggeringSample())); params.put("multiplier", Double.toString(getThresholdConfig().getValue())); // params.put("trigger", Integer.toString(getThresholdConfig().getTrigger())); // params.put("rearm", Double.toString(getThresholdConfig().getRearm())); return createBasicEvent(uei, date, dsValue, resource, params); }
threshold.setTrigger(3); ThresholdConfigWrapper wrapper=new ThresholdConfigWrapper(threshold); ThresholdEvaluatorStateRelativeChange evaluator = new ThresholdEvaluatorStateRelativeChange(wrapper); assertEquals("should not trigger", Status.NO_CHANGE, evaluator.evaluate(8.0)); assertEquals("should trigger", Status.TRIGGERED, evaluator.evaluate(10.0)); Event event = evaluator.getEventForState(Status.TRIGGERED, new Date(), 10.0, null); assertNotNull("should have created an event", event); assertEquals("UEIs should be the same", EventConstants.RELATIVE_CHANGE_THRESHOLD_EVENT_UEI, event.getUei()); event = evaluator.getEventForState(Status.TRIGGERED, new Date(), 10.0, new MockCollectionResourceWrapper("testInstance")); assertNotNull("should have created an event", event); assertEquals("UEIs should be the same", EventConstants.RELATIVE_CHANGE_THRESHOLD_EVENT_UEI, event.getUei());
@Override public Status evaluate(double dsValue) { //Fix for Bug 2275 so we handle negative numbers //It will not handle values which cross the 0 boundary (from - to +, or v.v.) properly, but // after some discussion, we can't come up with a sensible scenario when that would actually happen. // If such a scenario eventuates, reconsider dsValue=Math.abs(dsValue); if (getLastSample() != 0.0) { double threshold = getMultiplier() * getLastSample(); if (getMultiplier() < 1.0) { if (dsValue <= threshold) { setPreviousTriggeringSample(getLastSample()); setLastSample(dsValue); return Status.TRIGGERED; } } else { if (dsValue >= threshold) { setPreviousTriggeringSample(getLastSample()); setLastSample(dsValue); return Status.TRIGGERED; } } setLastSample(dsValue); } setLastSample(dsValue); return Status.NO_CHANGE; }
@Test public void testNegativeValueNoChange() { Threshold threshold = new Threshold(); threshold.setType(ThresholdType.RELATIVE_CHANGE); threshold.setDsName("ds-name"); threshold.setDsType("node"); threshold.setValue(0.9); threshold.setRearm(0.5); threshold.setTrigger(1); ThresholdConfigWrapper wrapper = new ThresholdConfigWrapper(threshold); ThresholdEvaluatorStateRelativeChange evaluator = new ThresholdEvaluatorStateRelativeChange( wrapper); assertEquals("should not trigger", Status.NO_CHANGE, evaluator .evaluate(-10.0)); assertEquals("should not trigger", Status.NO_CHANGE, evaluator .evaluate(-10.0)); } }
@Test public void testGetEventForStateCustomUEIS() { String triggeredUEI="uei.opennms.org/custom/relativeChangeThresholdTriggered"; Threshold threshold = new Threshold(); threshold.setType(ThresholdType.RELATIVE_CHANGE); threshold.setDsName("ds-name"); threshold.setDsType("node"); threshold.setValue(99.0); threshold.setRearm(95.0); threshold.setTrigger(1); threshold.setTriggeredUEI(triggeredUEI); ThresholdConfigWrapper wrapper=new ThresholdConfigWrapper(threshold); ThresholdEvaluatorStateRelativeChange item = new ThresholdEvaluatorStateRelativeChange(wrapper); Event event=item.getEventForState(Status.TRIGGERED, new Date(), 100.0, null); assertEquals("UEI should be the uei.opennms.org/custom/relativeChangeThresholdTriggered", triggeredUEI, event.getUei()); }
@Test public void testEvaluateThriceTriggerHighFirstZero() { Threshold threshold = new Threshold(); threshold.setType(ThresholdType.RELATIVE_CHANGE); threshold.setDsName("ds-name"); threshold.setDsType("node"); threshold.setValue(1.1); threshold.setRearm(0.5); threshold.setTrigger(3); ThresholdConfigWrapper wrapper=new ThresholdConfigWrapper(threshold); ThresholdEvaluatorStateRelativeChange evaluator = new ThresholdEvaluatorStateRelativeChange(wrapper); assertEquals("should not trigger on first evaluate", Status.NO_CHANGE, evaluator.evaluate(0.0)); assertEquals("should not trigger on second evaluate", Status.NO_CHANGE, evaluator.evaluate(1000.0)); assertEquals("should trigger on third evaluate", Status.TRIGGERED, evaluator.evaluate(1200.0)); }
@Test public void testEvaluateTwiceTriggerLowEqual() { Threshold threshold = new Threshold(); threshold.setType(ThresholdType.RELATIVE_CHANGE); threshold.setDsName("ds-name"); threshold.setDsType("node"); threshold.setValue(0.9); threshold.setRearm(0.5); threshold.setTrigger(3); ThresholdConfigWrapper wrapper=new ThresholdConfigWrapper(threshold); ThresholdEvaluatorStateRelativeChange evaluator = new ThresholdEvaluatorStateRelativeChange(wrapper); assertEquals("should not trigger", Status.NO_CHANGE, evaluator.evaluate(10.0)); assertEquals("should trigger", Status.TRIGGERED, evaluator.evaluate(9.0)); }
@Test public void testEvaluateTwiceTriggerLowBelow() { Threshold threshold = new Threshold(); threshold.setType(ThresholdType.RELATIVE_CHANGE); threshold.setDsName("ds-name"); threshold.setDsType("node"); threshold.setValue(0.9); threshold.setRearm(0.5); threshold.setTrigger(3); ThresholdConfigWrapper wrapper=new ThresholdConfigWrapper(threshold); ThresholdEvaluatorStateRelativeChange evaluator = new ThresholdEvaluatorStateRelativeChange(wrapper); assertEquals("should not trigger", Status.NO_CHANGE, evaluator.evaluate(10.0)); assertEquals("should trigger", Status.TRIGGERED, evaluator.evaluate(8.0)); }
@Test public void testGetEventForStateDefaultUEIS() { Threshold threshold = new Threshold(); threshold.setType(ThresholdType.RELATIVE_CHANGE); threshold.setDsName("ds-name"); threshold.setDsType("node"); threshold.setValue(99.0); threshold.setRearm(95.0); threshold.setTrigger(1); ThresholdConfigWrapper wrapper=new ThresholdConfigWrapper(threshold); ThresholdEvaluatorStateRelativeChange item = new ThresholdEvaluatorStateRelativeChange(wrapper); Event event=item.getEventForState(Status.TRIGGERED, new Date(), 100.0, null); assertEquals("UEI should be the relativeChangeThresholdTriggerd", EventConstants.RELATIVE_CHANGE_THRESHOLD_EVENT_UEI, event.getUei()); }
@Test public void testEvaluateTwiceNoTriggerHighBelow() { Threshold threshold = new Threshold(); threshold.setType(ThresholdType.RELATIVE_CHANGE); threshold.setDsName("ds-name"); threshold.setDsType("node"); threshold.setValue(1.1); threshold.setRearm(0.5); threshold.setTrigger(3); ThresholdConfigWrapper wrapper=new ThresholdConfigWrapper(threshold); ThresholdEvaluatorStateRelativeChange evaluator = new ThresholdEvaluatorStateRelativeChange(wrapper); assertEquals("should not trigger", Status.NO_CHANGE, evaluator.evaluate(10.0)); assertEquals("should not trigger", Status.NO_CHANGE, evaluator.evaluate(10.5)); }
@Test public void testEvaluateTwiceNoTriggerLowAbove() { Threshold threshold = new Threshold(); threshold.setType(ThresholdType.RELATIVE_CHANGE); threshold.setDsName("ds-name"); threshold.setDsType("node"); threshold.setValue(0.9); threshold.setRearm(0.5); threshold.setTrigger(3); ThresholdConfigWrapper wrapper=new ThresholdConfigWrapper(threshold); ThresholdEvaluatorStateRelativeChange evaluator = new ThresholdEvaluatorStateRelativeChange(wrapper); assertEquals("should not trigger", Status.NO_CHANGE, evaluator.evaluate(10.0)); assertEquals("should not trigger", Status.NO_CHANGE, evaluator.evaluate(9.5)); }
@Test public void testEvaluateTwiceNoTriggerHighFirstZero() { Threshold threshold = new Threshold(); threshold.setType(ThresholdType.RELATIVE_CHANGE); threshold.setDsName("ds-name"); threshold.setDsType("node"); threshold.setValue(1.1); threshold.setRearm(0.5); threshold.setTrigger(3); ThresholdConfigWrapper wrapper=new ThresholdConfigWrapper(threshold); ThresholdEvaluatorStateRelativeChange evaluator = new ThresholdEvaluatorStateRelativeChange(wrapper); assertEquals("should not trigger on first evaluate", Status.NO_CHANGE, evaluator.evaluate(0.0)); assertEquals("should not trigger on second evaluate", Status.NO_CHANGE, evaluator.evaluate(1000.0)); }
@Test public void testEvaluateTwiceTriggerHighEqual() { Threshold threshold = new Threshold(); threshold.setType(ThresholdType.RELATIVE_CHANGE); threshold.setDsName("ds-name"); threshold.setDsType("node"); threshold.setValue(1.1); threshold.setRearm(0.5); threshold.setTrigger(3); ThresholdConfigWrapper wrapper=new ThresholdConfigWrapper(threshold); ThresholdEvaluatorStateRelativeChange evaluator = new ThresholdEvaluatorStateRelativeChange(wrapper); assertEquals("should not trigger", Status.NO_CHANGE, evaluator.evaluate(10.0)); assertEquals("should trigger", Status.TRIGGERED, evaluator.evaluate(11.0)); }
@Test public void testEvaluateTwiceTriggerHighAbove() { Threshold threshold = new Threshold(); threshold.setType(ThresholdType.RELATIVE_CHANGE); threshold.setDsName("ds-name"); threshold.setDsType("node"); threshold.setValue(1.1); threshold.setRearm(0.5); threshold.setTrigger(3); ThresholdConfigWrapper wrapper=new ThresholdConfigWrapper(threshold); ThresholdEvaluatorStateRelativeChange evaluator = new ThresholdEvaluatorStateRelativeChange(wrapper); assertEquals("should not trigger", Status.NO_CHANGE, evaluator.evaluate(10.0)); assertEquals("should trigger", Status.TRIGGERED, evaluator.evaluate(12.0)); }
@Test public void testEvaluateOnce() { Threshold threshold = new Threshold(); threshold.setType(ThresholdType.RELATIVE_CHANGE); threshold.setDsName("ds-name"); threshold.setDsType("node"); threshold.setValue(0.9); threshold.setRearm(0.5); threshold.setTrigger(3); ThresholdConfigWrapper wrapper=new ThresholdConfigWrapper(threshold); ThresholdEvaluatorStateRelativeChange evaluator = new ThresholdEvaluatorStateRelativeChange(wrapper); assertEquals("should not trigger", Status.NO_CHANGE, evaluator.evaluate(10.0)); }
@Test public void testNegativeNumberNotTriggers() { Threshold threshold = new Threshold(); threshold.setType(ThresholdType.RELATIVE_CHANGE); threshold.setDsName("ds-name"); threshold.setDsType("node"); threshold.setValue(1.1); threshold.setRearm(0.5); threshold.setTrigger(3); ThresholdConfigWrapper wrapper=new ThresholdConfigWrapper(threshold); ThresholdEvaluatorStateRelativeChange evaluator = new ThresholdEvaluatorStateRelativeChange(wrapper); assertEquals("should not trigger", Status.NO_CHANGE, evaluator.evaluate(-10.0)); assertEquals("should not trigger", Status.NO_CHANGE, evaluator.evaluate(-10.5)); }
@Test public void testGetEventForStateNoChange() { Threshold threshold = new Threshold(); threshold.setType(ThresholdType.RELATIVE_CHANGE); threshold.setDsName("ds-name"); threshold.setDsType("node"); threshold.setValue(1.1); threshold.setRearm(0.5); threshold.setTrigger(3); ThresholdConfigWrapper wrapper=new ThresholdConfigWrapper(threshold); ThresholdEvaluatorStateRelativeChange evaluator = new ThresholdEvaluatorStateRelativeChange(wrapper); assertNull("should not have created an event", evaluator.getEventForState(Status.NO_CHANGE, new Date(), 10.0, null)); }
/** {@inheritDoc} */ @Override public ThresholdEvaluatorState getThresholdEvaluatorState(BaseThresholdDefConfigWrapper threshold) { return new ThresholdEvaluatorStateRelativeChange(threshold); }