GetVariableHandler implements OperationHandler<GetVariable> { @Override public Object doOperation(final GetVariable operation, final Context context, final Store store) throws OperationException { if (operation.getVariableName() == null) { throw new IllegalArgumentException("Variable name cannot be null"); } return context.getVariable(operation.getVariableName()); }  @Override Object doOperation(final GetVariable operation, final Context context, final Store store);  }
@Test public void shouldGetVariableWhenExists() throws OperationException { final Context context = mock(Context.class); final GetVariableHandler handler = new GetVariableHandler(); final GetVariable op = new GetVariable.Builder().variableName(varName).build(); given(context.getVariable(varName)).willReturn(varVal); final Object variableValueFromOp = handler.doOperation(op, context, store); assertEquals(varVal, variableValueFromOp); }
@Test public void shouldReturnNullWhenVariableDoesntExist() throws OperationException { final Context context = mock(Context.class); final GetVariableHandler handler = new GetVariableHandler(); final GetVariable op = new GetVariable.Builder().variableName(varName).build(); given(context.getVariable(varName)).willReturn(null); final Object variableValueFromOp = handler.doOperation(op, context, store); assertNull(variableValueFromOp); }
@Test public void shouldThrowExceptionWhenVariableKeyIsNull() throws OperationException { final Context context = mock(Context.class); final GetVariableHandler handler = new GetVariableHandler(); final GetVariable op = new GetVariable.Builder().variableName(null).build(); 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 shouldNotThrowNPEWhenVariablesSet() throws OperationException { final Context context = new Context(new User()); final GetVariableHandler handler = new GetVariableHandler(); final GetVariable op = new GetVariable.Builder().variableName(varName).build(); final Object variableValueFromOp = handler.doOperation(op, context, store); assertNull(variableValueFromOp); }