/** * {@inheritDoc} */ @Override public String toString() { return "SessionVariableInjection [variableName=" + variableName + ", optional=" + optional + ", getInjectee()=" + getInjectee() + "]"; }
/** * {@inheritDoc} */ @Override public Object determineValueToInject(ExecutionContext context) throws RuleExecutionException { // Fail fast if value is missing completely and not optional. if (!isOptional() && !context.getSessionParameters().containsKey(getVariableName())) { throw new RuleExecutionException("Non optional session variable \'" + getVariableName() + "\' not available.", context); } // This might return null if a null value is available for a certain key. // Needs to be discussed how to handle this. return context.getSessionParameters().get(getVariableName()); }
@Test(expectedExceptions = RuleExecutionException.class) public void shouldFailDueToMissingVariable() throws RuleExecutionException { // execute new SessionVariableInjection("var", false, RuleDummy.sessionVariableIntField()).execute(context); } }
@Override public SessionVariableInjection visit(SessionVariable annotation, Field field) { return new SessionVariableInjection(annotation.name(), annotation.optional(), field); } });
injection.execute(ctx);
@Test public void shouldRespectOptional() throws RuleExecutionException { // execute new SessionVariableInjection("var", true, RuleDummy.sessionVariableIntField()).execute(context); // verify assertThat(ruleImpl.sessionIntVariable, is(nullValue())); }
@Test public void testWithRuleAnnotation() throws Exception { RuleDefinition definition = Rules.define(ValidAndAnnotated.class); assertThat(definition.getName(), is("AnnotatedRule")); assertThat(definition.getDescription(), is("Description")); assertThat(definition.getFireCondition().getTagTypes(), containsInAnyOrder("T1", "T2")); // Test tag injections TagInjection tagInjection = new TagInjection("T1", ValidAndAnnotated.class.getDeclaredField("t1AsTag"), TagValue.InjectionStrategy.BY_TAG); TagInjection tagInjection1 = new TagInjection("T2", ValidAndAnnotated.class.getDeclaredField("t2TagValue"), TagValue.InjectionStrategy.BY_VALUE); assertThat(definition.getTagInjections(), is(notNullValue())); assertThat(definition.getTagInjections(), containsInAnyOrder(tagInjection, tagInjection1)); // Test session variables SessionVariableInjection s1 = new SessionVariableInjection("baseline", false, ValidAndAnnotated.class.getDeclaredField("baseline")); SessionVariableInjection s2 = new SessionVariableInjection("baseline2", true, ValidAndAnnotated.class.getDeclaredField("baseline2")); assertThat(definition.getSessionVariableInjections(), containsInAnyOrder(s1, s2)); // Test action method assertThat(definition.getActionMethod(), is(new ActionMethod(ValidAndAnnotated.class.getDeclaredMethod("action"), "T2", Action.Quantity.SINGLE))); // Test condition method ConditionMethod conditionMethod = new ConditionMethod("myCondition", "No way out", ValidAndAnnotated.class.getDeclaredMethod("condition")); assertThat(definition.getConditionMethods(), containsInAnyOrder(conditionMethod)); }
@Test public void shouldInjectValue() throws RuleExecutionException { // prepare doReturn(true).when(variables).containsKey("var"); when(variables.get("var")).thenReturn(123); // execute new SessionVariableInjection("var", false, RuleDummy.sessionVariableIntField()).execute(context); // verify assertThat(ruleImpl.sessionIntVariable, is(123)); }