SetVariableByJSONAttributeAction extends SetVariableAction { @Override public ExecuteResult execute(ConnectorManager connector) { VariableStoreConnector storeConnector = connector.getConnector(VariableStoreConnector.class); WebServiceConnector webServiceConnector = connector.getConnector(WebServiceConnector.class); String body = webServiceConnector.getBody(); String valueInTheBody = JSONHelpers.getAttributeValue(body, value); if (valueInTheBody == null) { LOGGER.info(LoggerMarkers.LEARNER, "Could not set variable '{}' to the value of the JSON attribute '{}' ", name, value); return getFailedOutput(); } storeConnector.set(name, valueInTheBody); return getSuccessOutput(); }  @Override ExecuteResult execute(ConnectorManager connector);  }
@Test public void shouldSetTheRightValue() { VariableStoreConnector storeConnector = mock(VariableStoreConnector.class); WebServiceConnector webServiceConnector = mock(WebServiceConnector.class); given(webServiceConnector.getBody()).willReturn("{\"foo\": \"bar\"}"); ConnectorManager connectors = mock(ConnectorManager.class); given(connectors.getConnector(VariableStoreConnector.class)).willReturn(storeConnector); given((connectors.getConnector(WebServiceConnector.class))).willReturn(webServiceConnector); ExecuteResult result = setAction.execute(connectors); assertTrue(result.isSuccess()); verify(storeConnector).set("variable", "bar"); }
@Test public void shouldSetNotihingIfThePropertyDoesNotExists() { VariableStoreConnector storeConnector = mock(VariableStoreConnector.class); WebServiceConnector webServiceConnector = mock(WebServiceConnector.class); given(webServiceConnector.getBody()).willReturn("{\"nope\": \"bar\"}"); ConnectorManager connectors = mock(ConnectorManager.class); given(connectors.getConnector(VariableStoreConnector.class)).willReturn(storeConnector); given((connectors.getConnector(WebServiceConnector.class))).willReturn(webServiceConnector); ExecuteResult result = setAction.execute(connectors); assertFalse(result.isSuccess()); verify(storeConnector, never()).set(eq("variable"), anyString()); }