GetVariablesHandler implements OperationHandler<GetVariables> { @Override public Map<String, Object> doOperation(final GetVariables operation, final Context context, final Store store) throws OperationException { final Map<String, Object> variableMap = new HashMap<>(); List<String> variableNames = operation.getVariableNames() != null ? operation.getVariableNames() : Lists.newArrayList(); for (final String key : variableNames) { if (null != key) { variableMap.put(key, context.getVariable(key)); } } return variableMap; }  @Override Map<String, Object> doOperation(final GetVariables operation, final Context context, final Store store);  }
@Test public void shouldGetAllVariableValuesWhenAllPresent() throws OperationException { final Context context = mock(Context.class); given(context.getVariable(key1)).willReturn(val1); given(context.getVariable(key2)).willReturn(val2); given(context.getVariable(key3)).willReturn(val3); final GetVariables op = new GetVariables.Builder().variableNames(Arrays.asList(key1, key2, key3)).build(); final GetVariablesHandler handler = new GetVariablesHandler(); Map<String, Object> resultMap = handler.doOperation(op, context, store); assertEquals(ImmutableMap.of(key1, val1, key2, val2, key3, val3), resultMap); }
@Test public void shouldReturnEmptyMapWhenNoValuesPresent() throws OperationException { final Context context = mock(Context.class); given(context.getVariable(key1)).willReturn(null); given(context.getVariable(key2)).willReturn(null); given(context.getVariable(key3)).willReturn(null); final GetVariables op = new GetVariables.Builder().variableNames(Arrays.asList(key1, key2, key3)).build(); final GetVariablesHandler handler = new GetVariablesHandler(); Map<String, Object> resultMap = handler.doOperation(op, context, store); final Map expected = new HashMap<>(); expected.put(key1, null); expected.put(key2, null); expected.put(key3, null); assertEquals(expected, resultMap); }
@Test public void shouldReturnPartiallyFilledMapWhenSomeValuesPresent() throws OperationException { final Context context = mock(Context.class); given(context.getVariable(key1)).willReturn(val1); given(context.getVariable(key2)).willReturn(null); given(context.getVariable(key3)).willReturn(val3); final GetVariables op = new GetVariables.Builder().variableNames(Arrays.asList(key1, key2, key3)).build(); final GetVariablesHandler handler = new GetVariablesHandler(); final Map<String, Object> resultMap = handler.doOperation(op, context, store); final Map expected = new HashMap<>(); expected.put(key1, val1); expected.put(key2, null); expected.put(key3, val3); assertEquals(expected, resultMap); }
@Test public void shouldReturnEmptyMapWhenGetVariablesIsNull() throws OperationException { final Context context = mock(Context.class); final GetVariables op = new GetVariables.Builder().variableNames(null).build(); final GetVariablesHandler handler = new GetVariablesHandler(); final Map<String, Object> resultMap = handler.doOperation(op, context, store); assertEquals(new HashMap<>(), resultMap); }
@Test public void shouldReturnNothingWhenGetVariablesKeysAreNull() throws OperationException { final Context context = mock(Context.class); final GetVariables op = new GetVariables.Builder().variableNames(Arrays.asList(null, null)).build(); final GetVariablesHandler handler = new GetVariablesHandler(); Map<String, Object> resultMap = handler.doOperation(op, context, store); assertEquals(new HashMap<>(), resultMap); }