ReplaceResourcePropertyValues implements Action { @Override public String doAction(@Nonnull Resource resource) throws PersistenceException { Node node = resource.adaptTo(Node.class); if (node == null) { return StringUtils.EMPTY; } boolean updated = false; try { PropertyIterator propertyIterator = node.getProperties(); while (propertyIterator.hasNext()) { Property property = propertyIterator.nextProperty(); if (doChangeProperty(property)) { if (property.isMultiple()) { boolean propUpdated = updateMulti(property); updated = updated || propUpdated; } else { boolean propUpdated = updateSingle(property); updated = updated || propUpdated; } } } } catch (RepositoryException e) { throw new PersistenceException("Rename failed", e); } if (updated) { return "Updated values from " + oldValue + " to " + newValue + " in " + resource.getPath(); } return StringUtils.EMPTY; } ReplaceResourcePropertyValues(@Nonnull String oldValue, @Nonnull String newValue,
            @Nonnull List<String> propertyNames); @Override String doAction(@Nonnull Resource resource);  }
@Test public void doAction_allProperties() throws PersistenceException, ValueFormatException, VersionException, LockException, ConstraintViolationException, RepositoryException { ReplaceResourcePropertyValues action = new ReplaceResourcePropertyValues(OLD_VAL, NEW_VAL, Arrays.asList()); String result = action.doAction(resource); assertEquals("Updated values from " + OLD_VAL + " to " + NEW_VAL + " in " + PATH, result); verify(prop1, times(1)).setValue("val1_newVal_suffix"); verify(prop2, never()).setValue(Mockito.anyString()); verify(prop3, times(1)).setValue(Mockito.any(Value[].class)); verify(prop4, never()).setValue(Mockito.anyString()); }
@Test public void doAction_prop1Only() throws PersistenceException, ValueFormatException, VersionException, LockException, ConstraintViolationException, RepositoryException { ReplaceResourcePropertyValues action = new ReplaceResourcePropertyValues(OLD_VAL, NEW_VAL, Arrays.asList(PROP1)); String result = action.doAction(resource); assertEquals("Updated values from " + OLD_VAL + " to " + NEW_VAL + " in " + PATH, result); verify(prop1, times(1)).setValue("val1_newVal_suffix"); verify(prop2, never()).setValue(Mockito.anyString()); verify(prop3, never()).setValue(Mockito.any(Value[].class)); verify(prop4, never()).setValue(Mockito.anyString()); }
@Test public void doAction_noMatch() throws PersistenceException, ValueFormatException, VersionException, LockException, ConstraintViolationException, RepositoryException { ReplaceResourcePropertyValues action = new ReplaceResourcePropertyValues("nomatch", NEW_VAL, Arrays.asList(PROP1)); String result = action.doAction(resource); assertEquals("", result); verify(prop1, never()).setValue("val1_newVal_suffix"); verify(prop2, never()).setValue(Mockito.anyString()); verify(prop3, never()).setValue(Mockito.any(Value[].class)); verify(prop4, never()).setValue(Mockito.anyString()); }