Best Citrus code snippet using com.consol.citrus.config.xml.ReceiveMessageActionParser.getScriptValidationContext
Source:ReceiveMessageActionParser.java
...113 JsonPathMessageValidationContext jsonPathMessageValidationContext = getJsonPathMessageValidationContext(messageElement);114 if (!jsonPathMessageValidationContext.getJsonPathExpressions().isEmpty()) {115 validationContexts.add(jsonPathMessageValidationContext);116 }117 ScriptValidationContext scriptValidationContext = getScriptValidationContext(messageElement, messageType);118 if (scriptValidationContext != null) {119 validationContexts.add(scriptValidationContext);120 }121 ManagedList<RuntimeBeanReference> validators = new ManagedList<>();122 String messageValidator = messageElement.getAttribute("validator");123 if (StringUtils.hasText(messageValidator)) {124 validators.add(new RuntimeBeanReference(messageValidator));125 }126 String messageValidatorExpression = messageElement.getAttribute("validators");127 if (StringUtils.hasText(messageValidatorExpression)) {128 Stream.of(messageValidatorExpression.split(","))129 .map(String::trim)130 .map(RuntimeBeanReference::new)131 .forEach(validators::add);132 }133 if (!validators.isEmpty()) {134 builder.addPropertyValue("validators", validators);135 }136 String dataDictionary = messageElement.getAttribute("data-dictionary");137 if (StringUtils.hasText(dataDictionary)) {138 builder.addPropertyReference("dataDictionary", dataDictionary);139 }140 } else {141 validationContexts.add(new HeaderValidationContext());142 }143 return validationContexts;144 }145 /**146 * Constructs a list of variable extractors.147 * @param element148 * @return149 */150 protected List<VariableExtractor> getVariableExtractors(Element element) {151 List<VariableExtractor> variableExtractors = new ArrayList<>();152 parseExtractHeaderElements(element, variableExtractors);153 Element extractElement = DomUtils.getChildElementByTagName(element, "extract");154 if (extractElement != null) {155 Map<String, String> extractFromPath = new HashMap<>();156 List<Element> messageValueElements = DomUtils.getChildElementsByTagName(extractElement, "message");157 messageValueElements.addAll(DomUtils.getChildElementsByTagName(extractElement, "body"));158 VariableExtractorParserUtil.parseMessageElement(messageValueElements, extractFromPath);159 if (!CollectionUtils.isEmpty(extractFromPath)) {160 VariableExtractorParserUtil.addPayloadVariableExtractors(element, variableExtractors, extractFromPath);161 }162 }163 return variableExtractors;164 }165 /**166 * Construct the basic Json message validation context.167 * @param messageElement168 * @return169 */170 private JsonMessageValidationContext getJsonMessageValidationContext(Element messageElement) {171 JsonMessageValidationContext context = new JsonMessageValidationContext();172 if (messageElement != null) {173 Set<String> ignoreExpressions = new HashSet<String>();174 List<?> ignoreElements = DomUtils.getChildElementsByTagName(messageElement, "ignore");175 for (Iterator<?> iter = ignoreElements.iterator(); iter.hasNext(); ) {176 Element ignoreValue = (Element) iter.next();177 ignoreExpressions.add(ignoreValue.getAttribute("path"));178 }179 context.setIgnoreExpressions(ignoreExpressions);180 addSchemaInformationToValidationContext(messageElement, context);181 }182 return context;183 }184 /**185 * Construct the basic Xml message validation context.186 * @param messageElement187 * @return188 */189 private XmlMessageValidationContext getXmlMessageValidationContext(Element messageElement) {190 XmlMessageValidationContext context = new XmlMessageValidationContext();191 if (messageElement != null) {192 addSchemaInformationToValidationContext(messageElement, context);193 Set<String> ignoreExpressions = new HashSet<String>();194 List<?> ignoreElements = DomUtils.getChildElementsByTagName(messageElement, "ignore");195 for (Iterator<?> iter = ignoreElements.iterator(); iter.hasNext();) {196 Element ignoreValue = (Element) iter.next();197 ignoreExpressions.add(ignoreValue.getAttribute("path"));198 }199 context.setIgnoreExpressions(ignoreExpressions);200 parseNamespaceValidationElements(messageElement, context);201 //Catch namespace declarations for namespace context202 Map<String, String> namespaces = new HashMap<String, String>();203 List<?> namespaceElements = DomUtils.getChildElementsByTagName(messageElement, "namespace");204 if (namespaceElements.size() > 0) {205 for (Iterator<?> iter = namespaceElements.iterator(); iter.hasNext();) {206 Element namespaceElement = (Element) iter.next();207 namespaces.put(namespaceElement.getAttribute("prefix"), namespaceElement.getAttribute("value"));208 }209 context.setNamespaces(namespaces);210 }211 }212 return context;213 }214 /**215 * Adds information about the validation of the message against a certain schema to the context216 * @param messageElement The message element to get the configuration from217 * @param context The context to set the schema validation configuration to218 */219 private void addSchemaInformationToValidationContext(Element messageElement, SchemaValidationContext context) {220 String schemaValidation = messageElement.getAttribute("schema-validation");221 if (StringUtils.hasText(schemaValidation)) {222 context.setSchemaValidation(Boolean.valueOf(schemaValidation));223 }224 String schema = messageElement.getAttribute("schema");225 if (StringUtils.hasText(schema)) {226 context.setSchema(schema);227 }228 String schemaRepository = messageElement.getAttribute("schema-repository");229 if (StringUtils.hasText(schemaRepository)) {230 context.setSchemaRepository(schemaRepository);231 }232 }233 /**234 * Construct the XPath message validation context.235 * @param messageElement236 * @param parentContext237 * @return238 */239 private XpathMessageValidationContext getXPathMessageValidationContext(Element messageElement, XmlMessageValidationContext parentContext) {240 XpathMessageValidationContext context = new XpathMessageValidationContext();241 242 parseXPathValidationElements(messageElement, context);243 context.setControlNamespaces(parentContext.getControlNamespaces());244 context.setNamespaces(parentContext.getNamespaces());245 context.setIgnoreExpressions(parentContext.getIgnoreExpressions());246 context.setSchema(parentContext.getSchema());247 context.setSchemaRepository(parentContext.getSchemaRepository());248 context.setSchemaValidation(parentContext.isSchemaValidationEnabled());249 context.setDTDResource(parentContext.getDTDResource());250 return context;251 }252 /**253 * Construct the JSONPath message validation context.254 * @param messageElement255 * @return256 */257 private JsonPathMessageValidationContext getJsonPathMessageValidationContext(Element messageElement) {258 JsonPathMessageValidationContext context = new JsonPathMessageValidationContext();259 //check for validate elements, these elements can either have script, jsonPath or namespace validation information260 //for now we only handle jsonPath validation261 Map<String, Object> validateJsonPathExpressions = new HashMap<>();262 List<?> validateElements = DomUtils.getChildElementsByTagName(messageElement, "validate");263 if (validateElements.size() > 0) {264 for (Iterator<?> iter = validateElements.iterator(); iter.hasNext();) {265 Element validateElement = (Element) iter.next();266 extractJsonPathValidateExpressions(validateElement, validateJsonPathExpressions);267 }268 context.setJsonPathExpressions(validateJsonPathExpressions);269 }270 return context;271 }272 /**273 * Construct the message validation context.274 * @param messageElement275 * @return276 */277 private ScriptValidationContext getScriptValidationContext(Element messageElement, String messageType) {278 ScriptValidationContext context = null;279 boolean done = false;280 List<?> validateElements = DomUtils.getChildElementsByTagName(messageElement, "validate");281 if (validateElements.size() > 0) {282 for (Iterator<?> iter = validateElements.iterator(); iter.hasNext();) {283 Element validateElement = (Element) iter.next();284 Element scriptElement = DomUtils.getChildElementByTagName(validateElement, "script");285 // check for nested validate script child node286 if (scriptElement != null) {287 if (!done) {288 done = true;289 } else {290 throw new BeanCreationException("Found multiple validation script definitions - " +291 "only supporting a single validation script for message validation");...
getScriptValidationContext
Using AI Code Generation
1import org.springframework.context.support.ClassPathXmlApplicationContext;2import org.springframework.core.io.ClassPathResource;3import org.testng.Assert;4import org.testng.annotations.Test;5import com.consol.citrus.message.Message;6import com.consol.citrus.testng.AbstractTestNGUnitTest;7import com.consol.citrus.validation.script.ScriptValidationContext;8public class ReceiveMessageActionParserTest extends AbstractTestNGUnitTest {9 public void testReceiveMessageActionParser() {10 ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new ClassPathResource("com/consol/citrus/config/xml/receive-message-action-parser-test.xml", ReceiveMessageActionParserTest.class).getPath());11 ReceiveMessageActionParser parser = new ReceiveMessageActionParser();12 parser.setApplicationContext(context);13 ReceiveMessageAction action = (ReceiveMessageAction) parser.parse(new Element("receive"));14 Assert.assertEquals(action.getName(), "receive");15 Assert.assertEquals(action.getEndpointName(), "testEndpoint");16 Assert.assertEquals(action.getPayloadData().toString(), "Hello Citrus!");17 Assert.assertEquals(action.getValidationContexts().size(), 1L);18 Assert.assertTrue(action.getValidationContexts().get(0) instanceof ScriptValidationContext);19 Message message = new DefaultMessage("Hello Citrus!");20 message.setHeader("operation", "foo");21 Assert.assertTrue(action.getValidationContexts().get(0).validateMessage(message, context));22 }23}24import org.testng.annotations.Test;25public class ReceiveMessageActionParserTest {26 public void testReceiveMessageActionParser() {
getScriptValidationContext
Using AI Code Generation
1final ReceiveMessageActionParser receiveMessageActionParser = new ReceiveMessageActionParser();2final AbstractBeanDefinitionBuilder builder = receiveMessageActionParser.doParse(element, parserContext);3final BeanDefinitionBuilder contextBuilder = BeanDefinitionBuilder.genericBeanDefinition(ScriptValidationContext.class);4contextBuilder.addConstructorArgReference("scriptEngineRegistry");5contextBuilder.addConstructorArgReference("scriptExecutorFactory");6contextBuilder.addConstructorArgValue("groovy");7contextBuilder.addConstructorArgValue("validationContext");8contextBuilder.addPropertyValue("script", "println 'Groovy script validation context'");9contextBuilder.addPropertyValue("scriptResourcePath", "classpath:com/consol/citrus/validation/script/validator.groovy");10contextBuilder.addPropertyValue("scriptResourcePathType", "FILE");11contextBuilder.addPropertyValue("scriptResourceCharset", "UTF-8");12builder.addPropertyValue("validationContext", contextBuilder.getBeanDefinition());13[INFO] --- maven-surefire-plugin:2.22.1:test (default-test) @ citrus-demo ---
getScriptValidationContext
Using AI Code Generation
1public static void main(String[] args) throws Exception {2 "assert context.getVariables().containsKey('messageId')" +3 "</receive>";4 XmlApplicationContext context = new XmlApplicationContext(new StringReader(xml));5 ReceiveMessageAction action = context.getBean("receive", ReceiveMessageAction.class);6 MessageValidator validator = action.getMessageValidator();7 MessageValidationContext validationContext = validator.getScriptValidationContext();8 validator.validateMessage(new DefaultMessage("hello"), validationContext);9}
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!