How to use testReceiveBuilder method of com.consol.citrus.dsl.runner.ReceiveMessageTestRunnerTest class

Best Citrus code snippet using com.consol.citrus.dsl.runner.ReceiveMessageTestRunnerTest.testReceiveBuilder

Source:ReceiveMessageTestRunnerTest.java Github

copy

Full Screen

...123 Assert.assertTrue(action.getValidationContexts().stream().anyMatch(XmlMessageValidationContext.class::isInstance));124 Assert.assertTrue(action.getValidationContexts().stream().anyMatch(JsonMessageValidationContext.class::isInstance));125 }126 @Test127 public void testReceiveBuilder() {128 reset(messageEndpoint, messageConsumer, configuration);129 when(messageEndpoint.createConsumer()).thenReturn(messageConsumer);130 when(messageEndpoint.getEndpointConfiguration()).thenReturn(configuration);131 when(configuration.getTimeout()).thenReturn(100L);132 when(messageEndpoint.getActor()).thenReturn(null);133 when(messageConsumer.receive(any(TestContext.class), anyLong())).thenReturn(new DefaultMessage("Foo").setHeader("operation", "foo"));134 MockTestRunner builder = new MockTestRunner(getClass().getSimpleName(), context) {135 @Override136 public void execute() {137 receive(action -> action.endpoint(messageEndpoint)138 .message(new DefaultMessage("Foo").setHeader("operation", "foo")));139 }140 };141 TestCase test = builder.getTestCase();142 Assert.assertEquals(test.getActionCount(), 1);143 Assert.assertEquals(test.getActions().get(0).getClass(), ReceiveMessageAction.class);144 ReceiveMessageAction action = ((ReceiveMessageAction)test.getActions().get(0));145 Assert.assertEquals(action.getName(), "receive");146 Assert.assertEquals(action.getMessageType(), MessageType.PLAINTEXT.name());147 Assert.assertEquals(action.getEndpoint(), messageEndpoint);148 Assert.assertEquals(action.getValidationContexts().size(), 3);149 Assert.assertTrue(action.getValidationContexts().stream().anyMatch(HeaderValidationContext.class::isInstance));150 Assert.assertTrue(action.getValidationContexts().stream().anyMatch(XmlMessageValidationContext.class::isInstance));151 Assert.assertTrue(action.getValidationContexts().stream().anyMatch(JsonMessageValidationContext.class::isInstance));152 Assert.assertTrue(action.getMessageBuilder() instanceof StaticMessageBuilder);153 Assert.assertEquals(((StaticMessageBuilder)action.getMessageBuilder()).getMessage().getPayload(), "Foo");154 Assert.assertNotNull(((StaticMessageBuilder)action.getMessageBuilder()).getMessage().getHeader("operation"));155 }156 @Test157 public void testReceiveBuilderWithPayloadModel() {158 reset(referenceResolver, messageEndpoint, messageConsumer, configuration);159 when(messageEndpoint.createConsumer()).thenReturn(messageConsumer);160 when(messageEndpoint.getEndpointConfiguration()).thenReturn(configuration);161 when(configuration.getTimeout()).thenReturn(100L);162 when(messageEndpoint.getActor()).thenReturn(null);163 when(messageConsumer.receive(any(TestContext.class), anyLong())).thenReturn(164 new DefaultMessage("<TestRequest><Message>Hello Citrus!</Message></TestRequest>")165 .setHeader("operation", "foo"));166 when(referenceResolver.resolve(TestContext.class)).thenReturn(applicationContext.getBean(TestContext.class));167 when(referenceResolver.resolve(TestActionListeners.class)).thenReturn(new TestActionListeners());168 when(referenceResolver.resolveAll(SequenceBeforeTest.class)).thenReturn(new HashMap<>());169 when(referenceResolver.resolveAll(SequenceAfterTest.class)).thenReturn(new HashMap<>());170 when(referenceResolver.resolveAll(Marshaller.class)).thenReturn(Collections.<String, Marshaller>singletonMap("marshaller", new MarshallerAdapter(marshaller)));171 when(referenceResolver.resolve(Marshaller.class)).thenReturn(new MarshallerAdapter(marshaller));172 context.setReferenceResolver(referenceResolver);173 MockTestRunner builder = new MockTestRunner(getClass().getSimpleName(), context) {174 @Override175 public void execute() {176 receive(action -> action.endpoint(messageEndpoint)177 .payloadModel(new TestRequest("Hello Citrus!")));178 }179 };180 TestCase test = builder.getTestCase();181 Assert.assertEquals(test.getActionCount(), 1);182 Assert.assertEquals(test.getActions().get(0).getClass(), ReceiveMessageAction.class);183 ReceiveMessageAction action = ((ReceiveMessageAction)test.getActions().get(0));184 Assert.assertEquals(action.getName(), "receive");185 Assert.assertEquals(action.getMessageType(), MessageType.XML.name());186 Assert.assertEquals(action.getEndpoint(), messageEndpoint);187 Assert.assertEquals(action.getValidationContexts().size(), 3);188 Assert.assertTrue(action.getValidationContexts().stream().anyMatch(HeaderValidationContext.class::isInstance));189 Assert.assertTrue(action.getValidationContexts().stream().anyMatch(XmlMessageValidationContext.class::isInstance));190 Assert.assertTrue(action.getValidationContexts().stream().anyMatch(JsonMessageValidationContext.class::isInstance));191 Assert.assertTrue(action.getMessageBuilder() instanceof DefaultMessageBuilder);192 Assert.assertEquals(((DefaultMessageBuilder)action.getMessageBuilder()).buildMessagePayload(context, action.getMessageType()),193 "<TestRequest><Message>Hello Citrus!</Message></TestRequest>");194 }195 @Test196 public void testReceiveBuilderWithPayloadModelExplicitMarshaller() {197 reset(messageEndpoint, messageConsumer, configuration);198 when(messageEndpoint.createConsumer()).thenReturn(messageConsumer);199 when(messageEndpoint.getEndpointConfiguration()).thenReturn(configuration);200 when(configuration.getTimeout()).thenReturn(100L);201 when(messageEndpoint.getActor()).thenReturn(null);202 when(messageConsumer.receive(any(TestContext.class), anyLong())).thenReturn(203 new DefaultMessage("<TestRequest><Message>Hello Citrus!</Message></TestRequest>")204 .setHeader("operation", "foo"));205 MockTestRunner builder = new MockTestRunner(getClass().getSimpleName(), context) {206 @Override207 public void execute() {208 receive(action -> action.endpoint(messageEndpoint)209 .payload(new TestRequest("Hello Citrus!"), marshaller));210 }211 };212 TestCase test = builder.getTestCase();213 Assert.assertEquals(test.getActionCount(), 1);214 Assert.assertEquals(test.getActions().get(0).getClass(), ReceiveMessageAction.class);215 ReceiveMessageAction action = ((ReceiveMessageAction)test.getActions().get(0));216 Assert.assertEquals(action.getName(), "receive");217 Assert.assertEquals(action.getMessageType(), MessageType.XML.name());218 Assert.assertEquals(action.getEndpoint(), messageEndpoint);219 Assert.assertEquals(action.getValidationContexts().size(), 3);220 Assert.assertTrue(action.getValidationContexts().stream().anyMatch(HeaderValidationContext.class::isInstance));221 Assert.assertTrue(action.getValidationContexts().stream().anyMatch(XmlMessageValidationContext.class::isInstance));222 Assert.assertTrue(action.getValidationContexts().stream().anyMatch(JsonMessageValidationContext.class::isInstance));223 Assert.assertTrue(action.getMessageBuilder() instanceof DefaultMessageBuilder);224 Assert.assertEquals(((DefaultMessageBuilder)action.getMessageBuilder()).buildMessagePayload(context, action.getMessageType()), "<TestRequest><Message>Hello Citrus!</Message></TestRequest>");225 }226 @Test227 public void testReceiveBuilderWithPayloadModelExplicitMarshallerName() {228 reset(referenceResolver, messageEndpoint, messageConsumer, configuration);229 when(messageEndpoint.createConsumer()).thenReturn(messageConsumer);230 when(messageEndpoint.getEndpointConfiguration()).thenReturn(configuration);231 when(configuration.getTimeout()).thenReturn(100L);232 when(messageEndpoint.getActor()).thenReturn(null);233 when(messageConsumer.receive(any(TestContext.class), anyLong())).thenReturn(234 new DefaultMessage("<TestRequest><Message>Hello Citrus!</Message></TestRequest>")235 .setHeader("operation", "foo"));236 when(referenceResolver.resolve(TestContext.class)).thenReturn(applicationContext.getBean(TestContext.class));237 when(referenceResolver.resolve(TestActionListeners.class)).thenReturn(new TestActionListeners());238 when(referenceResolver.resolveAll(SequenceBeforeTest.class)).thenReturn(new HashMap<>());239 when(referenceResolver.resolveAll(SequenceAfterTest.class)).thenReturn(new HashMap<>());240 when(referenceResolver.isResolvable("myMarshaller")).thenReturn(true);241 when(referenceResolver.resolve("myMarshaller", Marshaller.class)).thenReturn(new MarshallerAdapter(marshaller));242 context.setReferenceResolver(referenceResolver);243 MockTestRunner builder = new MockTestRunner(getClass().getSimpleName(), context) {244 @Override245 public void execute() {246 receive(action -> action.endpoint(messageEndpoint)247 .payload(new TestRequest("Hello Citrus!"), "myMarshaller"));248 }249 };250 TestCase test = builder.getTestCase();251 Assert.assertEquals(test.getActionCount(), 1);252 Assert.assertEquals(test.getActions().get(0).getClass(), ReceiveMessageAction.class);253 ReceiveMessageAction action = ((ReceiveMessageAction)test.getActions().get(0));254 Assert.assertEquals(action.getName(), "receive");255 Assert.assertEquals(action.getMessageType(), MessageType.XML.name());256 Assert.assertEquals(action.getEndpoint(), messageEndpoint);257 Assert.assertEquals(action.getValidationContexts().size(), 3);258 Assert.assertTrue(action.getValidationContexts().stream().anyMatch(HeaderValidationContext.class::isInstance));259 Assert.assertTrue(action.getValidationContexts().stream().anyMatch(XmlMessageValidationContext.class::isInstance));260 Assert.assertTrue(action.getValidationContexts().stream().anyMatch(JsonMessageValidationContext.class::isInstance));261 Assert.assertTrue(action.getMessageBuilder() instanceof DefaultMessageBuilder);262 Assert.assertEquals(((DefaultMessageBuilder)action.getMessageBuilder()).buildMessagePayload(context, action.getMessageType()), "<TestRequest><Message>Hello Citrus!</Message></TestRequest>");263 }264 @Test265 public void testReceiveBuilderWithPayloadString() {266 reset(messageEndpoint, messageConsumer, configuration);267 when(messageEndpoint.createConsumer()).thenReturn(messageConsumer);268 when(messageEndpoint.getEndpointConfiguration()).thenReturn(configuration);269 when(configuration.getTimeout()).thenReturn(100L);270 when(messageEndpoint.getActor()).thenReturn(null);271 when(messageConsumer.receive(any(TestContext.class), anyLong())).thenReturn(272 new DefaultMessage("<TestRequest><Message>Hello World!</Message></TestRequest>")273 .setHeader("operation", "foo"));274 MockTestRunner builder = new MockTestRunner(getClass().getSimpleName(), context) {275 @Override276 public void execute() {277 receive(action -> action.endpoint(messageEndpoint)278 .payload("<TestRequest><Message>Hello World!</Message></TestRequest>"));279 }280 };281 TestCase test = builder.getTestCase();282 Assert.assertEquals(test.getActionCount(), 1);283 Assert.assertEquals(test.getActions().get(0).getClass(), ReceiveMessageAction.class);284 ReceiveMessageAction action = ((ReceiveMessageAction)test.getActions().get(0));285 Assert.assertEquals(action.getName(), "receive");286 Assert.assertEquals(action.getMessageType(), MessageType.XML.name());287 Assert.assertEquals(action.getEndpoint(), messageEndpoint);288 Assert.assertEquals(action.getValidationContexts().size(), 3);289 Assert.assertTrue(action.getValidationContexts().stream().anyMatch(HeaderValidationContext.class::isInstance));290 Assert.assertTrue(action.getValidationContexts().stream().anyMatch(XmlMessageValidationContext.class::isInstance));291 Assert.assertTrue(action.getValidationContexts().stream().anyMatch(JsonMessageValidationContext.class::isInstance));292 Assert.assertTrue(action.getMessageBuilder() instanceof DefaultMessageBuilder);293 Assert.assertEquals(((DefaultMessageBuilder)action.getMessageBuilder()).buildMessagePayload(context, action.getMessageType()), "<TestRequest><Message>Hello World!</Message></TestRequest>");294 }295 @Test296 public void testReceiveBuilderWithPayloadResource() throws IOException {297 reset(resource, messageEndpoint, messageConsumer, configuration);298 when(messageEndpoint.createConsumer()).thenReturn(messageConsumer);299 when(messageEndpoint.getEndpointConfiguration()).thenReturn(configuration);300 when(configuration.getTimeout()).thenReturn(100L);301 when(messageEndpoint.getActor()).thenReturn(null);302 when(messageConsumer.receive(any(TestContext.class), anyLong())).thenReturn(303 new DefaultMessage("<TestRequest><Message>Hello World!</Message></TestRequest>")304 .setHeader("operation", "foo"));305 when(resource.getInputStream()).thenReturn(new ByteArrayInputStream("<TestRequest><Message>Hello World!</Message></TestRequest>".getBytes()));306 MockTestRunner builder = new MockTestRunner(getClass().getSimpleName(), context) {307 @Override308 public void execute() {309 receive(action -> action.endpoint(messageEndpoint)310 .payload(resource));311 }312 };313 TestCase test = builder.getTestCase();314 Assert.assertEquals(test.getActionCount(), 1);315 Assert.assertEquals(test.getActions().get(0).getClass(), ReceiveMessageAction.class);316 ReceiveMessageAction action = ((ReceiveMessageAction)test.getActions().get(0));317 Assert.assertEquals(action.getName(), "receive");318 Assert.assertEquals(action.getMessageType(), MessageType.XML.name());319 Assert.assertEquals(action.getEndpoint(), messageEndpoint);320 Assert.assertEquals(action.getValidationContexts().size(), 3);321 Assert.assertTrue(action.getValidationContexts().stream().anyMatch(HeaderValidationContext.class::isInstance));322 Assert.assertTrue(action.getValidationContexts().stream().anyMatch(XmlMessageValidationContext.class::isInstance));323 Assert.assertTrue(action.getValidationContexts().stream().anyMatch(JsonMessageValidationContext.class::isInstance));324 Assert.assertTrue(action.getMessageBuilder() instanceof DefaultMessageBuilder);325 Assert.assertEquals(((DefaultMessageBuilder)action.getMessageBuilder()).buildMessagePayload(context, action.getMessageType()), "<TestRequest><Message>Hello World!</Message></TestRequest>");326 }327 @Test328 public void testReceiveBuilderWithEndpointName() {329 TestContext context = applicationContext.getBean(TestContext.class);330 reset(referenceResolver, messageEndpoint, messageConsumer, configuration);331 when(messageEndpoint.createConsumer()).thenReturn(messageConsumer);332 when(messageEndpoint.getEndpointConfiguration()).thenReturn(configuration);333 when(configuration.getTimeout()).thenReturn(100L);334 when(messageEndpoint.getActor()).thenReturn(null);335 when(messageConsumer.receive(any(TestContext.class), anyLong())).thenReturn(336 new DefaultMessage("<TestRequest><Message>Hello World!</Message></TestRequest>")337 .setHeader("operation", "foo"));338 when(referenceResolver.resolve(TestContext.class)).thenReturn(context);339 when(referenceResolver.resolve("fooMessageEndpoint", Endpoint.class)).thenReturn(messageEndpoint);340 when(referenceResolver.resolve(TestActionListeners.class)).thenReturn(new TestActionListeners());341 when(referenceResolver.resolveAll(SequenceBeforeTest.class)).thenReturn(new HashMap<>());342 when(referenceResolver.resolveAll(SequenceAfterTest.class)).thenReturn(new HashMap<>());343 context.setReferenceResolver(referenceResolver);344 MockTestRunner builder = new MockTestRunner(getClass().getSimpleName(), context) {345 @Override346 public void execute() {347 receive(action -> action.endpoint("fooMessageEndpoint")348 .payload("<TestRequest><Message>Hello World!</Message></TestRequest>"));349 }350 };351 TestCase test = builder.getTestCase();352 Assert.assertEquals(test.getActionCount(), 1);353 Assert.assertEquals(test.getActions().get(0).getClass(), ReceiveMessageAction.class);354 ReceiveMessageAction action = ((ReceiveMessageAction)test.getActions().get(0));355 Assert.assertEquals(action.getName(), "receive");356 Assert.assertEquals(action.getEndpointUri(), "fooMessageEndpoint");357 Assert.assertEquals(action.getMessageType(), MessageType.XML.name());358 }359 @Test360 public void testReceiveBuilderWithTimeout() {361 reset(messageEndpoint, messageConsumer, configuration);362 when(messageEndpoint.createConsumer()).thenReturn(messageConsumer);363 when(messageEndpoint.getActor()).thenReturn(null);364 when(messageConsumer.receive(any(TestContext.class), anyLong())).thenReturn(365 new DefaultMessage("<TestRequest><Message>Hello World!</Message></TestRequest>")366 .setHeader("operation", "foo"));367 MockTestRunner builder = new MockTestRunner(getClass().getSimpleName(), context) {368 @Override369 public void execute() {370 receive(action -> action.endpoint(messageEndpoint)371 .payload("<TestRequest><Message>Hello World!</Message></TestRequest>")372 .timeout(1000L));373 }374 };375 TestCase test = builder.getTestCase();376 Assert.assertEquals(test.getActionCount(), 1);377 Assert.assertEquals(test.getActions().get(0).getClass(), ReceiveMessageAction.class);378 ReceiveMessageAction action = ((ReceiveMessageAction)test.getActions().get(0));379 Assert.assertEquals(action.getName(), "receive");380 Assert.assertEquals(action.getEndpoint(), messageEndpoint);381 Assert.assertEquals(action.getReceiveTimeout(), 1000L);382 }383 @Test384 public void testReceiveBuilderWithHeaders() {385 reset(messageEndpoint, messageConsumer, configuration);386 when(messageEndpoint.createConsumer()).thenReturn(messageConsumer);387 when(messageEndpoint.getEndpointConfiguration()).thenReturn(configuration);388 when(configuration.getTimeout()).thenReturn(100L);389 when(messageEndpoint.getActor()).thenReturn(null);390 when(messageConsumer.receive(any(TestContext.class), anyLong())).thenReturn(391 new DefaultMessage("<TestRequest><Message>Hello World!</Message></TestRequest>")392 .setHeader("some", "value")393 .setHeader("operation", "sayHello")394 .setHeader("foo", "bar"));395 MockTestRunner builder = new MockTestRunner(getClass().getSimpleName(), context) {396 @Override397 public void execute() {398 receive(action -> action.endpoint(messageEndpoint)399 .payload("<TestRequest><Message>Hello World!</Message></TestRequest>")400 .headers(Collections.singletonMap("some", "value"))401 .header("operation", "sayHello")402 .header("foo", "bar"));403 receive(action -> action.endpoint(messageEndpoint)404 .header("operation", "sayHello")405 .header("foo", "bar")406 .headers(Collections.singletonMap("some", "value"))407 .payload("<TestRequest><Message>Hello World!</Message></TestRequest>"));408 }409 };410 TestCase test = builder.getTestCase();411 Assert.assertEquals(test.getActionCount(), 2);412 Assert.assertEquals(test.getActions().get(0).getClass(), ReceiveMessageAction.class);413 Assert.assertEquals(test.getActions().get(1).getClass(), ReceiveMessageAction.class);414 ReceiveMessageAction action = ((ReceiveMessageAction)test.getActions().get(0));415 Assert.assertEquals(action.getName(), "receive");416 Assert.assertEquals(action.getEndpoint(), messageEndpoint);417 Assert.assertEquals(action.getMessageType(), MessageType.XML.name());418 Assert.assertTrue(action.getMessageBuilder() instanceof DefaultMessageBuilder);419 Assert.assertEquals(((DefaultMessageBuilder)action.getMessageBuilder()).buildMessagePayload(context, action.getMessageType()), "<TestRequest><Message>Hello World!</Message></TestRequest>");420 Assert.assertTrue(((DefaultMessageBuilder)action.getMessageBuilder()).buildMessageHeaders(context).containsKey("some"));421 Assert.assertTrue(((DefaultMessageBuilder)action.getMessageBuilder()).buildMessageHeaders(context).containsKey("operation"));422 Assert.assertTrue(((DefaultMessageBuilder)action.getMessageBuilder()).buildMessageHeaders(context).containsKey("foo"));423 action = ((ReceiveMessageAction)test.getActions().get(1));424 Assert.assertEquals(action.getName(), "receive");425 Assert.assertEquals(action.getEndpoint(), messageEndpoint);426 Assert.assertEquals(action.getMessageType(), MessageType.XML.name());427 Assert.assertTrue(action.getMessageBuilder() instanceof DefaultMessageBuilder);428 Assert.assertEquals(((DefaultMessageBuilder)action.getMessageBuilder()).buildMessagePayload(context, action.getMessageType()), "<TestRequest><Message>Hello World!</Message></TestRequest>");429 Assert.assertTrue(((DefaultMessageBuilder)action.getMessageBuilder()).buildMessageHeaders(context).containsKey("some"));430 Assert.assertTrue(((DefaultMessageBuilder)action.getMessageBuilder()).buildMessageHeaders(context).containsKey("operation"));431 Assert.assertTrue(((DefaultMessageBuilder)action.getMessageBuilder()).buildMessageHeaders(context).containsKey("foo"));432 }433 @Test434 public void testReceiveBuilderWithHeaderData() {435 reset(messageEndpoint, messageConsumer, configuration);436 when(messageEndpoint.createConsumer()).thenReturn(messageConsumer);437 when(messageEndpoint.getEndpointConfiguration()).thenReturn(configuration);438 when(configuration.getTimeout()).thenReturn(100L);439 when(messageEndpoint.getActor()).thenReturn(null);440 when(messageConsumer.receive(any(TestContext.class), anyLong())).thenReturn(441 new DefaultMessage("<TestRequest><Message>Hello World!</Message></TestRequest>")442 .setHeader("operation", "foo")443 .addHeaderData("<Header><Name>operation</Name><Value>foo</Value></Header>"));444 MockTestRunner builder = new MockTestRunner(getClass().getSimpleName(), context) {445 @Override446 public void execute() {447 receive(action -> action.endpoint(messageEndpoint)448 .payload("<TestRequest><Message>Hello World!</Message></TestRequest>")449 .header("<Header><Name>operation</Name><Value>foo</Value></Header>"));450 receive(action -> action.endpoint(messageEndpoint)451 .message(new DefaultMessage("<TestRequest><Message>Hello World!</Message></TestRequest>"))452 .header("<Header><Name>operation</Name><Value>foo</Value></Header>"));453 }454 };455 TestCase test = builder.getTestCase();456 Assert.assertEquals(test.getActionCount(), 2);457 Assert.assertEquals(test.getActions().get(0).getClass(), ReceiveMessageAction.class);458 Assert.assertEquals(test.getActions().get(1).getClass(), ReceiveMessageAction.class);459 ReceiveMessageAction action = ((ReceiveMessageAction)test.getActions().get(0));460 Assert.assertEquals(action.getName(), "receive");461 Assert.assertEquals(action.getEndpoint(), messageEndpoint);462 Assert.assertEquals(action.getMessageType(), MessageType.XML.name());463 Assert.assertTrue(action.getMessageBuilder() instanceof DefaultMessageBuilder);464 Assert.assertEquals(((DefaultMessageBuilder)action.getMessageBuilder()).buildMessagePayload(context, action.getMessageType()), "<TestRequest><Message>Hello World!</Message></TestRequest>");465 Assert.assertEquals(((DefaultMessageBuilder)action.getMessageBuilder()).buildMessageHeaderData(context).size(), 1L);466 Assert.assertEquals(((DefaultMessageBuilder)action.getMessageBuilder()).buildMessageHeaderData(context).get(0), "<Header><Name>operation</Name><Value>foo</Value></Header>");467 action = ((ReceiveMessageAction) test.getActions().get(1));468 Assert.assertEquals(action.getName(), "receive");469 Assert.assertEquals(action.getEndpoint(), messageEndpoint);470 Assert.assertEquals(action.getMessageType(), MessageType.XML.name());471 Assert.assertTrue(action.getMessageBuilder() instanceof StaticMessageBuilder);472 Assert.assertEquals(((StaticMessageBuilder)action.getMessageBuilder()).getMessage().getPayload(), "<TestRequest><Message>Hello World!</Message></TestRequest>");473 Assert.assertEquals(((StaticMessageBuilder)action.getMessageBuilder()).buildMessageHeaderData(context).size(), 1L);474 Assert.assertEquals(((StaticMessageBuilder)action.getMessageBuilder()).buildMessageHeaderData(context).get(0), "<Header><Name>operation</Name><Value>foo</Value></Header>");475 }476 @Test477 public void testReceiveBuilderWithMultipleHeaderData() {478 reset(messageEndpoint, messageConsumer, configuration);479 when(messageEndpoint.createConsumer()).thenReturn(messageConsumer);480 when(messageEndpoint.getEndpointConfiguration()).thenReturn(configuration);481 when(configuration.getTimeout()).thenReturn(100L);482 when(messageEndpoint.getActor()).thenReturn(null);483 when(messageConsumer.receive(any(TestContext.class), anyLong())).thenReturn(484 new DefaultMessage("<TestRequest><Message>Hello World!</Message></TestRequest>")485 .setHeader("operation", "foo")486 .addHeaderData("<Header><Name>operation</Name><Value>foo1</Value></Header>")487 .addHeaderData("<Header><Name>operation</Name><Value>foo2</Value></Header>"));488 MockTestRunner builder = new MockTestRunner(getClass().getSimpleName(), context) {489 @Override490 public void execute() {491 receive(action -> action.endpoint(messageEndpoint)492 .payload("<TestRequest><Message>Hello World!</Message></TestRequest>")493 .header("<Header><Name>operation</Name><Value>foo1</Value></Header>")494 .header("<Header><Name>operation</Name><Value>foo2</Value></Header>"));495 receive(action -> action.endpoint(messageEndpoint)496 .message(new DefaultMessage("<TestRequest><Message>Hello World!</Message></TestRequest>"))497 .header("<Header><Name>operation</Name><Value>foo1</Value></Header>")498 .header("<Header><Name>operation</Name><Value>foo2</Value></Header>"));499 }500 };501 TestCase test = builder.getTestCase();502 Assert.assertEquals(test.getActionCount(), 2);503 Assert.assertEquals(test.getActions().get(0).getClass(), ReceiveMessageAction.class);504 Assert.assertEquals(test.getActions().get(1).getClass(), ReceiveMessageAction.class);505 ReceiveMessageAction action = ((ReceiveMessageAction)test.getActions().get(0));506 Assert.assertEquals(action.getName(), "receive");507 Assert.assertEquals(action.getEndpoint(), messageEndpoint);508 Assert.assertEquals(action.getMessageType(), MessageType.XML.name());509 Assert.assertTrue(action.getMessageBuilder() instanceof DefaultMessageBuilder);510 Assert.assertEquals(((DefaultMessageBuilder)action.getMessageBuilder()).buildMessagePayload(context, action.getMessageType()), "<TestRequest><Message>Hello World!</Message></TestRequest>");511 Assert.assertEquals(((DefaultMessageBuilder)action.getMessageBuilder()).buildMessageHeaderData(context).size(), 2L);512 Assert.assertEquals(((DefaultMessageBuilder)action.getMessageBuilder()).buildMessageHeaderData(context).get(0), "<Header><Name>operation</Name><Value>foo1</Value></Header>");513 Assert.assertEquals(((DefaultMessageBuilder)action.getMessageBuilder()).buildMessageHeaderData(context).get(1), "<Header><Name>operation</Name><Value>foo2</Value></Header>");514 action = ((ReceiveMessageAction)test.getActions().get(1));515 Assert.assertEquals(action.getName(), "receive");516 Assert.assertEquals(action.getEndpoint(), messageEndpoint);517 Assert.assertEquals(action.getMessageType(), MessageType.XML.name());518 Assert.assertTrue(action.getMessageBuilder() instanceof StaticMessageBuilder);519 Assert.assertEquals(((StaticMessageBuilder)action.getMessageBuilder()).getMessage().getPayload(), "<TestRequest><Message>Hello World!</Message></TestRequest>");520 Assert.assertEquals(((StaticMessageBuilder)action.getMessageBuilder()).buildMessageHeaderData(context).size(), 2L);521 Assert.assertEquals(((StaticMessageBuilder)action.getMessageBuilder()).buildMessageHeaderData(context).get(0), "<Header><Name>operation</Name><Value>foo1</Value></Header>");522 Assert.assertEquals(((StaticMessageBuilder)action.getMessageBuilder()).buildMessageHeaderData(context).get(1), "<Header><Name>operation</Name><Value>foo2</Value></Header>");523 }524 @Test525 public void testReceiveBuilderWithHeaderFragment() {526 reset(referenceResolver, messageEndpoint, messageConsumer, configuration);527 when(messageEndpoint.createConsumer()).thenReturn(messageConsumer);528 when(messageEndpoint.getEndpointConfiguration()).thenReturn(configuration);529 when(configuration.getTimeout()).thenReturn(100L);530 when(messageEndpoint.getActor()).thenReturn(null);531 when(messageConsumer.receive(any(TestContext.class), anyLong())).thenReturn(532 new DefaultMessage()533 .addHeaderData("<TestRequest><Message>Hello Citrus!</Message></TestRequest>")534 .setHeader("operation", "foo"));535 when(referenceResolver.resolve(TestContext.class)).thenReturn(applicationContext.getBean(TestContext.class));536 when(referenceResolver.resolve(TestActionListeners.class)).thenReturn(new TestActionListeners());537 when(referenceResolver.resolveAll(SequenceBeforeTest.class)).thenReturn(new HashMap<>());538 when(referenceResolver.resolveAll(SequenceAfterTest.class)).thenReturn(new HashMap<>());539 when(referenceResolver.resolveAll(Marshaller.class)).thenReturn(Collections.<String, Marshaller>singletonMap("marshaller", new MarshallerAdapter(marshaller)));540 when(referenceResolver.resolve(Marshaller.class)).thenReturn(new MarshallerAdapter(marshaller));541 context.setReferenceResolver(referenceResolver);542 MockTestRunner builder = new MockTestRunner(getClass().getSimpleName(), context) {543 @Override544 public void execute() {545 receive(action -> action.endpoint(messageEndpoint)546 .headerFragment(new TestRequest("Hello Citrus!")));547 }548 };549 TestCase test = builder.getTestCase();550 Assert.assertEquals(test.getActionCount(), 1);551 Assert.assertEquals(test.getActions().get(0).getClass(), ReceiveMessageAction.class);552 ReceiveMessageAction action = ((ReceiveMessageAction)test.getActions().get(0));553 Assert.assertEquals(action.getName(), "receive");554 Assert.assertEquals(action.getMessageType(), MessageType.XML.name());555 Assert.assertEquals(action.getEndpoint(), messageEndpoint);556 Assert.assertEquals(action.getValidationContexts().size(), 3);557 Assert.assertTrue(action.getValidationContexts().stream().anyMatch(HeaderValidationContext.class::isInstance));558 Assert.assertTrue(action.getValidationContexts().stream().anyMatch(XmlMessageValidationContext.class::isInstance));559 Assert.assertTrue(action.getValidationContexts().stream().anyMatch(JsonMessageValidationContext.class::isInstance));560 Assert.assertTrue(action.getMessageBuilder() instanceof DefaultMessageBuilder);561 Assert.assertEquals(((DefaultMessageBuilder)action.getMessageBuilder()).buildMessageHeaderData(context).size(), 1L);562 Assert.assertEquals(((DefaultMessageBuilder)action.getMessageBuilder()).buildMessageHeaderData(context).get(0),563 "<TestRequest><Message>Hello Citrus!</Message></TestRequest>");564 }565 @Test566 public void testReceiveBuilderWithHeaderFragmentExplicitMarshaller() {567 reset(messageEndpoint, messageConsumer, configuration);568 when(messageEndpoint.createConsumer()).thenReturn(messageConsumer);569 when(messageEndpoint.getEndpointConfiguration()).thenReturn(configuration);570 when(configuration.getTimeout()).thenReturn(100L);571 when(messageEndpoint.getActor()).thenReturn(null);572 when(messageConsumer.receive(any(TestContext.class), anyLong())).thenReturn(573 new DefaultMessage()574 .addHeaderData("<TestRequest><Message>Hello Citrus!</Message></TestRequest>")575 .setHeader("operation", "foo"));576 MockTestRunner builder = new MockTestRunner(getClass().getSimpleName(), context) {577 @Override578 public void execute() {579 receive(action -> action.endpoint(messageEndpoint)580 .headerFragment(new TestRequest("Hello Citrus!"), marshaller));581 }582 };583 TestCase test = builder.getTestCase();584 Assert.assertEquals(test.getActionCount(), 1);585 Assert.assertEquals(test.getActions().get(0).getClass(), ReceiveMessageAction.class);586 ReceiveMessageAction action = ((ReceiveMessageAction)test.getActions().get(0));587 Assert.assertEquals(action.getName(), "receive");588 Assert.assertEquals(action.getMessageType(), MessageType.XML.name());589 Assert.assertEquals(action.getEndpoint(), messageEndpoint);590 Assert.assertEquals(action.getValidationContexts().size(), 3);591 Assert.assertTrue(action.getValidationContexts().stream().anyMatch(HeaderValidationContext.class::isInstance));592 Assert.assertTrue(action.getValidationContexts().stream().anyMatch(XmlMessageValidationContext.class::isInstance));593 Assert.assertTrue(action.getValidationContexts().stream().anyMatch(JsonMessageValidationContext.class::isInstance));594 Assert.assertTrue(action.getMessageBuilder() instanceof DefaultMessageBuilder);595 Assert.assertEquals(((DefaultMessageBuilder)action.getMessageBuilder()).buildMessageHeaderData(context).size(), 1L);596 Assert.assertEquals(((DefaultMessageBuilder)action.getMessageBuilder()).buildMessageHeaderData(context).get(0), "<TestRequest><Message>Hello Citrus!</Message></TestRequest>");597 }598 @Test599 public void testReceiveBuilderWithHeaderFragmentExplicitMarshallerName() {600 reset(referenceResolver, messageEndpoint, messageConsumer, configuration);601 when(messageEndpoint.createConsumer()).thenReturn(messageConsumer);602 when(messageEndpoint.getEndpointConfiguration()).thenReturn(configuration);603 when(configuration.getTimeout()).thenReturn(100L);604 when(messageEndpoint.getActor()).thenReturn(null);605 when(messageConsumer.receive(any(TestContext.class), anyLong())).thenReturn(606 new DefaultMessage()607 .addHeaderData("<TestRequest><Message>Hello Citrus!</Message></TestRequest>")608 .setHeader("operation", "foo"));609 when(referenceResolver.resolve(TestContext.class)).thenReturn(applicationContext.getBean(TestContext.class));610 when(referenceResolver.resolve(TestActionListeners.class)).thenReturn(new TestActionListeners());611 when(referenceResolver.resolveAll(SequenceBeforeTest.class)).thenReturn(new HashMap<>());612 when(referenceResolver.resolveAll(SequenceAfterTest.class)).thenReturn(new HashMap<>());613 when(referenceResolver.isResolvable("myMarshaller")).thenReturn(true);614 when(referenceResolver.resolve("myMarshaller", Marshaller.class)).thenReturn(new MarshallerAdapter(marshaller));615 context.setReferenceResolver(referenceResolver);616 MockTestRunner builder = new MockTestRunner(getClass().getSimpleName(), context) {617 @Override618 public void execute() {619 receive(action -> action.endpoint(messageEndpoint)620 .headerFragment(new TestRequest("Hello Citrus!"), "myMarshaller"));621 }622 };623 TestCase test = builder.getTestCase();624 Assert.assertEquals(test.getActionCount(), 1);625 Assert.assertEquals(test.getActions().get(0).getClass(), ReceiveMessageAction.class);626 ReceiveMessageAction action = ((ReceiveMessageAction)test.getActions().get(0));627 Assert.assertEquals(action.getName(), "receive");628 Assert.assertEquals(action.getMessageType(), MessageType.XML.name());629 Assert.assertEquals(action.getEndpoint(), messageEndpoint);630 Assert.assertEquals(action.getValidationContexts().size(), 3);631 Assert.assertTrue(action.getValidationContexts().stream().anyMatch(HeaderValidationContext.class::isInstance));632 Assert.assertTrue(action.getValidationContexts().stream().anyMatch(XmlMessageValidationContext.class::isInstance));633 Assert.assertTrue(action.getValidationContexts().stream().anyMatch(JsonMessageValidationContext.class::isInstance));634 Assert.assertTrue(action.getMessageBuilder() instanceof DefaultMessageBuilder);635 Assert.assertEquals(((DefaultMessageBuilder)action.getMessageBuilder()).buildMessageHeaderData(context).size(), 1L);636 Assert.assertEquals(((DefaultMessageBuilder)action.getMessageBuilder()).buildMessageHeaderData(context).get(0), "<TestRequest><Message>Hello Citrus!</Message></TestRequest>");637 }638 @Test639 public void testReceiveBuilderWithHeaderResource() throws IOException {640 reset(resource, messageEndpoint, messageConsumer, configuration);641 when(messageEndpoint.createConsumer()).thenReturn(messageConsumer);642 when(messageEndpoint.getEndpointConfiguration()).thenReturn(configuration);643 when(configuration.getTimeout()).thenReturn(100L);644 when(messageEndpoint.getActor()).thenReturn(null);645 when(messageConsumer.receive(any(TestContext.class), anyLong()))646 .thenReturn(new DefaultMessage("<TestRequest><Message>Hello World!</Message></TestRequest>")647 .setHeader("operation", "foo")648 .addHeaderData("<Header><Name>operation</Name><Value>foo</Value></Header>"))649 .thenReturn(new DefaultMessage("<TestRequest><Message>Hello World!</Message></TestRequest>")650 .setHeader("operation", "bar")651 .addHeaderData("<Header><Name>operation</Name><Value>bar</Value></Header>"));652 when(resource.getInputStream()).thenReturn(new ByteArrayInputStream("<Header><Name>operation</Name><Value>foo</Value></Header>".getBytes()))653 .thenReturn(new ByteArrayInputStream("<Header><Name>operation</Name><Value>bar</Value></Header>".getBytes()));654 MockTestRunner builder = new MockTestRunner(getClass().getSimpleName(), context) {655 @Override656 public void execute() {657 receive(action -> action.endpoint(messageEndpoint)658 .payload("<TestRequest><Message>Hello World!</Message></TestRequest>")659 .header(resource));660 receive(action -> action.endpoint(messageEndpoint)661 .message(new DefaultMessage("<TestRequest><Message>Hello World!</Message></TestRequest>"))662 .header(resource));663 }664 };665 TestCase test = builder.getTestCase();666 Assert.assertEquals(test.getActionCount(), 2);667 Assert.assertEquals(test.getActions().get(0).getClass(), ReceiveMessageAction.class);668 Assert.assertEquals(test.getActions().get(1).getClass(), ReceiveMessageAction.class);669 ReceiveMessageAction action = ((ReceiveMessageAction)test.getActions().get(0));670 Assert.assertEquals(action.getName(), "receive");671 Assert.assertEquals(action.getEndpoint(), messageEndpoint);672 Assert.assertEquals(action.getMessageType(), MessageType.XML.name());673 Assert.assertTrue(action.getMessageBuilder() instanceof DefaultMessageBuilder);674 Assert.assertEquals(((DefaultMessageBuilder)action.getMessageBuilder()).buildMessagePayload(context, action.getMessageType()), "<TestRequest><Message>Hello World!</Message></TestRequest>");675 Assert.assertEquals(((DefaultMessageBuilder)action.getMessageBuilder()).buildMessageHeaderData(context).size(), 1L);676 Assert.assertEquals(((DefaultMessageBuilder)action.getMessageBuilder()).buildMessageHeaderData(context).get(0), "<Header><Name>operation</Name><Value>foo</Value></Header>");677 action = ((ReceiveMessageAction)test.getActions().get(1));678 Assert.assertEquals(action.getName(), "receive");679 Assert.assertEquals(action.getEndpoint(), messageEndpoint);680 Assert.assertEquals(action.getMessageType(), MessageType.XML.name());681 Assert.assertTrue(action.getMessageBuilder() instanceof StaticMessageBuilder);682 Assert.assertEquals(((StaticMessageBuilder)action.getMessageBuilder()).getMessage().getPayload(), "<TestRequest><Message>Hello World!</Message></TestRequest>");683 Assert.assertEquals(((StaticMessageBuilder)action.getMessageBuilder()).buildMessageHeaderData(context).size(), 1L);684 Assert.assertEquals(((StaticMessageBuilder)action.getMessageBuilder()).buildMessageHeaderData(context).get(0), "<Header><Name>operation</Name><Value>bar</Value></Header>");685 }686 @Test687 public void testReceiveBuilderWithMultipleHeaderResource() throws IOException {688 reset(resource, messageEndpoint, messageConsumer, configuration);689 when(messageEndpoint.createConsumer()).thenReturn(messageConsumer);690 when(messageEndpoint.getEndpointConfiguration()).thenReturn(configuration);691 when(configuration.getTimeout()).thenReturn(100L);692 when(messageEndpoint.getActor()).thenReturn(null);693 when(messageConsumer.receive(any(TestContext.class), anyLong())).thenReturn(694 new DefaultMessage("<TestRequest><Message>Hello World!</Message></TestRequest>")695 .setHeader("operation", "foo")696 .addHeaderData("<Header><Name>operation</Name><Value>sayHello</Value></Header>")697 .addHeaderData("<Header><Name>operation</Name><Value>foo</Value></Header>")698 .addHeaderData("<Header><Name>operation</Name><Value>bar</Value></Header>"));699 when(resource.getInputStream()).thenReturn(new ByteArrayInputStream("<Header><Name>operation</Name><Value>foo</Value></Header>".getBytes()))700 .thenReturn(new ByteArrayInputStream("<Header><Name>operation</Name><Value>bar</Value></Header>".getBytes()))701 .thenReturn(new ByteArrayInputStream("<Header><Name>operation</Name><Value>foo</Value></Header>".getBytes()))702 .thenReturn(new ByteArrayInputStream("<Header><Name>operation</Name><Value>bar</Value></Header>".getBytes()));703 MockTestRunner builder = new MockTestRunner(getClass().getSimpleName(), context) {704 @Override705 public void execute() {706 receive(action -> action.endpoint(messageEndpoint)707 .payload("<TestRequest><Message>Hello World!</Message></TestRequest>")708 .header("<Header><Name>operation</Name><Value>sayHello</Value></Header>")709 .header(resource)710 .header(resource));711 receive(action -> action.endpoint(messageEndpoint)712 .message(new DefaultMessage("<TestRequest><Message>Hello World!</Message></TestRequest>"))713 .header("<Header><Name>operation</Name><Value>sayHello</Value></Header>")714 .header(resource)715 .header(resource));716 }717 };718 TestCase test = builder.getTestCase();719 Assert.assertEquals(test.getActionCount(), 2);720 Assert.assertEquals(test.getActions().get(0).getClass(), ReceiveMessageAction.class);721 Assert.assertEquals(test.getActions().get(1).getClass(), ReceiveMessageAction.class);722 ReceiveMessageAction action = ((ReceiveMessageAction)test.getActions().get(0));723 Assert.assertEquals(action.getName(), "receive");724 Assert.assertEquals(action.getEndpoint(), messageEndpoint);725 Assert.assertEquals(action.getMessageType(), MessageType.XML.name());726 Assert.assertTrue(action.getMessageBuilder() instanceof DefaultMessageBuilder);727 Assert.assertEquals(((DefaultMessageBuilder) action.getMessageBuilder()).buildMessagePayload(context, action.getMessageType()), "<TestRequest><Message>Hello World!</Message></TestRequest>");728 Assert.assertEquals(((DefaultMessageBuilder) action.getMessageBuilder()).buildMessageHeaderData(context).size(), 3L);729 Assert.assertEquals(((DefaultMessageBuilder) action.getMessageBuilder()).buildMessageHeaderData(context).get(0), "<Header><Name>operation</Name><Value>sayHello</Value></Header>");730 Assert.assertEquals(((DefaultMessageBuilder)action.getMessageBuilder()).buildMessageHeaderData(context).get(1), "<Header><Name>operation</Name><Value>foo</Value></Header>");731 Assert.assertEquals(((DefaultMessageBuilder)action.getMessageBuilder()).buildMessageHeaderData(context).get(2), "<Header><Name>operation</Name><Value>bar</Value></Header>");732 action = ((ReceiveMessageAction)test.getActions().get(1));733 Assert.assertEquals(action.getName(), "receive");734 Assert.assertEquals(action.getEndpoint(), messageEndpoint);735 Assert.assertEquals(action.getMessageType(), MessageType.XML.name());736 Assert.assertTrue(action.getMessageBuilder() instanceof StaticMessageBuilder);737 Assert.assertEquals(((StaticMessageBuilder) action.getMessageBuilder()).getMessage().getPayload(), "<TestRequest><Message>Hello World!</Message></TestRequest>");738 Assert.assertEquals(((StaticMessageBuilder) action.getMessageBuilder()).buildMessageHeaderData(context).size(), 3L);739 Assert.assertEquals(((StaticMessageBuilder)action.getMessageBuilder()).buildMessageHeaderData(context).get(0), "<Header><Name>operation</Name><Value>sayHello</Value></Header>");740 Assert.assertEquals(((StaticMessageBuilder)action.getMessageBuilder()).buildMessageHeaderData(context).get(1), "<Header><Name>operation</Name><Value>foo</Value></Header>");741 Assert.assertEquals(((StaticMessageBuilder)action.getMessageBuilder()).buildMessageHeaderData(context).get(2), "<Header><Name>operation</Name><Value>bar</Value></Header>");742 }743 @Test744 public void testReceiveBuilderWithValidator() {745 reset(messageEndpoint, messageConsumer, configuration);746 when(messageEndpoint.createConsumer()).thenReturn(messageConsumer);747 when(messageEndpoint.getEndpointConfiguration()).thenReturn(configuration);748 when(configuration.getTimeout()).thenReturn(100L);749 when(messageEndpoint.getActor()).thenReturn(null);750 when(messageConsumer.receive(any(TestContext.class), anyLong())).thenReturn(new DefaultMessage("TestMessage").setHeader("operation", "sayHello"));751 final PlainTextMessageValidator validator = new PlainTextMessageValidator();752 MockTestRunner builder = new MockTestRunner(getClass().getSimpleName(), context) {753 @Override754 public void execute() {755 receive(action -> action.endpoint(messageEndpoint)756 .messageType(MessageType.PLAINTEXT)757 .payload("TestMessage")758 .header("operation", "sayHello")759 .validator(validator));760 }761 };762 TestCase test = builder.getTestCase();763 Assert.assertEquals(test.getActionCount(), 1);764 Assert.assertEquals(test.getActions().get(0).getClass(), ReceiveMessageAction.class);765 ReceiveMessageAction action = ((ReceiveMessageAction)test.getActions().get(0));766 Assert.assertEquals(action.getName(), "receive");767 Assert.assertEquals(action.getEndpoint(), messageEndpoint);768 Assert.assertEquals(action.getMessageType(), MessageType.PLAINTEXT.name());769 Assert.assertEquals(action.getValidators().size(), 1L);770 Assert.assertEquals(action.getValidators().get(0), validator);771 Assert.assertTrue(action.getMessageBuilder() instanceof DefaultMessageBuilder);772 Assert.assertEquals(((DefaultMessageBuilder) action.getMessageBuilder()).buildMessagePayload(context, action.getMessageType()), "TestMessage");773 Assert.assertTrue(((DefaultMessageBuilder) action.getMessageBuilder()).buildMessageHeaders(context).containsKey("operation"));774 }775 @Test776 public void testReceiveBuilderWithValidatorName() {777 final PlainTextMessageValidator validator = new PlainTextMessageValidator();778 reset(referenceResolver, messageEndpoint, messageConsumer, configuration);779 when(messageEndpoint.createConsumer()).thenReturn(messageConsumer);780 when(messageEndpoint.getEndpointConfiguration()).thenReturn(configuration);781 when(configuration.getTimeout()).thenReturn(100L);782 when(messageEndpoint.getActor()).thenReturn(null);783 when(messageConsumer.receive(any(TestContext.class), anyLong())).thenReturn(new DefaultMessage("TestMessage").setHeader("operation", "sayHello"));784 when(referenceResolver.resolve(TestContext.class)).thenReturn(applicationContext.getBean(TestContext.class));785 when(referenceResolver.resolve("plainTextValidator")).thenReturn(validator);786 when(referenceResolver.resolve(TestActionListeners.class)).thenReturn(new TestActionListeners());787 when(referenceResolver.resolveAll(SequenceBeforeTest.class)).thenReturn(new HashMap<>());788 when(referenceResolver.resolveAll(SequenceAfterTest.class)).thenReturn(new HashMap<>());789 context.setReferenceResolver(referenceResolver);790 MockTestRunner builder = new MockTestRunner(getClass().getSimpleName(), context) {791 @Override792 public void execute() {793 receive(action -> action.endpoint(messageEndpoint)794 .messageType(MessageType.PLAINTEXT)795 .payload("TestMessage")796 .header("operation", "sayHello")797 .validator("plainTextValidator"));798 }799 };800 TestCase test = builder.getTestCase();801 Assert.assertEquals(test.getActionCount(), 1);802 Assert.assertEquals(test.getActions().get(0).getClass(), ReceiveMessageAction.class);803 ReceiveMessageAction action = ((ReceiveMessageAction)test.getActions().get(0));804 Assert.assertEquals(action.getName(), "receive");805 Assert.assertEquals(action.getEndpoint(), messageEndpoint);806 Assert.assertEquals(action.getMessageType(), MessageType.PLAINTEXT.name());807 Assert.assertEquals(action.getValidators().size(), 1L);808 Assert.assertEquals(action.getValidators().get(0), validator);809 Assert.assertTrue(action.getMessageBuilder() instanceof DefaultMessageBuilder);810 Assert.assertEquals(((DefaultMessageBuilder)action.getMessageBuilder()).buildMessagePayload(context, action.getMessageType()), "TestMessage");811 Assert.assertTrue(((DefaultMessageBuilder)action.getMessageBuilder()).buildMessageHeaders(context).containsKey("operation"));812 }813 @Test814 public void testReceiveBuilderWithDictionary() {815 final DataDictionary<?> dictionary = new NodeMappingDataDictionary();816 reset(referenceResolver, messageEndpoint, messageConsumer, configuration);817 when(messageEndpoint.createConsumer()).thenReturn(messageConsumer);818 when(messageEndpoint.getEndpointConfiguration()).thenReturn(configuration);819 when(configuration.getTimeout()).thenReturn(100L);820 when(messageEndpoint.getActor()).thenReturn(null);821 when(messageConsumer.receive(any(TestContext.class), anyLong())).thenReturn(new DefaultMessage("TestMessage").setHeader("operation", "sayHello"));822 when(referenceResolver.resolve(TestContext.class)).thenReturn(applicationContext.getBean(TestContext.class));823 when(referenceResolver.resolve(TestActionListeners.class)).thenReturn(new TestActionListeners());824 when(referenceResolver.resolveAll(SequenceBeforeTest.class)).thenReturn(new HashMap<>());825 when(referenceResolver.resolveAll(SequenceAfterTest.class)).thenReturn(new HashMap<>());826 context.setReferenceResolver(referenceResolver);827 MockTestRunner builder = new MockTestRunner(getClass().getSimpleName(), context) {828 @Override829 public void execute() {830 receive(action -> action.endpoint(messageEndpoint)831 .messageType(MessageType.PLAINTEXT)832 .payload("TestMessage")833 .header("operation", "sayHello")834 .dictionary(dictionary));835 }836 };837 TestCase test = builder.getTestCase();838 Assert.assertEquals(test.getActionCount(), 1);839 Assert.assertEquals(test.getActions().get(0).getClass(), ReceiveMessageAction.class);840 ReceiveMessageAction action = ((ReceiveMessageAction)test.getActions().get(0));841 Assert.assertEquals(action.getName(), "receive");842 Assert.assertEquals(action.getEndpoint(), messageEndpoint);843 Assert.assertEquals(action.getMessageType(), MessageType.PLAINTEXT.name());844 Assert.assertEquals(action.getDataDictionary(), dictionary);845 Assert.assertTrue(action.getMessageBuilder() instanceof DefaultMessageBuilder);846 Assert.assertEquals(((DefaultMessageBuilder)action.getMessageBuilder()).buildMessagePayload(context, action.getMessageType()), "TestMessage");847 Assert.assertTrue(((DefaultMessageBuilder)action.getMessageBuilder()).buildMessageHeaders(context).containsKey("operation"));848 }849 @Test850 public void testReceiveBuilderWithDictionaryName() {851 final DataDictionary<?> dictionary = new NodeMappingDataDictionary();852 reset(referenceResolver, messageEndpoint, messageConsumer, configuration);853 when(messageEndpoint.createConsumer()).thenReturn(messageConsumer);854 when(messageEndpoint.getEndpointConfiguration()).thenReturn(configuration);855 when(configuration.getTimeout()).thenReturn(100L);856 when(messageEndpoint.getActor()).thenReturn(null);857 when(messageConsumer.receive(any(TestContext.class), anyLong())).thenReturn(new DefaultMessage("TestMessage").setHeader("operation", "sayHello"));858 when(referenceResolver.resolve(TestContext.class)).thenReturn(applicationContext.getBean(TestContext.class));859 when(referenceResolver.resolve("customDictionary", DataDictionary.class)).thenReturn(dictionary);860 when(referenceResolver.resolve(TestActionListeners.class)).thenReturn(new TestActionListeners());861 when(referenceResolver.resolveAll(SequenceBeforeTest.class)).thenReturn(new HashMap<>());862 when(referenceResolver.resolveAll(SequenceAfterTest.class)).thenReturn(new HashMap<>());863 context.setReferenceResolver(referenceResolver);864 MockTestRunner builder = new MockTestRunner(getClass().getSimpleName(), context) {865 @Override866 public void execute() {867 receive(action -> action.endpoint(messageEndpoint)868 .messageType(MessageType.PLAINTEXT)869 .payload("TestMessage")870 .header("operation", "sayHello")871 .dictionary("customDictionary"));872 }873 };874 TestCase test = builder.getTestCase();875 Assert.assertEquals(test.getActionCount(), 1);876 Assert.assertEquals(test.getActions().get(0).getClass(), ReceiveMessageAction.class);877 ReceiveMessageAction action = ((ReceiveMessageAction)test.getActions().get(0));878 Assert.assertEquals(action.getName(), "receive");879 Assert.assertEquals(action.getEndpoint(), messageEndpoint);880 Assert.assertEquals(action.getMessageType(), MessageType.PLAINTEXT.name());881 Assert.assertEquals(action.getDataDictionary(), dictionary);882 Assert.assertTrue(action.getMessageBuilder() instanceof DefaultMessageBuilder);883 Assert.assertEquals(((DefaultMessageBuilder)action.getMessageBuilder()).buildMessagePayload(context, action.getMessageType()), "TestMessage");884 Assert.assertTrue(((DefaultMessageBuilder)action.getMessageBuilder()).buildMessageHeaders(context).containsKey("operation"));885 }886 @Test887 public void testReceiveBuilderWithSelector() {888 SelectiveConsumer selectiveConsumer = Mockito.mock(SelectiveConsumer.class);889 reset(messageEndpoint, selectiveConsumer, configuration);890 when(messageEndpoint.createConsumer()).thenReturn(selectiveConsumer);891 when(messageEndpoint.getEndpointConfiguration()).thenReturn(configuration);892 when(configuration.getTimeout()).thenReturn(100L);893 when(messageEndpoint.getActor()).thenReturn(null);894 when(selectiveConsumer.receive(eq("operation = 'sayHello'"), any(TestContext.class), anyLong())).thenReturn(895 new DefaultMessage("<TestRequest><Message>Hello World!</Message></TestRequest>")896 .setHeader("operation", "sayHello"));897 final Map<String, String> messageSelector = new HashMap<>();898 messageSelector.put("operation", "sayHello");899 MockTestRunner builder = new MockTestRunner(getClass().getSimpleName(), context) {900 @Override901 public void execute() {902 receive(action -> action.endpoint(messageEndpoint)903 .payload("<TestRequest><Message>Hello World!</Message></TestRequest>")904 .selector(messageSelector));905 }906 };907 TestCase test = builder.getTestCase();908 Assert.assertEquals(test.getActionCount(), 1);909 Assert.assertEquals(test.getActions().get(0).getClass(), ReceiveMessageAction.class);910 ReceiveMessageAction action = ((ReceiveMessageAction)test.getActions().get(0));911 Assert.assertEquals(action.getName(), "receive");912 Assert.assertEquals(action.getMessageType(), MessageType.XML.name());913 Assert.assertEquals(action.getEndpoint(), messageEndpoint);914 Assert.assertEquals(action.getMessageSelectorMap(), messageSelector);915 }916 @Test917 public void testReceiveBuilderWithSelectorExpression() {918 SelectiveConsumer selectiveConsumer = Mockito.mock(SelectiveConsumer.class);919 reset(messageEndpoint, selectiveConsumer, configuration);920 when(messageEndpoint.createConsumer()).thenReturn(selectiveConsumer);921 when(messageEndpoint.getEndpointConfiguration()).thenReturn(configuration);922 when(configuration.getTimeout()).thenReturn(100L);923 when(messageEndpoint.getActor()).thenReturn(null);924 when(selectiveConsumer.receive(eq("operation = 'sayHello'"), any(TestContext.class), anyLong())).thenReturn(925 new DefaultMessage("<TestRequest><Message>Hello World!</Message></TestRequest>")926 .setHeader("operation", "sayHello"));927 MockTestRunner builder = new MockTestRunner(getClass().getSimpleName(), context) {928 @Override929 public void execute() {930 receive(action -> action.endpoint(messageEndpoint)931 .payload("<TestRequest><Message>Hello World!</Message></TestRequest>")932 .selector("operation = 'sayHello'"));933 }934 };935 TestCase test = builder.getTestCase();936 Assert.assertEquals(test.getActionCount(), 1);937 Assert.assertEquals(test.getActions().get(0).getClass(), ReceiveMessageAction.class);938 ReceiveMessageAction action = ((ReceiveMessageAction)test.getActions().get(0));939 Assert.assertEquals(action.getName(), "receive");940 Assert.assertEquals(action.getMessageType(), MessageType.XML.name());941 Assert.assertEquals(action.getEndpoint(), messageEndpoint);942 Assert.assertTrue(action.getMessageSelectorMap().isEmpty());943 Assert.assertEquals(action.getMessageSelector(), "operation = 'sayHello'");944 }945 @Test946 public void testReceiveBuilderExtractFromPayload() {947 reset(referenceResolver, messageEndpoint, messageConsumer, configuration);948 when(messageEndpoint.createConsumer()).thenReturn(messageConsumer);949 when(messageEndpoint.getEndpointConfiguration()).thenReturn(configuration);950 when(configuration.getTimeout()).thenReturn(100L);951 when(messageEndpoint.getActor()).thenReturn(null);952 when(messageConsumer.receive(any(TestContext.class), anyLong())).thenReturn(953 new DefaultMessage("<TestRequest><Message lang=\"ENG\">Hello World!</Message></TestRequest>")954 .setHeader("operation", "sayHello"));955 when(referenceResolver.resolve(TestContext.class)).thenReturn(applicationContext.getBean(TestContext.class));956 when(referenceResolver.resolve(TestActionListeners.class)).thenReturn(new TestActionListeners());957 when(referenceResolver.resolveAll(SequenceBeforeTest.class)).thenReturn(new HashMap<>());958 when(referenceResolver.resolveAll(SequenceAfterTest.class)).thenReturn(new HashMap<>());959 context.setReferenceResolver(referenceResolver);960 MockTestRunner builder = new MockTestRunner(getClass().getSimpleName(), context) {961 @Override962 public void execute() {963 receive(action -> action.endpoint(messageEndpoint)964 .payload("<TestRequest><Message lang=\"ENG\">Hello World!</Message></TestRequest>")965 .extractFromPayload("/TestRequest/Message", "text")966 .extractFromPayload("/TestRequest/Message/@lang", "language"));967 }968 };969 TestContext context = builder.getTestContext();970 Assert.assertNotNull(context.getVariable("text"));971 Assert.assertNotNull(context.getVariable("language"));972 Assert.assertEquals(context.getVariable("text"), "Hello World!");973 Assert.assertEquals(context.getVariable("language"), "ENG");974 TestCase test = builder.getTestCase();975 Assert.assertEquals(test.getActionCount(), 1);976 Assert.assertEquals(test.getActions().get(0).getClass(), ReceiveMessageAction.class);977 ReceiveMessageAction action = ((ReceiveMessageAction)test.getActions().get(0));978 Assert.assertEquals(action.getName(), "receive");979 Assert.assertEquals(action.getMessageType(), MessageType.XML.name());980 Assert.assertEquals(action.getEndpoint(), messageEndpoint);981 Assert.assertEquals(action.getVariableExtractors().size(), 2);982 Assert.assertTrue(action.getVariableExtractors().get(0) instanceof XpathPayloadVariableExtractor);983 Assert.assertTrue(action.getVariableExtractors().get(1) instanceof XpathPayloadVariableExtractor);984 Assert.assertTrue(((XpathPayloadVariableExtractor) action.getVariableExtractors().get(0)).getXpathExpressions().containsKey("/TestRequest/Message"));985 Assert.assertTrue(((XpathPayloadVariableExtractor) action.getVariableExtractors().get(1)).getXpathExpressions().containsKey("/TestRequest/Message/@lang"));986 }987 @Test988 public void testReceiveBuilderExtractJsonPathFromPayload() {989 reset(referenceResolver, messageEndpoint, messageConsumer, configuration);990 when(messageEndpoint.createConsumer()).thenReturn(messageConsumer);991 when(messageEndpoint.getEndpointConfiguration()).thenReturn(configuration);992 when(configuration.getTimeout()).thenReturn(100L);993 when(messageEndpoint.getActor()).thenReturn(null);994 when(messageConsumer.receive(any(TestContext.class), anyLong())).thenReturn(995 new DefaultMessage("{\"text\":\"Hello World!\", \"person\":{\"name\":\"John\",\"surname\":\"Doe\"}, \"index\":5, \"id\":\"x123456789x\"}")996 .setHeader("operation", "sayHello"));997 when(referenceResolver.resolve(TestContext.class)).thenReturn(applicationContext.getBean(TestContext.class));998 when(referenceResolver.resolve(TestActionListeners.class)).thenReturn(new TestActionListeners());999 when(referenceResolver.resolveAll(SequenceBeforeTest.class)).thenReturn(new HashMap<>());1000 when(referenceResolver.resolveAll(SequenceAfterTest.class)).thenReturn(new HashMap<>());1001 context.setReferenceResolver(referenceResolver);1002 MockTestRunner builder = new MockTestRunner(getClass().getSimpleName(), context) {1003 @Override1004 public void execute() {1005 receive(action -> action.endpoint(messageEndpoint)1006 .messageType(MessageType.JSON)1007 .payload("{\"text\":\"Hello World!\", \"person\":{\"name\":\"John\",\"surname\":\"Doe\"}, \"index\":5, \"id\":\"x123456789x\"}")1008 .extractFromPayload("$.text", "text")1009 .extractFromPayload("$.toString()", "payload")1010 .extractFromPayload("$.person", "person"));1011 }1012 };1013 TestContext context = builder.getTestContext();1014 Assert.assertNotNull(context.getVariable("text"));1015 Assert.assertNotNull(context.getVariable("person"));1016 Assert.assertNotNull(context.getVariable("payload"));1017 Assert.assertEquals(context.getVariable("text"), "Hello World!");1018 Assert.assertEquals(context.getVariable("payload"), "{\"person\":{\"surname\":\"Doe\",\"name\":\"John\"},\"index\":5,\"text\":\"Hello World!\",\"id\":\"x123456789x\"}");1019 Assert.assertTrue(context.getVariable("person").contains("\"John\""));1020 TestCase test = builder.getTestCase();1021 Assert.assertEquals(test.getActionCount(), 1);1022 Assert.assertEquals(test.getActions().get(0).getClass(), ReceiveMessageAction.class);1023 ReceiveMessageAction action = ((ReceiveMessageAction)test.getActions().get(0));1024 Assert.assertEquals(action.getName(), "receive");1025 Assert.assertEquals(action.getMessageType(), MessageType.JSON.name());1026 Assert.assertEquals(action.getEndpoint(), messageEndpoint);1027 Assert.assertEquals(action.getVariableExtractors().size(), 3);1028 Assert.assertTrue(action.getVariableExtractors().get(0) instanceof JsonPathVariableExtractor);1029 Assert.assertTrue(action.getVariableExtractors().get(1) instanceof JsonPathVariableExtractor);1030 Assert.assertTrue(action.getVariableExtractors().get(2) instanceof JsonPathVariableExtractor);1031 Assert.assertTrue(((JsonPathVariableExtractor) action.getVariableExtractors().get(0)).getJsonPathExpressions().containsKey("$.text"));1032 Assert.assertTrue(((JsonPathVariableExtractor) action.getVariableExtractors().get(1)).getJsonPathExpressions().containsKey("$.toString()"));1033 Assert.assertTrue(((JsonPathVariableExtractor) action.getVariableExtractors().get(2)).getJsonPathExpressions().containsKey("$.person"));1034 }1035 @Test1036 public void testReceiveBuilderExtractFromHeader() {1037 reset(messageEndpoint, messageConsumer, configuration);1038 when(messageEndpoint.createConsumer()).thenReturn(messageConsumer);1039 when(messageEndpoint.getEndpointConfiguration()).thenReturn(configuration);1040 when(configuration.getTimeout()).thenReturn(100L);1041 when(messageEndpoint.getActor()).thenReturn(null);1042 when(messageConsumer.receive(any(TestContext.class), anyLong())).thenReturn(1043 new DefaultMessage("<TestRequest><Message lang=\"ENG\">Hello World!</Message></TestRequest>")1044 .setHeader("operation", "sayHello")1045 .setHeader("requestId", "123456"));1046 MockTestRunner builder = new MockTestRunner(getClass().getSimpleName(), context) {1047 @Override1048 public void execute() {1049 receive(action -> action.endpoint(messageEndpoint)1050 .payload("<TestRequest><Message lang=\"ENG\">Hello World!</Message></TestRequest>")1051 .extractFromHeader("operation", "operationHeader")1052 .extractFromHeader("requestId", "id"));1053 }1054 };1055 TestContext context = builder.getTestContext();1056 Assert.assertNotNull(context.getVariable("operationHeader"));1057 Assert.assertNotNull(context.getVariable("id"));1058 Assert.assertEquals(context.getVariable("operationHeader"), "sayHello");1059 Assert.assertEquals(context.getVariable("id"), "123456");1060 TestCase test = builder.getTestCase();1061 Assert.assertEquals(test.getActionCount(), 1);1062 Assert.assertEquals(test.getActions().get(0).getClass(), ReceiveMessageAction.class);1063 ReceiveMessageAction action = ((ReceiveMessageAction)test.getActions().get(0));1064 Assert.assertEquals(action.getName(), "receive");1065 Assert.assertEquals(action.getMessageType(), MessageType.XML.name());1066 Assert.assertEquals(action.getEndpoint(), messageEndpoint);1067 Assert.assertEquals(action.getVariableExtractors().size(), 2);1068 Assert.assertTrue(action.getVariableExtractors().get(0) instanceof MessageHeaderVariableExtractor);1069 Assert.assertTrue(action.getVariableExtractors().get(1) instanceof MessageHeaderVariableExtractor);1070 Assert.assertTrue(((MessageHeaderVariableExtractor) action.getVariableExtractors().get(0)).getHeaderMappings().containsKey("operation"));1071 Assert.assertTrue(((MessageHeaderVariableExtractor) action.getVariableExtractors().get(1)).getHeaderMappings().containsKey("requestId"));1072 }1073 @Test1074 public void testReceiveBuilderExtractCombined() {1075 reset(messageEndpoint, messageConsumer, configuration);1076 when(messageEndpoint.createConsumer()).thenReturn(messageConsumer);1077 when(messageEndpoint.getEndpointConfiguration()).thenReturn(configuration);1078 when(configuration.getTimeout()).thenReturn(100L);1079 when(messageEndpoint.getActor()).thenReturn(null);1080 when(messageConsumer.receive(any(TestContext.class), anyLong())).thenReturn(1081 new DefaultMessage("<TestRequest><Message lang=\"ENG\">Hello World!</Message></TestRequest>")1082 .setHeader("operation", "sayHello")1083 .setHeader("requestId", "123456"));1084 MockTestRunner builder = new MockTestRunner(getClass().getSimpleName(), context) {1085 @Override1086 public void execute() {1087 receive(action -> action.endpoint(messageEndpoint)1088 .payload("<TestRequest><Message lang=\"ENG\">Hello World!</Message></TestRequest>")1089 .extractFromHeader("operation", "operationHeader")1090 .extractFromHeader("requestId", "id")1091 .extractFromPayload("/TestRequest/Message", "text")1092 .extractFromPayload("/TestRequest/Message/@lang", "language"));1093 }1094 };1095 TestContext context = builder.getTestContext();1096 Assert.assertNotNull(context.getVariable("operationHeader"));1097 Assert.assertNotNull(context.getVariable("id"));1098 Assert.assertEquals(context.getVariable("operationHeader"), "sayHello");1099 Assert.assertEquals(context.getVariable("id"), "123456");1100 Assert.assertNotNull(context.getVariable("text"));1101 Assert.assertNotNull(context.getVariable("language"));1102 Assert.assertEquals(context.getVariable("text"), "Hello World!");1103 Assert.assertEquals(context.getVariable("language"), "ENG");1104 TestCase test = builder.getTestCase();1105 Assert.assertEquals(test.getActionCount(), 1);1106 Assert.assertEquals(test.getActions().get(0).getClass(), ReceiveMessageAction.class);1107 ReceiveMessageAction action = ((ReceiveMessageAction)test.getActions().get(0));1108 Assert.assertEquals(action.getName(), "receive");1109 Assert.assertEquals(action.getMessageType(), MessageType.XML.name());1110 Assert.assertEquals(action.getEndpoint(), messageEndpoint);1111 Assert.assertEquals(action.getVariableExtractors().size(), 4);1112 Assert.assertTrue(action.getVariableExtractors().get(0) instanceof MessageHeaderVariableExtractor);1113 Assert.assertTrue(action.getVariableExtractors().get(1) instanceof MessageHeaderVariableExtractor);1114 Assert.assertTrue(((MessageHeaderVariableExtractor) action.getVariableExtractors().get(0)).getHeaderMappings().containsKey("operation"));1115 Assert.assertTrue(((MessageHeaderVariableExtractor) action.getVariableExtractors().get(1)).getHeaderMappings().containsKey("requestId"));1116 Assert.assertTrue(action.getVariableExtractors().get(2) instanceof XpathPayloadVariableExtractor);1117 Assert.assertTrue(action.getVariableExtractors().get(3) instanceof XpathPayloadVariableExtractor);1118 Assert.assertTrue(((XpathPayloadVariableExtractor) action.getVariableExtractors().get(2)).getXpathExpressions().containsKey("/TestRequest/Message"));1119 Assert.assertTrue(((XpathPayloadVariableExtractor) action.getVariableExtractors().get(3)).getXpathExpressions().containsKey("/TestRequest/Message/@lang"));1120 }1121 @Test1122 public void testReceiveBuilderWithValidationProcessor() {1123 final AbstractValidationProcessor processor = Mockito.mock(AbstractValidationProcessor.class);1124 reset(processor, messageEndpoint, messageConsumer, configuration);1125 when(messageEndpoint.createConsumer()).thenReturn(messageConsumer);1126 when(messageEndpoint.getEndpointConfiguration()).thenReturn(configuration);1127 when(configuration.getTimeout()).thenReturn(100L);1128 when(messageEndpoint.getActor()).thenReturn(null);1129 when(messageConsumer.receive(any(TestContext.class), anyLong())).thenReturn(new DefaultMessage("TestMessage").setHeader("operation", "sayHello"));1130 MockTestRunner builder = new MockTestRunner(getClass().getSimpleName(), context) {1131 @Override1132 public void execute() {1133 receive(action -> action.endpoint(messageEndpoint)1134 .messageType(MessageType.PLAINTEXT)1135 .payload("TestMessage")1136 .header("operation", "sayHello")1137 .validationCallback(processor));1138 }1139 };1140 TestCase test = builder.getTestCase();1141 Assert.assertEquals(test.getActionCount(), 1);1142 Assert.assertEquals(test.getActions().get(0).getClass(), ReceiveMessageAction.class);1143 ReceiveMessageAction action = ((ReceiveMessageAction)test.getActions().get(0));1144 Assert.assertEquals(action.getName(), "receive");1145 Assert.assertEquals(action.getEndpoint(), messageEndpoint);1146 Assert.assertEquals(action.getMessageType(), MessageType.PLAINTEXT.name());1147 Assert.assertEquals(action.getValidationProcessor(), processor);1148 Assert.assertTrue(action.getMessageBuilder() instanceof DefaultMessageBuilder);1149 Assert.assertEquals(((DefaultMessageBuilder) action.getMessageBuilder()).buildMessagePayload(context, action.getMessageType()), "TestMessage");1150 Assert.assertTrue(((DefaultMessageBuilder) action.getMessageBuilder()).buildMessageHeaders(context).containsKey("operation"));1151 verify(processor, atLeastOnce()).setReferenceResolver(context.getReferenceResolver());1152 verify(processor).validate(any(Message.class), any(TestContext.class));1153 }1154 @Test1155 public void testReceiveBuilderWithValidationScript() {1156 final GroovyJsonMessageValidator validator = new GroovyJsonMessageValidator();1157 reset(referenceResolver, messageEndpoint, messageConsumer, configuration);1158 when(messageEndpoint.createConsumer()).thenReturn(messageConsumer);1159 when(messageEndpoint.getEndpointConfiguration()).thenReturn(configuration);1160 when(configuration.getTimeout()).thenReturn(100L);1161 when(messageEndpoint.getActor()).thenReturn(null);1162 when(messageConsumer.receive(any(TestContext.class), anyLong())).thenReturn(new DefaultMessage("{\"message\": \"Hello Citrus!\"}").setHeader("operation", "sayHello"));1163 when(referenceResolver.resolve(TestContext.class)).thenReturn(applicationContext.getBean(TestContext.class));1164 when(referenceResolver.resolve("groovyMessageValidator")).thenReturn(validator);1165 when(referenceResolver.resolve(TestActionListeners.class)).thenReturn(new TestActionListeners());1166 when(referenceResolver.resolveAll(SequenceBeforeTest.class)).thenReturn(new HashMap<>());1167 when(referenceResolver.resolveAll(SequenceAfterTest.class)).thenReturn(new HashMap<>());1168 context.setReferenceResolver(referenceResolver);1169 MockTestRunner builder = new MockTestRunner(getClass().getSimpleName(), context) {1170 @Override1171 public void execute() {1172 receive(action -> action.endpoint(messageEndpoint)1173 .messageType(MessageType.JSON)1174 .validateScript("assert json.message == 'Hello Citrus!'")1175 .validator("groovyMessageValidator"));1176 }1177 };1178 TestCase test = builder.getTestCase();1179 Assert.assertEquals(test.getActionCount(), 1);1180 Assert.assertEquals(test.getActions().get(0).getClass(), ReceiveMessageAction.class);1181 ReceiveMessageAction action = ((ReceiveMessageAction)test.getActions().get(0));1182 Assert.assertEquals(action.getName(), "receive");1183 Assert.assertEquals(action.getEndpoint(), messageEndpoint);1184 Assert.assertEquals(action.getMessageType(), MessageType.JSON.name());1185 Assert.assertEquals(action.getValidators().size(), 1L);1186 Assert.assertEquals(action.getValidators().get(0), validator);1187 Assert.assertEquals(action.getValidationContexts().size(), 3L);1188 Assert.assertTrue(action.getValidationContexts().stream().anyMatch(HeaderValidationContext.class::isInstance));1189 Assert.assertTrue(action.getValidationContexts().stream().anyMatch(JsonMessageValidationContext.class::isInstance));1190 Assert.assertTrue(action.getValidationContexts().stream().anyMatch(ScriptValidationContext.class::isInstance));1191 ScriptValidationContext validationContext = action.getValidationContexts().stream()1192 .filter(ScriptValidationContext.class::isInstance).findFirst()1193 .map(ScriptValidationContext.class::cast)1194 .orElseThrow(() -> new AssertionError("Missing validation context"));1195 Assert.assertEquals(validationContext.getScriptType(), ScriptTypes.GROOVY);1196 Assert.assertEquals(validationContext.getValidationScript().trim(), "assert json.message == 'Hello Citrus!'");1197 Assert.assertNull(validationContext.getValidationScriptResourcePath());1198 }1199 @Test1200 public void testReceiveBuilderWithValidationScriptResourcePath() throws IOException {1201 final GroovyJsonMessageValidator validator = new GroovyJsonMessageValidator();1202 reset(referenceResolver, messageEndpoint, messageConsumer, configuration);1203 when(messageEndpoint.createConsumer()).thenReturn(messageConsumer);1204 when(messageEndpoint.getEndpointConfiguration()).thenReturn(configuration);1205 when(configuration.getTimeout()).thenReturn(100L);1206 when(messageEndpoint.getActor()).thenReturn(null);1207 when(messageConsumer.receive(any(TestContext.class), anyLong())).thenReturn(new DefaultMessage("{\"message\": \"Hello Citrus!\"}").setHeader("operation", "sayHello"));1208 when(referenceResolver.resolve(TestContext.class)).thenReturn(applicationContext.getBean(TestContext.class));1209 when(referenceResolver.resolve("groovyMessageValidator")).thenReturn(validator);1210 when(referenceResolver.resolve(TestActionListeners.class)).thenReturn(new TestActionListeners());1211 when(referenceResolver.resolveAll(SequenceBeforeTest.class)).thenReturn(new HashMap<>());1212 when(referenceResolver.resolveAll(SequenceAfterTest.class)).thenReturn(new HashMap<>());1213 context.setReferenceResolver(referenceResolver);1214 MockTestRunner builder = new MockTestRunner(getClass().getSimpleName(), context) {1215 @Override1216 public void execute() {1217 receive(action -> action.endpoint(messageEndpoint)1218 .messageType(MessageType.JSON)1219 .validateScriptResource("classpath:com/consol/citrus/dsl/runner/validation.groovy")1220 .validator("groovyMessageValidator"));1221 }1222 };1223 TestCase test = builder.getTestCase();1224 Assert.assertEquals(test.getActionCount(), 1);1225 Assert.assertEquals(test.getActions().get(0).getClass(), ReceiveMessageAction.class);1226 ReceiveMessageAction action = ((ReceiveMessageAction)test.getActions().get(0));1227 Assert.assertEquals(action.getName(), "receive");1228 Assert.assertEquals(action.getEndpoint(), messageEndpoint);1229 Assert.assertEquals(action.getMessageType(), MessageType.JSON.name());1230 Assert.assertEquals(action.getValidators().size(), 1L);1231 Assert.assertEquals(action.getValidators().get(0), validator);1232 Assert.assertEquals(action.getValidationContexts().size(), 3L);1233 Assert.assertTrue(action.getValidationContexts().stream().anyMatch(HeaderValidationContext.class::isInstance));1234 Assert.assertTrue(action.getValidationContexts().stream().anyMatch(JsonMessageValidationContext.class::isInstance));1235 Assert.assertTrue(action.getValidationContexts().stream().anyMatch(ScriptValidationContext.class::isInstance));1236 ScriptValidationContext validationContext = action.getValidationContexts().stream()1237 .filter(ScriptValidationContext.class::isInstance).findFirst()1238 .map(ScriptValidationContext.class::cast)1239 .orElseThrow(() -> new AssertionError("Missing validation context"));1240 Assert.assertEquals(validationContext.getScriptType(), ScriptTypes.GROOVY);1241 Assert.assertEquals(validationContext.getValidationScript(), "");1242 Assert.assertEquals(validationContext.getValidationScriptResourcePath(), "classpath:com/consol/citrus/dsl/runner/validation.groovy");1243 }1244 @Test1245 public void testReceiveBuilderWithValidationScriptResource() throws IOException {1246 final GroovyJsonMessageValidator validator = new GroovyJsonMessageValidator();1247 reset(referenceResolver, messageEndpoint, messageConsumer, configuration);1248 when(messageEndpoint.createConsumer()).thenReturn(messageConsumer);1249 when(messageEndpoint.getEndpointConfiguration()).thenReturn(configuration);1250 when(configuration.getTimeout()).thenReturn(100L);1251 when(messageEndpoint.getActor()).thenReturn(null);1252 when(messageConsumer.receive(any(TestContext.class), anyLong())).thenReturn(new DefaultMessage("{\"message\": \"Hello Citrus!\"}").setHeader("operation", "sayHello"));1253 when(referenceResolver.resolve(TestContext.class)).thenReturn(applicationContext.getBean(TestContext.class));1254 when(referenceResolver.resolve("groovyMessageValidator")).thenReturn(validator);1255 when(referenceResolver.resolve(TestActionListeners.class)).thenReturn(new TestActionListeners());1256 when(referenceResolver.resolveAll(SequenceBeforeTest.class)).thenReturn(new HashMap<>());1257 when(referenceResolver.resolveAll(SequenceAfterTest.class)).thenReturn(new HashMap<>());1258 context.setReferenceResolver(referenceResolver);1259 MockTestRunner builder = new MockTestRunner(getClass().getSimpleName(), context) {1260 @Override1261 public void execute() {1262 receive(action -> action.endpoint(messageEndpoint)1263 .messageType(MessageType.JSON)1264 .validateScript(new ClassPathResource("com/consol/citrus/dsl/runner/validation.groovy"))1265 .validator("groovyMessageValidator"));1266 }1267 };1268 TestCase test = builder.getTestCase();1269 Assert.assertEquals(test.getActionCount(), 1);1270 Assert.assertEquals(test.getActions().get(0).getClass(), ReceiveMessageAction.class);1271 ReceiveMessageAction action = ((ReceiveMessageAction)test.getActions().get(0));1272 Assert.assertEquals(action.getName(), "receive");1273 Assert.assertEquals(action.getEndpoint(), messageEndpoint);1274 Assert.assertEquals(action.getMessageType(), MessageType.JSON.name());1275 Assert.assertEquals(action.getValidators().size(), 1L);1276 Assert.assertEquals(action.getValidators().get(0), validator);1277 Assert.assertEquals(action.getValidationContexts().size(), 3L);1278 Assert.assertTrue(action.getValidationContexts().stream().anyMatch(HeaderValidationContext.class::isInstance));1279 Assert.assertTrue(action.getValidationContexts().stream().anyMatch(JsonMessageValidationContext.class::isInstance));1280 Assert.assertTrue(action.getValidationContexts().stream().anyMatch(ScriptValidationContext.class::isInstance));1281 ScriptValidationContext validationContext = action.getValidationContexts().stream()1282 .filter(ScriptValidationContext.class::isInstance).findFirst()1283 .map(ScriptValidationContext.class::cast)1284 .orElseThrow(() -> new AssertionError("Missing validation context"));1285 Assert.assertEquals(validationContext.getScriptType(), ScriptTypes.GROOVY);1286 Assert.assertEquals(validationContext.getValidationScript().trim(), "assert json.message == 'Hello Citrus!'");1287 Assert.assertNull(validationContext.getValidationScriptResourcePath());1288 }1289 @Test1290 public void testReceiveBuilderWithValidationScriptAndHeader() {1291 final GroovyJsonMessageValidator validator = new GroovyJsonMessageValidator();1292 reset(referenceResolver, messageEndpoint, messageConsumer, configuration);1293 when(messageEndpoint.createConsumer()).thenReturn(messageConsumer);1294 when(messageEndpoint.getEndpointConfiguration()).thenReturn(configuration);1295 when(configuration.getTimeout()).thenReturn(100L);1296 when(messageEndpoint.getActor()).thenReturn(null);1297 when(messageConsumer.receive(any(TestContext.class), anyLong())).thenReturn(new DefaultMessage("{\"message\": \"Hello Citrus!\"}").setHeader("operation", "sayHello"));1298 when(referenceResolver.resolve(TestContext.class)).thenReturn(applicationContext.getBean(TestContext.class));1299 when(referenceResolver.resolve("groovyMessageValidator")).thenReturn(validator);1300 when(referenceResolver.resolve(TestActionListeners.class)).thenReturn(new TestActionListeners());1301 when(referenceResolver.resolveAll(SequenceBeforeTest.class)).thenReturn(new HashMap<>());1302 when(referenceResolver.resolveAll(SequenceAfterTest.class)).thenReturn(new HashMap<>());1303 context.setReferenceResolver(referenceResolver);1304 MockTestRunner builder = new MockTestRunner(getClass().getSimpleName(), context) {1305 @Override1306 public void execute() {1307 receive(action -> action.endpoint(messageEndpoint)1308 .messageType(MessageType.JSON)1309 .validateScript("assert json.message == 'Hello Citrus!'")1310 .validator("groovyMessageValidator")1311 .header("operation", "sayHello"));1312 }1313 };1314 TestCase test = builder.getTestCase();1315 Assert.assertEquals(test.getActionCount(), 1);1316 Assert.assertEquals(test.getActions().get(0).getClass(), ReceiveMessageAction.class);1317 ReceiveMessageAction action = ((ReceiveMessageAction)test.getActions().get(0));1318 Assert.assertEquals(action.getName(), "receive");1319 Assert.assertEquals(action.getEndpoint(), messageEndpoint);1320 Assert.assertEquals(action.getMessageType(), MessageType.JSON.name());1321 Assert.assertEquals(action.getValidators().size(), 1L);1322 Assert.assertEquals(action.getValidators().get(0), validator);1323 Assert.assertEquals(action.getValidationContexts().size(), 3L);1324 Assert.assertTrue(action.getValidationContexts().stream().anyMatch(HeaderValidationContext.class::isInstance));1325 Assert.assertTrue(action.getValidationContexts().stream().anyMatch(JsonMessageValidationContext.class::isInstance));1326 Assert.assertTrue(action.getValidationContexts().stream().anyMatch(ScriptValidationContext.class::isInstance));1327 ScriptValidationContext validationContext = action.getValidationContexts().stream()1328 .filter(ScriptValidationContext.class::isInstance).findFirst()1329 .map(ScriptValidationContext.class::cast)1330 .orElseThrow(() -> new AssertionError("Missing validation context"));1331 Assert.assertEquals(validationContext.getScriptType(), ScriptTypes.GROOVY);1332 Assert.assertEquals(validationContext.getValidationScript().trim(), "assert json.message == 'Hello Citrus!'");1333 Assert.assertNull(validationContext.getValidationScriptResourcePath());1334 Assert.assertTrue(action.getMessageBuilder() instanceof DefaultMessageBuilder);1335 Assert.assertEquals(((DefaultMessageBuilder)action.getMessageBuilder()).buildMessagePayload(context, action.getMessageType()), "");1336 Assert.assertEquals(((DefaultMessageBuilder)action.getMessageBuilder()).buildMessageHeaders(context).size(), 1L);1337 Assert.assertTrue(((DefaultMessageBuilder)action.getMessageBuilder()).buildMessageHeaders(context).containsKey("operation"));1338 }1339 @Test1340 public void testReceiveBuilderWithNamespaceValidation() {1341 reset(messageEndpoint, messageConsumer, configuration);1342 when(messageEndpoint.createConsumer()).thenReturn(messageConsumer);1343 when(messageEndpoint.getEndpointConfiguration()).thenReturn(configuration);1344 when(configuration.getTimeout()).thenReturn(100L);1345 when(messageEndpoint.getActor()).thenReturn(null);1346 when(messageConsumer.receive(any(TestContext.class), anyLong())).thenReturn(1347 new DefaultMessage("<TestRequest xmlns:pfx=\"http://www.consol.de/schemas/test\"><Message>Hello World!</Message></TestRequest>")1348 .setHeader("operation", "foo"));1349 MockTestRunner builder = new MockTestRunner(getClass().getSimpleName(), context) {1350 @Override1351 public void execute() {1352 receive(action -> action.endpoint(messageEndpoint)1353 .payload("<TestRequest xmlns:pfx=\"http://www.consol.de/schemas/test\"><Message>Hello World!</Message></TestRequest>")1354 .validateNamespace("pfx", "http://www.consol.de/schemas/test"));1355 }1356 };1357 TestCase test = builder.getTestCase();1358 Assert.assertEquals(test.getActionCount(), 1);1359 Assert.assertEquals(test.getActions().get(0).getClass(), ReceiveMessageAction.class);1360 ReceiveMessageAction action = ((ReceiveMessageAction)test.getActions().get(0));1361 Assert.assertEquals(action.getName(), "receive");1362 Assert.assertEquals(action.getMessageType(), MessageType.XML.name());1363 Assert.assertEquals(action.getEndpoint(), messageEndpoint);1364 Assert.assertEquals(action.getValidationContexts().size(), 2);1365 Assert.assertTrue(action.getValidationContexts().stream().anyMatch(HeaderValidationContext.class::isInstance));1366 Assert.assertTrue(action.getValidationContexts().stream().anyMatch(XmlMessageValidationContext.class::isInstance));1367 XmlMessageValidationContext validationContext = action.getValidationContexts().stream()1368 .filter(XmlMessageValidationContext.class::isInstance).findFirst()1369 .map(XmlMessageValidationContext.class::cast)1370 .orElseThrow(() -> new AssertionError("Missing validation context"));1371 Assert.assertTrue(action.getMessageBuilder() instanceof DefaultMessageBuilder);1372 Assert.assertEquals(((DefaultMessageBuilder)action.getMessageBuilder()).buildMessagePayload(context, action.getMessageType()),1373 "<TestRequest xmlns:pfx=\"http://www.consol.de/schemas/test\"><Message>Hello World!</Message></TestRequest>");1374 Assert.assertEquals(validationContext.getControlNamespaces().get("pfx"), "http://www.consol.de/schemas/test");1375 }1376 @Test1377 public void testReceiveBuilderWithXpathExpressions() {1378 reset(messageEndpoint, messageConsumer, configuration);1379 when(messageEndpoint.createConsumer()).thenReturn(messageConsumer);1380 when(messageEndpoint.getEndpointConfiguration()).thenReturn(configuration);1381 when(configuration.getTimeout()).thenReturn(100L);1382 when(messageEndpoint.getActor()).thenReturn(null);1383 when(messageConsumer.receive(any(TestContext.class), anyLong())).thenReturn(1384 new DefaultMessage("<TestRequest><Message lang=\"ENG\">Hello World!</Message><Operation>SayHello</Operation></TestRequest>")1385 .setHeader("operation", "sayHello"));1386 MockTestRunner builder = new MockTestRunner(getClass().getSimpleName(), context) {1387 @Override1388 public void execute() {1389 receive(action -> action.endpoint(messageEndpoint)1390 .payload("<TestRequest><Message lang=\"ENG\">Hello World!</Message><Operation>SayHello</Operation></TestRequest>")1391 .validate("TestRequest.Message", "Hello World!")1392 .validate("TestRequest.Operation", "SayHello"));1393 }1394 };1395 TestCase test = builder.getTestCase();1396 Assert.assertEquals(test.getActionCount(), 1);1397 Assert.assertEquals(test.getActions().get(0).getClass(), ReceiveMessageAction.class);1398 ReceiveMessageAction action = ((ReceiveMessageAction)test.getActions().get(0));1399 Assert.assertEquals(action.getName(), "receive");1400 Assert.assertEquals(action.getMessageType(), MessageType.XML.name());1401 Assert.assertEquals(action.getEndpoint(), messageEndpoint);1402 Assert.assertEquals(action.getValidationContexts().size(), 2);1403 Assert.assertTrue(action.getValidationContexts().stream().anyMatch(HeaderValidationContext.class::isInstance));1404 Assert.assertTrue(action.getValidationContexts().stream().anyMatch(XpathMessageValidationContext.class::isInstance));1405 XpathMessageValidationContext validationContext = action.getValidationContexts().stream()1406 .filter(XpathMessageValidationContext.class::isInstance).findFirst()1407 .map(XpathMessageValidationContext.class::cast)1408 .orElseThrow(() -> new AssertionError("Missing validation context"));1409 Assert.assertTrue(action.getMessageBuilder() instanceof DefaultMessageBuilder);1410 Assert.assertEquals(validationContext.getXpathExpressions().size(), 2L);1411 Assert.assertEquals(validationContext.getXpathExpressions().get("TestRequest.Message"), "Hello World!");1412 Assert.assertEquals(validationContext.getXpathExpressions().get("TestRequest.Operation"), "SayHello");1413 }1414 @Test1415 public void testReceiveBuilderWithJsonPathExpressions() {1416 reset(messageEndpoint, messageConsumer, configuration);1417 when(messageEndpoint.createConsumer()).thenReturn(messageConsumer);1418 when(messageEndpoint.getEndpointConfiguration()).thenReturn(configuration);1419 when(configuration.getTimeout()).thenReturn(100L);1420 when(messageEndpoint.getActor()).thenReturn(null);1421 when(messageConsumer.receive(any(TestContext.class), anyLong())).thenReturn(1422 new DefaultMessage("{\"text\":\"Hello World!\", \"person\":{\"name\":\"John\",\"surname\":\"Doe\",\"active\": true}, \"index\":5, \"id\":\"x123456789x\"}")1423 .setHeader("operation", "sayHello"));1424 MockTestRunner builder = new MockTestRunner(getClass().getSimpleName(), context) {1425 @Override1426 public void execute() {1427 receive(action -> action.endpoint(messageEndpoint)1428 .messageType(MessageType.JSON)1429 .payload("{\"text\":\"Hello World!\", \"person\":{\"name\":\"John\",\"surname\":\"Doe\",\"active\": true}, \"index\":5, \"id\":\"x123456789x\"}")1430 .validate("$.person.name", "John")1431 .validate("$.person.active", true)1432 .validate("$.id", anyOf(containsString("123456789"), nullValue()))1433 .validate("$.text", "Hello World!")1434 .validate("$.index", 5));1435 }1436 };1437 TestCase test = builder.getTestCase();1438 Assert.assertEquals(test.getActionCount(), 1);1439 Assert.assertEquals(test.getActions().get(0).getClass(), ReceiveMessageAction.class);1440 ReceiveMessageAction action = ((ReceiveMessageAction)test.getActions().get(0));1441 Assert.assertEquals(action.getName(), "receive");1442 Assert.assertEquals(action.getMessageType(), MessageType.JSON.name());1443 Assert.assertEquals(action.getEndpoint(), messageEndpoint);1444 Assert.assertEquals(action.getValidationContexts().size(), 3);1445 Assert.assertTrue(action.getValidationContexts().stream().anyMatch(HeaderValidationContext.class::isInstance));1446 Assert.assertTrue(action.getValidationContexts().stream().anyMatch(JsonMessageValidationContext.class::isInstance));1447 Assert.assertTrue(action.getValidationContexts().stream().anyMatch(JsonPathMessageValidationContext.class::isInstance));1448 JsonPathMessageValidationContext validationContext = action.getValidationContexts().stream()1449 .filter(JsonPathMessageValidationContext.class::isInstance).findFirst()1450 .map(JsonPathMessageValidationContext.class::cast)1451 .orElseThrow(() -> new AssertionError("Missing validation context"));1452 Assert.assertTrue(action.getMessageBuilder() instanceof DefaultMessageBuilder);1453 Assert.assertEquals(validationContext.getJsonPathExpressions().size(), 5L);1454 Assert.assertEquals(validationContext.getJsonPathExpressions().get("$.person.name"), "John");1455 Assert.assertEquals(validationContext.getJsonPathExpressions().get("$.person.active"), true);1456 Assert.assertEquals(validationContext.getJsonPathExpressions().get("$.text"), "Hello World!");1457 Assert.assertEquals(validationContext.getJsonPathExpressions().get("$.index"), 5);1458 Assert.assertEquals(validationContext.getJsonPathExpressions().get("$.id").getClass(), AnyOf.class);1459 }1460 @Test(expectedExceptions = TestCaseFailedException.class)1461 public void testReceiveBuilderWithJsonPathExpressionsFailure() {1462 reset(messageEndpoint, messageConsumer, configuration);1463 when(messageEndpoint.createConsumer()).thenReturn(messageConsumer);1464 when(messageEndpoint.getEndpointConfiguration()).thenReturn(configuration);1465 when(configuration.getTimeout()).thenReturn(100L);1466 when(messageEndpoint.getActor()).thenReturn(null);1467 when(messageConsumer.receive(any(TestContext.class), anyLong())).thenReturn(1468 new DefaultMessage("{\"text\":\"Hello World!\", \"person\":{\"name\":\"John\",\"surname\":\"Doe\"}, \"index\":5, \"id\":\"x123456789x\"}")1469 .setHeader("operation", "sayHello"));1470 new MockTestRunner(getClass().getSimpleName(), context) {1471 @Override1472 public void execute() {1473 receive(action -> action.endpoint(messageEndpoint)1474 .messageType(MessageType.JSON)1475 .payload("{\"text\":\"Hello World!\", \"person\":{\"name\":\"John\",\"surname\":\"Doe\"}, \"index\":5, \"id\":\"x123456789x\"}")1476 .validate("$.person.name", "John")1477 .validate("$.text", "Hello Citrus!"));1478 }1479 };1480 }1481 @Test(expectedExceptions = TestCaseFailedException.class)1482 public void testReceiveBuilderWithJsonValidationFailure() {1483 reset(messageEndpoint, messageConsumer, configuration);1484 when(messageEndpoint.createConsumer()).thenReturn(messageConsumer);1485 when(messageEndpoint.getEndpointConfiguration()).thenReturn(configuration);1486 when(configuration.getTimeout()).thenReturn(100L);1487 when(messageEndpoint.getActor()).thenReturn(null);1488 when(messageConsumer.receive(any(TestContext.class), anyLong())).thenReturn(1489 new DefaultMessage("{\"text\":\"Hello World!\", \"person\":{\"name\":\"John\",\"surname\":\"Doe\"}, \"index\":5, \"id\":\"x123456789x\"}")1490 .setHeader("operation", "sayHello"));1491 new MockTestRunner(getClass().getSimpleName(), context) {1492 @Override1493 public void execute() {1494 receive(action -> action.endpoint(messageEndpoint)1495 .messageType(MessageType.JSON)1496 .payload("{\"text\":\"Hello Citrus!\", \"person\":{\"name\":\"John\",\"surname\":\"Doe\"}, \"index\":5, \"id\":\"x123456789x\"}")1497 .validate("$.person.name", "John")1498 .validate("$.text", "Hello World!"));1499 }1500 };1501 }1502 @Test1503 public void testReceiveBuilderWithIgnoreElementsXpath() {1504 reset(messageEndpoint, messageConsumer, configuration);1505 when(messageEndpoint.createConsumer()).thenReturn(messageConsumer);1506 when(messageEndpoint.getEndpointConfiguration()).thenReturn(configuration);1507 when(configuration.getTimeout()).thenReturn(100L);1508 when(messageEndpoint.getActor()).thenReturn(null);1509 when(messageConsumer.receive(any(TestContext.class), anyLong())).thenReturn(1510 new DefaultMessage("<TestRequest><Message>Hello World!</Message></TestRequest>")1511 .setHeader("operation", "sayHello"));1512 MockTestRunner builder = new MockTestRunner(getClass().getSimpleName(), context) {1513 @Override1514 public void execute() {1515 receive(action -> action.endpoint(messageEndpoint)1516 .payload("<TestRequest><Message>?</Message></TestRequest>")1517 .ignore("TestRequest.Message"));1518 }1519 };1520 TestCase test = builder.getTestCase();1521 Assert.assertEquals(test.getActionCount(), 1);1522 Assert.assertEquals(test.getActions().get(0).getClass(), ReceiveMessageAction.class);1523 ReceiveMessageAction action = ((ReceiveMessageAction)test.getActions().get(0));1524 Assert.assertEquals(action.getName(), "receive");1525 Assert.assertEquals(action.getMessageType(), MessageType.XML.name());1526 Assert.assertEquals(action.getEndpoint(), messageEndpoint);1527 Assert.assertEquals(action.getValidationContexts().size(), 2);1528 Assert.assertTrue(action.getValidationContexts().stream().anyMatch(HeaderValidationContext.class::isInstance));1529 Assert.assertTrue(action.getValidationContexts().stream().anyMatch(XmlMessageValidationContext.class::isInstance));1530 XmlMessageValidationContext validationContext = action.getValidationContexts().stream()1531 .filter(XmlMessageValidationContext.class::isInstance).findFirst()1532 .map(XmlMessageValidationContext.class::cast)1533 .orElseThrow(() -> new AssertionError("Missing validation context"));1534 Assert.assertTrue(action.getMessageBuilder() instanceof DefaultMessageBuilder);1535 Assert.assertEquals(((DefaultMessageBuilder)action.getMessageBuilder()).buildMessagePayload(context, action.getMessageType()), "<TestRequest><Message>?</Message></TestRequest>");1536 Assert.assertEquals(validationContext.getIgnoreExpressions().size(), 1L);1537 Assert.assertEquals(validationContext.getIgnoreExpressions().iterator().next(), "TestRequest.Message");1538 }1539 @Test1540 public void testReceiveBuilderWithIgnoreElementsJson() {1541 reset(messageEndpoint, messageConsumer, configuration);1542 when(messageEndpoint.createConsumer()).thenReturn(messageConsumer);1543 when(messageEndpoint.getEndpointConfiguration()).thenReturn(configuration);1544 when(configuration.getTimeout()).thenReturn(100L);1545 when(messageEndpoint.getActor()).thenReturn(null);1546 when(messageConsumer.receive(any(TestContext.class), anyLong())).thenReturn(1547 new DefaultMessage("{\"text\":\"Hello World!\", \"person\":{\"name\":\"John\",\"surname\":\"Doe\"}, \"index\":5, \"id\":\"x123456789x\"}")1548 .setHeader("operation", "sayHello"));1549 MockTestRunner builder = new MockTestRunner(getClass().getSimpleName(), context) {1550 @Override1551 public void execute() {1552 receive(action -> action.endpoint(messageEndpoint)1553 .messageType(MessageType.JSON)1554 .payload("{\"text\":\"?\", \"person\":{\"name\":\"John\",\"surname\":\"?\"}, \"index\":0, \"id\":\"x123456789x\"}")1555 .ignore("$..text")1556 .ignore("$.person.surname")1557 .ignore("$.index"));1558 }1559 };1560 TestCase test = builder.getTestCase();1561 Assert.assertEquals(test.getActionCount(), 1);1562 Assert.assertEquals(test.getActions().get(0).getClass(), ReceiveMessageAction.class);1563 ReceiveMessageAction action = ((ReceiveMessageAction)test.getActions().get(0));1564 Assert.assertEquals(action.getName(), "receive");1565 Assert.assertEquals(action.getMessageType(), MessageType.JSON.name());1566 Assert.assertEquals(action.getEndpoint(), messageEndpoint);1567 Assert.assertEquals(action.getValidationContexts().size(), 2);1568 Assert.assertTrue(action.getValidationContexts().stream().anyMatch(HeaderValidationContext.class::isInstance));1569 Assert.assertTrue(action.getValidationContexts().stream().anyMatch(JsonMessageValidationContext.class::isInstance));1570 JsonMessageValidationContext validationContext = action.getValidationContexts().stream()1571 .filter(JsonMessageValidationContext.class::isInstance).findFirst()1572 .map(JsonMessageValidationContext.class::cast)1573 .orElseThrow(() -> new AssertionError("Missing validation context"));1574 Assert.assertTrue(action.getMessageBuilder() instanceof DefaultMessageBuilder);1575 Assert.assertEquals(((DefaultMessageBuilder) action.getMessageBuilder()).buildMessagePayload(context, action.getMessageType()), "{\"text\":\"?\", \"person\":{\"name\":\"John\",\"surname\":\"?\"}, \"index\":0, \"id\":\"x123456789x\"}");1576 Assert.assertEquals(validationContext.getIgnoreExpressions().size(), 3L);1577 Assert.assertTrue(validationContext.getIgnoreExpressions().contains("$..text"));1578 Assert.assertTrue(validationContext.getIgnoreExpressions().contains("$.person.surname"));1579 Assert.assertTrue(validationContext.getIgnoreExpressions().contains("$.index"));1580 }1581 @Test1582 public void testReceiveBuilderWithSchema() throws IOException {1583 XsdSchema schema = applicationContext.getBean("testSchema", XsdSchema.class);1584 XmlValidator validator = Mockito.mock(XmlValidator.class);1585 reset(referenceResolver, schema, validator, messageEndpoint, messageConsumer, configuration);1586 when(messageEndpoint.createConsumer()).thenReturn(messageConsumer);1587 when(messageEndpoint.getEndpointConfiguration()).thenReturn(configuration);1588 when(configuration.getTimeout()).thenReturn(100L);1589 when(messageEndpoint.getActor()).thenReturn(null);1590 when(messageConsumer.receive(any(TestContext.class), anyLong())).thenReturn(1591 new DefaultMessage("<TestRequest xmlns=\"http://citrusframework.org/test\"><Message>Hello World!</Message></TestRequest>")1592 .setHeader("operation", "sayHello"));1593 when(referenceResolver.resolve(TestContext.class)).thenReturn(applicationContext.getBean(TestContext.class));1594 when(referenceResolver.resolve(TestActionListeners.class)).thenReturn(new TestActionListeners());1595 when(referenceResolver.resolveAll(SequenceBeforeTest.class)).thenReturn(new HashMap<>());1596 when(referenceResolver.resolveAll(SequenceAfterTest.class)).thenReturn(new HashMap<>());1597 when(schema.createValidator()).thenReturn(validator);1598 when(validator.validate(any(Source.class))).thenReturn(new SAXParseException[] {});1599 MockTestRunner builder = new MockTestRunner(getClass().getSimpleName(), context) {1600 @Override1601 public void execute() {1602 receive(action -> action.endpoint(messageEndpoint)1603 .payload("<TestRequest xmlns=\"http://citrusframework.org/test\"><Message>Hello World!</Message></TestRequest>")1604 .xsd("testSchema"));1605 }1606 };1607 TestCase test = builder.getTestCase();1608 Assert.assertEquals(test.getActionCount(), 1);1609 Assert.assertEquals(test.getActions().get(0).getClass(), ReceiveMessageAction.class);1610 ReceiveMessageAction action = ((ReceiveMessageAction)test.getActions().get(0));1611 Assert.assertEquals(action.getName(), "receive");1612 Assert.assertEquals(action.getMessageType(), MessageType.XML.name());1613 Assert.assertEquals(action.getEndpoint(), messageEndpoint);1614 Assert.assertEquals(action.getValidationContexts().size(), 2);1615 Assert.assertTrue(action.getValidationContexts().stream().anyMatch(HeaderValidationContext.class::isInstance));1616 Assert.assertTrue(action.getValidationContexts().stream().anyMatch(XmlMessageValidationContext.class::isInstance));1617 XmlMessageValidationContext validationContext = action.getValidationContexts().stream()1618 .filter(XmlMessageValidationContext.class::isInstance).findFirst()1619 .map(XmlMessageValidationContext.class::cast)1620 .orElseThrow(() -> new AssertionError("Missing validation context"));1621 Assert.assertTrue(action.getMessageBuilder() instanceof DefaultMessageBuilder);1622 Assert.assertEquals(((DefaultMessageBuilder) action.getMessageBuilder()).buildMessagePayload(context, action.getMessageType()), "<TestRequest xmlns=\"http://citrusframework.org/test\"><Message>Hello World!</Message></TestRequest>");1623 Assert.assertEquals(validationContext.getSchema(), "testSchema");1624 }1625 @Test1626 public void testReceiveBuilderWithSchemaRepository() throws IOException {1627 XsdSchema schema = applicationContext.getBean("testSchema", XsdSchema.class);1628 reset(schema, messageEndpoint, messageConsumer, configuration);1629 when(messageEndpoint.createConsumer()).thenReturn(messageConsumer);1630 when(messageEndpoint.getEndpointConfiguration()).thenReturn(configuration);1631 when(configuration.getTimeout()).thenReturn(100L);1632 when(messageEndpoint.getActor()).thenReturn(null);1633 when(messageConsumer.receive(any(TestContext.class), anyLong())).thenReturn(1634 new DefaultMessage("<TestRequest xmlns=\"http://citrusframework.org/test\"><Message>Hello World!</Message></TestRequest>")1635 .setHeader("operation", "sayHello"));1636 when(schema.getTargetNamespace()).thenReturn("http://citrusframework.org/test");1637 when(schema.getSource()).thenReturn(new StringSource("<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\"\n" +1638 " xmlns=\"http://citrusframework.org/test\"\n" +1639 " targetNamespace=\"http://citrusframework.org/test\"\n" +1640 " elementFormDefault=\"qualified\"\n" +1641 " attributeFormDefault=\"unqualified\">\n" +1642 " <xs:element name=\"TestRequest\">\n" +1643 " <xs:complexType>\n" +1644 " <xs:sequence>\n" +1645 " <xs:element name=\"Message\" type=\"xs:string\"/>\n" +1646 " </xs:sequence>\n" +1647 " </xs:complexType>\n" +1648 " </xs:element></xs:schema>"));1649 MockTestRunner builder = new MockTestRunner(getClass().getSimpleName(), context) {1650 @Override1651 public void execute() {1652 receive(action -> action.endpoint(messageEndpoint)1653 .payload("<TestRequest xmlns=\"http://citrusframework.org/test\"><Message>Hello World!</Message></TestRequest>")1654 .xsdSchemaRepository("customSchemaRepository"));1655 }1656 };1657 TestCase test = builder.getTestCase();1658 Assert.assertEquals(test.getActionCount(), 1);1659 Assert.assertEquals(test.getActions().get(0).getClass(), ReceiveMessageAction.class);1660 ReceiveMessageAction action = ((ReceiveMessageAction)test.getActions().get(0));1661 Assert.assertEquals(action.getName(), "receive");1662 Assert.assertEquals(action.getMessageType(), MessageType.XML.name());1663 Assert.assertEquals(action.getEndpoint(), messageEndpoint);1664 Assert.assertEquals(action.getValidationContexts().size(), 2);1665 Assert.assertTrue(action.getValidationContexts().stream().anyMatch(HeaderValidationContext.class::isInstance));1666 Assert.assertTrue(action.getValidationContexts().stream().anyMatch(XmlMessageValidationContext.class::isInstance));1667 XmlMessageValidationContext validationContext = action.getValidationContexts().stream()1668 .filter(XmlMessageValidationContext.class::isInstance).findFirst()1669 .map(XmlMessageValidationContext.class::cast)1670 .orElseThrow(() -> new AssertionError("Missing validation context"));1671 Assert.assertTrue(action.getMessageBuilder() instanceof DefaultMessageBuilder);1672 Assert.assertEquals(((DefaultMessageBuilder)action.getMessageBuilder()).buildMessagePayload(context, action.getMessageType()), "<TestRequest xmlns=\"http://citrusframework.org/test\"><Message>Hello World!</Message></TestRequest>");1673 Assert.assertEquals(validationContext.getSchemaRepository(), "customSchemaRepository");1674 }1675 @Test1676 public void testReceiveBuilderWithJsonSchemaRepository() throws ProcessingException {1677 SimpleJsonSchema schema = applicationContext.getBean("jsonTestSchema", SimpleJsonSchema.class);1678 reset(schema, messageEndpoint, messageConsumer, configuration);1679 when(messageEndpoint.createConsumer()).thenReturn(messageConsumer);1680 when(messageEndpoint.getEndpointConfiguration()).thenReturn(configuration);1681 when(configuration.getTimeout()).thenReturn(100L);1682 when(messageEndpoint.getActor()).thenReturn(null);1683 when(messageConsumer.receive(any(TestContext.class), anyLong())).thenReturn(1684 new DefaultMessage("{}")1685 .setHeader("operation", "sayHello"));1686 JsonSchema jsonSchemaMock = mock(JsonSchema.class);1687 when(jsonSchemaMock.validate(any())).thenReturn(new GraciousProcessingReport(true));1688 when(schema.getSchema()).thenReturn(jsonSchemaMock);1689 MockTestRunner builder = new MockTestRunner(getClass().getSimpleName(), context) {1690 @Override1691 public void execute() {1692 receive(action -> action.endpoint(messageEndpoint)1693 .payload("{}")1694 .jsonSchemaRepository("customJsonSchemaRepository"));1695 }1696 };1697 TestCase test = builder.getTestCase();1698 Assert.assertEquals(test.getActionCount(), 1);1699 Assert.assertEquals(test.getActions().get(0).getClass(), ReceiveMessageAction.class);1700 ReceiveMessageAction action = ((ReceiveMessageAction)test.getActions().get(0));1701 Assert.assertEquals(action.getName(), "receive");1702 Assert.assertEquals(action.getEndpoint(), messageEndpoint);1703 Assert.assertEquals(action.getValidationContexts().size(), 2);1704 Assert.assertTrue(action.getValidationContexts().stream().anyMatch(HeaderValidationContext.class::isInstance));1705 Assert.assertTrue(action.getValidationContexts().stream().anyMatch(JsonMessageValidationContext.class::isInstance));1706 JsonMessageValidationContext validationContext = action.getValidationContexts().stream()1707 .filter(JsonMessageValidationContext.class::isInstance).findFirst()1708 .map(JsonMessageValidationContext.class::cast)1709 .orElseThrow(() -> new AssertionError("Missing validation context"));1710 Assert.assertTrue(action.getMessageBuilder() instanceof DefaultMessageBuilder);1711 Assert.assertEquals(((DefaultMessageBuilder)action.getMessageBuilder()).buildMessagePayload(context, action.getMessageType()), "{}");1712 Assert.assertEquals(validationContext.getSchemaRepository(), "customJsonSchemaRepository");1713 }1714 @Test1715 public void testReceiveBuilderWithJsonSchema() throws ProcessingException {1716 SimpleJsonSchema schema = applicationContext.getBean("jsonTestSchema", SimpleJsonSchema.class);1717 reset(schema, messageEndpoint, messageConsumer, configuration);1718 when(messageEndpoint.createConsumer()).thenReturn(messageConsumer);1719 when(messageEndpoint.getEndpointConfiguration()).thenReturn(configuration);1720 when(configuration.getTimeout()).thenReturn(100L);1721 when(messageEndpoint.getActor()).thenReturn(null);1722 when(messageConsumer.receive(any(TestContext.class), anyLong())).thenReturn(1723 new DefaultMessage("{}")1724 .setHeader("operation", "sayHello"));1725 JsonSchema jsonSchemaMock = mock(JsonSchema.class);1726 when(jsonSchemaMock.validate(any())).thenReturn(new GraciousProcessingReport(true));1727 when(schema.getSchema()).thenReturn(jsonSchemaMock);1728 MockTestRunner builder = new MockTestRunner(getClass().getSimpleName(), context) {1729 @Override...

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Citrus automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used method in ReceiveMessageTestRunnerTest

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful