JoinProperty implements Action { @Override public String doAction(@Nonnull Resource resource) throws PersistenceException { ModifiableValueMap properties = resource.adaptTo(ModifiableValueMap.class); if (properties == null) { return "WARNING: could not get ModifiableValueMap for resource " + resource.getPath(); } if (!properties.containsKey(name)) { return StringUtils.EMPTY; } Object value = properties.get(name); if (!value.getClass().isArray()) { return StringUtils.EMPTY; } Object[] values = (Object[]) value; if (values.length > 0) { Node node = resource.adaptTo(Node.class); try { node.getProperty(name).remove(); node.setProperty(name, StringUtils.join(values, separator)); } catch (RepositoryException e) { throw new PersistenceException(e.getMessage(), e); } return "Joined " + value.getClass().getSimpleName() + " property " + name + " for resource " + resource.getPath(); } if (this.emptyValue == null) { properties.remove(name); return "Removed empty " + value.getClass().getSimpleName() + " property " + name + " for resource " + resource.getPath(); } properties.remove(name); properties.put(name, this.emptyValue); return "Replaced empty " + value.getClass().getSimpleName() + " property " + name + " with " + this.emptyValue + " for resource " + resource.getPath(); } JoinProperty(@Nonnull String name);  JoinProperty(@Nonnull String name, Object emptyValue);  JoinProperty(@Nonnull String name, Object emptyValue, @Nonnull String separator); @Override String doAction(@Nonnull Resource resource);  }
@Test public void doAction() throws PersistenceException, ValueFormatException, VersionException, LockException, ConstraintViolationException, RepositoryException { when(valueMap.containsKey(ATTR)).thenReturn(true); when(valueMap.get(ATTR)).thenReturn(new String[] {VAL1}); JoinProperty action = new JoinProperty(ATTR, EMPTY_VALUE); action.doAction(resource); verify(property, times(1)).remove(); verify(node, times(1)).setProperty(ATTR, VAL1); }
@Test public void doActionNewProperty() throws PersistenceException { JoinProperty action = new JoinProperty(ATTR, EMPTY_VALUE); action.doAction(resource); verify(valueMap, never()).remove(""); verify(valueMap, never()).put("", ""); }
@Test public void doActionReplaceArrayWithMultipleValues() throws PersistenceException { when(valueMap.containsKey(ATTR)).thenReturn(true); when(valueMap.get(ATTR)).thenReturn(new String[] {VAL1, VAL1}); JoinProperty action = new JoinProperty(ATTR, EMPTY_VALUE); action.doAction(resource); }
@Test public void doActionReplaceEmptyArray() throws PersistenceException { when(valueMap.containsKey(ATTR)).thenReturn(true); when(valueMap.get(ATTR)).thenReturn(new Boolean[] {}); JoinProperty action = new JoinProperty(ATTR, EMPTY_VALUE); action.doAction(resource); verify(valueMap, times(1)).remove(ATTR); verify(valueMap, times(1)).put(ATTR, EMPTY_VALUE); }
@Test public void doActionNoArgs() throws PersistenceException { when(valueMap.containsKey(ATTR)).thenReturn(true); when(valueMap.get(ATTR)).thenReturn(new Boolean[] {}); JoinProperty action = new JoinProperty(ATTR); action.doAction(resource); verify(valueMap, times(1)).remove(ATTR); }
@Test public void doActionCustomSeparator() throws PersistenceException, AccessDeniedException, VersionException, LockException, ConstraintViolationException, RepositoryException { when(valueMap.containsKey(ATTR)).thenReturn(true); when(valueMap.get(ATTR)).thenReturn(new String[] {VAL1, VAL1}); JoinProperty action = new JoinProperty(ATTR, EMPTY_VALUE, SEPARATOR); action.doAction(resource); verify(property, times(1)).remove(); verify(node, times(1)).setProperty(ATTR, VAL1 + SEPARATOR + VAL1); }
@Test public void doActionReplaceExistingFlat() throws PersistenceException { when(valueMap.containsKey(ATTR)).thenReturn(true); when(valueMap.get(ATTR)).thenReturn(10); JoinProperty action = new JoinProperty(ATTR, EMPTY_VALUE); action.doAction(resource); }