DeleteResource implements Action { @Override public String doAction(@Nonnull Resource resource) throws PersistenceException { List<String> deletedResources = new ArrayList<>(); List<String> nonExistingResources = new ArrayList<>(); String resourcePath = resource.getPath(); if (0 == children.length) { resourceResolver.delete(resource); return "Deleted resource " + resourcePath; } for (String child : children) { Resource childResource = resourceResolver.getResource(resource, child); String childResourcePath = resourcePath + "/" + child; if (null != childResource) { resourceResolver.delete(childResource); deletedResources.add(childResourcePath); } else { nonExistingResources.add(childResourcePath); } } String message = "Deleted child resource(s) " + Arrays.toString(deletedResources.toArray()) + "."; if (!nonExistingResources.isEmpty()) { message += " Child resource(s) " + Arrays.toString(nonExistingResources.toArray()) + " were not found."; } return message; } DeleteResource(@Nonnull ResourceResolver resourceResolver, String... children); @Override String doAction(@Nonnull Resource resource);  }
@Test public void test_doAction_noChildResource() throws RepositoryException, PersistenceException { when(resource.getPath()).thenReturn(COMPONENT); DeleteResource deleteResourceAction = new DeleteResource(resourceResolver); doNothing().when(resourceResolver).delete(resource); String result = deleteResourceAction.doAction(resource); verify(resourceResolver, times(1)).delete(resource); assertEquals(result, "Deleted resource " + COMPONENT); }
@Test public void test_doAction_withChildResource() throws RepositoryException, PersistenceException { when(resourceResolver.getResource(eq(resource), eq(CHILD1))).thenReturn(mock(Resource.class)); when(resourceResolver.getResource(eq(resource), eq(CHILD2))).thenReturn(null); when(resourceResolver.getResource(eq(resource), eq(CHILD3))).thenReturn(mock(Resource.class)); when(resource.getPath()).thenReturn(COMPONENT); doNothing().when(resourceResolver).delete(any(Resource.class)); DeleteResource deleteResourceAction = new DeleteResource(resourceResolver, CHILD1, CHILD2, CHILD3); String result = deleteResourceAction.doAction(resource); String expectedResult = String.format("Deleted child resource(s) [%s]. Child resource(s) [%s] were not found.", COMPONENT + PATH_SEPARATOR + CHILD1 + ", " + COMPONENT + PATH_SEPARATOR + CHILD3, COMPONENT + PATH_SEPARATOR + CHILD2); verify(resourceResolver, times(2)).delete(any(Resource.class)); assertEquals(result, expectedResult); }