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