RandomStringFunction implements Function { public String execute(List<String> parameterList, TestContext context) { int numberOfLetters; String notationMethod = MIXED; boolean includeNumbers = false; if (CollectionUtils.isEmpty(parameterList)) { throw new InvalidFunctionUsageException("Function parameters must not be empty"); } if (parameterList.size() > 3) { throw new InvalidFunctionUsageException("Too many parameters for function"); } numberOfLetters = Integer.valueOf(parameterList.get(0)); if (numberOfLetters < 0) { throw new InvalidFunctionUsageException("Invalid parameter definition. Number of letters must not be positive non-zero integer value"); } if (parameterList.size() > 1) { notationMethod = parameterList.get(1); } if (parameterList.size() > 2) { includeNumbers = Boolean.valueOf(parameterList.get(2)); } if (notationMethod.equals(UPPERCASE)) { return getRandomString(numberOfLetters, ALPHABET_UPPER, includeNumbers); } else if (notationMethod.equals(LOWERCASE)) { return getRandomString(numberOfLetters, ALPHABET_LOWER, includeNumbers); } else { return getRandomString(numberOfLetters, ALPHABET_MIXED, includeNumbers); } }  String execute(List<String> parameterList, TestContext context); static String getRandomString(int numberOfLetters, char[] alphabet, boolean includeNumbers); static final String UPPERCASE; static final String LOWERCASE; static final String MIXED; }
@Test public void testFunction() { List<String> params = new ArrayList<String>(); params.add("3"); Assert.assertTrue(function.execute(params, context).length() == 3); params = new ArrayList<String>(); params.add("3"); params.add("UPPERCASE"); Assert.assertTrue(function.execute(params, context).length() == 3); params = new ArrayList<String>(); params.add("3"); params.add("LOWERCASE"); Assert.assertTrue(function.execute(params, context).length() == 3); params = new ArrayList<String>(); params.add("3"); params.add("MIXED"); Assert.assertTrue(function.execute(params, context).length() == 3); params = new ArrayList<String>(); params.add("3"); params.add("UNKNOWN"); Assert.assertTrue(function.execute(params, context).length() == 3); }
@Test public void testWithNumbers() { List<String> params = new ArrayList<String>(); params = new ArrayList<String>(); params.add("10"); params.add("UPPERCASE"); params.add("true"); Assert.assertTrue(function.execute(params, context).length() == 10); params = new ArrayList<String>(); params.add("10"); params.add("LOWERCASE"); params.add("true"); Assert.assertTrue(function.execute(params, context).length() == 10); params = new ArrayList<String>(); params.add("10"); params.add("MIXED"); params.add("true"); Assert.assertTrue(function.execute(params, context).length() == 10); params = new ArrayList<String>(); params.add("10"); params.add("UNKNOWN"); params.add("true"); Assert.assertTrue(function.execute(params, context).length() == 10); }
@Test(expectedExceptions = {InvalidFunctionUsageException.class}) public void testWrongParameterUsage() { function.execute(Collections.singletonList("-1"), context); }
@Test(expectedExceptions = {InvalidFunctionUsageException.class}) public void testNoParameters() { function.execute(Collections.<String>emptyList(), context); }
@Test(expectedExceptions = {InvalidFunctionUsageException.class}) public void testTooManyParameters() { List<String> params = new ArrayList<String>(); params.add("3"); params.add("UPPERCASE"); params.add("true"); params.add("too much"); function.execute(params, context); }