SetVariableHandler implements OperationHandler<SetVariable> { @Override public Void doOperation(final SetVariable operation, final Context context, final Store store) throws OperationException { if (null == operation.getVariableName()) { throw new IllegalArgumentException("Variable name cannot be null"); } if (null == operation.getInput()) { throw new IllegalArgumentException("Variable input value cannot be null"); } context.setVariable(operation.getVariableName(), operation.getInput()); return null; }  @Override Void doOperation(final SetVariable operation, final Context context, final Store store);  }
@Test public void shouldSetVariableInContext() throws OperationException { final Context context = new Context(new User()); final Store store = mock(Store.class); final String testVarName = "testVarName"; final int testVarValue = 4; SetVariableHandler handler = new SetVariableHandler(); SetVariable op = new SetVariable.Builder().variableName(testVarName).input(testVarValue).build(); handler.doOperation(op, context, store); assertTrue(context.getVariable(testVarName).equals(testVarValue)); assertTrue(context.getVariables().equals(ImmutableMap.of(testVarName, testVarValue))); }
@Test public void shouldThrowExceptionWithNullVariableKey() throws OperationException { final Context context = new Context(new User()); final Store store = mock(Store.class); SetVariableHandler handler = new SetVariableHandler(); SetVariable op = new SetVariable(); try { handler.doOperation(op, context, store); fail("Exception expected"); } catch (final IllegalArgumentException e) { assertTrue(e.getMessage().contains("Variable name cannot be null")); } }
@Test public void shouldNotAllowNullInputVariableToBeAdded() throws OperationException { final Context context = new Context(new User()); final Store store = mock(Store.class); final String testVarName = "testVarName"; final Object testVarValue = null; SetVariableHandler handler = new SetVariableHandler(); SetVariable op = new SetVariable.Builder().variableName(testVarName).input(testVarValue).build(); try { handler.doOperation(op, context, store); fail("Exception expected"); } catch (final IllegalArgumentException e) { assertTrue(e.getMessage().contains("Variable input value cannot be null")); } }
@Test public void setTwoVarsWithoutFailure() throws OperationException { final Context context = new Context(new User()); final Store store = mock(Store.class); final String varName = "testVarName"; final String varVal = "varVal"; final String varName1 = "testVarName1"; final String varVal1 = "varVal1"; SetVariableHandler handler = new SetVariableHandler(); SetVariable op = new SetVariable.Builder() .variableName(varName) .input(varVal) .build(); SetVariable op1 = new SetVariable.Builder() .variableName(varName1) .input(varVal1) .build(); handler.doOperation(op, context, store); handler.doOperation(op1, context, store); assertEquals(2, context.getVariables().size()); assertEquals(ImmutableMap.of(varName, varVal, varName1, varVal1), context.getVariables()); }