ChangePrimaryType implements Action { @Override public String doAction(@Nonnull Resource resource) throws PersistenceException { Node node = resource.adaptTo(Node.class); try { if (null != node) { node.setPrimaryType(newPrimaryType); return "Updated jcr:primaryType to " + newPrimaryType; } } catch (RepositoryException e) { throw new PersistenceException("ERROR: could not update jcr:primaryType to " + newPrimaryType, e); } return "WARNING: could not update jcr:primaryType to " + newPrimaryType; } ChangePrimaryType(@Nonnull String newPrimaryType); @Override String doAction(@Nonnull Resource resource);  }
@Test public void test_doAction_setPrimaryType() throws PersistenceException, RepositoryException { ChangePrimaryType changePrimaryType = new ChangePrimaryType(newPrimaryType); String result = changePrimaryType.doAction(resource); verify(node, times(1)).setPrimaryType(newPrimaryType); assertEquals("Updated jcr:primaryType to " + newPrimaryType, result); }
@Test public void test_doAction_nodeIsNull() throws PersistenceException, RepositoryException { when(resource.adaptTo(Node.class)).thenReturn(null); ChangePrimaryType changePrimaryType = new ChangePrimaryType(newPrimaryType); changePrimaryType.doAction(resource); verify(node, never()).setPrimaryType(newPrimaryType); }