WDSObjectField implements IField { @Override public <T> void out(final IObject env, final T in) throws ChangeValueException, InvalidArgumentException { if (null == env) { throw new InvalidArgumentException("Environment should not be null."); } Object value = in; try { for (IObject rule : rules) { List<String> args = (ArrayList<String>) rule.getValue(this.args); String name = (String) rule.getValue(this.strategyName); IFunction<StrategyAndArgs, Object> func = this.strategyExecutors.get(name); if (null == func) { func = this.strategyExecutors.get(OUTER_STRATEGY_NAME); } value = func.execute( new StrategyAndArgs(this.strategies.get(name), env, args, value) ); } } catch (Exception e) { throw new ChangeValueException("Could not apply rules.", e); } } WDSObjectField(final List<IObject> methodRules); @Override T in(final IObject env); @Override T in(final IObject env, final Class type); @Override void out(final IObject env, final T in);  }
@Test public void checkOutMethod() throws Exception { IResolveDependencyStrategy strategy1 = mock(IResolveDependencyStrategy.class); IOC.resolve( Keys.getOrAdd(IResolveDependencyStrategy.class.getCanonicalName()), "GetValue", strategy1 ); IResolveDependencyStrategy strategy2 = mock(IResolveDependencyStrategy.class); IOC.resolve( Keys.getOrAdd(IResolveDependencyStrategy.class.getCanonicalName()), "Transform", strategy2 ); when(strategy1.resolve(2, 1)).thenReturn(3); when(strategy2.resolve("CONST", 3)).thenReturn("CONST3"); IObject rule1 = mock(IObject.class); IObject rule2 = mock(IObject.class); IObject rule3 = mock(IObject.class); IObject env = mock(IObject.class); IObject message = mock(IObject.class); IObject response = mock(IObject.class); when(rule1.getValue(new FieldName("name"))).thenReturn("GetValue"); when(rule1.getValue(new FieldName("args"))).thenReturn(new ArrayList<String>(){{add("local/value"); add("message/intValue");}}); when(rule2.getValue(new FieldName("name"))).thenReturn("Transform"); when(rule2.getValue(new FieldName("args"))).thenReturn(new ArrayList<String>(){{add("const/CONST"); add("local/value");}}); when(rule3.getValue(new FieldName("name"))).thenReturn("wds_target_strategy"); when(rule3.getValue(new FieldName("args"))).thenReturn(new ArrayList<String>(){{add("local/value"); add("response/intValue");}}); when(env.getValue(new FieldName("message"))).thenReturn(message); when(env.getValue(new FieldName("response"))).thenReturn(response); when(message.getValue(new FieldName("intValue"))).thenReturn(1); doNothing().when(response).setValue(new FieldName("intValue"), "CONST3"); IField field = new WDSObjectField(new ArrayList<IObject>(){{add(rule1);add(rule2);add(rule3);}}); field.out(env, 2); verify(response, times(1)).setValue(new FieldName("intValue"), "CONST3"); }
@Test (expected = InvalidArgumentException.class) public void checkOutMethodExceptionOnNullArgs() throws Exception { IObject rule = mock(IObject.class); when(rule.getValue(new FieldName("name"))).thenReturn("wds_target_strategy"); IField field = new WDSObjectField(new ArrayList<IObject>(){{add(rule);}}); field.out(null, 1); fail(); }
@Test (expected = ChangeValueException.class) public void checkOutMethodExceptionOnWrongArgs() throws Exception { IResolveDependencyStrategy strategy1 = mock(IResolveDependencyStrategy.class); IOC.resolve( Keys.getOrAdd(IResolveDependencyStrategy.class.getCanonicalName()), "GetValue", strategy1 ); IObject rule = mock(IObject.class); IObject env = mock(IObject.class); when(rule.getValue(new FieldName("name"))).thenReturn("wds_target_strategy"); when(rule.getValue(new FieldName("args"))).thenReturn(new ArrayList<String>(){{add("response/getInt");}}); IField field = new WDSObjectField(new ArrayList<IObject>(){{add(rule);}}); field.out(env, 1); fail(); }