ReadFileResourceFunction implements Function { @Override public String execute(List<String> parameterList, TestContext context) { if (CollectionUtils.isEmpty(parameterList)) { throw new InvalidFunctionUsageException("Missing file path function parameter"); } boolean base64 = parameterList.size() > 1 ? Boolean.valueOf(parameterList.get(1)) : false; try { if (base64) { return Base64.encodeBase64String(FileCopyUtils.copyToByteArray(FileUtils.getFileResource(parameterList.get(0), context).getInputStream())); } else { return context.replaceDynamicContentInString(FileUtils.readToString(FileUtils.getFileResource(parameterList.get(0), context), FileUtils.getCharset(parameterList.get(0)))); } } catch (IOException e) { throw new CitrusRuntimeException("Failed to read file", e); } }  @Override String execute(List<String> parameterList, TestContext context);  }
@Test public void testExecute() throws Exception { context.setVariable("filename", "file.txt"); context.setVariable("user", "Christoph"); String path = "classpath:com/consol/citrus/functions/${filename}"; String result = function.execute(Arrays.asList(path), context); Assert.assertTrue(result.startsWith("This is a sample file content!")); Assert.assertTrue(result.contains("'Christoph'")); }
@Test public void testExecuteBase64() throws Exception { context.setVariable("filename", "file.txt"); context.setVariable("user", "Christoph"); String path = "classpath:com/consol/citrus/functions/${filename}"; String result = function.execute(Arrays.asList(path, "true"), context); Assert.assertEquals(result, "VGhpcyBpcyBhIHNhbXBsZSBmaWxlIGNvbnRlbnQhCldlIGNhbiBhbHNvIHVzZSB2YXJpYWJsZXMgJyR7dXNlcn0nIGFuZCBmdW5jdGlvbnMgY2l0cnVzOmN1cnJlbnREYXRlKCk="); }
@Test(expectedExceptions = InvalidFunctionUsageException.class) public void testInvalidFunctionUsage() { function.execute(Collections.<String>emptyList(), context); }