ReplicateResourceAction implements Action { @Override public String doAction(Resource resource) throws PersistenceException { String action = activate ? "activate" : "deactivate"; String successMessage = "Replicated resource " + resource.getPath() + " with action " + action; if (context.isDryRun()) { return successMessage; } try { ReplicationActionType type = activate ? ReplicationActionType.ACTIVATE : ReplicationActionType.DEACTIVATE; context.getReplicator().replicate(context.getResolver().adaptTo(Session.class), type, resource.getPath()); } catch (ReplicationException e) { throw new PersistenceException("Unable to replicate " + resource.getPath()); } return successMessage; } ReplicateResourceAction(boolean activate, BindingContext context); @Override String doAction(Resource resource);  }
@Test(expected = PersistenceException.class) public void doAction_replicatorException() throws PersistenceException, ReplicationException { doThrow(ReplicationException.class).when(replicator).replicate(session, ReplicationActionType.ACTIVATE, PATH); action.doAction(resource); }
@Test public void doAction_success() throws PersistenceException, ReplicationException { String result = action.doAction(resource); assertTrue(result.contains("Replicated resource")); verify(replicator, times(1)).replicate(session, ReplicationActionType.ACTIVATE, PATH); }
@Test public void doAction_successDeactivate() throws PersistenceException, ReplicationException { this.action = new ReplicateResourceAction(false, context); String result = action.doAction(resource); assertTrue(result.contains("Replicated resource")); verify(replicator, times(1)).replicate(session, ReplicationActionType.DEACTIVATE, PATH); }
@Test public void doAction_successDryRun() throws PersistenceException, ReplicationException { when(context.isDryRun()).thenReturn(true); String result = action.doAction(resource); assertTrue(result.contains("Replicated resource")); verify(replicator, never()).replicate(session, ReplicationActionType.ACTIVATE, PATH); }