Do not tolerate duplicate identities 79/87079/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 13:26:51 +0000 (14:26 +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 4e300e925bf080a9918eb2c0c9e8515da741979f..c6c40a82bd7350858cecb4dece7e1ec1dcb9a011 100644 (file)
@@ -9,11 +9,14 @@ 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.Collection;
 import java.util.Iterator;
 import org.junit.Test;
@@ -23,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 {
@@ -101,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();
-        Collection<? extends 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 "));
+        }
     }
 }