Best Citrus code snippet using com.consol.citrus.validation.text.BinaryMessageValidator
Source:MessageValidatorRegistryTest.java
1/*2 * Copyright 2006-2014 the original author or authors.3 *4 * Licensed under the Apache License, Version 2.0 (the "License");5 * you may not use this file except in compliance with the License.6 * You may obtain a copy of the License at7 *8 * http://www.apache.org/licenses/LICENSE-2.09 *10 * Unless required by applicable law or agreed to in writing, software11 * distributed under the License is distributed on an "AS IS" BASIS,12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.13 * See the License for the specific language governing permissions and14 * limitations under the License.15 */16package com.consol.citrus.validation;17import java.util.HashMap;18import java.util.List;19import java.util.Map;20import java.util.Optional;21import com.consol.citrus.exceptions.CitrusRuntimeException;22import com.consol.citrus.message.DefaultMessage;23import com.consol.citrus.message.Message;24import com.consol.citrus.message.MessageType;25import com.consol.citrus.util.MessageUtils;26import com.consol.citrus.validation.context.SchemaValidationContext;27import com.consol.citrus.validation.context.ValidationContext;28import org.mockito.Mock;29import org.mockito.MockitoAnnotations;30import org.testng.Assert;31import org.testng.annotations.BeforeClass;32import org.testng.annotations.Test;33import javax.xml.validation.Schema;34import static org.mockito.ArgumentMatchers.any;35import static org.mockito.Mockito.when;36/**37 * @author Christoph Deppisch38 * @since 2.039 */40public class MessageValidatorRegistryTest {41 @Mock42 private MessageValidator<?> plainTextMessageValidator;43 @Mock44 private MessageValidator<?> xmlMessageValidator;45 @Mock46 private MessageValidator<?> xhtmlMessageValidator;47 @Mock48 private MessageValidator<?> jsonTextMessageValidator;49 @Mock50 private MessageValidator<?> jsonPathMessageValidator;51 @Mock52 private MessageValidator<?> groovyScriptMessageValidator;53 @Mock54 private MessageValidator<?> groovyJsonMessageValidator;55 @Mock56 private MessageValidator<?> groovyXmlMessageValidator;57 @Mock58 private MessageValidator<?> xPathMessageValidator;59 @Mock60 private MessageValidator<?> xhtmlXpathMessageValidator;61 @Mock62 private MessageValidator<?> binaryMessageValidator;63 @Mock64 private MessageValidator<?> binaryBase64MessageValidator;65 @Mock66 private MessageValidator<?> gzipBinaryBase64MessageValidator;67 @Mock68 private SchemaValidator<?> jsonSchemaValidator;69 @Mock70 private SchemaValidator<?> xmlSchemaValidator;71 private MessageValidatorRegistry messageValidatorRegistry = new MessageValidatorRegistry();72 @BeforeClass73 public void setupMocks() {74 MockitoAnnotations.openMocks(this);75 when(plainTextMessageValidator.supportsMessageType(any(String.class), any(Message.class))).thenAnswer(invocation -> invocation.getArgument(0).equals(MessageType.PLAINTEXT.name()));76 when(xmlMessageValidator.supportsMessageType(any(String.class), any(Message.class))).thenAnswer(invocation -> invocation.getArgument(0).equals(MessageType.XML.name()) && MessageUtils.hasXmlPayload(invocation.getArgument(1)));77 when(xhtmlMessageValidator.supportsMessageType(any(String.class), any(Message.class))).thenAnswer(invocation -> invocation.getArgument(0).equals(MessageType.XHTML.name()) && MessageUtils.hasXmlPayload(invocation.getArgument(1)));78 when(jsonTextMessageValidator.supportsMessageType(any(String.class), any(Message.class))).thenAnswer(invocation -> invocation.getArgument(0).equals(MessageType.JSON.name()) && MessageUtils.hasJsonPayload(invocation.getArgument(1)));79 when(jsonPathMessageValidator.supportsMessageType(any(String.class), any(Message.class))).thenAnswer(invocation -> invocation.getArgument(0).equals(MessageType.JSON.name()) && MessageUtils.hasJsonPayload(invocation.getArgument(1)));80 when(groovyScriptMessageValidator.supportsMessageType(any(String.class), any(Message.class))).thenAnswer(invocation -> invocation.getArgument(0).equals(MessageType.PLAINTEXT.name()));81 when(groovyJsonMessageValidator.supportsMessageType(any(String.class), any(Message.class))).thenAnswer(invocation -> invocation.getArgument(0).equals(MessageType.JSON.name()) && MessageUtils.hasJsonPayload(invocation.getArgument(1)));82 when(groovyXmlMessageValidator.supportsMessageType(any(String.class), any(Message.class))).thenAnswer(invocation -> invocation.getArgument(0).equals(MessageType.XML.name()) && MessageUtils.hasXmlPayload(invocation.getArgument(1)));83 when(xPathMessageValidator.supportsMessageType(any(String.class), any(Message.class))).thenAnswer(invocation -> invocation.getArgument(0).equals(MessageType.XML.name()) && MessageUtils.hasXmlPayload(invocation.getArgument(1)));84 when(xhtmlXpathMessageValidator.supportsMessageType(any(String.class), any(Message.class))).thenAnswer(invocation -> invocation.getArgument(0).equals(MessageType.XHTML.name()) && MessageUtils.hasXmlPayload(invocation.getArgument(1)));85 when(binaryMessageValidator.supportsMessageType(any(String.class), any(Message.class))).thenAnswer(invocation -> invocation.getArgument(0).equals(MessageType.BINARY.name()));86 when(binaryBase64MessageValidator.supportsMessageType(any(String.class), any(Message.class))).thenAnswer(invocation -> invocation.getArgument(0).equals(MessageType.BINARY_BASE64.name()));87 when(gzipBinaryBase64MessageValidator.supportsMessageType(any(String.class), any(Message.class))).thenAnswer(invocation -> invocation.getArgument(0).equals(MessageType.GZIP_BASE64.name()));88 // Schema validators89 when(jsonSchemaValidator.supportsMessageType(any(String.class), any(Message.class))).thenAnswer(invocation -> invocation.getArgument(0).equals(MessageType.JSON.name()));90 when(xmlSchemaValidator.supportsMessageType(any(String.class), any(Message.class))).thenAnswer(invocation -> invocation.getArgument(0).equals(MessageType.XML.name()));91 messageValidatorRegistry.addMessageValidator("xmlMessageValidator", xmlMessageValidator);92 messageValidatorRegistry.addMessageValidator("xPathMessageValidator", xPathMessageValidator);93 messageValidatorRegistry.addMessageValidator("groovyXmlMessageValidator", groovyXmlMessageValidator);94 messageValidatorRegistry.addMessageValidator("jsonTextMessageValidator", jsonTextMessageValidator);95 messageValidatorRegistry.addMessageValidator("jsonPathMessageValidator", jsonPathMessageValidator);96 messageValidatorRegistry.addMessageValidator("plainTextMessageValidator", plainTextMessageValidator);97 messageValidatorRegistry.addMessageValidator("headerMessageValidator", new DefaultMessageHeaderValidator());98 messageValidatorRegistry.addMessageValidator("binaryMessageValidator", binaryMessageValidator);99 messageValidatorRegistry.addMessageValidator("binaryBase64MessageValidator", binaryBase64MessageValidator);100 messageValidatorRegistry.addMessageValidator("gzipBinaryBase64MessageValidator", gzipBinaryBase64MessageValidator);101 messageValidatorRegistry.addMessageValidator("groovyJsonMessageValidator", groovyJsonMessageValidator);102 messageValidatorRegistry.addMessageValidator("groovyScriptMessageValidator", groovyScriptMessageValidator);103 messageValidatorRegistry.addMessageValidator("xhtmlMessageValidator", xhtmlMessageValidator);104 messageValidatorRegistry.addMessageValidator("xhtmlXpathMessageValidator", xhtmlXpathMessageValidator);105 messageValidatorRegistry.addSchemaValidator("jsonSchemaValidator", jsonSchemaValidator);106 messageValidatorRegistry.addSchemaValidator("xmlSchemaValidator", xmlSchemaValidator);107 }108 @Test109 public void testFindMessageValidators() throws Exception {110 MessageValidatorRegistry messageValidatorRegistry = new MessageValidatorRegistry();111 Map<String, MessageValidator<? extends ValidationContext>> messageValidators = new HashMap<>();112 messageValidators.put("plainTextMessageValidator", plainTextMessageValidator);113 messageValidatorRegistry.setMessageValidators(messageValidators);114 List<MessageValidator<? extends ValidationContext>> matchingValidators = messageValidatorRegistry.findMessageValidators(MessageType.PLAINTEXT.name(), new DefaultMessage(""));115 Assert.assertNotNull(matchingValidators);116 Assert.assertEquals(matchingValidators.size(), 1L);117 Assert.assertEquals(matchingValidators.get(0), plainTextMessageValidator);118 matchingValidators = messageValidatorRegistry.findMessageValidators(MessageType.PLAINTEXT.name(), new DefaultMessage("Hello"));119 Assert.assertNotNull(matchingValidators);120 Assert.assertEquals(matchingValidators.size(), 1L);121 Assert.assertEquals(matchingValidators.get(0), plainTextMessageValidator);122 matchingValidators = messageValidatorRegistry.findMessageValidators(MessageType.JSON.name(), new DefaultMessage(""));123 Assert.assertNotNull(matchingValidators);124 Assert.assertEquals(matchingValidators.size(), 1L);125 Assert.assertEquals(matchingValidators.get(0).getClass(), DefaultEmptyMessageValidator.class);126 messageValidatorRegistry.addMessageValidator("xmlMessageValidator", xmlMessageValidator);127 messageValidatorRegistry.addMessageValidator("groovyScriptMessageValidator", groovyScriptMessageValidator);128 matchingValidators = messageValidatorRegistry.findMessageValidators(MessageType.PLAINTEXT.name(), new DefaultMessage(""));129 Assert.assertNotNull(matchingValidators);130 Assert.assertEquals(matchingValidators.size(), 2L);131 Assert.assertEquals(matchingValidators.get(0), groovyScriptMessageValidator);132 Assert.assertEquals(matchingValidators.get(1), plainTextMessageValidator);133 matchingValidators = messageValidatorRegistry.findMessageValidators(MessageType.XML.name(), new DefaultMessage(""));134 Assert.assertNotNull(matchingValidators);135 Assert.assertEquals(matchingValidators.size(), 1L);136 Assert.assertEquals(matchingValidators.get(0), xmlMessageValidator);137 }138 @Test139 public void testMessageValidatorRegistryXmlConfig() throws Exception {140 //non XML message type141 List<MessageValidator<? extends ValidationContext>> matchingValidators = messageValidatorRegistry.findMessageValidators(MessageType.PLAINTEXT.name(), new DefaultMessage(""));142 Assert.assertNotNull(matchingValidators);143 Assert.assertEquals(matchingValidators.size(), 3L);144 Assert.assertEquals(matchingValidators.get(0), plainTextMessageValidator);145 Assert.assertEquals(matchingValidators.get(1).getClass(), DefaultMessageHeaderValidator.class);146 Assert.assertEquals(matchingValidators.get(2), groovyScriptMessageValidator);147 //XML message type and empty payload148 matchingValidators = messageValidatorRegistry.findMessageValidators(MessageType.XML.name(), new DefaultMessage(""));149 Assert.assertNotNull(matchingValidators);150 Assert.assertEquals(matchingValidators.size(), 4L);151 Assert.assertEquals(matchingValidators.get(0), xmlMessageValidator);152 Assert.assertEquals(matchingValidators.get(1), xPathMessageValidator);153 Assert.assertEquals(matchingValidators.get(2), groovyXmlMessageValidator);154 Assert.assertEquals(matchingValidators.get(3).getClass(), DefaultMessageHeaderValidator.class);155 //XML message type and non empty payload156 matchingValidators = messageValidatorRegistry.findMessageValidators(MessageType.XML.name(), new DefaultMessage("<id>12345</id>"));157 Assert.assertNotNull(matchingValidators);158 Assert.assertEquals(matchingValidators.size(), 4L);159 Assert.assertEquals(matchingValidators.get(0), xmlMessageValidator);160 Assert.assertEquals(matchingValidators.get(1), xPathMessageValidator);161 Assert.assertEquals(matchingValidators.get(2), groovyXmlMessageValidator);162 Assert.assertEquals(matchingValidators.get(3).getClass(), DefaultMessageHeaderValidator.class);163 }164 @Test165 public void testMessageValidatorRegistryJsonConfig() throws Exception {166 //JSON message type and empty payload167 List<MessageValidator<? extends ValidationContext>> matchingValidators = messageValidatorRegistry.findMessageValidators(MessageType.JSON.name(), new DefaultMessage(""));168 Assert.assertNotNull(matchingValidators);169 Assert.assertEquals(matchingValidators.size(), 4L);170 Assert.assertEquals(matchingValidators.get(0), jsonTextMessageValidator);171 Assert.assertEquals(matchingValidators.get(1), jsonPathMessageValidator);172 Assert.assertEquals(matchingValidators.get(2).getClass(), DefaultMessageHeaderValidator.class);173 Assert.assertEquals(matchingValidators.get(3), groovyJsonMessageValidator);174 //JSON message type and non empty payload175 matchingValidators = messageValidatorRegistry.findMessageValidators(MessageType.JSON.name(), new DefaultMessage("{ \"id\": 12345 }"));176 Assert.assertNotNull(matchingValidators);177 Assert.assertEquals(matchingValidators.size(), 4L);178 Assert.assertEquals(matchingValidators.get(0), jsonTextMessageValidator);179 Assert.assertEquals(matchingValidators.get(1), jsonPathMessageValidator);180 Assert.assertEquals(matchingValidators.get(2).getClass(), DefaultMessageHeaderValidator.class);181 Assert.assertEquals(matchingValidators.get(3), groovyJsonMessageValidator);182 }183 @Test184 public void testMessageValidatorRegistryPlaintextConfig() throws Exception {185 //Plaintext message type and empty payload186 List<MessageValidator<? extends ValidationContext>> matchingValidators = messageValidatorRegistry.findMessageValidators(MessageType.PLAINTEXT.name(), new DefaultMessage(""));187 Assert.assertNotNull(matchingValidators);188 Assert.assertEquals(matchingValidators.size(), 3L);189 Assert.assertEquals(matchingValidators.get(0), plainTextMessageValidator);190 Assert.assertEquals(matchingValidators.get(1).getClass(), DefaultMessageHeaderValidator.class);191 Assert.assertEquals(matchingValidators.get(2), groovyScriptMessageValidator);192 //Plaintext message type and non empty payload193 matchingValidators = messageValidatorRegistry.findMessageValidators(MessageType.PLAINTEXT.name(), new DefaultMessage("id=12345"));194 Assert.assertNotNull(matchingValidators);195 Assert.assertEquals(matchingValidators.size(), 3L);196 Assert.assertEquals(matchingValidators.get(0), plainTextMessageValidator);197 Assert.assertEquals(matchingValidators.get(1).getClass(), DefaultMessageHeaderValidator.class);198 Assert.assertEquals(matchingValidators.get(2), groovyScriptMessageValidator);199 }200 @Test201 public void testMessageValidatorRegistryFallback() throws Exception {202 List<MessageValidator<? extends ValidationContext>> matchingValidators = messageValidatorRegistry.findMessageValidators(MessageType.XML.name(), new DefaultMessage("{ \"id\": 12345 }"));203 Assert.assertNotNull(matchingValidators);204 Assert.assertEquals(matchingValidators.size(), 4L);205 Assert.assertEquals(matchingValidators.get(0), jsonTextMessageValidator);206 Assert.assertEquals(matchingValidators.get(1), jsonPathMessageValidator);207 Assert.assertEquals(matchingValidators.get(2).getClass(), DefaultMessageHeaderValidator.class);208 Assert.assertEquals(matchingValidators.get(3), groovyJsonMessageValidator);209 matchingValidators = messageValidatorRegistry.findMessageValidators(MessageType.JSON.name(), new DefaultMessage("<id>12345</id>"));210 Assert.assertNotNull(matchingValidators);211 Assert.assertEquals(matchingValidators.size(), 4L);212 Assert.assertEquals(matchingValidators.get(0), xmlMessageValidator);213 Assert.assertEquals(matchingValidators.get(1), xPathMessageValidator);214 Assert.assertEquals(matchingValidators.get(2), groovyXmlMessageValidator);215 Assert.assertEquals(matchingValidators.get(3).getClass(), DefaultMessageHeaderValidator.class);216 matchingValidators = messageValidatorRegistry.findMessageValidators(MessageType.PLAINTEXT.name(), new DefaultMessage("<id>12345</id>"));217 Assert.assertNotNull(matchingValidators);218 Assert.assertEquals(matchingValidators.size(), 3L);219 Assert.assertEquals(matchingValidators.get(0), plainTextMessageValidator);220 Assert.assertEquals(matchingValidators.get(1).getClass(), DefaultMessageHeaderValidator.class);221 Assert.assertEquals(matchingValidators.get(2), groovyScriptMessageValidator);222 matchingValidators = messageValidatorRegistry.findMessageValidators(MessageType.PLAINTEXT.name(), new DefaultMessage("{ \"id\": 12345 }"));223 Assert.assertNotNull(matchingValidators);224 Assert.assertEquals(matchingValidators.size(), 3L);225 Assert.assertEquals(matchingValidators.get(0), plainTextMessageValidator);226 Assert.assertEquals(matchingValidators.get(1).getClass(), DefaultMessageHeaderValidator.class);227 Assert.assertEquals(matchingValidators.get(2), groovyScriptMessageValidator);228 matchingValidators = messageValidatorRegistry.findMessageValidators(MessageType.XML.name(), new DefaultMessage("id=12345"));229 Assert.assertNotNull(matchingValidators);230 Assert.assertEquals(matchingValidators.size(), 3L);231 Assert.assertEquals(matchingValidators.get(0), plainTextMessageValidator);232 Assert.assertEquals(matchingValidators.get(1).getClass(), DefaultMessageHeaderValidator.class);233 Assert.assertEquals(matchingValidators.get(2), groovyScriptMessageValidator);234 matchingValidators = messageValidatorRegistry.findMessageValidators(MessageType.JSON.name(), new DefaultMessage("id=12345"));235 Assert.assertNotNull(matchingValidators);236 Assert.assertEquals(matchingValidators.size(), 3L);237 Assert.assertEquals(matchingValidators.get(0), plainTextMessageValidator);238 Assert.assertEquals(matchingValidators.get(1).getClass(), DefaultMessageHeaderValidator.class);239 Assert.assertEquals(matchingValidators.get(2), groovyScriptMessageValidator);240 }241 @Test242 public void shouldAddDefaultEmptyMessagePayloadValidator() {243 MessageValidatorRegistry messageValidatorRegistry = new MessageValidatorRegistry();244 List<MessageValidator<? extends ValidationContext>> matchingValidators = messageValidatorRegistry.findMessageValidators(MessageType.PLAINTEXT.name(), new DefaultMessage(""));245 Assert.assertNotNull(matchingValidators);246 Assert.assertEquals(matchingValidators.size(), 1L);247 Assert.assertEquals(matchingValidators.get(0).getClass(), DefaultEmptyMessageValidator.class);248 messageValidatorRegistry.addMessageValidator("jsonTextMessageValidator", jsonTextMessageValidator);249 messageValidatorRegistry.addMessageValidator("headerMessageValidator", new DefaultMessageHeaderValidator());250 matchingValidators = messageValidatorRegistry.findMessageValidators(MessageType.PLAINTEXT.name(), new DefaultMessage(""));251 Assert.assertNotNull(matchingValidators);252 Assert.assertEquals(matchingValidators.size(), 2L);253 Assert.assertEquals(matchingValidators.get(0).getClass(), DefaultMessageHeaderValidator.class);254 Assert.assertEquals(matchingValidators.get(1).getClass(), DefaultEmptyMessageValidator.class);255 matchingValidators = messageValidatorRegistry.findMessageValidators(MessageType.JSON.name(), new DefaultMessage("{}"));256 Assert.assertNotNull(matchingValidators);257 Assert.assertEquals(matchingValidators.size(), 2L);258 Assert.assertEquals(matchingValidators.get(0), jsonTextMessageValidator);259 Assert.assertEquals(matchingValidators.get(1).getClass(), DefaultMessageHeaderValidator.class);260 try {261 messageValidatorRegistry.findMessageValidators(MessageType.JSON.name(), new DefaultMessage("Hello"));262 Assert.fail("Missing exception due to no proper message validator found");263 } catch (CitrusRuntimeException e) {264 Assert.assertEquals(e.getMessage(), "Failed to find proper message validator for message");265 }266 messageValidatorRegistry.addMessageValidator("plainTextMessageValidator", plainTextMessageValidator);267 matchingValidators = messageValidatorRegistry.findMessageValidators(MessageType.JSON.name(), new DefaultMessage("Hello"));268 Assert.assertNotNull(matchingValidators);269 Assert.assertEquals(matchingValidators.size(), 2L);270 Assert.assertEquals(matchingValidators.get(0).getClass(), DefaultMessageHeaderValidator.class);271 Assert.assertEquals(matchingValidators.get(1), plainTextMessageValidator);272 }273 @Test274 public void testSchemaValidators() throws Exception {275 MessageValidatorRegistry messageValidatorRegistry = new MessageValidatorRegistry();276 Map<String, SchemaValidator<? extends SchemaValidationContext>> schemaValidators = new HashMap<>();277 schemaValidators.put("jsonSchema", jsonSchemaValidator);278 schemaValidators.put("xmlSchema", xmlSchemaValidator);279 messageValidatorRegistry.setSchemaValidators(schemaValidators);280 List<SchemaValidator<? extends SchemaValidationContext>> matchingValidators = messageValidatorRegistry.findSchemaValidators(MessageType.JSON.name(), new DefaultMessage(""));281 Assert.assertNotNull(matchingValidators);282 Assert.assertEquals(matchingValidators.size(), 1L);283 Assert.assertEquals(matchingValidators.get(0), jsonSchemaValidator);284 Optional<SchemaValidator<? extends SchemaValidationContext>> jsonSchema = messageValidatorRegistry.findSchemaValidator("jsonSchema");285 Assert.assertTrue(jsonSchema.isPresent());286 Assert.assertEquals(jsonSchema.get(), jsonSchemaValidator);287 }288}...
Source:MessageValidatorConfig.java
...35 private final XpathMessageValidator defaultXpathMessageValidator = new XpathMessageValidator();36 private final JsonTextMessageValidator defaultJsonMessageValidator = new JsonTextMessageValidator();37 private final JsonPathMessageValidator defaultJsonPathMessageValidator = new JsonPathMessageValidator();38 private final PlainTextMessageValidator defaultPlaintextMessageValidator = new PlainTextMessageValidator();39 private final BinaryMessageValidator defaultBinaryMessageValidator = new BinaryMessageValidator();40 private final BinaryBase64MessageValidator defaultBinaryBase64MessageValidator = new BinaryBase64MessageValidator();41 private final GzipBinaryBase64MessageValidator defaultGzipBinaryBase64MessageValidator = new GzipBinaryBase64MessageValidator();42 private final XhtmlMessageValidator defaultXhtmlMessageValidator = new XhtmlMessageValidator();43 private final XhtmlXpathMessageValidator defaultXhtmlXpathMessageValidator = new XhtmlXpathMessageValidator();44 private final GroovyXmlMessageValidator defaultGroovyXmlMessageValidator = new GroovyXmlMessageValidator();45 private final GroovyJsonMessageValidator defaultGroovyJsonMessageValidator = new GroovyJsonMessageValidator();46 private final GroovyScriptMessageValidator defaultGroovyTextMessageValidator = new GroovyScriptMessageValidator();47 @Bean(name = "defaultXmlMessageValidator")48 public DomXmlMessageValidator getDefaultXmlMessageValidator() {49 return defaultXmlMessageValidator;50 }51 @Bean(name = "defaultMessageHeaderValidator")52 public DefaultMessageHeaderValidator getDefaultMessageHeaderValidator() {53 return defaultMessageHeaderValidator;54 }55 @Bean(name = "defaultXpathMessageValidator")56 public XpathMessageValidator getDefaultXpathMessageValidator() {57 return defaultXpathMessageValidator;58 }59 @Bean(name = "defaultJsonMessageValidator")60 public JsonTextMessageValidator getDefaultJsonTextMessageValidator() {61 return defaultJsonMessageValidator;62 }63 @Bean(name = "defaultJsonPathMessageValidator")64 public JsonPathMessageValidator getDefaultJsonPathMessageValidator() {65 return defaultJsonPathMessageValidator;66 }67 @Bean(name = "defaultPlaintextMessageValidator")68 public PlainTextMessageValidator getDefaultPlainTextMessageValidator() {69 return defaultPlaintextMessageValidator;70 }71 @Bean(name = "defaultBinaryMessageValidator")72 public BinaryMessageValidator getDefaultBinaryMessageValidator() {73 return defaultBinaryMessageValidator;74 }75 @Bean(name = "defaultBinaryBase64MessageValidator")76 public BinaryBase64MessageValidator getDefaultBinaryBase64MessageValidator() {77 return defaultBinaryBase64MessageValidator;78 }79 @Bean(name = "defaultGzipBinaryBase64MessageValidator")80 public GzipBinaryBase64MessageValidator getDefaultGzipBinaryBase64MessageValidator() {81 return defaultGzipBinaryBase64MessageValidator;82 }83 @Bean(name = "defaultXhtmlMessageValidator")84 public XhtmlMessageValidator getDefaultXhtmlMessageValidator() {85 return defaultXhtmlMessageValidator;86 }87 @Bean(name = "defaultXhtmlXpathMessageValidator")88 public XhtmlXpathMessageValidator getDefaultXhtmlXpathMessageValidator() {89 return defaultXhtmlXpathMessageValidator;90 }91 @Bean(name = "defaultGroovyXmlMessageValidator")92 public GroovyXmlMessageValidator getDefaultGroovyXmlMessageValidator() {93 return defaultGroovyXmlMessageValidator;94 }95 @Bean(name = "defaultGroovyJsonMessageValidator")96 public GroovyJsonMessageValidator getDefaultGroovyJsonMessageValidator() {97 return defaultGroovyJsonMessageValidator;98 }99 @Bean(name = "defaultGroovyTextMessageValidator")100 public GroovyScriptMessageValidator getDefaultGroovyTextMessageValidator() {101 return defaultGroovyTextMessageValidator;102 }103 @Bean(name = MessageValidatorRegistry.BEAN_NAME)104 public MessageValidatorRegistry getMessageValidatorRegistry() {105 MessageValidatorRegistry citrusMessageValidatorRegistry = new MessageValidatorRegistry();106 citrusMessageValidatorRegistry.getMessageValidators().add(defaultXmlMessageValidator);107 citrusMessageValidatorRegistry.getMessageValidators().add(defaultXpathMessageValidator);108 citrusMessageValidatorRegistry.getMessageValidators().add(defaultGroovyXmlMessageValidator);109 citrusMessageValidatorRegistry.getMessageValidators().add(defaultJsonMessageValidator);110 citrusMessageValidatorRegistry.getMessageValidators().add(defaultJsonPathMessageValidator);111 citrusMessageValidatorRegistry.getMessageValidators().add(defaultPlaintextMessageValidator);112 citrusMessageValidatorRegistry.getMessageValidators().add(defaultMessageHeaderValidator);113 citrusMessageValidatorRegistry.getMessageValidators().add(defaultBinaryMessageValidator);114 citrusMessageValidatorRegistry.getMessageValidators().add(defaultBinaryBase64MessageValidator);115 citrusMessageValidatorRegistry.getMessageValidators().add(defaultGzipBinaryBase64MessageValidator);116 citrusMessageValidatorRegistry.getMessageValidators().add(defaultGroovyJsonMessageValidator);117 citrusMessageValidatorRegistry.getMessageValidators().add(defaultGroovyTextMessageValidator);118 citrusMessageValidatorRegistry.getMessageValidators().add(defaultXhtmlMessageValidator);119 citrusMessageValidatorRegistry.getMessageValidators().add(defaultXhtmlXpathMessageValidator);120 return citrusMessageValidatorRegistry;121 }122}...
Source:BinaryMessageValidator.java
...30 * message payload is convertable to an input stream so we can compare the stream data with buffer read.31 * 32 * @author Christoph Deppisch33 */34public class BinaryMessageValidator extends DefaultMessageValidator {35 private static final int BUFFER_SIZE = 1024;36 @Override37 public void validateMessage(Message receivedMessage, Message controlMessage,38 TestContext context, ValidationContext validationContext) throws ValidationException {39 try (InputStream receivedInput = receivedMessage.getPayload(InputStream.class);40 InputStream controlInput = controlMessage.getPayload(InputStream.class)) {41 ReadableByteChannel receivedBytes = Channels.newChannel(receivedInput);42 ReadableByteChannel controlBytes = Channels.newChannel(controlInput);43 ByteBuffer receivedBuffer = ByteBuffer.allocateDirect(BUFFER_SIZE);44 ByteBuffer controlBuffer = ByteBuffer.allocateDirect(BUFFER_SIZE);45 while (true) {46 int n1 = receivedBytes.read(receivedBuffer);47 int n2 = controlBytes.read(controlBuffer);48 if (n1 == -1 || n2 == -1) return;...
BinaryMessageValidator
Using AI Code Generation
1import com.consol.citrus.context.TestContext;2import com.consol.citrus.exceptions.ValidationException;3import com.consol.citrus.message.Message;4import com.consol.citrus.message.MessageType;5import com.consol.citrus.validation.context.ValidationContext;6import com.consol.citrus.validation.text.BinaryMessageValidator;7public class BinaryMessageValidator extends BinaryMessageValidator {8public void validateMessage(Message receivedMessage, Message controlMessage, TestContext context, ValidationContext validationContext) {9byte[] receivedPayload = receivedMessage.getPayload(byte[].class);10byte[] controlPayload = controlMessage.getPayload(byte[].class);11if (!Arrays.equals(receivedPayload, controlPayload)) {12throw new ValidationException("Binary message validation failed!", "Binary message validation failed!");13}14}15}16import com.consol.citrus.context.TestContext;17import com.consol.citrus.exceptions.ValidationException;18import com.consol.citrus.message.Message;19import com.consol.citrus.message.MessageType;20import com.consol.citrus.validation.context.ValidationContext;21import com.consol.citrus.validation.text.BinaryMessageValidator;22public class BinaryMessageValidator extends BinaryMessageValidator {23public void validateMessage(Message receivedMessage, Message controlMessage, TestContext context, ValidationContext validationContext) {24byte[] receivedPayload = receivedMessage.getPayload(byte[].class);25byte[] controlPayload = controlMessage.getPayload(byte[].class);26if (!Arrays.equals(receivedPayload, controlPayload)) {27throw new ValidationException("Binary message validation failed!", "Binary message validation failed!");28}29}30}31import com.consol.citrus.context.TestContext;32import com.consol.citrus.exceptions.ValidationException;33import com.consol.citrus.message.Message;34import com.consol.citrus.message.MessageType;35import com.consol.citrus.validation.context.ValidationContext;36import com.consol.citrus.validation.text.BinaryMessageValidator;37public class BinaryMessageValidator extends BinaryMessageValidator {
BinaryMessageValidator
Using AI Code Generation
1import com.consol.citrus.dsl.testng.TestNGCitrusTestDesigner;2import com.consol.citrus.validation.text.BinaryMessageValidator;3import org.springframework.core.io.ClassPathResource;4import org.testng.annotations.Test;5public class 4 extends TestNGCitrusTestDesigner {6 public void configure() {7 http()8 .client("httpClient")9 .send()10 .post("/upload")11 .contentType("application/json")12 .payload(new ClassPathResource("request.json"));13 http()14 .client("httpClient")15 .receive()16 .response(HttpStatus.OK)17 .messageType(MessageType.BINARY)18 .validate(binaryMessageValidator()19 .controlMessage(new ClassPathResource("response.json"))20 .ignoreBytes(0, 2)21 .ignoreBytes(4, 5));22 }23}
BinaryMessageValidator
Using AI Code Generation
1import com.consol.citrus.dsl.runner.TestRunner;2import com.consol.citrus.dsl.testng.TestNGCitrusTestDesigner;3import org.testng.annotations.Test;4public class 4 extends TestNGCitrusTestDesigner {5 public void 4() {6 variable("message", "Hello World!");7 variable("messageLength", "12");8 TestRunner runner = createTestRunner();9 runner.echo("${message}");10 runner.run(binaryMessageValidator()11 .messageType("text")12 .controlMessage("${message}")13 .extractFromPayload("${messageLength}", "messageLength"));14 runner.echo("${messageLength}");15 }16}
BinaryMessageValidator
Using AI Code Generation
1package com.consol.citrus.samples;2import com.consol.citrus.dsl.builder.BuilderSupport;3import com.consol.citrus.dsl.builder.ReceiveMessageActionBuilder;4import com.consol.citrus.dsl.runner.TestRunner;5import com.consol.citrus.dsl.runner.TestRunnerSupport;6import com.consol.citrus.dsl.validation.ValidationContext;7import com.consol.citrus.dsl.validation.ValidationContextSupport;8import com.consol.citrus.samples.message.BinaryMessage;9import com.consol.citrus.samples.message.BinaryMessageValidator;10import com.consol.citrus.samples.message.MessageFactory;11import com.consol.citrus.samples.message.MessageType;12import com.consol.citrus.samples.message.MessageValidator;13import com.consol.citrus.samples.message.MessageValidatorFactory;14import com.consol.citrus.samples.message.MessageValidatorType;15import com.consol.citrus.samples.message.TextMessage;16import com.consol.citrus.samples.message.TextMessageValidator;17import org.springframework.core.io.ClassPathResource;18import org.testng.annotations.Test;19import java.util.HashMap;20import java.util.Map;21import static com.consol.citrus.actions.CreateVariablesAction.Builder.createVariable;22import static com.consol.citrus.actions.EchoAction.Builder.echo;23import static com.consol.citrus.actions.SendMessageAction.Builder.send;24import static com.consol.citrus.dsl.builder.BuilderSupport.BuilderSupportBuilder.action;25import static com.consol.citrus.dsl.builder.BuilderSupport.BuilderSupportBuilder.apply;26import static com.consol.citrus.dsl.builder.BuilderSupport.BuilderSupportBuilder.applyAction;27import static com.consol.citrus.dsl.builder.BuilderSupport.BuilderSupportBuilder.applyBehavior;28import static com.consol.citrus.dsl.builder.BuilderSupport.BuilderSupportBuilder.applyBehaviorAnd;29import static com.consol.citrus.dsl.builder.BuilderSupport.BuilderSupportBuilder.applyBehaviorOr;30import static com.consol.citrus.dsl.builder.BuilderSupport.BuilderSupportBuilder.applyBehaviorXor;31import static com.consol.citrus.dsl.builder.BuilderSupport.BuilderSupportBuilder.applyBehaviorXorAnd;32import static com.consol.citrus.dsl.builder.BuilderSupport.BuilderSupportBuilder.applyBehaviorXorOr;33import static com.consol.citrus.dsl.builder.BuilderSupport.BuilderSupportBuilder.applyBehaviorXorXor;34import static com.consol.citrus.dsl.builder.BuilderSupport.BuilderSupportBuilder.applyBehaviorXorXorAnd;35import static com.consol.citrus.dsl.builder.BuilderSupport.BuilderSupportBuilder.apply
BinaryMessageValidator
Using AI Code Generation
1package com.consol.citrus.samples;2import com.consol.citrus.annotations.CitrusTest;3import com.consol.citrus.dsl.testng.TestNGCitrusTestRunner;4import com.consol.citrus.validation.text.BinaryMessageValidator;5import org.springframework.core.io.ClassPathResource;6import org.testng.annotations.Test;7public class BinaryMessageValidationIT extends TestNGCitrusTestRunner {8 public void binaryMessageValidation() {9 variable("message", "Hello Citrus!");10 http()11 .client("httpClient")12 .send()13 .post("/test")14 .contentType("application/octet-stream")15 .payload("${message}");16 http()17 .client("httpClient")18 .receive()19 .response(HttpStatus.OK)20 .validator(new BinaryMessageValidator())21 .payload(new ClassPathResource("files/4.txt"));22 }23}24package com.consol.citrus.samples;25import com.consol.citrus.annotations.CitrusTest;26import com.consol.citrus.dsl.testng.TestNGCitrusTestRunner;27import com.consol.citrus.validation.json.JsonMessageValidator;28import org.springframework.core.io.ClassPathResource;29import org.testng.annotations.Test;30public class JsonMessageValidationIT extends TestNGCitrusTestRunner {31 public void jsonMessageValidation() {32 variable("message", "Hello Citrus!");33 http()34 .client("httpClient")35 .send()36 .post("/test")37 .contentType("application/json")38 .payload("{ \"message\": \"${message}\"}");39 http()40 .client("httpClient")41 .receive()42 .response(HttpStatus.OK)43 .validator(new JsonMessageValidator())44 .payload(new ClassPathResource("files/5.json"));45 }46}47package com.consol.citrus.samples;48import com.consol.citrus.annotations.CitrusTest;49import com.consol.citrus.dsl.testng.TestNGCitrusTestRunner;50import com.consol.citrus.validation.xml.XmlMessageValidator;51import org.springframework.core.io
BinaryMessageValidator
Using AI Code Generation
1import com.consol.citrus.validation.text.BinaryMessageValidator;2import com.consol.citrus.validation.text.BinaryValidationContext;3import com.consol.citrus.validation.MessageValidatorRegistry;4import com.consol.citrus.validation.MessageValidator;5import com.consol.citrus.validation.MessageValidatorContext;6import com.consol.citrus.message.Message;7import com.consol.citrus.context.TestContext;8import com.consol.citrus.exceptions.ValidationException;9import com.consol.citrus.exceptions.CitrusRuntimeException;10import java.util.Map;11import java.util.HashMap;12import java.util.List;13import java.util.ArrayList;14import java.io.IOException;15import java.io.File;16import java.io.FileInputStream;17import java.io.ByteArrayInputStream;18import java.io.InputStream;19import java.io
BinaryMessageValidator
Using AI Code Generation
1public class BinaryMessageValidator extends AbstractMessageValidator<BinaryMessage> {2 public boolean supportsMessageType(String messageType, Object message) {3 return messageType.equals("binary");4 }5 public void validateMessagePayload(BinaryMessage message, Object messagePayload, MessageValidationContext validationContext) {6 }7}
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!!