YANGTOOLS-827: fix revision compare 78/65278/2
authorRobert Varga <robert.varga@pantheon.tech>
Tue, 7 Nov 2017 18:28:53 +0000 (19:28 +0100)
committerRobert Varga <nite@hq.sk>
Tue, 7 Nov 2017 20:25:47 +0000 (20:25 +0000)
When revision statements do not follow guidance from RFC6020/RFC7950
section 7.1.9 and are not order in the order of descending date and
the delta between previous and next version strings is not exactly 1,
we end up picking the wrong version.

This is caused by wrong compareTo() check, which should compare '< 0',
not '== -1'.

Change-Id: Ib7f9a77a9950b6da93ffa30e4c13cc940887ad19
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/impl/util/YangModelDependencyInfo.java
yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/parser/impl/util/YangModelDependencyInfoTest.java
yang/yang-parser-impl/src/test/resources/bugs/YT827/foo.yang [new file with mode: 0644]

index 0019afff702a797e83b5029cf611f9ee8863372a..8a4110a742141c31264babb71608c04bcb80702c 100644 (file)
@@ -243,7 +243,7 @@ public abstract class YangModelDependencyInfo {
                 final String revisionDateStr = getRevisionDateString(subStatementContext, sourceName);
                 final String importedModuleName = Utils.stringFromStringContext(subStatementContext.argument(),
                         getReference(sourceName, subStatementContext));
-                final Date revisionDate = (revisionDateStr == null) ? null : QName
+                final Date revisionDate = revisionDateStr == null ? null : QName
                         .parseRevision(revisionDateStr);
                 final Optional<SemVer> importSemVer = Optional.fromNullable(getSemanticVersion(subStatementContext, sourceName));
                 result.add(new ModuleImportImpl(importedModuleName,
@@ -285,7 +285,7 @@ public abstract class YangModelDependencyInfo {
                 final String revisionDateStr = getRevisionDateString(subStatementContext, sourceName);
                 final String IncludeModuleName = Utils.stringFromStringContext(subStatementContext.argument(),
                         getReference(sourceName, subStatementContext));
-                final Date revisionDate = (revisionDateStr == null) ? null : QName
+                final Date revisionDate = revisionDateStr == null ? null : QName
                         .parseRevision(revisionDateStr);
                 result.add(new ModuleImportImpl(IncludeModuleName, revisionDate));
             }
@@ -320,8 +320,7 @@ public abstract class YangModelDependencyInfo {
                             .getLocalName())) {
                 final String currentRevision = Utils.stringFromStringContext(subStatementContext.argument(),
                         getReference(sourceName, subStatementContext));
-                if (latestRevision == null
-                        || latestRevision.compareTo(currentRevision) == -1) {
+                if (latestRevision == null || latestRevision.compareTo(currentRevision) < 0) {
                     latestRevision = currentRevision;
                 }
             }
index 3935ac5d80e6987e4ad1cb1cb05da59a1696b33a..3b11336f6b02810911c2e322b0702ca6582a1b3e 100644 (file)
@@ -13,6 +13,7 @@ import static org.junit.Assert.assertNotEquals;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertTrue;
+
 import java.io.InputStream;
 import org.junit.Test;
 
@@ -63,6 +64,14 @@ public class YangModelDependencyInfoTest {
         assertFalse(info1.equals(info2));
     }
 
+    @Test
+    public void testYangtools827() {
+        // Latest revision needs to be picked up irrespective of ordering
+        InputStream stream = getClass().getResourceAsStream("/bugs/YT827/foo.yang");
+        YangModelDependencyInfo info = YangModelDependencyInfo.fromInputStream(stream);
+        assertEquals("2014-12-24", info.getFormattedRevision());
+    }
+
     @Test
     public void testHashcode() {
         InputStream stream = getClass().getResourceAsStream("/no-revision/module-without-revision.yang");
diff --git a/yang/yang-parser-impl/src/test/resources/bugs/YT827/foo.yang b/yang/yang-parser-impl/src/test/resources/bugs/YT827/foo.yang
new file mode 100644 (file)
index 0000000..c096177
--- /dev/null
@@ -0,0 +1,8 @@
+module foo {
+    namespace foo;
+    prefix foo;
+
+    revision "2010-10-10";
+
+    revision "2014-12-24";
+}