MoveResourceToPathRegex implements Action { @Override public String doAction(@Nonnull Resource resource) throws PersistenceException { ResourceResolver resourceResolver = context.getResolver(); String resourcePath = resource.getPath(); if (resourcePath.matches(matchPattern)) { String targetPath = resourcePath.replaceAll(matchPattern, targetPathExpr); Resource destinationResource = resourceResolver.getResource(targetPath); if (destinationResource != null) { PageUtil pageUtil = new PageUtil(); if (pageUtil.isPageResource(resource)) { PageManager pageManager = resourceResolver.adaptTo(PageManager.class); try { if (!context.isDryRun()) { pageManager.move(resource, targetPath + "/" + resource.getName(), null, false, false, null); } } catch (WCMException | IllegalArgumentException e) { throw new PersistenceException("Unable to move " + resourcePath + ": " + e.getMessage()); } } else { resourceResolver.move(resourcePath, targetPath); } return "Moved " + resourcePath + " to path " + targetPath; } return "WARNING: could not read move destination resource " + targetPath; } return "INFO: resource " + resourcePath + " does not match path regex " + matchPattern + ", skipping"; } MoveResourceToPathRegex(@Nonnull String matchPattern, @Nonnull String targetPathExpr,
            @Nonnull BindingContext context); @Override String doAction(@Nonnull Resource resource);  }
@Test public void testDoAction_noMatch() { MoveResourceToPathRegex underTest = createObjectUnderTest("/content/bla/(\\w+)/(\\w+)", "/content/abc/$1"); mockWithValues("/content/somewhere/else", "/doesnt/matter", true); try { String returnString = underTest.doAction(resource); assertTrue(returnString.startsWith("INFO")); verify(resourceResolver, never()).getResource(anyString()); verify(resourceResolver, never()).move(anyString(), anyString()); } catch (PersistenceException pe) { fail(); } }
@Test public void testDoAction_match_invalidDestination() { MoveResourceToPathRegex underTest = createObjectUnderTest("/content/(\\w+)/mmm/(\\w+)/(\\w+)", "/content/ooo/$1/$2/i"); mockWithValues("/content/en/mmm/sub/resource", "/content/ooo/en/sub/i", false); try { String returnString = underTest.doAction(resource); assertTrue(returnString.startsWith("WARN")); verify(resourceResolver, times(1)).getResource(eq("/content/ooo/en/sub/i")); verify(resourceResolver, never()).move(anyString(), anyString()); } catch (PersistenceException pe) { fail(); } }
@Test public void testDoAction_match_validDestination() { MoveResourceToPathRegex underTest = createObjectUnderTest("/content/(\\w+)/mmm/(\\w+)/(\\w+)", "/content/ooo/$1/$2/i"); String resourcePath = "/content/en/mmm/sub/resource"; String targetPath = "/content/ooo/en/sub/i"; mockWithValues(resourcePath, targetPath, true); try { String returnString = underTest.doAction(resource); assertTrue(returnString.startsWith("Moved")); verify(resourceResolver, times(1)).getResource(eq(targetPath)); verify(resourceResolver, times(1)).move(resourcePath, targetPath); } catch (PersistenceException pe) { fail(); } }