You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ac...@apache.org on 2021/07/16 05:22:50 UTC
[camel] 02/02: CAMEL-15663 Added validation errors info in
exception message
This is an automated email from the ASF dual-hosted git repository.
acosentino pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git
commit 854a08789c9ec74af99d65f338cdbdd310bf5f00
Author: g.dedlovskiy <g....@isimplelab.com>
AuthorDate: Thu Jul 15 22:35:08 2021 +0300
CAMEL-15663 Added validation errors info in exception message
---
.../jsonvalidator/JsonValidationException.java | 7 +++-
.../jsonvalidator/JsonValidationExceptionTest.java | 43 ++++++++++++++++++++++
2 files changed, 49 insertions(+), 1 deletion(-)
diff --git a/components/camel-json-validator/src/main/java/org/apache/camel/component/jsonvalidator/JsonValidationException.java b/components/camel-json-validator/src/main/java/org/apache/camel/component/jsonvalidator/JsonValidationException.java
index 917955c..0284ac1 100644
--- a/components/camel-json-validator/src/main/java/org/apache/camel/component/jsonvalidator/JsonValidationException.java
+++ b/components/camel-json-validator/src/main/java/org/apache/camel/component/jsonvalidator/JsonValidationException.java
@@ -17,6 +17,7 @@
package org.apache.camel.component.jsonvalidator;
import java.util.Set;
+import java.util.stream.Collectors;
import com.networknt.schema.JsonSchema;
import com.networknt.schema.ValidationMessage;
@@ -31,7 +32,7 @@ public class JsonValidationException extends ValidationException {
private final Set<ValidationMessage> errors;
public JsonValidationException(Exchange exchange, JsonSchema schema, Set<ValidationMessage> errors) {
- super(exchange, "JSON validation error with " + errors.size() + " errors");
+ super(exchange, "JSON validation error with " + errors.size() + " errors:\n" + toString(errors));
this.schema = schema;
this.errors = errors;
}
@@ -53,4 +54,8 @@ public class JsonValidationException extends ValidationException {
public int getNumberOfErrors() {
return errors.size();
}
+
+ private static String toString(Set<ValidationMessage> errors) {
+ return errors.stream().map(ValidationMessage::toString).collect(Collectors.joining("\n"));
+ }
}
diff --git a/components/camel-json-validator/src/test/java/org/apache/camel/component/jsonvalidator/JsonValidationExceptionTest.java b/components/camel-json-validator/src/test/java/org/apache/camel/component/jsonvalidator/JsonValidationExceptionTest.java
new file mode 100644
index 0000000..aa1b812
--- /dev/null
+++ b/components/camel-json-validator/src/test/java/org/apache/camel/component/jsonvalidator/JsonValidationExceptionTest.java
@@ -0,0 +1,43 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.jsonvalidator;
+
+import java.text.MessageFormat;
+import java.util.LinkedHashSet;
+import java.util.Set;
+
+import com.networknt.schema.ValidationMessage;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+public class JsonValidationExceptionTest {
+
+ @Test
+ void testErrorsInfoInMessage() {
+ Set<ValidationMessage> errors = new LinkedHashSet<>();
+ errors.add(createError("name: is missing but it is required"));
+ errors.add(createError("id: string found, integer expected"));
+ assertEquals(
+ "JSON validation error with 2 errors:\nname: is missing but it is required\nid: string found, integer expected",
+ new JsonValidationException(null, null, errors).getMessage());
+ }
+
+ private ValidationMessage createError(String msg) {
+ return new ValidationMessage.Builder().format(new MessageFormat(msg)).build();
+ }
+}