Best Citrus code snippet using com.consol.citrus.dsl.design.ReceiveMessageTestDesignerTest.testReceiveBuilderWithValidatonScript
Source:ReceiveMessageTestDesignerTest.java
...990 Assert.assertTrue(((PayloadTemplateMessageBuilder)action.getMessageBuilder()).getMessageHeaders().containsKey("operation"));991 }992 993 @Test994 public void testReceiveBuilderWithValidatonScript() {995 final GroovyJsonMessageValidator validator = new GroovyJsonMessageValidator();996 997 reset(applicationContextMock);998 when(applicationContextMock.getBean("groovyMessageValidator", MessageValidator.class)).thenReturn(validator);999 when(applicationContextMock.getBean(TestActionListeners.class)).thenReturn(new TestActionListeners());1000 when(applicationContextMock.getBeansOfType(SequenceBeforeTest.class)).thenReturn(new HashMap<String, SequenceBeforeTest>());1001 when(applicationContextMock.getBeansOfType(SequenceAfterTest.class)).thenReturn(new HashMap<String, SequenceAfterTest>());1002 MockTestDesigner builder = new MockTestDesigner(applicationContextMock, context) {1003 @Override1004 public void configure() {1005 receive(messageEndpoint)1006 .messageType(MessageType.JSON)1007 .validateScript("assert true")1008 .validator("groovyMessageValidator");1009 }1010 };1011 builder.configure();1012 TestCase test = builder.getTestCase();1013 Assert.assertEquals(test.getActionCount(), 1);1014 Assert.assertEquals(test.getActions().get(0).getClass(), DelegatingTestAction.class);1015 Assert.assertEquals(((DelegatingTestAction)test.getActions().get(0)).getDelegate().getClass(), ReceiveMessageAction.class);1016 1017 ReceiveMessageAction action = (ReceiveMessageAction) ((DelegatingTestAction)test.getActions().get(0)).getDelegate();1018 Assert.assertEquals(action.getName(), "receive");1019 1020 Assert.assertEquals(action.getEndpoint(), messageEndpoint);1021 Assert.assertEquals(action.getMessageType(), MessageType.JSON.name());1022 Assert.assertEquals(action.getValidators().size(), 1L);1023 Assert.assertEquals(action.getValidators().get(0), validator);1024 Assert.assertEquals(action.getValidationContexts().size(), 4L);1025 Assert.assertEquals(action.getValidationContexts().get(0).getClass(), HeaderValidationContext.class);1026 Assert.assertEquals(action.getValidationContexts().get(1).getClass(), XmlMessageValidationContext.class);1027 Assert.assertEquals(action.getValidationContexts().get(2).getClass(), JsonMessageValidationContext.class);1028 Assert.assertEquals(action.getValidationContexts().get(3).getClass(), ScriptValidationContext.class);1029 ScriptValidationContext validationContext = (ScriptValidationContext) action.getValidationContexts().get(3);1030 1031 Assert.assertEquals(validationContext.getScriptType(), ScriptTypes.GROOVY);1032 Assert.assertEquals(validationContext.getValidationScript(), "assert true");1033 Assert.assertNull(validationContext.getValidationScriptResourcePath());1034 }1035 1036 @Test1037 public void testReceiveBuilderWithValidatonScriptResource() throws IOException {1038 final GroovyJsonMessageValidator validator = new GroovyJsonMessageValidator();1039 1040 reset(applicationContextMock);1041 when(applicationContextMock.getBean("groovyMessageValidator", MessageValidator.class)).thenReturn(validator);1042 when(applicationContextMock.getBean(TestActionListeners.class)).thenReturn(new TestActionListeners());1043 when(applicationContextMock.getBeansOfType(SequenceBeforeTest.class)).thenReturn(new HashMap<String, SequenceBeforeTest>());1044 when(applicationContextMock.getBeansOfType(SequenceAfterTest.class)).thenReturn(new HashMap<String, SequenceAfterTest>());1045 MockTestDesigner builder = new MockTestDesigner(applicationContextMock, context) {1046 @Override1047 public void configure() {1048 receive(messageEndpoint)1049 .messageType(MessageType.JSON)1050 .validateScript(new ClassPathResource("com/consol/citrus/dsl/runner/validation.groovy"))1051 .validator("groovyMessageValidator");1052 }1053 };1054 builder.configure();1055 TestCase test = builder.getTestCase();1056 Assert.assertEquals(test.getActionCount(), 1);1057 Assert.assertEquals(test.getActions().get(0).getClass(), DelegatingTestAction.class);1058 Assert.assertEquals(((DelegatingTestAction)test.getActions().get(0)).getDelegate().getClass(), ReceiveMessageAction.class);1059 1060 ReceiveMessageAction action = (ReceiveMessageAction) ((DelegatingTestAction)test.getActions().get(0)).getDelegate();1061 Assert.assertEquals(action.getName(), "receive");1062 1063 Assert.assertEquals(action.getEndpoint(), messageEndpoint);1064 Assert.assertEquals(action.getMessageType(), MessageType.JSON.name());1065 Assert.assertEquals(action.getValidators().size(), 1L);1066 Assert.assertEquals(action.getValidators().get(0), validator);1067 Assert.assertEquals(action.getValidationContexts().get(0).getClass(), HeaderValidationContext.class);1068 Assert.assertEquals(action.getValidationContexts().get(1).getClass(), XmlMessageValidationContext.class);1069 Assert.assertEquals(action.getValidationContexts().get(2).getClass(), JsonMessageValidationContext.class);1070 Assert.assertEquals(action.getValidationContexts().get(3).getClass(), ScriptValidationContext.class);1071 ScriptValidationContext validationContext = (ScriptValidationContext) action.getValidationContexts().get(3);1072 1073 Assert.assertEquals(validationContext.getScriptType(), ScriptTypes.GROOVY);1074 Assert.assertEquals(validationContext.getValidationScript(), "assert json.message == 'Hello Citrus!'");1075 Assert.assertNull(validationContext.getValidationScriptResourcePath());1076 }1077 @Test1078 public void testReceiveBuilderWithValidatonScriptResourcePath() throws IOException {1079 final GroovyJsonMessageValidator validator = new GroovyJsonMessageValidator();1080 reset(applicationContextMock);1081 when(applicationContextMock.getBean("groovyMessageValidator", MessageValidator.class)).thenReturn(validator);1082 when(applicationContextMock.getBean(TestActionListeners.class)).thenReturn(new TestActionListeners());1083 when(applicationContextMock.getBeansOfType(SequenceBeforeTest.class)).thenReturn(new HashMap<String, SequenceBeforeTest>());1084 when(applicationContextMock.getBeansOfType(SequenceAfterTest.class)).thenReturn(new HashMap<String, SequenceAfterTest>());1085 MockTestDesigner builder = new MockTestDesigner(applicationContextMock, context) {1086 @Override1087 public void configure() {1088 receive(messageEndpoint)1089 .messageType(MessageType.JSON)1090 .validateScriptResource("/path/to/file/File.groovy")1091 .validator("groovyMessageValidator");1092 }1093 };1094 builder.configure();1095 TestCase test = builder.getTestCase();1096 Assert.assertEquals(test.getActionCount(), 1);1097 Assert.assertEquals(test.getActions().get(0).getClass(), DelegatingTestAction.class);1098 Assert.assertEquals(((DelegatingTestAction)test.getActions().get(0)).getDelegate().getClass(), ReceiveMessageAction.class);1099 ReceiveMessageAction action = (ReceiveMessageAction) ((DelegatingTestAction)test.getActions().get(0)).getDelegate();1100 Assert.assertEquals(action.getName(), "receive");1101 Assert.assertEquals(action.getEndpoint(), messageEndpoint);1102 Assert.assertEquals(action.getMessageType(), MessageType.JSON.name());1103 Assert.assertEquals(action.getValidators().size(), 1L);1104 Assert.assertEquals(action.getValidators().get(0), validator);1105 Assert.assertEquals(action.getValidationContexts().get(0).getClass(), HeaderValidationContext.class);1106 Assert.assertEquals(action.getValidationContexts().get(1).getClass(), XmlMessageValidationContext.class);1107 Assert.assertEquals(action.getValidationContexts().get(2).getClass(), JsonMessageValidationContext.class);1108 Assert.assertEquals(action.getValidationContexts().get(3).getClass(), ScriptValidationContext.class);1109 ScriptValidationContext validationContext = (ScriptValidationContext) action.getValidationContexts().get(3);1110 Assert.assertEquals(validationContext.getScriptType(), ScriptTypes.GROOVY);1111 Assert.assertEquals(validationContext.getValidationScript(), "");1112 Assert.assertEquals(validationContext.getValidationScriptResourcePath(), "/path/to/file/File.groovy");1113 }1114 1115 @Test1116 public void testReceiveBuilderWithValidatonScriptAndHeader() {1117 final GroovyJsonMessageValidator validator = new GroovyJsonMessageValidator();1118 1119 reset(applicationContextMock);1120 when(applicationContextMock.getBean("groovyMessageValidator", MessageValidator.class)).thenReturn(validator);1121 when(applicationContextMock.getBean(TestActionListeners.class)).thenReturn(new TestActionListeners());1122 when(applicationContextMock.getBeansOfType(SequenceBeforeTest.class)).thenReturn(new HashMap<String, SequenceBeforeTest>());1123 when(applicationContextMock.getBeansOfType(SequenceAfterTest.class)).thenReturn(new HashMap<String, SequenceAfterTest>());1124 MockTestDesigner builder = new MockTestDesigner(applicationContextMock, context) {1125 @Override1126 public void configure() {1127 receive(messageEndpoint)1128 .messageType(MessageType.JSON)1129 .validateScript("assert true")1130 .validator("groovyMessageValidator")...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!