Do not tolerate duplicate identities 82/87082/1
authorRobert Varga <robert.varga@pantheon.tech>
Wed, 22 Jan 2020 13:25:15 +0000 (14:25 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Wed, 22 Jan 2020 14:20:42 +0000 (15:20 +0100)
We have a unit test asserting identity definition squashing -- which
really is papering over the parser accepting the identities.

As per RFC7950, identity declarations must be unique, and we should
report them through normal SourceException.

JIRA: YANGTOOLS-1075
Change-Id: I98ce8615bcdb502cb79caa23fc9c3a127f850ea9
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
yang/yang-parser-rfc7950/src/main/java/org/opendaylight/yangtools/yang/parser/rfc7950/stmt/identity/AbstractIdentityStatementSupport.java
yang/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/IdentityStmtTest.java

index b1037dbd2170b4354a9645b112176c4560008f8f..a90edf344395d5f220b7a682286450b4ac7eaea1 100644 (file)
@@ -16,6 +16,7 @@ import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractQNameStatementSup
 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
+import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
 
 abstract class AbstractIdentityStatementSupport
         extends AbstractQNameStatementSupport<IdentityStatement, IdentityEffectiveStatement> {
@@ -43,6 +44,10 @@ abstract class AbstractIdentityStatementSupport
     @Override
     public final void onStatementDefinitionDeclared(
             final Mutable<QName, IdentityStatement, IdentityEffectiveStatement> stmt) {
-        stmt.addToNs(IdentityNamespace.class, stmt.coerceStatementArgument(), stmt);
+        final QName qname = stmt.coerceStatementArgument();
+        final StmtContext<?, ?, ?> prev = stmt.getFromNamespace(IdentityNamespace.class, qname);
+        SourceException.throwIf(prev != null, stmt.getStatementSourceReference(), "Duplicate identity definition %s",
+                qname);
+        stmt.addToNs(IdentityNamespace.class, qname, stmt);
     }
 }
index a58ece2738cd0294aded3f9298ff4d43399669f5..6f04c422c130bcbc01aaad1e8ee04f0418a06648 100644 (file)
@@ -5,16 +5,18 @@
  * 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.stmt;
 
 import static org.hamcrest.CoreMatchers.anyOf;
 import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.CoreMatchers.startsWith;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertThat;
+import static org.junit.Assert.fail;
 import static org.opendaylight.yangtools.yang.stmt.StmtTestUtils.sourceForResource;
 
+import com.google.common.base.Throwables;
 import java.util.Iterator;
 import java.util.Set;
 import org.junit.Test;
@@ -24,6 +26,7 @@ import org.opendaylight.yangtools.yang.model.api.SchemaContext;
 import org.opendaylight.yangtools.yang.parser.rfc7950.reactor.RFC7950Reactors;
 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
 import org.opendaylight.yangtools.yang.parser.spi.meta.SomeModifiersUnresolvedException;
+import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
 
 public class IdentityStmtTest {
@@ -102,13 +105,12 @@ public class IdentityStmtTest {
 
     @Test
     public void duplicateIdentityTest() throws ReactorException {
-        SchemaContext result = RFC7950Reactors.defaultReactor().newBuild()
-                .addSource(DUPLICATE_IDENTITY_MODULE)
-                .buildEffective();
-        assertNotNull(result);
-
-        Module testModule = result.findModules("duplicate-identity-test").iterator().next();
-        Set<IdentitySchemaNode> identities = testModule.getIdentities();
-        assertEquals(1, identities.size());
+        try {
+            RFC7950Reactors.defaultReactor().newBuild().addSource(DUPLICATE_IDENTITY_MODULE).buildEffective();
+            fail("Duplicate identities should have been detected");
+        } catch (SomeModifiersUnresolvedException e) {
+            final SourceException cause = Throwables.getCauseAs(e, SourceException.class);
+            assertThat(cause.getMessage(), startsWith("Duplicate identity definition "));
+        }
     }
 }