Best Citrus code snippet using com.consol.citrus.validation.xml.XpathMessageValidationContext.setXpathExpressions
Source:MessageElementsTest.java
...76 77 PayloadTemplateMessageBuilder controlMessageBuilder = new PayloadTemplateMessageBuilder();78 XpathMessageValidationContext validationContext = new XpathMessageValidationContext();79 receiveMessageBean.setMessageBuilder(controlMessageBuilder);80 validationContext.setXpathExpressions(validateMessageElements);81 82 List<ValidationContext> validationContexts = new ArrayList<ValidationContext>();83 validationContexts.add(validationContext);84 receiveMessageBean.setValidationContexts(validationContexts);85 86 receiveMessageBean.execute(context);87 }88 89 @Test90 @SuppressWarnings({ "unchecked", "rawtypes" })91 public void testValidateEmptyMessageElements() {92 reset(endpoint, consumer, endpointConfiguration);93 when(endpoint.createConsumer()).thenReturn(consumer);94 when(endpoint.getEndpointConfiguration()).thenReturn(endpointConfiguration);95 when(endpointConfiguration.getTimeout()).thenReturn(5000L);96 97 Message message = new DefaultMessage("<root>"98 + "<element attributeA='attribute-value' attributeB='attribute-value' >"99 + "<sub-elementA attribute='A'></sub-elementA>"100 + "<sub-elementB attribute='B'></sub-elementB>"101 + "<sub-elementC attribute='C'>text-value</sub-elementC>"102 + "</element>" 103 + "</root>");104 105 when(consumer.receive(any(TestContext.class), anyLong())).thenReturn(message);106 when(endpoint.getActor()).thenReturn(null);107 HashMap<String, Object> validateMessageElements = new HashMap<>();108 validateMessageElements.put("//root/element/sub-elementA", "");109 validateMessageElements.put("//sub-elementB", "");110 111 PayloadTemplateMessageBuilder controlMessageBuilder = new PayloadTemplateMessageBuilder();112 XpathMessageValidationContext validationContext = new XpathMessageValidationContext();113 receiveMessageBean.setMessageBuilder(controlMessageBuilder);114 validationContext.setXpathExpressions(validateMessageElements);115 116 List<ValidationContext> validationContexts = new ArrayList<ValidationContext>();117 validationContexts.add(validationContext);118 receiveMessageBean.setValidationContexts(validationContexts);119 120 receiveMessageBean.execute(context);121 }122 123 @Test124 @SuppressWarnings({ "unchecked", "rawtypes" })125 public void testValidateEmptyMessageAttributes() {126 reset(endpoint, consumer, endpointConfiguration);127 when(endpoint.createConsumer()).thenReturn(consumer);128 when(endpoint.getEndpointConfiguration()).thenReturn(endpointConfiguration);129 when(endpointConfiguration.getTimeout()).thenReturn(5000L);130 131 Message message = new DefaultMessage("<root>"132 + "<element attributeA='attribute-value' attributeB='attribute-value' >"133 + "<sub-elementA attribute=''>text-value</sub-elementA>"134 + "<sub-elementB attribute=''>text-value</sub-elementB>"135 + "<sub-elementC attribute=''>text-value</sub-elementC>"136 + "</element>" 137 + "</root>");138 139 when(consumer.receive(any(TestContext.class), anyLong())).thenReturn(message);140 when(endpoint.getActor()).thenReturn(null);141 142 HashMap<String, Object> validateMessageElements = new HashMap<>();143 validateMessageElements.put("//root/element/sub-elementA/@attribute", "");144 validateMessageElements.put("//root/element/sub-elementB/@attribute", "");145 validateMessageElements.put("//root/element/sub-elementC/@attribute", "");146 147 PayloadTemplateMessageBuilder controlMessageBuilder = new PayloadTemplateMessageBuilder();148 XpathMessageValidationContext validationContext = new XpathMessageValidationContext();149 receiveMessageBean.setMessageBuilder(controlMessageBuilder);150 validationContext.setXpathExpressions(validateMessageElements);151 152 List<ValidationContext> validationContexts = new ArrayList<ValidationContext>();153 validationContexts.add(validationContext);154 receiveMessageBean.setValidationContexts(validationContexts);155 156 receiveMessageBean.execute(context);157 }158 159 @Test(expectedExceptions = {ValidationException.class})160 @SuppressWarnings({ "unchecked", "rawtypes" })161 public void testValidateNullElements() {162 reset(endpoint, consumer, endpointConfiguration);163 when(endpoint.createConsumer()).thenReturn(consumer);164 when(endpoint.getEndpointConfiguration()).thenReturn(endpointConfiguration);165 when(endpointConfiguration.getTimeout()).thenReturn(5000L);166 167 Message message = new DefaultMessage("<root>"168 + "<element attributeA='attribute-value' attributeB='attribute-value' >"169 + "<sub-elementA attribute='A'></sub-elementA>"170 + "<sub-elementB attribute='B'></sub-elementB>"171 + "<sub-elementC attribute='C'>text-value</sub-elementC>"172 + "</element>" 173 + "</root>");174 175 when(consumer.receive(any(TestContext.class), anyLong())).thenReturn(message);176 when(endpoint.getActor()).thenReturn(null);177 HashMap<String, Object> validateMessageElements = new HashMap<>();178 validateMessageElements.put("//root/element/sub-elementA", "null");179 validateMessageElements.put("//sub-elementB", "null");180 181 PayloadTemplateMessageBuilder controlMessageBuilder = new PayloadTemplateMessageBuilder();182 XpathMessageValidationContext validationContext = new XpathMessageValidationContext();183 receiveMessageBean.setMessageBuilder(controlMessageBuilder);184 validationContext.setXpathExpressions(validateMessageElements);185 186 List<ValidationContext> validationContexts = new ArrayList<ValidationContext>();187 validationContexts.add(validationContext);188 receiveMessageBean.setValidationContexts(validationContexts);189 190 receiveMessageBean.execute(context);191 }192 193 @Test194 @SuppressWarnings({ "unchecked", "rawtypes" })195 public void testValidateMessageElementAttributes() {196 reset(endpoint, consumer, endpointConfiguration);197 when(endpoint.createConsumer()).thenReturn(consumer);198 when(endpoint.getEndpointConfiguration()).thenReturn(endpointConfiguration);199 when(endpointConfiguration.getTimeout()).thenReturn(5000L);200 201 Message message = new DefaultMessage("<root>"202 + "<element attributeA='attribute-value' attributeB='attribute-value' >"203 + "<sub-elementA attribute='A'>text-value</sub-elementA>"204 + "<sub-elementB attribute='B'>text-value</sub-elementB>"205 + "<sub-elementC attribute='C'>text-value</sub-elementC>"206 + "</element>" 207 + "</root>");208 209 when(consumer.receive(any(TestContext.class), anyLong())).thenReturn(message);210 when(endpoint.getActor()).thenReturn(null);211 HashMap<String, Object> validateMessageElements = new HashMap<>();212 validateMessageElements.put("//root/element/sub-elementA/@attribute", "A");213 validateMessageElements.put("//sub-elementB/@attribute", "B");214 215 PayloadTemplateMessageBuilder controlMessageBuilder = new PayloadTemplateMessageBuilder();216 XpathMessageValidationContext validationContext = new XpathMessageValidationContext();217 receiveMessageBean.setMessageBuilder(controlMessageBuilder);218 validationContext.setXpathExpressions(validateMessageElements);219 220 List<ValidationContext> validationContexts = new ArrayList<ValidationContext>();221 validationContexts.add(validationContext);222 receiveMessageBean.setValidationContexts(validationContexts);223 224 receiveMessageBean.execute(context);225 }226 227 @Test(expectedExceptions = {CitrusRuntimeException.class})228 @SuppressWarnings({ "unchecked", "rawtypes" })229 public void testValidateMessageElementsWrongExpectedElement() {230 reset(endpoint, consumer, endpointConfiguration);231 when(endpoint.createConsumer()).thenReturn(consumer);232 when(endpoint.getEndpointConfiguration()).thenReturn(endpointConfiguration);233 when(endpointConfiguration.getTimeout()).thenReturn(5000L);234 235 Message message = new DefaultMessage("<root>"236 + "<element attributeA='attribute-value' attributeB='attribute-value' >"237 + "<sub-elementA attribute='A'>text-value</sub-elementA>"238 + "<sub-elementB attribute='B'>text-value</sub-elementB>"239 + "<sub-elementC attribute='C'>text-value</sub-elementC>"240 + "</element>" 241 + "</root>");242 243 when(consumer.receive(any(TestContext.class), anyLong())).thenReturn(message);244 when(endpoint.getActor()).thenReturn(null);245 HashMap<String, Object> validateMessageElements = new HashMap<>();246 validateMessageElements.put("//root/element/sub-element-wrong", "text-value");247 validateMessageElements.put("//sub-element-wrong", "text-value");248 249 PayloadTemplateMessageBuilder controlMessageBuilder = new PayloadTemplateMessageBuilder();250 XpathMessageValidationContext validationContext = new XpathMessageValidationContext();251 receiveMessageBean.setMessageBuilder(controlMessageBuilder);252 validationContext.setXpathExpressions(validateMessageElements);253 254 List<ValidationContext> validationContexts = new ArrayList<ValidationContext>();255 validationContexts.add(validationContext);256 receiveMessageBean.setValidationContexts(validationContexts);257 258 receiveMessageBean.execute(context);259 }260 261 @Test(expectedExceptions = {ValidationException.class})262 @SuppressWarnings({ "unchecked", "rawtypes" })263 public void testValidateMessageElementsWrongExpectedValue() {264 reset(endpoint, consumer, endpointConfiguration);265 when(endpoint.createConsumer()).thenReturn(consumer);266 when(endpoint.getEndpointConfiguration()).thenReturn(endpointConfiguration);267 when(endpointConfiguration.getTimeout()).thenReturn(5000L);268 269 Message message = new DefaultMessage("<root>"270 + "<element attributeA='attribute-value' attributeB='attribute-value' >"271 + "<sub-elementA attribute='A'>text-value</sub-elementA>"272 + "<sub-elementB attribute='B'>text-value</sub-elementB>"273 + "<sub-elementC attribute='C'>text-value</sub-elementC>"274 + "</element>" 275 + "</root>");276 277 when(consumer.receive(any(TestContext.class), anyLong())).thenReturn(message);278 when(endpoint.getActor()).thenReturn(null);279 HashMap<String, Object> validateMessageElements = new HashMap<>();280 validateMessageElements.put("//root/element/sub-elementA", "text-value-wrong");281 validateMessageElements.put("//sub-elementB", "text-value-wrong");282 283 PayloadTemplateMessageBuilder controlMessageBuilder = new PayloadTemplateMessageBuilder();284 XpathMessageValidationContext validationContext = new XpathMessageValidationContext();285 receiveMessageBean.setMessageBuilder(controlMessageBuilder);286 validationContext.setXpathExpressions(validateMessageElements);287 288 List<ValidationContext> validationContexts = new ArrayList<ValidationContext>();289 validationContexts.add(validationContext);290 receiveMessageBean.setValidationContexts(validationContexts);291 292 receiveMessageBean.execute(context);293 }294 295 @Test(expectedExceptions = {ValidationException.class})296 @SuppressWarnings({ "unchecked", "rawtypes" })297 public void testValidateMessageElementAttributesWrongExpectedValue() {298 reset(endpoint, consumer, endpointConfiguration);299 when(endpoint.createConsumer()).thenReturn(consumer);300 when(endpoint.getEndpointConfiguration()).thenReturn(endpointConfiguration);301 when(endpointConfiguration.getTimeout()).thenReturn(5000L);302 303 Message message = new DefaultMessage("<root>"304 + "<element attributeA='attribute-value' attributeB='attribute-value' >"305 + "<sub-elementA attribute='A'>text-value</sub-elementA>"306 + "<sub-elementB attribute='B'>text-value</sub-elementB>"307 + "<sub-elementC attribute='C'>text-value</sub-elementC>"308 + "</element>" 309 + "</root>");310 311 when(consumer.receive(any(TestContext.class), anyLong())).thenReturn(message);312 when(endpoint.getActor()).thenReturn(null);313 HashMap<String, Object> validateMessageElements = new HashMap<>();314 validateMessageElements.put("//root/element/sub-elementA/@attribute", "wrong-value");315 validateMessageElements.put("//sub-elementB/@attribute", "wrong-value");316 317 PayloadTemplateMessageBuilder controlMessageBuilder = new PayloadTemplateMessageBuilder();318 XpathMessageValidationContext validationContext = new XpathMessageValidationContext();319 receiveMessageBean.setMessageBuilder(controlMessageBuilder);320 validationContext.setXpathExpressions(validateMessageElements);321 322 List<ValidationContext> validationContexts = new ArrayList<ValidationContext>();323 validationContexts.add(validationContext);324 receiveMessageBean.setValidationContexts(validationContexts);325 326 receiveMessageBean.execute(context);327 }328 329 @Test(expectedExceptions = {CitrusRuntimeException.class})330 @SuppressWarnings({ "unchecked", "rawtypes" })331 public void testValidateMessageElementAttributesWrongExpectedAttribute() {332 reset(endpoint, consumer, endpointConfiguration);333 when(endpoint.createConsumer()).thenReturn(consumer);334 when(endpoint.getEndpointConfiguration()).thenReturn(endpointConfiguration);335 when(endpointConfiguration.getTimeout()).thenReturn(5000L);336 337 Message message = new DefaultMessage("<root>"338 + "<element attributeA='attribute-value' attributeB='attribute-value' >"339 + "<sub-elementA attribute='A'>text-value</sub-elementA>"340 + "<sub-elementB attribute='B'>text-value</sub-elementB>"341 + "<sub-elementC attribute='C'>text-value</sub-elementC>"342 + "</element>" 343 + "</root>");344 345 when(consumer.receive(any(TestContext.class), anyLong())).thenReturn(message);346 when(endpoint.getActor()).thenReturn(null);347 HashMap<String, Object> validateMessageElements = new HashMap<>();348 validateMessageElements.put("//root/element/sub-elementA/@attribute-wrong", "A");349 validateMessageElements.put("//sub-elementB/@attribute-wrong", "B");350 351 PayloadTemplateMessageBuilder controlMessageBuilder = new PayloadTemplateMessageBuilder();352 XpathMessageValidationContext validationContext = new XpathMessageValidationContext();353 receiveMessageBean.setMessageBuilder(controlMessageBuilder);354 validationContext.setXpathExpressions(validateMessageElements);355 356 List<ValidationContext> validationContexts = new ArrayList<ValidationContext>();357 validationContexts.add(validationContext);358 receiveMessageBean.setValidationContexts(validationContexts);359 360 receiveMessageBean.execute(context);361 }362 363 @Test364 @SuppressWarnings({ "unchecked", "rawtypes" })365 public void testSetMessageElements() {366 reset(endpoint, consumer, endpointConfiguration);367 when(endpoint.createConsumer()).thenReturn(consumer);368 when(endpoint.getEndpointConfiguration()).thenReturn(endpointConfiguration);369 when(endpointConfiguration.getTimeout()).thenReturn(5000L);370 371 Message message = new DefaultMessage("<root>"372 + "<element attributeA='attribute-value' attributeB='attribute-value' >"373 + "<sub-elementA attribute='A'>text-value</sub-elementA>"374 + "<sub-elementB attribute='B'>text-value</sub-elementB>"375 + "<sub-elementC attribute='C'>text-value</sub-elementC>"376 + "</element>" 377 + "</root>");378 379 when(consumer.receive(any(TestContext.class), anyLong())).thenReturn(message);380 when(endpoint.getActor()).thenReturn(null);381 382 PayloadTemplateMessageBuilder controlMessageBuilder = new PayloadTemplateMessageBuilder();383 XmlMessageValidationContext validationContext = new XmlMessageValidationContext();384 receiveMessageBean.setMessageBuilder(controlMessageBuilder);385 386 controlMessageBuilder.setPayloadData("<root>"387 + "<element attributeA='attribute-value' attributeB='attribute-value' >"388 + "<sub-elementA attribute='A'>to be overwritten</sub-elementA>"389 + "<sub-elementB attribute='B'>to be overwritten</sub-elementB>"390 + "<sub-elementC attribute='C'>text-value</sub-elementC>"391 + "</element>" 392 + "</root>");393 394 HashMap<String, String> messageElements = new HashMap<String, String>();395 messageElements.put("//root/element/sub-elementA", "text-value");396 messageElements.put("//sub-elementB", "text-value");397 398 XpathMessageConstructionInterceptor interceptor = new XpathMessageConstructionInterceptor(messageElements);399 controlMessageBuilder.add(interceptor);400 List<ValidationContext> validationContexts = new ArrayList<ValidationContext>();401 validationContexts.add(validationContext);402 receiveMessageBean.setValidationContexts(validationContexts);403 404 receiveMessageBean.execute(context);405 }406 407 @Test408 @SuppressWarnings({ "unchecked", "rawtypes" })409 public void testSetMessageElementsUsingEmptyString() {410 reset(endpoint, consumer, endpointConfiguration);411 when(endpoint.createConsumer()).thenReturn(consumer);412 when(endpoint.getEndpointConfiguration()).thenReturn(endpointConfiguration);413 when(endpointConfiguration.getTimeout()).thenReturn(5000L);414 415 Message message = new DefaultMessage("<root>"416 + "<element attributeA='attribute-value' attributeB='attribute-value' >"417 + "<sub-elementA attribute='A'></sub-elementA>"418 + "<sub-elementB attribute='B'></sub-elementB>"419 + "<sub-elementC attribute='C'>text-value</sub-elementC>"420 + "</element>" 421 + "</root>");422 423 when(consumer.receive(any(TestContext.class), anyLong())).thenReturn(message);424 when(endpoint.getActor()).thenReturn(null);425 426 PayloadTemplateMessageBuilder controlMessageBuilder = new PayloadTemplateMessageBuilder();427 XmlMessageValidationContext validationContext = new XmlMessageValidationContext();428 receiveMessageBean.setMessageBuilder(controlMessageBuilder);429 430 controlMessageBuilder.setPayloadData("<root>"431 + "<element attributeA='attribute-value' attributeB='attribute-value' >"432 + "<sub-elementA attribute='A'>to be overwritten</sub-elementA>"433 + "<sub-elementB attribute='B'>to be overwritten</sub-elementB>"434 + "<sub-elementC attribute='C'>text-value</sub-elementC>"435 + "</element>" 436 + "</root>");437 438 HashMap<String, String> messageElements = new HashMap<String, String>();439 messageElements.put("//root/element/sub-elementA", "");440 messageElements.put("//sub-elementB", "");441 442 XpathMessageConstructionInterceptor interceptor = new XpathMessageConstructionInterceptor(messageElements);443 controlMessageBuilder.add(interceptor);444 List<ValidationContext> validationContexts = new ArrayList<ValidationContext>();445 validationContexts.add(validationContext);446 receiveMessageBean.setValidationContexts(validationContexts);447 448 receiveMessageBean.execute(context);449 }450 451 @Test452 @SuppressWarnings({ "unchecked", "rawtypes" })453 public void testSetMessageElementsAndValidate() {454 reset(endpoint, consumer, endpointConfiguration);455 when(endpoint.createConsumer()).thenReturn(consumer);456 when(endpoint.getEndpointConfiguration()).thenReturn(endpointConfiguration);457 when(endpointConfiguration.getTimeout()).thenReturn(5000L);458 459 Message message = new DefaultMessage("<root>"460 + "<element attributeA='attribute-value' attributeB='attribute-value' >"461 + "<sub-elementA attribute='A'>text-value</sub-elementA>"462 + "<sub-elementB attribute='B'>text-value</sub-elementB>"463 + "<sub-elementC attribute='C'>text-value</sub-elementC>"464 + "</element>" 465 + "</root>");466 467 when(consumer.receive(any(TestContext.class), anyLong())).thenReturn(message);468 when(endpoint.getActor()).thenReturn(null);469 470 PayloadTemplateMessageBuilder controlMessageBuilder = new PayloadTemplateMessageBuilder();471 XpathMessageValidationContext validationContext = new XpathMessageValidationContext();472 receiveMessageBean.setMessageBuilder(controlMessageBuilder);473 474 controlMessageBuilder.setPayloadData("<root>"475 + "<element attributeA='attribute-value' attributeB='attribute-value' >"476 + "<sub-elementA attribute='A'>to be overwritten</sub-elementA>"477 + "<sub-elementB attribute='B'>to be overwritten</sub-elementB>"478 + "<sub-elementC attribute='C'>text-value</sub-elementC>"479 + "</element>" 480 + "</root>");481 482 HashMap<String, String> messageElements = new HashMap<String, String>();483 messageElements.put("//root/element/sub-elementA", "text-value");484 messageElements.put("//sub-elementB", "text-value");485 486 XpathMessageConstructionInterceptor interceptor = new XpathMessageConstructionInterceptor(messageElements);487 controlMessageBuilder.add(interceptor);488 List<ValidationContext> validationContexts = new ArrayList<ValidationContext>();489 validationContexts.add(validationContext);490 receiveMessageBean.setValidationContexts(validationContexts);491 492 HashMap<String, Object> validateElements = new HashMap<>();493 validateElements.put("//root/element/sub-elementA", "text-value");494 validateElements.put("//sub-elementB", "text-value");495 496 validationContext.setXpathExpressions(validateElements);497 498 receiveMessageBean.execute(context);499 }500 501 @Test502 @SuppressWarnings({ "unchecked", "rawtypes" })503 public void testSetMessageElementAttributes() {504 reset(endpoint, consumer, endpointConfiguration);505 when(endpoint.createConsumer()).thenReturn(consumer);506 when(endpoint.getEndpointConfiguration()).thenReturn(endpointConfiguration);507 when(endpointConfiguration.getTimeout()).thenReturn(5000L);508 509 Message message = new DefaultMessage("<root>"510 + "<element attributeA='attribute-value' attributeB='attribute-value' >"511 + "<sub-elementA attribute='A'>text-value</sub-elementA>"512 + "<sub-elementB attribute='B'>text-value</sub-elementB>"513 + "<sub-elementC attribute='C'>text-value</sub-elementC>"514 + "</element>" 515 + "</root>");516 517 when(consumer.receive(any(TestContext.class), anyLong())).thenReturn(message);518 when(endpoint.getActor()).thenReturn(null);519 520 PayloadTemplateMessageBuilder controlMessageBuilder = new PayloadTemplateMessageBuilder();521 XmlMessageValidationContext validationContext = new XmlMessageValidationContext();522 receiveMessageBean.setMessageBuilder(controlMessageBuilder);523 524 controlMessageBuilder.setPayloadData("<root>"525 + "<element attributeA='attribute-value' attributeB='attribute-value' >"526 + "<sub-elementA attribute='to be overwritten'>text-value</sub-elementA>"527 + "<sub-elementB attribute='to be overwritten'>text-value</sub-elementB>"528 + "<sub-elementC attribute='C'>text-value</sub-elementC>"529 + "</element>" 530 + "</root>");531 532 HashMap<String, String> messageElements = new HashMap<String, String>();533 messageElements.put("//root/element/sub-elementA/@attribute", "A");534 messageElements.put("//sub-elementB/@attribute", "B");535 536 XpathMessageConstructionInterceptor interceptor = new XpathMessageConstructionInterceptor(messageElements);537 controlMessageBuilder.add(interceptor);538 List<ValidationContext> validationContexts = new ArrayList<ValidationContext>();539 validationContexts.add(validationContext);540 receiveMessageBean.setValidationContexts(validationContexts);541 542 receiveMessageBean.execute(context);543 }544 545 @Test(expectedExceptions = {CitrusRuntimeException.class})546 @SuppressWarnings({ "unchecked", "rawtypes" })547 public void testSetMessageElementsError() {548 reset(endpoint, consumer, endpointConfiguration);549 when(endpoint.createConsumer()).thenReturn(consumer);550 when(endpoint.getEndpointConfiguration()).thenReturn(endpointConfiguration);551 when(endpointConfiguration.getTimeout()).thenReturn(5000L);552 553 Message message = new DefaultMessage("<root>"554 + "<element attributeA='attribute-value' attributeB='attribute-value' >"555 + "<sub-elementA attribute='A'>text-value</sub-elementA>"556 + "<sub-elementB attribute='B'>text-value</sub-elementB>"557 + "<sub-elementC attribute='C'>text-value</sub-elementC>"558 + "</element>" 559 + "</root>");560 561 when(consumer.receive(any(TestContext.class), anyLong())).thenReturn(message);562 when(endpoint.getActor()).thenReturn(null);563 564 PayloadTemplateMessageBuilder controlMessageBuilder = new PayloadTemplateMessageBuilder();565 XmlMessageValidationContext validationContext = new XmlMessageValidationContext();566 receiveMessageBean.setMessageBuilder(controlMessageBuilder);567 568 controlMessageBuilder.setPayloadData("<root>"569 + "<element attributeA='attribute-value' attributeB='attribute-value' >"570 + "<sub-elementA attribute='A'>to be overwritten</sub-elementA>"571 + "<sub-elementB attribute='B'>to be overwritten</sub-elementB>"572 + "<sub-elementC attribute='C'>text-value</sub-elementC>"573 + "</element>" 574 + "</root>");575 576 HashMap<String, String> messageElements = new HashMap<String, String>();577 messageElements.put("//root/element/sub-element-wrong", "text-value");578 messageElements.put("//sub-element-wrong", "text-value");579 580 XpathMessageConstructionInterceptor interceptor = new XpathMessageConstructionInterceptor(messageElements);581 controlMessageBuilder.add(interceptor);582 List<ValidationContext> validationContexts = new ArrayList<ValidationContext>();583 validationContexts.add(validationContext);584 receiveMessageBean.setValidationContexts(validationContexts);585 586 receiveMessageBean.execute(context);587 }588 589 @Test(expectedExceptions = {CitrusRuntimeException.class})590 @SuppressWarnings({ "unchecked", "rawtypes" })591 public void testSetMessageElementAttributesError() {592 reset(endpoint, consumer, endpointConfiguration);593 when(endpoint.createConsumer()).thenReturn(consumer);594 when(endpoint.getEndpointConfiguration()).thenReturn(endpointConfiguration);595 when(endpointConfiguration.getTimeout()).thenReturn(5000L);596 597 Message message = new DefaultMessage("<root>"598 + "<element attributeA='attribute-value' attributeB='attribute-value' >"599 + "<sub-elementA attribute='A'>text-value</sub-elementA>"600 + "<sub-elementB attribute='B'>text-value</sub-elementB>"601 + "<sub-elementC attribute='C'>text-value</sub-elementC>"602 + "</element>" 603 + "</root>");604 605 when(consumer.receive(any(TestContext.class), anyLong())).thenReturn(message);606 when(endpoint.getActor()).thenReturn(null);607 608 PayloadTemplateMessageBuilder controlMessageBuilder = new PayloadTemplateMessageBuilder();609 XmlMessageValidationContext validationContext = new XmlMessageValidationContext();610 receiveMessageBean.setMessageBuilder(controlMessageBuilder);611 612 controlMessageBuilder.setPayloadData("<root>"613 + "<element attributeA='attribute-value' attributeB='attribute-value' >"614 + "<sub-elementA attribute='to be overwritten'>text-value</sub-elementA>"615 + "<sub-elementB attribute='to be overwritten'>text-value</sub-elementB>"616 + "<sub-elementC attribute='C'>text-value</sub-elementC>"617 + "</element>" 618 + "</root>");619 620 HashMap<String, String> messageElements = new HashMap<String, String>();621 messageElements.put("//root/element/sub-elementA/@attribute-wrong", "A");622 messageElements.put("//sub-elementB/@attribute-wrong", "B");623 624 XpathMessageConstructionInterceptor interceptor = new XpathMessageConstructionInterceptor(messageElements);625 controlMessageBuilder.add(interceptor);626 List<ValidationContext> validationContexts = new ArrayList<ValidationContext>();627 validationContexts.add(validationContext);628 receiveMessageBean.setValidationContexts(validationContexts);629 630 receiveMessageBean.execute(context);631 }632 633 @Test(expectedExceptions = {CitrusRuntimeException.class})634 @SuppressWarnings({ "unchecked", "rawtypes" })635 public void testSetMessageElementAttributesErrorWrongElement() {636 reset(endpoint, consumer, endpointConfiguration);637 when(endpoint.createConsumer()).thenReturn(consumer);638 when(endpoint.getEndpointConfiguration()).thenReturn(endpointConfiguration);639 when(endpointConfiguration.getTimeout()).thenReturn(5000L);640 641 Message message = new DefaultMessage("<root>"642 + "<element attributeA='attribute-value' attributeB='attribute-value' >"643 + "<sub-elementA attribute='A'>text-value</sub-elementA>"644 + "<sub-elementB attribute='B'>text-value</sub-elementB>"645 + "<sub-elementC attribute='C'>text-value</sub-elementC>"646 + "</element>" 647 + "</root>");648 649 when(consumer.receive(any(TestContext.class), anyLong())).thenReturn(message);650 when(endpoint.getActor()).thenReturn(null);651 652 PayloadTemplateMessageBuilder controlMessageBuilder = new PayloadTemplateMessageBuilder();653 XmlMessageValidationContext validationContext = new XmlMessageValidationContext();654 receiveMessageBean.setMessageBuilder(controlMessageBuilder);655 656 controlMessageBuilder.setPayloadData("<root>"657 + "<element attributeA='attribute-value' attributeB='attribute-value' >"658 + "<sub-elementA attribute='to be overwritten'>text-value</sub-elementA>"659 + "<sub-elementB attribute='to be overwritten'>text-value</sub-elementB>"660 + "<sub-elementC attribute='C'>text-value</sub-elementC>"661 + "</element>" 662 + "</root>");663 664 HashMap<String, String> messageElements = new HashMap<String, String>();665 messageElements.put("//root/element/sub-elementA-wrong/@attribute", "A");666 messageElements.put("//sub-elementB-wrong/@attribute", "B");667 668 XpathMessageConstructionInterceptor interceptor = new XpathMessageConstructionInterceptor(messageElements);669 controlMessageBuilder.add(interceptor);670 List<ValidationContext> validationContexts = new ArrayList<ValidationContext>();671 validationContexts.add(validationContext);672 receiveMessageBean.setValidationContexts(validationContexts);673 674 receiveMessageBean.execute(context);675 }676 @Test677 @SuppressWarnings({ "unchecked", "rawtypes" })678 public void testExtractMessageElements() {679 reset(endpoint, consumer, endpointConfiguration);680 when(endpoint.createConsumer()).thenReturn(consumer);681 when(endpoint.getEndpointConfiguration()).thenReturn(endpointConfiguration);682 when(endpointConfiguration.getTimeout()).thenReturn(5000L);683 684 Message message = new DefaultMessage("<root>"685 + "<element attributeA='attribute-value' attributeB='attribute-value' >"686 + "<sub-elementA attribute='A'>text-value</sub-elementA>"687 + "<sub-elementB attribute='B'>text-value</sub-elementB>"688 + "<sub-elementC attribute='C'>text-value</sub-elementC>"689 + "</element>" 690 + "</root>");691 692 when(consumer.receive(any(TestContext.class), anyLong())).thenReturn(message);693 when(endpoint.getActor()).thenReturn(null);694 695 PayloadTemplateMessageBuilder controlMessageBuilder = new PayloadTemplateMessageBuilder();696 XmlMessageValidationContext validationContext = new XmlMessageValidationContext();697 receiveMessageBean.setMessageBuilder(controlMessageBuilder);698 699 controlMessageBuilder.setPayloadData("<root>"700 + "<element attributeA='attribute-value' attributeB='attribute-value' >"701 + "<sub-elementA attribute='A'>text-value</sub-elementA>"702 + "<sub-elementB attribute='B'>text-value</sub-elementB>"703 + "<sub-elementC attribute='C'>text-value</sub-elementC>"704 + "</element>" 705 + "</root>");706 707 HashMap<String, String> extractMessageElements = new HashMap<String, String>();708 extractMessageElements.put("//root/element/sub-elementA", "${valueA}");709 extractMessageElements.put("//root/element/sub-elementB", "${valueB}");710 711 XpathPayloadVariableExtractor variableExtractor = new XpathPayloadVariableExtractor();712 variableExtractor.setXpathExpressions(extractMessageElements);713 714 receiveMessageBean.addVariableExtractors(variableExtractor);715 716 List<ValidationContext> validationContexts = new ArrayList<ValidationContext>();717 validationContexts.add(validationContext);718 receiveMessageBean.setValidationContexts(validationContexts);719 720 receiveMessageBean.execute(context);721 722 Assert.assertTrue(context.getVariables().containsKey("valueA"));723 Assert.assertEquals(context.getVariables().get("valueA"), "text-value");724 Assert.assertTrue(context.getVariables().containsKey("valueB"));725 Assert.assertEquals(context.getVariables().get("valueB"), "text-value");726 }727 728 @Test729 @SuppressWarnings({ "unchecked", "rawtypes" })730 public void testExtractMessageAttributes() {731 reset(endpoint, consumer, endpointConfiguration);732 when(endpoint.createConsumer()).thenReturn(consumer);733 when(endpoint.getEndpointConfiguration()).thenReturn(endpointConfiguration);734 when(endpointConfiguration.getTimeout()).thenReturn(5000L);735 736 Message message = new DefaultMessage("<root>"737 + "<element attributeA='attribute-value' attributeB='attribute-value' >"738 + "<sub-elementA attribute='A'>text-value</sub-elementA>"739 + "<sub-elementB attribute='B'>text-value</sub-elementB>"740 + "<sub-elementC attribute='C'>text-value</sub-elementC>"741 + "</element>" 742 + "</root>");743 744 when(consumer.receive(any(TestContext.class), anyLong())).thenReturn(message);745 when(endpoint.getActor()).thenReturn(null);746 747 PayloadTemplateMessageBuilder controlMessageBuilder = new PayloadTemplateMessageBuilder();748 XmlMessageValidationContext validationContext = new XmlMessageValidationContext();749 receiveMessageBean.setMessageBuilder(controlMessageBuilder);750 751 controlMessageBuilder.setPayloadData("<root>"752 + "<element attributeA='attribute-value' attributeB='attribute-value' >"753 + "<sub-elementA attribute='A'>text-value</sub-elementA>"754 + "<sub-elementB attribute='B'>text-value</sub-elementB>"755 + "<sub-elementC attribute='C'>text-value</sub-elementC>"756 + "</element>" 757 + "</root>");758 759 HashMap<String, String> extractMessageElements = new HashMap<String, String>();760 extractMessageElements.put("//root/element/sub-elementA/@attribute", "${valueA}");761 extractMessageElements.put("//root/element/sub-elementB/@attribute", "${valueB}");762 763 XpathPayloadVariableExtractor variableExtractor = new XpathPayloadVariableExtractor();764 variableExtractor.setXpathExpressions(extractMessageElements);765 766 receiveMessageBean.addVariableExtractors(variableExtractor);767 768 List<ValidationContext> validationContexts = new ArrayList<ValidationContext>();769 validationContexts.add(validationContext);770 receiveMessageBean.setValidationContexts(validationContexts);771 772 receiveMessageBean.execute(context);773 774 Assert.assertTrue(context.getVariables().containsKey("valueA"));775 Assert.assertEquals(context.getVariables().get("valueA"), "A");776 Assert.assertTrue(context.getVariables().containsKey("valueB"));777 Assert.assertEquals(context.getVariables().get("valueB"), "B");778 }779 780 @Test(expectedExceptions = {CitrusRuntimeException.class})781 @SuppressWarnings({ "unchecked", "rawtypes" })782 public void testExtractMessageElementsForWrongElement() {783 reset(endpoint, consumer, endpointConfiguration);784 when(endpoint.createConsumer()).thenReturn(consumer);785 when(endpoint.getEndpointConfiguration()).thenReturn(endpointConfiguration);786 when(endpointConfiguration.getTimeout()).thenReturn(5000L);787 788 Message message = new DefaultMessage("<root>"789 + "<element attributeA='attribute-value' attributeB='attribute-value' >"790 + "<sub-elementA attribute='A'>text-value</sub-elementA>"791 + "<sub-elementB attribute='B'>text-value</sub-elementB>"792 + "<sub-elementC attribute='C'>text-value</sub-elementC>"793 + "</element>" 794 + "</root>");795 796 when(consumer.receive(any(TestContext.class), anyLong())).thenReturn(message);797 when(endpoint.getActor()).thenReturn(null);798 799 PayloadTemplateMessageBuilder controlMessageBuilder = new PayloadTemplateMessageBuilder();800 XmlMessageValidationContext validationContext = new XmlMessageValidationContext();801 receiveMessageBean.setMessageBuilder(controlMessageBuilder);802 803 controlMessageBuilder.setPayloadData("<root>"804 + "<element attributeA='attribute-value' attributeB='attribute-value' >"805 + "<sub-elementA attribute='A'>text-value</sub-elementA>"806 + "<sub-elementB attribute='B'>text-value</sub-elementB>"807 + "<sub-elementC attribute='C'>text-value</sub-elementC>"808 + "</element>" 809 + "</root>");810 811 HashMap<String, String> extractMessageElements = new HashMap<String, String>();812 extractMessageElements.put("//root/element/sub-element-wrong", "${valueA}");813 extractMessageElements.put("//element/sub-element-wrong", "${valueB}");814 815 XpathPayloadVariableExtractor variableExtractor = new XpathPayloadVariableExtractor();816 variableExtractor.setXpathExpressions(extractMessageElements);817 818 receiveMessageBean.addVariableExtractors(variableExtractor);819 820 List<ValidationContext> validationContexts = new ArrayList<ValidationContext>();821 validationContexts.add(validationContext);822 receiveMessageBean.setValidationContexts(validationContexts);823 824 receiveMessageBean.execute(context);825 826 Assert.assertFalse(context.getVariables().containsKey("valueA"));827 Assert.assertFalse(context.getVariables().containsKey("valueB"));828 }829 830 @Test(expectedExceptions = {CitrusRuntimeException.class})831 @SuppressWarnings({ "unchecked", "rawtypes" })832 public void testExtractMessageElementsForWrongAtribute() {833 reset(endpoint, consumer, endpointConfiguration);834 when(endpoint.createConsumer()).thenReturn(consumer);835 when(endpoint.getEndpointConfiguration()).thenReturn(endpointConfiguration);836 when(endpointConfiguration.getTimeout()).thenReturn(5000L);837 838 Message message = new DefaultMessage("<root>"839 + "<element attributeA='attribute-value' attributeB='attribute-value' >"840 + "<sub-elementA attribute='A'>text-value</sub-elementA>"841 + "<sub-elementB attribute='B'>text-value</sub-elementB>"842 + "<sub-elementC attribute='C'>text-value</sub-elementC>"843 + "</element>" 844 + "</root>");845 846 when(consumer.receive(any(TestContext.class), anyLong())).thenReturn(message);847 when(endpoint.getActor()).thenReturn(null);848 849 PayloadTemplateMessageBuilder controlMessageBuilder = new PayloadTemplateMessageBuilder();850 XmlMessageValidationContext validationContext = new XmlMessageValidationContext();851 receiveMessageBean.setMessageBuilder(controlMessageBuilder);852 853 controlMessageBuilder.setPayloadData("<root>"854 + "<element attributeA='attribute-value' attributeB='attribute-value' >"855 + "<sub-elementA attribute='A'>text-value</sub-elementA>"856 + "<sub-elementB attribute='B'>text-value</sub-elementB>"857 + "<sub-elementC attribute='C'>text-value</sub-elementC>"858 + "</element>" 859 + "</root>");860 861 HashMap<String, String> extractMessageElements = new HashMap<String, String>();862 extractMessageElements.put("//root/element/sub-elementA/@attribute-wrong", "${attributeA}");863 864 XpathPayloadVariableExtractor variableExtractor = new XpathPayloadVariableExtractor();865 variableExtractor.setXpathExpressions(extractMessageElements);866 867 receiveMessageBean.addVariableExtractors(variableExtractor);868 869 List<ValidationContext> validationContexts = new ArrayList<ValidationContext>();870 validationContexts.add(validationContext);871 receiveMessageBean.setValidationContexts(validationContexts);872 873 receiveMessageBean.execute(context);874 875 Assert.assertFalse(context.getVariables().containsKey("attributeA"));876 }877}...
Source:XPathTest.java
...78 79 PayloadTemplateMessageBuilder controlMessageBuilder = new PayloadTemplateMessageBuilder();80 XpathMessageValidationContext validationContext = new XpathMessageValidationContext();81 receiveMessageBean.setMessageBuilder(controlMessageBuilder);82 validationContext.setXpathExpressions(validateMessageElements);83 84 validationContext.setSchemaValidation(false);85 86 List<ValidationContext> validationContexts = new ArrayList<ValidationContext>();87 validationContexts.add(validationContext);88 receiveMessageBean.setValidationContexts(validationContexts);89 receiveMessageBean.execute(context);90 }91 92 @Test93 @SuppressWarnings({ "unchecked", "rawtypes" })94 public void testUsingXPathWithDefaultNamespace() {95 reset(endpoint, consumer, endpointConfiguration);96 when(endpoint.createConsumer()).thenReturn(consumer);97 when(endpoint.getEndpointConfiguration()).thenReturn(endpointConfiguration);98 when(endpointConfiguration.getTimeout()).thenReturn(5000L);99 100 Message message = new DefaultMessage("<root xmlns='http://test'>"101 + "<element attributeA='attribute-value' attributeB='attribute-value'>"102 + "<sub-elementA attribute='A'>text-value</sub-elementA>"103 + "<sub-elementB attribute='B'>text-value</sub-elementB>"104 + "<sub-elementC attribute='C'>text-value</sub-elementC>"105 + "</element>"106 + "<ns-element>namespace</ns-element>"107 + "<search-element>search-for</search-element>"108 + "</root>");109 when(consumer.receive(any(TestContext.class), anyLong())).thenReturn(message);110 when(endpoint.getActor()).thenReturn(null);111 HashMap<String, Object> validateMessageElements = new HashMap<>();112 validateMessageElements.put("//:element/:sub-elementA", "text-value");113 validateMessageElements.put("//:element/:sub-elementA[@attribute='A']", "text-value");114 validateMessageElements.put("//:element/:sub-elementB", "text-value");115 validateMessageElements.put("//:element/:sub-elementB/@attribute", "B");116 validateMessageElements.put("//:ns-element", "namespace");117 validateMessageElements.put("//*[.='search-for']", "search-for");118 119 PayloadTemplateMessageBuilder controlMessageBuilder = new PayloadTemplateMessageBuilder();120 XpathMessageValidationContext validationContext = new XpathMessageValidationContext();121 receiveMessageBean.setMessageBuilder(controlMessageBuilder);122 validationContext.setXpathExpressions(validateMessageElements);123 124 validationContext.setSchemaValidation(false);125 126 List<ValidationContext> validationContexts = new ArrayList<ValidationContext>();127 validationContexts.add(validationContext);128 receiveMessageBean.setValidationContexts(validationContexts);129 receiveMessageBean.execute(context);130 }131 132 @Test133 @SuppressWarnings({ "unchecked", "rawtypes" })134 public void testUsingXPathWithExplicitNamespace() {135 reset(endpoint, consumer, endpointConfiguration);136 when(endpoint.createConsumer()).thenReturn(consumer);137 when(endpoint.getEndpointConfiguration()).thenReturn(endpointConfiguration);138 when(endpointConfiguration.getTimeout()).thenReturn(5000L);139 140 Message message = new DefaultMessage("<root xmlns='http://test' xmlns:ns1='http://citrus'>"141 + "<element attributeA='attribute-value' attributeB='attribute-value'>"142 + "<sub-elementA attribute='A'>text-value</sub-elementA>"143 + "<sub-elementB attribute='B'>text-value</sub-elementB>"144 + "<sub-elementC attribute='C'>text-value</sub-elementC>"145 + "</element>"146 + "<ns1:ns-element>namespace</ns1:ns-element>"147 + "<search-element>search-for</search-element>"148 + "</root>");149 when(consumer.receive(any(TestContext.class), anyLong())).thenReturn(message);150 when(endpoint.getActor()).thenReturn(null);151 HashMap<String, Object> validateMessageElements = new HashMap<>();152 validateMessageElements.put("//:element/:sub-elementA", "text-value");153 validateMessageElements.put("//ns1:ns-element", "namespace");154 155 PayloadTemplateMessageBuilder controlMessageBuilder = new PayloadTemplateMessageBuilder();156 XpathMessageValidationContext validationContext = new XpathMessageValidationContext();157 receiveMessageBean.setMessageBuilder(controlMessageBuilder);158 validationContext.setXpathExpressions(validateMessageElements);159 160 validationContext.setSchemaValidation(false);161 162 List<ValidationContext> validationContexts = new ArrayList<ValidationContext>();163 validationContexts.add(validationContext);164 receiveMessageBean.setValidationContexts(validationContexts);165 receiveMessageBean.execute(context);166 }167 168 @Test169 @SuppressWarnings({ "unchecked", "rawtypes" })170 public void testUsingXPathWithExplicitNamespaceInElementDefinition() {171 reset(endpoint, consumer, endpointConfiguration);172 when(endpoint.createConsumer()).thenReturn(consumer);173 when(endpoint.getEndpointConfiguration()).thenReturn(endpointConfiguration);174 when(endpointConfiguration.getTimeout()).thenReturn(5000L);175 176 Message message = new DefaultMessage("<root xmlns='http://test'>"177 + "<element attributeA='attribute-value' attributeB='attribute-value'>"178 + "<sub-elementA attribute='A'>text-value</sub-elementA>"179 + "<sub-elementB attribute='B'>text-value</sub-elementB>"180 + "<sub-elementC attribute='C'>text-value</sub-elementC>"181 + "</element>"182 + "<ns1:ns-element xmlns:ns1='http://citrus'>namespace</ns1:ns-element>"183 + "<search-element>search-for</search-element>"184 + "</root>");185 when(consumer.receive(any(TestContext.class), anyLong())).thenReturn(message);186 when(endpoint.getActor()).thenReturn(null);187 HashMap<String, Object> validateMessageElements = new HashMap<>();188 validateMessageElements.put("//:element/:sub-elementA", "text-value");189 validateMessageElements.put("//ns1:ns-element", "namespace");190 191 PayloadTemplateMessageBuilder controlMessageBuilder = new PayloadTemplateMessageBuilder();192 XpathMessageValidationContext validationContext = new XpathMessageValidationContext();193 receiveMessageBean.setMessageBuilder(controlMessageBuilder);194 validationContext.setXpathExpressions(validateMessageElements);195 196 Map<String, String> namespaces = new HashMap<String, String>();197 namespaces.put("ns1", "http://citrus");198 199 validationContext.setNamespaces(namespaces);200 201 validationContext.setSchemaValidation(false);202 203 List<ValidationContext> validationContexts = new ArrayList<ValidationContext>();204 validationContexts.add(validationContext);205 receiveMessageBean.setValidationContexts(validationContexts);206 receiveMessageBean.execute(context);207 }208 209 @Test210 @SuppressWarnings({ "unchecked", "rawtypes" })211 public void testValidateMessageElementsUsingXPathWithResultTypes() {212 reset(endpoint, consumer, endpointConfiguration);213 when(endpoint.createConsumer()).thenReturn(consumer);214 when(endpoint.getEndpointConfiguration()).thenReturn(endpointConfiguration);215 when(endpointConfiguration.getTimeout()).thenReturn(5000L);216 217 Message message = new DefaultMessage("<ns1:root xmlns='http://test' xmlns:ns1='http://citrus'>"218 + "<element attributeA='attribute-value' attributeB='attribute-value'>"219 + "<sub-elementA attribute='A'>text-value</sub-elementA>"220 + "<sub-elementB attribute='B'>text-value</sub-elementB>"221 + "<sub-elementC attribute='C'>text-value</sub-elementC>"222 + "</element>"223 + "<ns1:ns-element>namespace</ns1:ns-element>"224 + "<search-element>search-for</search-element>"225 + "</ns1:root>");226 when(consumer.receive(any(TestContext.class), anyLong())).thenReturn(message);227 when(endpoint.getActor()).thenReturn(null);228 HashMap<String, Object> validateMessageElements = new HashMap<>();229 validateMessageElements.put("node://:element/:sub-elementA", "text-value");230 validateMessageElements.put("node://:element/:sub-elementA[@attribute='A']", "text-value");231 validateMessageElements.put("node://:element/:sub-elementB", "text-value");232 validateMessageElements.put("node://:element/:sub-elementB/@attribute", "B");233 validateMessageElements.put("node://ns1:ns-element", "namespace");234 validateMessageElements.put("node://*[.='search-for']", "search-for");235 validateMessageElements.put("number:count(/ns1:root/:element/*)", "3.0");236 validateMessageElements.put("string:concat(/ns1:root/ns1:ns-element, ' is the value')", "namespace is the value");237 validateMessageElements.put("string:local-name(/*)", "root");238 validateMessageElements.put("string:namespace-uri(/*)", "http://citrus");239 validateMessageElements.put("boolean:contains(/ns1:root/:search-element, 'search')", "true");240 validateMessageElements.put("boolean:/ns1:root/:element", "true");241 validateMessageElements.put("boolean:/ns1:root/:element-does-not-exist", "false");242 243 PayloadTemplateMessageBuilder controlMessageBuilder = new PayloadTemplateMessageBuilder();244 XpathMessageValidationContext validationContext = new XpathMessageValidationContext();245 receiveMessageBean.setMessageBuilder(controlMessageBuilder);246 validationContext.setXpathExpressions(validateMessageElements);247 248 validationContext.setSchemaValidation(false);249 250 List<ValidationContext> validationContexts = new ArrayList<ValidationContext>();251 validationContexts.add(validationContext);252 receiveMessageBean.setValidationContexts(validationContexts);253 receiveMessageBean.execute(context);254 }255 256 @Test257 @SuppressWarnings({ "unchecked", "rawtypes" })258 public void testExtractMessageValuesUsingXPathWithResultTypes() {259 reset(endpoint, consumer, endpointConfiguration);260 when(endpoint.createConsumer()).thenReturn(consumer);261 when(endpoint.getEndpointConfiguration()).thenReturn(endpointConfiguration);262 when(endpointConfiguration.getTimeout()).thenReturn(5000L);263 264 Message message = new DefaultMessage("<ns1:root xmlns='http://test' xmlns:ns1='http://citrus'>"265 + "<element attributeA='attribute-value' attributeB='attribute-value'>"266 + "<sub-elementA attribute='A'>text-value</sub-elementA>"267 + "<sub-elementB attribute='B'>text-value</sub-elementB>"268 + "<sub-elementC attribute='C'>text-value</sub-elementC>"269 + "</element>"270 + "<ns1:ns-element>namespace</ns1:ns-element>"271 + "<search-element>search-for</search-element>"272 + "</ns1:root>");273 when(consumer.receive(any(TestContext.class), anyLong())).thenReturn(message);274 when(endpoint.getActor()).thenReturn(null);275 HashMap<String, String> extractMessageElements = new HashMap<String, String>();276 extractMessageElements.put("node://:element/:sub-elementA", "elementA");277 extractMessageElements.put("node://:element/:sub-elementA/@attribute", "elementAttribute");278 extractMessageElements.put("node://*[.='search-for']", "search");279 extractMessageElements.put("number:count(/ns1:root/:element/*)", "count");280 extractMessageElements.put("string:concat(/ns1:root/ns1:ns-element, ' is the value')", "concat");281 extractMessageElements.put("string:local-name(/*)", "localName");282 extractMessageElements.put("string:namespace-uri(/*)", "namespaceUri");283 extractMessageElements.put("boolean:contains(/ns1:root/:search-element, 'search')", "contains");284 extractMessageElements.put("boolean:/ns1:root/:element", "exists");285 extractMessageElements.put("boolean:/ns1:root/:element-does-not-exist", "existsNot");286 287 XpathPayloadVariableExtractor variableExtractor = new XpathPayloadVariableExtractor();288 variableExtractor.setXpathExpressions(extractMessageElements);289 290 receiveMessageBean.addVariableExtractors(variableExtractor);291 292 PayloadTemplateMessageBuilder controlMessageBuilder = new PayloadTemplateMessageBuilder();293 XmlMessageValidationContext validationContext = new XmlMessageValidationContext();294 receiveMessageBean.setMessageBuilder(controlMessageBuilder);295 296 validationContext.setSchemaValidation(false);297 298 List<ValidationContext> validationContexts = new ArrayList<ValidationContext>();299 validationContexts.add(validationContext);300 receiveMessageBean.setValidationContexts(validationContexts);301 receiveMessageBean.execute(context);302 ...
Source:XpathMessageValidatorTest.java
...38 + "<sub-element attribute='A'>text-value</sub-element>"39 + "</element>"40 + "</root>");41 XpathMessageValidationContext validationContext = new XpathMessageValidationContext();42 validationContext.setXpathExpressions(Collections.<String, Object>singletonMap("//element/sub-element", "text-value"));43 validator.validateMessage(message, new DefaultMessage(), context, validationContext);44 }45 @Test46 public void testValidateMessageElementsWithValidationMatcherSuccessful() {47 Message message = new DefaultMessage("<root>"48 + "<element attributeA='attribute-value' attributeB='attribute-value'>"49 + "<sub-element attribute='A'>text-value</sub-element>"50 + "</element>"51 + "</root>");52 XpathMessageValidationContext validationContext = new XpathMessageValidationContext();53 Map<String, Object> validationExpressions = new HashMap<>();54 validationExpressions.put("//element/@attributeA", "@startsWith('attribute-')@");55 validationExpressions.put("//element/@attributeB", "@endsWith('-value')@");56 validationExpressions.put("//element/sub-element", "@equalsIgnoreCase('TEXT-VALUE')@");57 validationContext.setXpathExpressions(validationExpressions);58 validator.validateMessage(message, new DefaultMessage(), context, validationContext);59 }60 @Test(expectedExceptions = {ValidationException.class})61 public void testValidateMessageElementsWithValidationMatcherNotSuccessful() {62 Message message = new DefaultMessage("<root>"63 + "<element attributeA='attribute-value' attributeB='attribute-value'>"64 + "<sub-element attribute='A'>text-value</sub-element>"65 + "</element>"66 + "</root>");67 XpathMessageValidationContext validationContext = new XpathMessageValidationContext();68 Map<String, Object> validationExpressions = new HashMap<>();69 validationExpressions.put("//element/@attributeA", "@startsWith('attribute-')@");70 validationExpressions.put("//element/@attributeB", "@endsWith('-value')@");71 validationExpressions.put("//element/sub-element", "@contains('FAIL')@");72 validationContext.setXpathExpressions(validationExpressions);73 validator.validateMessage(message, new DefaultMessage(), context, validationContext);74 }75 @Test(expectedExceptions = {ValidationException.class})76 public void testValidateMessageElementsWithXPathNotSuccessful() {77 Message message = new DefaultMessage("<root>"78 + "<element attributeA='attribute-value' attributeB='attribute-value'>"79 + "<sub-element attribute='A'>text-value</sub-element>"80 + "</element>"81 + "</root>");82 XpathMessageValidationContext validationContext = new XpathMessageValidationContext();83 validationContext.setXpathExpressions(Collections.<String, Object>singletonMap(84 "//element/sub-element", "false-value"));85 validator.validateMessage(message, new DefaultMessage(), context, validationContext);86 }87 @Test88 public void testValidateMessageElementsWithDotNotationSuccessful() {89 Message message = new DefaultMessage("<root>"90 + "<element attributeA='attribute-value' attributeB='attribute-value'>"91 + "<sub-element attribute='A'>text-value</sub-element>"92 + "</element>"93 + "</root>");94 XpathMessageValidationContext validationContext = new XpathMessageValidationContext();95 validationContext.setXpathExpressions(Collections.<String, Object>singletonMap(96 "root.element.sub-element", "text-value"));97 validator.validateMessage(message, new DefaultMessage(), context, validationContext);98 }99 @Test100 public void testValidateMessageElementsWithDotNotationValidationMatcherSuccessful() {101 Message message = new DefaultMessage("<root>"102 + "<element attributeA='attribute-value' attributeB='attribute-value'>"103 + "<sub-element attribute='A'>text-value</sub-element>"104 + "</element>"105 + "</root>");106 XpathMessageValidationContext validationContext = new XpathMessageValidationContext();107 validationContext.setXpathExpressions(Collections.<String, Object>singletonMap(108 "root.element.sub-element", "@contains('ext-val')@"));109 validator.validateMessage(message, new DefaultMessage(), context, validationContext);110 }111 @Test(expectedExceptions = {ValidationException.class})112 public void testValidateMessageElementsWithDotNotationValidationMatcherNotSuccessful() {113 Message message = new DefaultMessage("<root>"114 + "<element attributeA='attribute-value' attributeB='attribute-value'>"115 + "<sub-element attribute='A'>text-value</sub-element>"116 + "</element>"117 + "</root>");118 XpathMessageValidationContext validationContext = new XpathMessageValidationContext();119 validationContext.setXpathExpressions(Collections.<String, Object>singletonMap(120 "root.element.sub-element", "@contains(false-value)@"));121 validator.validateMessage(message, new DefaultMessage(), context, validationContext);122 }123 @Test(expectedExceptions = {ValidationException.class})124 public void testValidateMessageElementsWithDotNotationNotSuccessful() {125 Message message = new DefaultMessage("<root>"126 + "<element attributeA='attribute-value' attributeB='attribute-value'>"127 + "<sub-element attribute='A'>text-value</sub-element>"128 + "</element>"129 + "</root>");130 XpathMessageValidationContext validationContext = new XpathMessageValidationContext();131 validationContext.setXpathExpressions(Collections.<String, Object>singletonMap(132 "root.element.sub-element", "false-value"));133 validator.validateMessage(message, new DefaultMessage(), context, validationContext);134 }135 @Test136 public void testValidateMessageElementsWithMixedNotationsSuccessful() {137 Message message = new DefaultMessage("<root>"138 + "<element attributeA='attribute-value' attributeB='attribute-value'>"139 + "<sub-element attribute='A'>text-value</sub-element>"140 + "</element>"141 + "</root>");142 XpathMessageValidationContext validationContext = new XpathMessageValidationContext();143 //mix of xpath and dot-notation144 Map<String, Object> validationExpressions = new HashMap<>();145 validationExpressions.put("//element/sub-element", "text-value");146 validationExpressions.put("root.element.sub-element", "text-value");147 validationContext.setXpathExpressions(validationExpressions);148 validator.validateMessage(message, new DefaultMessage(), context, validationContext);149 }150 @Test151 public void testValidateMessageElementsWithNodeListResult() {152 Message message = new DefaultMessage("<root>"153 + "<element attributeA='attribute-value' attributeB='attribute-value'>"154 + "<sub-element attribute='A'>text-value</sub-element>"155 + "<sub-element attribute='B'>other-value</sub-element>"156 + "</element>"157 + "</root>");158 XpathMessageValidationContext validationContext = new XpathMessageValidationContext();159 HashMap<String, Object> expressions = new HashMap<>();160 validationContext.setXpathExpressions(expressions);161 expressions.put("node-set://element/sub-element", "text-value,other-value");162 validator.validateMessage(message, new DefaultMessage(), context, validationContext);163 expressions.put("node-set://element/sub-element", allOf(hasSize(greaterThan(1)), not(empty())));164 validator.validateMessage(message, new DefaultMessage(), context, validationContext);165 expressions.put("node-set://element/sub-element", "[text-value, other-value]");166 validator.validateMessage(message, new DefaultMessage(), context, validationContext);167 expressions.put("node-set://element/sub-element", "[text-value,other-value]");168 validator.validateMessage(message, new DefaultMessage(), context, validationContext);169 expressions.put("node-set://@attribute", "[A, B]");170 validator.validateMessage(message, new DefaultMessage(), context, validationContext);171 expressions.put("node-set://@attribute", hasSize(2));172 validator.validateMessage(message, new DefaultMessage(), context, validationContext);173 expressions.put("node-set://@attribute", contains("A", "B"));174 validator.validateMessage(message, new DefaultMessage(), context, validationContext);175 }176 @Test177 public void testValidateMessageElementsWithNodeListResultNoMatch() {178 Message message = new DefaultMessage("<root>"179 + "<element attributeA='attribute-value' attributeB='attribute-value'>"180 + "<sub-element attribute='A'>text-value</sub-element>"181 + "<sub-element attribute='B'>other-value</sub-element>"182 + "</element>"183 + "</root>");184 XpathMessageValidationContext validationContext = new XpathMessageValidationContext();185 HashMap<String, Object> expressions = new HashMap<>();186 expressions.put("node-set://element/other-element", "");187 expressions.put("boolean://element/other-element", "false");188 expressions.put("boolean://element/sub-element", "true");189 validationContext.setXpathExpressions(expressions);190 validator.validateMessage(message, new DefaultMessage(), context, validationContext);191 }192 @Test193 public void testValidateMessageElementsWithNodeListCount() {194 Message message = new DefaultMessage("<root>"195 + "<element attributeA='attribute-value' attributeB='attribute-value'>"196 + "<sub-element attribute='A'>text-value</sub-element>"197 + "<sub-element attribute='B'>text-value</sub-element>"198 + "</element>"199 + "</root>");200 XpathMessageValidationContext validationContext = new XpathMessageValidationContext();201 HashMap<String, Object> expressions = new HashMap<>();202 expressions.put("number:count(//element/sub-element[.='text-value'])", "2.0");203 expressions.put("integer:count(//element/sub-element[.='text-value'])", "2");204 expressions.put("number:count(//element/sub-element)", greaterThan(1.0));205 expressions.put("integer:count(//element/sub-element)", greaterThan(1));206 validationContext.setXpathExpressions(expressions);207 validator.validateMessage(message, new DefaultMessage(), context, validationContext);208 }209}...
setXpathExpressions
Using AI Code Generation
1package com.consol.citrus.validation.xml;2import com.consol.citrus.context.TestContext;3import com.consol.citrus.message.Message;4import com.consol.citrus.testng.AbstractTestNGUnitTest;5import org.testng.annotations.Test;6import java.util.HashMap;7import java.util.Map;8import static org.mockito.Mockito.*;9public class XpathMessageValidationContextTest extends AbstractTestNGUnitTest {10 private XpathMessageValidationContext validationContext = new XpathMessageValidationContext();11 private Message message = mock(Message.class);12 private TestContext context = mock(TestContext.class);13 public void testSetXpathExpressions() {14 Map<String, String> xpathExpressions = new HashMap<String, String>();15 validationContext.setXpathExpressions(xpathExpressions);16 validationContext.validateMessagePayload(message, context);17 verify(message, times(2)).getPayload(String.class);18 }19}
setXpathExpressions
Using AI Code Generation
1package com.consol.citrus.validation.xml;2import java.util.HashMap;3import java.util.Map;4import org.testng.annotations.Test;5import com.consol.citrus.testng.AbstractTestNGUnitTest;6public class setXpathExpressionsTest extends AbstractTestNGUnitTest {7public void testSetXpathExpressions() {8XpathMessageValidationContext validationContext = new XpathMessageValidationContext();9Map<String, String> xpathExpressions = new HashMap<String, String>();10xpathExpressions.put("/bookstore/book[1]/price", "10");11xpathExpressions.put("/bookstore/book[2]/price", "20");12validationContext.setXpathExpressions(xpathExpressions);13}14}15package com.consol.citrus.validation.xml;16import java.util.HashMap;17import java.util.Map;18import org.testng.annotations.Test;19import com.consol.citrus.testng.AbstractTestNGUnitTest;20public class setXpathNamespacesTest extends AbstractTestNGUnitTest {21public void testSetXpathNamespaces() {22XpathMessageValidationContext validationContext = new XpathMessageValidationContext();23Map<String, String> xpathNamespaces = new HashMap<String, String>();24validationContext.setXpathNamespaces(xpathNamespaces);25}26}27package com.consol.citrus.validation.xml;28import org.testng.annotations.Test;29import com.consol.citrus.testng.AbstractTestNGUnitTest;30public class setXpathValidationTest extends AbstractTestNGUnitTest {31public void testSetXpathValidation() {32XpathMessageValidationContext validationContext = new XpathMessageValidationContext();33validationContext.setXpathValidation(false);34}35}36package com.consol.citrus.validation.xml;37import java.util.HashMap;38import java.util.Map;39import org.testng.annotations
setXpathExpressions
Using AI Code Generation
1package com.consol.citrus.validation.xml;2import org.testng.annotations.Test;3import org.testng.Assert;4import java.util.ArrayList;5import java.util.List;6public class SetXpathExpressionsTest {7public void testSetXpathExpressions() {8XpathMessageValidationContext obj = new XpathMessageValidationContext();9List<String> xPathExpressions = new ArrayList<String>();10xPathExpressions.add("string1");11xPathExpressions.add("string2");12obj.setXpathExpressions(xPathExpressions);13Assert.assertEquals(obj.getXpathExpressions(), xPathExpressions);14}15}
setXpathExpressions
Using AI Code Generation
1public void testSetXpathExpressions() {2 XpathMessageValidationContext context = new XpathMessageValidationContext();3 context.setXpathExpressions(new HashMap<String, String>());4}5public void testSetXpathExpressions() {6 XpathMessageValidationContext context = new XpathMessageValidationContext();7 context.setXpathExpressions(new HashMap<String, String>());8}
setXpathExpressions
Using AI Code Generation
1public class 4 {2 public static void main(String[] args) {3 XpathMessageValidationContext xpathMessageValidationContext = new XpathMessageValidationContext();4 }5}6public class 5 {7 public static void main(String[] args) {8 XpathMessageValidationContext xpathMessageValidationContext = new XpathMessageValidationContext();9 }10}11public class 6 {12 public static void main(String[] args) {13 XpathMessageValidationContext xpathMessageValidationContext = new XpathMessageValidationContext();14 xpathMessageValidationContext.setXpathMessageSelector("name");15 }16}17public class 7 {18 public static void main(String[] args) {19 XpathMessageValidationContext xpathMessageValidationContext = new XpathMessageValidationContext();20 }21}22public class 8 {23 public static void main(String[] args) {24 XpathMessageValidationContext xpathMessageValidationContext = new XpathMessageValidationContext();25 xpathMessageValidationContext.setXpathMessageSelector("name");26 }27}28public class 9 {29 public static void main(String[] args) {30 XpathMessageValidationContext xpathMessageValidationContext = new XpathMessageValidationContext();
setXpathExpressions
Using AI Code Generation
1package com.consol.citrus.validation.xml;2import java.util.HashMap;3import java.util.Map;4import org.testng.Assert;5import org.testng.annotations.Test;6import com.consol.citrus.testng.AbstractTestNGUnitTest;7import com.consol.citrus.validation.context.ValidationContext;8import com.consol.citrus.validation.xml.XpathMessageValidationContext;9import com.consol.citrus.xml.namespace.NamespaceContextBuilder;10public class XpathMessageValidationContextTest extends AbstractTestNGUnitTest {11 public void testSetXpathExpressions() {12 XpathMessageValidationContext validationContext = new XpathMessageValidationContext();13 Map<String, String> xpathExpressions = new HashMap<>();14 xpathExpressions.put("/root/child", "expected value");15 xpathExpressions.put("/root/child2", "expected value2");16 validationContext.setXpathExpressions(xpathExpressions);17 Assert.assertEquals(validationContext.getXpathExpressions(), xpathExpressions);18 }19 public void testSetXpathExpressionsWithNamespaceContext() {20 XpathMessageValidationContext validationContext = new XpathMessageValidationContext();21 Map<String, String> xpathExpressions = new HashMap<>();22 xpathExpressions.put("/root/child", "expected value");23 xpathExpressions.put("/root/child2", "expected value2");24 validationContext.setXpathExpressions(xpathExpressions);25 Map<String, String> namespaces = new HashMap<>();26 NamespaceContextBuilder namespaceContextBuilder = new NamespaceContextBuilder(namespaces);27 validationContext.setNamespaceContextBuilder(namespaceContextBuilder);28 Assert.assertEquals(validationContext.getXpathExpressions(), xpathExpressions);29 Assert.assertEquals(validationContext.getNamespaceContextBuilder(), namespaceContextBuilder);30 }31 public void testSetXpathExpressionsWithNamespaceContextAndValidationContext() {32 XpathMessageValidationContext validationContext = new XpathMessageValidationContext();33 Map<String, String> xpathExpressions = new HashMap<>();34 xpathExpressions.put("/root/child", "expected value");35 xpathExpressions.put("/root/child2", "expected value2");36 validationContext.setXpathExpressions(xpathExpressions);37 Map<String, String> namespaces = new HashMap<>();38 NamespaceContextBuilder namespaceContextBuilder = new NamespaceContextBuilder(namespaces);
setXpathExpressions
Using AI Code Generation
1package com.consol.citrus.dsl.design;2import com.consol.citrus.dsl.runner.TestRunner;3import com.consol.citrus.dsl.runner.TestRunnerBeforeTestSupport;4import org.testng.annotations.Test;5import java.util.HashMap;6import java.util.Map;7public class SetXpathExpressionsJavaITest extends TestRunnerBeforeTestSupport {8 public void setXpathExpressionsJavaITest() {9 TestRunner runner = citrus.createTestRunner();10 Map<String, String> xpathExpressions = new HashMap<>();11 xpathExpressions.put("/ns:Order/ns:Item/ns:Price", "100");12 xpathExpressions.put("/ns:Order/ns:Item/ns:Quantity", "2");13 runner.http(action -> action.client("httpClient")14 .send()15 .post()16 "</Order>"));17 runner.http(action -> action.client("httpClient")18 .receive()19 .response(HttpStatus.OK)20 .messageType(MessageType.XML)21 .validateMessage()22 .messageType(MessageType.XML)23 .xpathExpressions(xpathExpressions));24 }25}26package com.consol.citrus.dsl.design;27import com.consol.citrus.dsl.runner.TestRunner;28import com.consol.citrus.dsl.runner.TestRunnerBeforeTestSupport;29import org.testng.annotations.Test;30import java.util.HashMap;31import java.util.Map;32public class SetXpathNamespacesJavaITest extends TestRunnerBeforeTestSupport {33 public void setXpathNamespacesJavaITest() {34 TestRunner runner = citrus.createTestRunner();35 Map<String, String> xpathNamespaces = new HashMap<>();36 runner.http(action -> action.client("httpClient")37 .send()38 .post()
setXpathExpressions
Using AI Code Generation
1package com.consol.citrus.samples;2import com.consol.citrus.annotations.CitrusTest;3import com.consol.citrus.dsl.testng.TestNGCitrusTestRunner;4import com.consol.citrus.message.MessageType;5import com.consol.citrus.validation.xml.XpathMessageValidationContext;6import org.springframework.core.io.ClassPathResource;7import org.testng.annotations.Test;8public class XpathMessageValidationContext_4_Test extends TestNGCitrusTestRunner {9 public void XpathMessageValidationContext_4_Test() {10 variable("name", "John Doe");11 variable("age", "42");12 echo("4. XpathMessageValidationContext - use setXpathExpressions and setNamespaces");13 http()14 .client("httpClient")15 .send()16 .post("/greeting")17 .payload("<greetingRequest><name>${name}</name><age>${age}</age></greetingRequest>");18 http()19 .client("httpClient")20 .receive()21 .response(HttpStatus.OK)22 .messageType(MessageType.XML)23 .payload(new ClassPathResource("com/consol/citrus/samples/xpathMessageValidationContext_4_response.xml"))24 .validationContext(xpathMessageValidationContext()25 .setXpathExpressions("/greetingResponse/greetingMessage", "/greetingResponse/greetingMessage2")26 }27}
setXpathExpressions
Using AI Code Generation
1package com.consol.citrus.dsl.testng;2import com.consol.citrus.annotations.CitrusTest;3import com.consol.citrus.dsl.builder.HttpServerResponseActionBuilder;4import com.consol.citrus.dsl.builder.HttpServerResponseActionBuilder.HttpResponseActionBuilder;5import com.consol.citrus.dsl.builder.HttpServerResponseBuilder;6import com.consol.citrus.dsl.runner.TestRunner;7import com.consol.citrus.dsl.testng.TestNGCitrusTestBuilder;8import com.consol.citrus.http.message.HttpMessage;9import com.consol.citrus.message.MessageType;10import com.consol.citrus.testng.CitrusParameters;11import com.consol.citrus.validation.xml.XpathMessageValidationContext;12import org.springframework.http.HttpStatus;13import org.testng.annotations.Test;14import java.util.Arrays;15import java.util.HashMap;16import java.util.Map;17public class HttpServerResponseActionBuilder_Path_Test extends TestNGCitrusTestBuilder {18 public void test() {19 variable("myVar", "World");20 http(httpServer -> httpServer21 .receive()22 .post()23 .payload("<TestRequest><Message>Hello Citrus!</Message></TestRequest>"));24 http(httpServer -> httpServer25 .send()26 .response(HttpStatus.OK)27 .payload("<TestResponse><Message>Hello World!</Message></TestResponse>"));28 http(httpServer -> httpServer29 .receive()30 .post()31 .payload("<TestRequest><Message>Hello Citrus!</Message></TestRequest>"));32 http(httpServer -> httpServer33 .send()34 .response(HttpStatus.OK)35 .payload("<TestResponse><Message>Hello World!</Message></TestResponse>"));36 http(httpServer -> httpServer37 .receive()38 .post()39 .payload("<TestRequest><Message>Hello Citrus!</Message></TestRequest>"));40 http(httpServer -> httpServer41 .send()42 .response(HttpStatus.OK)43 .payload("<TestResponse><Message>Hello World!</Message></TestResponse>"));44 http(httpServer -> httpServer45 .receive()46 .post()47 .payload("<TestRequest><Message>Hello Citrus!</Message></TestRequest>"));48 http(httpServer -> httpServer
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!!