private void startNodeAndVerify() throws Exception { node.start(); verify(nodeContext).stateChanged("STARTED"); assertEquals("STARTED", node.getState()); PowerMockito.verifyPrivate(node).invoke("onStateChanged", "NEW", "STARTED"); }
@Before public void setup() throws Exception { // TODO: figure out how initialization can be refactored with @InjectMocks final StorageNode n = new StorageNode("node1", storage); node = spy(n); nodeContext = new StorageExecutionContext(communicationContext, node); nodeContext = spy(nodeContext); n.configure(nodeContext); node.configure(nodeContext); Mockito.reset(node); verifyZeroInteractions(nodeContext); verifyZeroInteractions(storage); verifyZeroInteractions(node); assertEquals("node1", node.getId()); assertEquals("NEW", node.getState()); // verify(node, atLeastOnce()).getId(); // verify(node).getState(); verifyZeroInteractions(nodeContext, storage, node); doAnswer( invocation -> { final StorageOperation op = (StorageOperation) invocation.getArguments()[0]; doReturn(op).when(operationContext).operation(); return operationContext; }) .when(nodeContext) .contextFor(any(StorageOperation.class)); }
@Test public void rejectAllStorageOperationsBeingInInappropriateState() throws Exception { // -- generate list of "inappropriate" states final List<String> states = new LinkedList<>(); final EnumSet<StorageNode.StorageState> allowedStates = EnumSet.of(StorageNode.StorageState.RECOVERING, StorageNode.StorageState.RUNNING); for (StorageNode.StorageState s : StorageNode.StorageState.values()) { if (!allowedStates.contains(s)) { states.add(s.name()); } } // -- for (String s : states) { if (!node.getState().equals(s)) { manageNode(new ControlMessage.Builder().setState(s)); } assertEquals(s, node.getState()); for (StorageOperation op : allOperations) { // TODO: rewrite with Exception Matcher to introduce message checking try { node.onStorageRequest(op); fail(); } catch (IllegalStateException ex) { } } } }
private void stopNodeAndVerify() throws Exception { final String state = node.getState(); node.stop(); verify(nodeContext).stateChanged("STOPPED"); assertEquals("STOPPED", node.getState()); PowerMockito.verifyPrivate(node).invoke("onStateChanged", state, "STOPPED"); }
@Test public void checkBehavior_of_StorageOperationProcessingDuringRecovery() throws Exception { verifyZeroInteractions(storage); startNodeAndVerify(); manageNode(new ControlMessage.Builder().setState(StorageNode.StorageState.RECOVERING.name())); for (StorageOperation op : allOperations) { node.onStorageRequest(op); } // all the operations must be queued for further processing after recovery will be completed verify(storage, after(2000).never()).process(any()); verifyZeroInteractions(storage); // should process all previously queued operations manageNode(new ControlMessage.Builder().setState(StorageNode.StorageState.RUNNING.name())); // all the enqueued operation should be processed verify(storage, after(1000).times(allOperations.length)).process(any()); // each successfully processed operation should be acknowledged verify(operationContext, times(allOperations.length)).ackOperation(any()); stopNodeAndVerify(); }
@Test public void checkBehavior_of_StateChangeUsingControlMessage() throws Exception { startNodeAndVerify(); manageNode(new ControlMessage.Builder().setState(StorageNode.StorageState.RUNNING.name())); assertEquals("RUNNING", node.getState()); PowerMockito.verifyPrivate(node).invoke("onStateChanged", "STARTED", "RUNNING"); stopNodeAndVerify(); }
private void manageNode(ControlMessage.Builder builder) { final ControlMessage msg = builder.setSender("Anonymous").build(); try { nodeContext.onMessage(node.getId(), Messages.MessageTypes.ControlMessage, msg); } catch (Exception e) { if (e instanceof RuntimeException) { throw (RuntimeException) e; } throw new RuntimeException(e); } }