ConnectorContextHandler implements ContextExecutableInputSUL.ContextHandler<ConnectorManager> { @Override public ConnectorManager createContext() throws LearnerException { int retries = 0; Exception exception = null; while (retries < 2) { try { try { for (Connector connector : connectors) { connector.reset(); } } catch (Exception e) { throw new LearnerException("An error occurred while resetting a connector.", e); } ExecuteResult resetResult; try { final CounterStoreConnector counterStore = connectors.getConnector(CounterStoreConnector.class); resetSymbol.getSymbol().getInputs().stream() .filter(in -> in.getParameterType().equals(SymbolParameter.ParameterType.COUNTER)) .forEach(in -> { try { counterStore.get(in.getName()); } catch (IllegalStateException e) { counterStore.set(resetSymbol.getSymbol().getProjectId(), in.getName(), 0); } }); resetResult = resetSymbol.execute(connectors); } catch (Exception e) { throw new LearnerException("An error occurred while executing the reset symbol.", e); } if (!resetResult.isSuccess()) { throw new LearnerException("The execution of the reset symbol failed: " + resetResult.toString() + "."); } return connectors; } catch (Exception e) { disposeContext(connectors); exception = e; retries++; } } throw new LearnerException("Failed to create a context. " + exception.getMessage(), exception); } ConnectorContextHandler(ConnectorManager connectors,
                                   ParameterizedSymbol resetSymbol,
                                   ParameterizedSymbol postSymbol); @Override ConnectorManager createContext(); @Override void disposeContext(ConnectorManager connectorManager); void post();  }
@Test public void shouldCreateTheContextCorrectly() { given(resetSymbol.execute(any(ConnectorManager.class))).willReturn(new ExecuteResult(true)); ConnectorManager connectorManager = new ConnectorManager(new ProjectEnvironment()); VariableStoreConnector connector1 = mock(VariableStoreConnector.class); CounterStoreConnector connector2 = mock(CounterStoreConnector.class); connectorManager.addConnector(connector1); connectorManager.addConnector(connector2); handler = new ConnectorContextHandler(connectorManager, resetSymbol, null); assertEquals(connector1, connectorManager.getConnector(connector1.getClass())); assertEquals(connector2, connectorManager.getConnector(connector2.getClass())); handler.createContext(); verify(connector1).reset(); verify(connector2).reset(); verify(resetSymbol).execute(any(ConnectorManager.class)); }
@Test(expected = LearnerException.class) public void shouldThrowAnExceptionIfTheResetSymbolExecutionFailed() { given(resetSymbol.execute(any(ConnectorManager.class))).willReturn(new ExecuteResult(false)); handler = new ConnectorContextHandler(createConnectorManager(), resetSymbol, null); handler.createContext(); }