|
27 | 27 | import io.cdap.plugin.jms.common.JMSMessageHeader;
|
28 | 28 | import io.cdap.plugin.jms.common.JMSMessageParts;
|
29 | 29 | import io.cdap.plugin.jms.common.JMSMessageType;
|
| 30 | +import io.cdap.plugin.jms.common.SchemaValidationUtils; |
30 | 31 |
|
31 | 32 | import java.io.IOException;
|
32 | 33 | import java.io.Serializable;
|
33 | 34 | import java.util.ArrayList;
|
34 |
| -import java.util.Arrays; |
35 | 35 | import java.util.List;
|
36 | 36 | import java.util.stream.Collectors;
|
37 | 37 | import javax.annotation.Nullable;
|
@@ -113,52 +113,46 @@ public void validate(FailureCollector failureCollector) {
|
113 | 113 | }
|
114 | 114 |
|
115 | 115 | if (!containsMacro(NAME_SCHEMA)) {
|
116 |
| - |
117 | 116 | Schema schema = getSchema();
|
118 | 117 |
|
119 |
| - boolean otherFieldsExist = schema |
120 |
| - .getFields() |
121 |
| - .stream() |
122 |
| - .anyMatch(field -> !JMSMessageParts.getJMSMessageParts().contains(field.getName())); |
123 |
| - |
124 |
| - if (otherFieldsExist) { |
125 |
| - failureCollector |
126 |
| - .addFailure("New fields detected in the output schema!", |
127 |
| - String.format("Only \"%s\", \"%s\" and \"%s\" fields are allowed.", |
128 |
| - JMSMessageParts.HEADER, JMSMessageParts.BODY, JMSMessageParts.PROPERTIES)) |
129 |
| - .withConfigProperty(NAME_SCHEMA); |
| 118 | + SchemaValidationUtils.validateIfAnyNotSupportedRootFieldExists(schema, failureCollector); |
| 119 | + |
| 120 | + if (getMessageHeader()) { |
| 121 | + SchemaValidationUtils.validateHeaderSchema(schema, failureCollector); |
| 122 | + } |
| 123 | + |
| 124 | + if (getMessageProperties()) { |
| 125 | + SchemaValidationUtils.validatePropertiesSchema(schema, failureCollector); |
130 | 126 | }
|
131 | 127 |
|
132 | 128 | switch (messageType) {
|
133 | 129 | case JMSMessageType.TEXT:
|
134 |
| - if (!schema.getField(JMSMessageParts.BODY).getSchema().getType().equals(Schema.Type.STRING)) { |
135 |
| - failureCollector |
136 |
| - .addFailure(String.format("Wrong data type for field \"%s\".", JMSMessageParts.BODY), |
137 |
| - String.format("For JMS %s type of message, \"%s\" must be of String data type.", |
138 |
| - messageType, JMSMessageParts.BODY)).withConfigProperty(NAME_SCHEMA); |
139 |
| - } |
| 130 | + SchemaValidationUtils.validateIfBodyNotInSchema(schema, failureCollector); |
| 131 | + SchemaValidationUtils.validateTextMessageSchema(schema, failureCollector); |
| 132 | + |
140 | 133 | break;
|
141 | 134 |
|
142 | 135 | case JMSMessageType.OBJECT:
|
143 |
| - if (!schema.getField(JMSMessageParts.BODY).getSchema().getType().equals(Schema.Type.ARRAY)) { |
144 |
| - failureCollector |
145 |
| - .addFailure(String.format("Wrong data type for field \"%s\".", JMSMessageParts.BODY), |
146 |
| - String.format("For JMS %s type of message, \"%s\" must be of Array[Bytes] data type.", |
147 |
| - messageType, JMSMessageParts.BODY)).withConfigProperty(NAME_SCHEMA); |
148 |
| - } |
| 136 | + SchemaValidationUtils.validateIfBodyNotInSchema(schema, failureCollector); |
| 137 | + SchemaValidationUtils.validateObjectMessageSchema(schema, failureCollector); |
149 | 138 | break;
|
150 | 139 |
|
151 | 140 | case JMSMessageType.BYTES:
|
| 141 | + SchemaValidationUtils.validateIfBodyNotInSchema(schema, failureCollector); |
| 142 | + SchemaValidationUtils.validateBytesMessageSchema(schema, failureCollector); |
| 143 | + |
| 144 | + break; |
| 145 | + |
152 | 146 | case JMSMessageType.MAP:
|
153 |
| - case JMSMessageType.MESSAGE: |
154 |
| - if (!Arrays.asList(Schema.Type.STRING, Schema.Type.RECORD) |
155 |
| - .contains(schema.getField(JMSMessageParts.BODY).getSchema().getType())) { |
156 |
| - failureCollector |
157 |
| - .addFailure(String.format("Wrong data type for field \"%s\"", JMSMessageParts.BODY), |
158 |
| - String.format("For JMS %s type of message, \"%s\" must be of String or Record data type.", |
159 |
| - messageType, JMSMessageParts.BODY)).withConfigProperty(NAME_SCHEMA); |
160 |
| - } |
| 147 | + SchemaValidationUtils.validateIfBodyNotInSchema(schema, failureCollector); |
| 148 | + SchemaValidationUtils.validateMapMessageSchema(schema, failureCollector); |
| 149 | + |
161 | 150 | break;
|
| 151 | + |
| 152 | + case JMSMessageType.MESSAGE: |
| 153 | + SchemaValidationUtils.validateMessageSchema(schema, failureCollector); |
| 154 | + |
| 155 | + |
162 | 156 | }
|
163 | 157 | }
|
164 | 158 | }
|
|
0 commit comments