PrintProperty implements Action { @Override public String doAction(@Nonnull Resource resource) { Node node = resource.adaptTo(Node.class); String output = propertyName + " = "; try { if (node.hasProperty(propertyName)) { Property prop = node.getProperty(propertyName); if (prop.isMultiple()) { Value[] values = prop.getValues(); String[] valuesAsStrings = new String[values.length]; for (int i = 0; i < values.length; i++) { valuesAsStrings[i] = values[i].getString(); } output = output + Arrays.toString(valuesAsStrings); } else { output = output + prop.getValue().getString(); } return output; } return propertyName + " not defined"; } catch (RepositoryException e) { LOG.debug("Cannot read value of [{}]. Reason [{}]", propertyName, e.getMessage()); } return ""; } PrintProperty(String propertyName); @Override String doAction(@Nonnull Resource resource);  }
@Test public void test_doAction_noProperty() throws RepositoryException { PrintProperty printPropertyAction = new PrintProperty(NOT_DEFINED_PROPERTY); String result = printPropertyAction.doAction(resource); verify(singleValueProperty, never()).isMultiple(); assertTrue(result.contains(NOT_DEFINED_PROPERTY + " not defined")); }
@Test public void test_doAction_existingSingleValueProperty() throws RepositoryException { PrintProperty printPropertyAction = new PrintProperty(EXISTING_SINGLE_VALUE_PROPERTY); String result = printPropertyAction.doAction(resource); verify(singleValueProperty, atLeastOnce()).isMultiple(); verify(singleValueProperty, atLeastOnce()).getValue(); assertTrue(result.contains(EXISTING_SINGLE_VALUE_PROPERTY + " = " + ANY_VALUE)); }
@Test public void test_doAction_existingMultiValueProperty() throws RepositoryException { PrintProperty printPropertyAction = new PrintProperty(EXISTING_MULTI_VALUE_PROPERTY); String result = printPropertyAction.doAction(resource); verify(multiValueProperty, atLeastOnce()).isMultiple(); verify(multiValueProperty, atLeastOnce()).getValues(); assertTrue(result.contains(EXISTING_MULTI_VALUE_PROPERTY + " = [" + ANY_VALUE + ", " + ANY_VALUE + "]")); }