LoadMessageFunction implements Function { @Override public String execute(List<String> parameterList, TestContext context) { if (CollectionUtils.isEmpty(parameterList)) { throw new InvalidFunctionUsageException("Function parameters must not be empty"); } String messageName = parameterList.get(0); String messageHeader = null; if (messageName.endsWith(".payload()")) { messageName = messageName.substring(0, messageName.indexOf(".payload()")); } else if (messageName.contains(".header(") && messageName.endsWith(")")) { messageHeader = messageName.substring(messageName.indexOf(".header(") + 8, messageName.length() - 1); if (messageHeader.startsWith("'") && messageHeader.endsWith("'")) { messageHeader = messageHeader.substring(1, messageHeader.length() - 1); } if (!StringUtils.hasText(messageHeader)) { throw new CitrusRuntimeException("Missing header name in function parameter"); } messageName = messageName.substring(0, messageName.indexOf(".header(")); } Message stored = context.getMessageStore().getMessage(messageName); if (stored == null) { throw new CitrusRuntimeException(String.format("Failed to find stored message of name: '%s'", messageName)); } if (StringUtils.hasText(messageHeader)) { Object headerValue = stored.getHeader(messageHeader); if (headerValue == null) { throw new CitrusRuntimeException(String.format("Failed to find header '%s' in stored message", messageHeader)); } return headerValue.toString(); } else { return stored.getPayload(String.class); } }  @Override String execute(List<String> parameterList, TestContext context);  }
@Test public void testLoadMessagePayload() throws Exception { context.getMessageStore().storeMessage("request", message); Assert.assertEquals(function.execute(Collections.singletonList("request"), context), "This is a sample message"); Assert.assertEquals(function.execute(Collections.singletonList("request.payload()"), context), "This is a sample message"); }
@Test public void testLoadMessageHeader() throws Exception { context.getMessageStore().storeMessage("request", message); Assert.assertEquals(function.execute(Collections.singletonList("request.header(operation)"), context), "sampleOperation"); Assert.assertEquals(function.execute(Collections.singletonList("request.header('operation')"), context), "sampleOperation"); }
@Test(expectedExceptions = CitrusRuntimeException.class, expectedExceptionsMessageRegExp = "Missing header name.*") public void testLoadMessageHeaderEmpty() throws Exception { context.getMessageStore().storeMessage("request", message); function.execute(Collections.singletonList("request.header()"), context); }
@Test(expectedExceptions = CitrusRuntimeException.class, expectedExceptionsMessageRegExp = "Failed to find header 'wrong'.*") public void testLoadMessageHeaderUnknown() throws Exception { context.getMessageStore().storeMessage("request", message); function.execute(Collections.singletonList("request.header('wrong')"), context); }