Fix typedef/bits code generation 41/104041/5
authorRobert Varga <robert.varga@pantheon.tech>
Mon, 16 Jan 2023 13:42:30 +0000 (14:42 +0100)
committerRobert Varga <nite@hq.sk>
Thu, 19 Jan 2023 17:23:54 +0000 (17:23 +0000)
bits/typedef seemed to be generating code which does not compile.

Add a test to check if the generated code contains desired method and
fix the check for bits type.

JIRA: MDSAL-807
Change-Id: I4618269fff1fc284b65e9efb1d49501248d233c3
Signed-off-by: matus.matok <matus.matok@pantheon.tech>
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/ClassTemplate.xtend
binding/mdsal-binding-java-api-generator/src/test/java/org/opendaylight/mdsal/binding/java/api/generator/Mdsal807Test.java [new file with mode: 0644]
binding/mdsal-binding-java-api-generator/src/test/resources/compilation/mdsal807/foo.yang [new file with mode: 0644]

index df7be75af6500637be2a86e50251c07440370c4a..534fab13bd6f0df2af2a21997dd559b0f610a229 100644 (file)
@@ -435,7 +435,7 @@ class ClassTemplate extends BaseTemplate {
             «val propType = prop.returnType»
             «IF !(INSTANCE_IDENTIFIER.identifier.equals(propType.identifier))»
             public static «genTO.name» getDefaultInstance(final String defaultValue) {
-                «IF allProperties.size > 1»
+                «IF propType.equals(Types.primitiveBooleanType())»
                     «bitsArgs»
                 «ELSEIF VALUEOF_TYPES.contains(propType)»
                     return new «genTO.name»(«propType.importedName».valueOf(defaultValue));
diff --git a/binding/mdsal-binding-java-api-generator/src/test/java/org/opendaylight/mdsal/binding/java/api/generator/Mdsal807Test.java b/binding/mdsal-binding-java-api-generator/src/test/java/org/opendaylight/mdsal/binding/java/api/generator/Mdsal807Test.java
new file mode 100644 (file)
index 0000000..620b95c
--- /dev/null
@@ -0,0 +1,56 @@
+/*
+ * 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 Mdsal807Test extends BaseCompilationTest {
+    private File sourcesOutputDir;
+    private File compiledOutputDir;
+
+    @Before
+    public void before() throws IOException, URISyntaxException {
+        sourcesOutputDir = CompilationTestUtils.generatorOutput("mdsal807");
+        compiledOutputDir = CompilationTestUtils.compilerOutput("mdsal807");
+    }
+
+    @After
+    public void after() {
+        CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
+    }
+
+    @Test
+    public void testBitsTypedef() throws IOException, URISyntaxException {
+        generateTestSources("/compilation/mdsal807", sourcesOutputDir);
+        final var pmDataType = FileSearchUtil.getFiles(sourcesOutputDir).get("TableConfig.java");
+        assertNotNull(pmDataType);
+
+        FileSearchUtil.assertFileContainsConsecutiveLines(pmDataType, Files.readString(pmDataType.toPath()),
+            "    public static TableConfig getDefaultInstance(final String defaultValue) {",
+            "        List<String> properties = Lists.newArrayList(\"oFPTCDEPRECATEDMASK\"",
+            "        );",
+            "        if (!properties.contains(defaultValue)) {",
+            "            throw new IllegalArgumentException(\"invalid default parameter\");",
+            "        }",
+            "        int i = 0;",
+            "        return new TableConfig(",
+            "        properties.get(i++).equals(defaultValue) ? true : false",
+            "        );",
+            "    }"
+        );
+        CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
+    }
+}
diff --git a/binding/mdsal-binding-java-api-generator/src/test/resources/compilation/mdsal807/foo.yang b/binding/mdsal-binding-java-api-generator/src/test/resources/compilation/mdsal807/foo.yang
new file mode 100644 (file)
index 0000000..37fcb56
--- /dev/null
@@ -0,0 +1,12 @@
+module foo {
+  namespace foo;
+  prefix foo;
+
+  typedef table-config {
+    type bits {
+      bit OFPTC_DEPRECATED_MASK {
+        position 3;
+      }
+    }
+  }
+}