Fix union stringValue() with Decimal64 09/100309/4
authorRobert Varga <robert.varga@pantheon.tech>
Thu, 31 Mar 2022 20:25:15 +0000 (22:25 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Fri, 1 Apr 2022 08:55:38 +0000 (10:55 +0200)
UnionTemplate still things decimal64 maps to java.math.BigInteger,
resulting in string conversion which does not compile. Fix the template
to recognize Decimal64 as a proper CanonicalValue.

JIRA: MDSAL-738
Change-Id: I0f161b62887f4aea6e0c625d105e48a20c8dc048
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
binding/mdsal-binding-java-api-generator/src/main/java/org/opendaylight/mdsal/binding/java/api/generator/UnionTemplate.xtend
binding/mdsal-binding-java-api-generator/src/test/java/org/opendaylight/mdsal/binding/java/api/generator/Mdsal738Test.java [new file with mode: 0644]
binding/mdsal-binding-java-api-generator/src/test/resources/compilation/mdsal738/foo.yang [new file with mode: 0644]

index cf6632036c8c8906553c4c3764efa10c0966ba37..c940dea1950d4e983c3d4c8cc9268df341860c14 100644 (file)
@@ -118,13 +118,12 @@ class UnionTemplate extends ClassTemplate {
                 «ELSEIF BINARY_TYPE.equals(propRet)»
                     ««« type binary
                 return new «STRING.importedName»(«field»);
-                «ELSEIF propRet.fullyQualifiedName.startsWith("java.lang") || propRet instanceof Enumeration
-                        || propRet.fullyQualifiedName.startsWith("java.math")»
-                    ««« type int*, decimal64 or enumeration*
+                «ELSEIF propRet.fullyQualifiedName.startsWith("java.lang") || propRet instanceof Enumeration»
+                    ««« type int* or enumeration*
                 return «field».toString();
                 «ELSEIF "org.opendaylight.yangtools.yang.common".equals(propRet.packageName)
-                        && propRet.name.startsWith("Uint"
-                    ««« type uint*
+                        && (propRet.name.startsWith("Uint") || "Decimal64".equals(propRet.name)
+                    ««« type uint*, decimal64
                 return «field».toCanonicalString();
                 «ELSEIF propRet instanceof GeneratedTransferObject && (propRet as GeneratedTransferObject).unionType»
                     ««« union type
diff --git a/binding/mdsal-binding-java-api-generator/src/test/java/org/opendaylight/mdsal/binding/java/api/generator/Mdsal738Test.java b/binding/mdsal-binding-java-api-generator/src/test/java/org/opendaylight/mdsal/binding/java/api/generator/Mdsal738Test.java
new file mode 100644 (file)
index 0000000..8ebe528
--- /dev/null
@@ -0,0 +1,58 @@
+/*
+ * Copyright (c) 2022 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.mdsal.binding.java.api.generator;
+
+import static org.junit.Assert.assertNotNull;
+
+import java.io.File;
+import java.io.IOException;
+import java.net.URISyntaxException;
+import java.nio.file.Files;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+public class Mdsal738Test extends BaseCompilationTest {
+    private File sourcesOutputDir;
+    private File compiledOutputDir;
+
+    @Before
+    public void before() throws IOException, URISyntaxException {
+        sourcesOutputDir = CompilationTestUtils.generatorOutput("mdsal738");
+        compiledOutputDir = CompilationTestUtils.compilerOutput("mdsal738");
+    }
+
+    @After
+    public void after() {
+        CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
+    }
+
+    @Test
+    public void testUnionOfDecimal64() throws IOException, URISyntaxException {
+        generateTestSources("/compilation/mdsal738", sourcesOutputDir);
+        final var pmDataType = FileSearchUtil.getFiles(sourcesOutputDir).get("PmDataType.java");
+        assertNotNull(pmDataType);
+
+        final var content = Files.readString(pmDataType.toPath());
+        FileSearchUtil.assertFileContainsConsecutiveLines(pmDataType, content,
+            "    public String stringValue() {",
+            "        if (_uint64 != null) {",
+            "            return _uint64.toCanonicalString();",
+            "        }",
+            "        if (_int64 != null) {",
+            "            return _int64.toString();",
+            "        }",
+            "        if (_decimal64 != null) {",
+            "            return _decimal64.toCanonicalString();",
+            "        }",
+            "        throw new IllegalStateException(\"No value assigned\");",
+            "    }");
+
+        CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
+    }
+}
diff --git a/binding/mdsal-binding-java-api-generator/src/test/resources/compilation/mdsal738/foo.yang b/binding/mdsal-binding-java-api-generator/src/test/resources/compilation/mdsal738/foo.yang
new file mode 100644 (file)
index 0000000..fd97d7e
--- /dev/null
@@ -0,0 +1,17 @@
+module foo {
+  namespace foo;
+  prefix foo;
+
+  typedef pm-data-type {
+    type union {
+      type uint64;
+      type int64;
+      type decimal64 {
+        fraction-digits 2;
+      }
+      type decimal64 {
+        fraction-digits 17;
+      }
+    }
+  }
+}