ReplicatePageAction implements Action { @Override public String doAction(Resource resource) throws PersistenceException { Page page = context.getPageManager().getContainingPage(resource); if (page == null) { return "Unable to find a page for resource " + resource.getPath(); } String action = activate ? "activate" : "deactivate"; String successMessage = "Replicated page " + page.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, page.getPath()); } catch (ReplicationException e) { throw new PersistenceException("Unable to replicate " + page.getPath()); } return successMessage; } ReplicatePageAction(boolean activate, BindingContext context); @Override String doAction(Resource resource);  }
@Test public void doAction_pageNotFound() throws PersistenceException { when(pageManager.getContainingPage(resource)).thenReturn(null); String result = action.doAction(resource); assertTrue(result.contains("Unable to find a page")); }
@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 page")); verify(replicator, times(1)).replicate(session, ReplicationActionType.ACTIVATE, "path"); }
@Test public void doAction_successDeactivate() throws PersistenceException, ReplicationException { this.action = new ReplicatePageAction(false, context); String result = action.doAction(resource); assertTrue(result.contains("Replicated page")); 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 page")); verify(replicator, never()).replicate(session, ReplicationActionType.ACTIVATE, "path"); }