Report a dedicated exception on unique failure 12/93912/1
authorRobert Varga <robert.varga@pantheon.tech>
Fri, 20 Nov 2020 20:05:16 +0000 (21:05 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Sun, 22 Nov 2020 16:51:51 +0000 (17:51 +0100)
Rather than using plain IllegalArgumentException, report a dedicated
exception derived from SchemaValidationFailedException. Also refactor
UniqueValidation a bit.

Change-Id: If24d97aebab516a461f673c169fa41f9447e0413
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
(cherry picked from commit ba3080a178abe5f76b45fa88f442990c62ca5d43)

yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/schema/tree/SchemaValidationFailedException.java
yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/schema/tree/UniqueValidation.java
yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/schema/tree/UniqueValidationFailedException.java [new file with mode: 0644]
yang/yang-data-impl/src/test/java/org/opendaylight/yangtools/yang/data/impl/schema/tree/UniqueConstraintTest.java

index a1804d07f1b66d57fbaaa323c74e944c6ea1a1f1..b1356bf29cd15726a4527b11902379d509f32629 100644 (file)
@@ -5,7 +5,6 @@
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
  * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
-
 package org.opendaylight.yangtools.yang.data.impl.schema.tree;
 
 /**
@@ -13,7 +12,6 @@ package org.opendaylight.yangtools.yang.data.impl.schema.tree;
  * does not match the schema context.
  */
 public class SchemaValidationFailedException extends IllegalArgumentException {
-
     private static final long serialVersionUID = 1L;
 
     public SchemaValidationFailedException(final String message) {
@@ -23,5 +21,4 @@ public class SchemaValidationFailedException extends IllegalArgumentException {
     public SchemaValidationFailedException(final String message, final Throwable cause) {
         super(message, cause);
     }
-
 }
index b308a194b599bb67367101e5423dc2bf614859a2..6aab3ab4d8c4cf6d037171933444232a0f6a2e78 100644 (file)
@@ -9,6 +9,7 @@ package org.opendaylight.yangtools.yang.data.impl.schema.tree;
 
 import static com.google.common.base.Preconditions.checkState;
 import static com.google.common.base.Verify.verify;
+import static java.util.Objects.requireNonNull;
 
 import com.google.common.base.MoreObjects.ToStringHelper;
 import com.google.common.base.Stopwatch;
@@ -47,33 +48,40 @@ final class UniqueValidation extends AbstractValidation {
 
     private final @NonNull ImmutableList<UniqueValidator<?>> validators;
 
-    private UniqueValidation(final ModificationApplyOperation delegate, final List<UniqueValidator<?>> validators) {
+    private UniqueValidation(final ModificationApplyOperation delegate,
+            final ImmutableList<UniqueValidator<?>> validators) {
         super(delegate);
-        this.validators = ImmutableList.copyOf(validators);
+        this.validators = requireNonNull(validators);
     }
 
     static ModificationApplyOperation of(final ListSchemaNode schema, final DataTreeConfiguration treeConfig,
             final ModificationApplyOperation delegate) {
+        final ImmutableList<UniqueValidator<?>> validators = validatorsOf(schema, treeConfig);
+        return validators.isEmpty() ? delegate : new UniqueValidation(delegate, validators);
+    }
+
+    static ImmutableList<UniqueValidator<?>> validatorsOf(final ListSchemaNode schema,
+            final DataTreeConfiguration treeConfig) {
         final Collection<? extends @NonNull UniqueEffectiveStatement> uniques = schema.getUniqueConstraints();
         if (!treeConfig.isUniqueIndexEnabled() || uniques.isEmpty()) {
-            return delegate;
+            return ImmutableList.of();
         }
 
         final Stopwatch sw = Stopwatch.createStarted();
         final Map<Descendant, List<NodeIdentifier>> paths = new HashMap<>();
-        final List<UniqueValidator<?>> validators = uniques.stream()
+        final ImmutableList<UniqueValidator<?>> validators = uniques.stream()
             .map(unique -> UniqueValidator.of(unique.argument().stream()
                 .map(descendant -> paths.computeIfAbsent(descendant, key -> toDescendantPath(schema, key)))
                 .collect(ImmutableList.toImmutableList())))
             .collect(ImmutableList.toImmutableList());
         LOG.debug("Constructed {} validators in {}", validators.size(), sw);
 
-        return validators.isEmpty() ? delegate : new UniqueValidation(delegate, validators);
+        return validators;
     }
 
     @Override
     void enforceOnData(final NormalizedNode<?, ?> data) {
-        enforceOnData(data, (message, values) -> new IllegalArgumentException(message));
+        enforceOnData(data, (message, values) -> new UniqueValidationFailedException(message));
     }
 
     @Override
diff --git a/yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/schema/tree/UniqueValidationFailedException.java b/yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/schema/tree/UniqueValidationFailedException.java
new file mode 100644 (file)
index 0000000..79e41a9
--- /dev/null
@@ -0,0 +1,21 @@
+/*
+ * Copyright (c) 2020 PANTHEON.tech, s.r.o. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.yangtools.yang.data.impl.schema.tree;
+
+import org.opendaylight.yangtools.yang.data.api.schema.tree.UniqueConstraintException;
+
+/**
+ * Exception thrown when unique constraints would be violated and we cannot throw a {@link UniqueConstraintException}.
+ */
+final class UniqueValidationFailedException extends SchemaValidationFailedException {
+    private static final long serialVersionUID = 1L;
+
+    UniqueValidationFailedException(final String message) {
+        super(message);
+    }
+}
index 4db997ad8b11d1d6f7c7912407311a6f9e1a86b8..dbccc7be8ca6dd8ad259ae963b06588ff9abad58 100644 (file)
@@ -78,7 +78,8 @@ public class UniqueConstraintTest {
         final InMemoryDataTree inMemoryDataTree = emptyDataTree(TEST_MODEL, true);
 
 
-        verifyExceptionMessage(assertThrows(IllegalArgumentException.class, () -> writeMap(inMemoryDataTree, true)),
+        verifyExceptionMessage(assertThrows(UniqueValidationFailedException.class,
+            () -> writeMap(inMemoryDataTree, true)),
             "(foo?revision=2016-05-17)task[{(foo?revision=2016-05-17)task-id=",
             "}] violates unique constraint on [l2, l1] of ",
             "(foo?revision=2016-05-17)my-leaf-1",