SystemPropertyFunction implements Function { @Override public String execute(List<String> parameterList, TestContext context) { if (CollectionUtils.isEmpty(parameterList)) { throw new InvalidFunctionUsageException("Invalid function parameters - must set system property name"); } String propertyName = parameterList.get(0); final Optional<String> defaultValue; if (parameterList.size() > 1) { defaultValue = Optional.of(parameterList.get(1)); } else { defaultValue = Optional.empty(); } return Optional.ofNullable(System.getProperty(propertyName)) .orElseGet(() -> defaultValue.orElseThrow(() -> new CitrusRuntimeException(String.format("Failed to resolve system property '%s'", propertyName)))); }  @Override String execute(List<String> parameterList, TestContext context);  }
@Test public void testFunction() { System.setProperty("foo.property", "Citrus rocks!"); Assert.assertEquals(function.execute(Collections.singletonList("foo.property"), context), "Citrus rocks!"); }
@Test public void testFunctionDefaultValue() { Assert.assertEquals(function.execute(Arrays.asList("bar.property", "This is a default"), context), "This is a default"); }
@Test(expectedExceptions = {CitrusRuntimeException.class}, expectedExceptionsMessageRegExp = "Failed to resolve system property 'bar.property'") public void testPropertyNotFound() { function.execute(Collections.singletonList("bar.property"), context); }
@Test(expectedExceptions = {InvalidFunctionUsageException.class}) public void testNoParameters() { function.execute(Collections.emptyList(), context); }