@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); }
@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 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 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); }
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); }
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);  }