Do not generate union builders
[mdsal.git] / binding / mdsal-binding-java-api-generator / src / test / java / org / opendaylight / mdsal / binding / java / api / generator / BuilderGeneratorTest.java
index 7232ff4260e68f17dbdba26853235eba41246cfc..a233dc27d027a2ae9585e508df914a84df06a0a2 100644 (file)
@@ -16,6 +16,7 @@ import static org.mockito.Mockito.spy;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.stream.Collectors;
+import org.eclipse.xtend2.lib.StringConcatenation;
 import org.junit.Test;
 import org.opendaylight.mdsal.binding.generator.impl.DefaultBindingGenerator;
 import org.opendaylight.mdsal.binding.model.api.GeneratedType;
@@ -23,7 +24,6 @@ import org.opendaylight.mdsal.binding.model.api.JavaTypeName;
 import org.opendaylight.mdsal.binding.model.api.MethodSignature;
 import org.opendaylight.mdsal.binding.model.api.MethodSignature.ValueMechanics;
 import org.opendaylight.mdsal.binding.model.api.Type;
-import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
 
 public class BuilderGeneratorTest {
@@ -39,7 +39,7 @@ public class BuilderGeneratorTest {
     public void builderTemplateGenerateHashcodeWithPropertyTest() {
         final GeneratedType genType = mockGenType("get" + TEST);
 
-        assertEquals("/**\n"
+        assertXtendEquals("/**\n"
                 + " * Default implementation of {@link Object#hashCode()} contract for this interface.\n"
                 + " * Implementations of this interface are encouraged to defer to this method to get consistent"
                 + " hashing\n"
@@ -64,7 +64,7 @@ public class BuilderGeneratorTest {
 
     @Test
     public void builderTemplateGenerateHashCodeWithMorePropertiesTest() throws Exception {
-        assertEquals("/**\n"
+        assertXtendEquals("/**\n"
                 + " * Default implementation of {@link Object#hashCode()} contract for this interface.\n"
                 + " * Implementations of this interface are encouraged to defer to this method to get consistent"
                 + " hashing\n"
@@ -85,7 +85,7 @@ public class BuilderGeneratorTest {
 
     @Test
     public void builderTemplateGenerateHashCodeWithoutPropertyWithAugmentTest() throws Exception {
-        assertEquals("/**\n"
+        assertXtendEquals("/**\n"
                 + " * Default implementation of {@link Object#hashCode()} contract for this interface.\n"
                 + " * Implementations of this interface are encouraged to defer to this method to get consistent"
                 + " hashing\n"
@@ -98,14 +98,16 @@ public class BuilderGeneratorTest {
                 + "static int bindingHashCode(final test.@NonNull test obj) {\n"
                 + "    final int prime = 31;\n"
                 + "    int result = 1;\n"
-                + "    result = prime * result + obj.augmentations().hashCode();\n"
+                + "    for (var augmentation : obj.augmentations().values()) {\n"
+                + "        result += augmentation.hashCode();\n"
+                + "    }\n"
                 + "    return result;\n"
                 + "}\n", genHashCode(mockAugment(mockGenType(TEST))).toString());
     }
 
     @Test
     public void builderTemplateGenerateHashCodeWithPropertyWithAugmentTest() throws Exception {
-        assertEquals("/**\n"
+        assertXtendEquals("/**\n"
                 + " * Default implementation of {@link Object#hashCode()} contract for this interface.\n"
                 + " * Implementations of this interface are encouraged to defer to this method to get consistent"
                 + " hashing\n"
@@ -119,14 +121,16 @@ public class BuilderGeneratorTest {
                 + "    final int prime = 31;\n"
                 + "    int result = 1;\n"
                 + "    result = prime * result + Objects.hashCode(obj.getTest());\n"
-                + "    result = prime * result + obj.augmentations().hashCode();\n"
+                + "    for (var augmentation : obj.augmentations().values()) {\n"
+                + "        result += augmentation.hashCode();\n"
+                + "    }\n"
                 + "    return result;\n"
                 + "}\n", genHashCode(mockAugment(mockGenType("get" + TEST))).toString());
     }
 
     @Test
     public void builderTemplateGenerateHashCodeWithMorePropertiesWithAugmentTest() throws Exception {
-        assertEquals("/**\n"
+        assertXtendEquals("/**\n"
                 + " * Default implementation of {@link Object#hashCode()} contract for this interface.\n"
                 + " * Implementations of this interface are encouraged to defer to this method to get consistent"
                 + " hashing\n"
@@ -141,7 +145,9 @@ public class BuilderGeneratorTest {
                 + "    int result = 1;\n"
                 + "    result = prime * result + Objects.hashCode(obj.getTest1());\n"
                 + "    result = prime * result + Objects.hashCode(obj.getTest2());\n"
-                + "    result = prime * result + obj.augmentations().hashCode();\n"
+                + "    for (var augmentation : obj.augmentations().values()) {\n"
+                + "        result += augmentation.hashCode();\n"
+                + "    }\n"
                 + "    return result;\n"
                 + "}\n", genHashCode(mockAugment(mockGenTypeMoreMeth("get" + TEST))).toString());
     }
@@ -150,7 +156,7 @@ public class BuilderGeneratorTest {
     public void builderTemplateGenerateToStringWithPropertyTest() {
         final GeneratedType genType = mockGenType("get" + TEST);
 
-        assertEquals("/**\n"
+        assertXtendEquals("/**\n"
                 + " * Default implementation of {@link Object#toString()} contract for this interface.\n"
                 + " * Implementations of this interface are encouraged to defer to this method to get consistent string"
                 + "\n * representations across all implementations.\n"
@@ -160,7 +166,7 @@ public class BuilderGeneratorTest {
                 + " * @throws NullPointerException if {@code obj} is null\n"
                 + " */\n"
                 + "static String bindingToString(final test.@NonNull test obj) {\n"
-                + "    final MoreObjects.ToStringHelper helper = MoreObjects.toStringHelper(\"test\");\n"
+                + "    final var helper = MoreObjects.toStringHelper(\"test\");\n"
                 + "    CodeHelpers.appendValue(helper, \"test\", obj.gettest());\n"
                 + "    return helper.toString();\n"
                 + "}\n", genToString(genType).toString());
@@ -168,7 +174,7 @@ public class BuilderGeneratorTest {
 
     @Test
     public void builderTemplateGenerateToStringWithoutAnyPropertyTest() throws Exception {
-        assertEquals("/**\n"
+        assertXtendEquals("/**\n"
                 + " * Default implementation of {@link Object#toString()} contract for this interface.\n"
                 + " * Implementations of this interface are encouraged to defer to this method to get consistent string"
                 + "\n * representations across all implementations.\n"
@@ -178,14 +184,14 @@ public class BuilderGeneratorTest {
                 + " * @throws NullPointerException if {@code obj} is null\n"
                 + " */\n"
                 + "static String bindingToString(final test.@NonNull test obj) {\n"
-                + "    final MoreObjects.ToStringHelper helper = MoreObjects.toStringHelper(\"test\");\n"
+                + "    final var helper = MoreObjects.toStringHelper(\"test\");\n"
                 + "    return helper.toString();\n"
                 + "}\n", genToString(mockGenType(TEST)).toString());
     }
 
     @Test
     public void builderTemplateGenerateToStringWithMorePropertiesTest() throws Exception {
-        assertEquals("/**\n"
+        assertXtendEquals("/**\n"
                 + " * Default implementation of {@link Object#toString()} contract for this interface.\n"
                 + " * Implementations of this interface are encouraged to defer to this method to get consistent string"
                 + "\n * representations across all implementations.\n"
@@ -195,7 +201,7 @@ public class BuilderGeneratorTest {
                 + " * @throws NullPointerException if {@code obj} is null\n"
                 + " */\n"
                 + "static String bindingToString(final test.@NonNull test obj) {\n"
-                + "    final MoreObjects.ToStringHelper helper = MoreObjects.toStringHelper(\"test\");\n"
+                + "    final var helper = MoreObjects.toStringHelper(\"test\");\n"
                 + "    CodeHelpers.appendValue(helper, \"test1\", obj.gettest1());\n"
                 + "    CodeHelpers.appendValue(helper, \"test2\", obj.gettest2());\n"
                 + "    return helper.toString();\n"
@@ -204,7 +210,7 @@ public class BuilderGeneratorTest {
 
     @Test
     public void builderTemplateGenerateToStringWithoutPropertyWithAugmentTest() throws Exception {
-        assertEquals("/**\n"
+        assertXtendEquals("/**\n"
                 + " * Default implementation of {@link Object#toString()} contract for this interface.\n"
                 + " * Implementations of this interface are encouraged to defer to this method to get consistent string"
                 + "\n * representations across all implementations.\n"
@@ -214,15 +220,15 @@ public class BuilderGeneratorTest {
                 + " * @throws NullPointerException if {@code obj} is null\n"
                 + " */\n"
                 + "static String bindingToString(final test.@NonNull test obj) {\n"
-                + "    final MoreObjects.ToStringHelper helper = MoreObjects.toStringHelper(\"test\");\n"
-                + "    CodeHelpers.appendValue(helper, \"augmentation\", obj.augmentations().values());\n"
+                + "    final var helper = MoreObjects.toStringHelper(\"test\");\n"
+                + "    CodeHelpers.appendAugmentations(helper, \"augmentation\", obj);\n"
                 + "    return helper.toString();\n"
                 + "}\n", genToString(mockAugment(mockGenType(TEST))).toString());
     }
 
     @Test
     public void builderTemplateGenerateToStringWithPropertyWithAugmentTest() throws Exception {
-        assertEquals("/**\n"
+        assertXtendEquals("/**\n"
                 + " * Default implementation of {@link Object#toString()} contract for this interface.\n"
                 + " * Implementations of this interface are encouraged to defer to this method to get consistent string"
                 + "\n * representations across all implementations.\n"
@@ -232,16 +238,16 @@ public class BuilderGeneratorTest {
                 + " * @throws NullPointerException if {@code obj} is null\n"
                 + " */\n"
                 + "static String bindingToString(final test.@NonNull test obj) {\n"
-                + "    final MoreObjects.ToStringHelper helper = MoreObjects.toStringHelper(\"test\");\n"
+                + "    final var helper = MoreObjects.toStringHelper(\"test\");\n"
                 + "    CodeHelpers.appendValue(helper, \"test\", obj.gettest());\n"
-                + "    CodeHelpers.appendValue(helper, \"augmentation\", obj.augmentations().values());\n"
+                + "    CodeHelpers.appendAugmentations(helper, \"augmentation\", obj);\n"
                 + "    return helper.toString();\n"
                 + "}\n", genToString(mockAugment(mockGenType("get" + TEST))).toString());
     }
 
     @Test
     public void builderTemplateGenerateToStringWithMorePropertiesWithAugmentTest() throws Exception {
-        assertEquals("/**\n"
+        assertXtendEquals("/**\n"
                 + " * Default implementation of {@link Object#toString()} contract for this interface.\n"
                 + " * Implementations of this interface are encouraged to defer to this method to get consistent string"
                 + "\n * representations across all implementations.\n"
@@ -251,20 +257,22 @@ public class BuilderGeneratorTest {
                 + " * @throws NullPointerException if {@code obj} is null\n"
                 + " */\n"
                 + "static String bindingToString(final test.@NonNull test obj) {\n"
-                + "    final MoreObjects.ToStringHelper helper = MoreObjects.toStringHelper(\"test\");\n"
+                + "    final var helper = MoreObjects.toStringHelper(\"test\");\n"
                 + "    CodeHelpers.appendValue(helper, \"test1\", obj.gettest1());\n"
                 + "    CodeHelpers.appendValue(helper, \"test2\", obj.gettest2());\n"
-                + "    CodeHelpers.appendValue(helper, \"augmentation\", obj.augmentations().values());\n"
+                + "    CodeHelpers.appendAugmentations(helper, \"augmentation\", obj);\n"
                 + "    return helper.toString();\n"
                 + "}\n", genToString(mockAugment(mockGenTypeMoreMeth("get" + TEST))).toString());
     }
 
     @Test
     public void builderTemplateGenerateToEqualsComparingOrderTest() {
-        final EffectiveModelContext context = YangParserTestUtils.parseYangResource(
-                "/test-types.yang");
-        final List<GeneratedType> types = new DefaultBindingGenerator().generateTypes(context);
-        final BuilderTemplate bt = BuilderGenerator.templateForType(types.get(19));
+        final var context = YangParserTestUtils.parseYangResource("/test-types.yang");
+        final var types = new DefaultBindingGenerator().generateTypes(context);
+        assertEquals(27, types.size());
+
+        final BuilderTemplate bt = BuilderGenerator.templateForType(
+            types.stream().filter(t -> t.getName().equals("Nodes")).findFirst().orElseThrow());
 
         final List<String> sortedProperties = bt.properties.stream()
                 .sorted(ByTypeMemberComparator.getInstance())
@@ -349,4 +357,10 @@ public class BuilderGeneratorTest {
         doReturn(ValueMechanics.NORMAL).when(methSign).getMechanics();
         return methSign;
     }
+
+    // Xtend's StringConcatenation is using runtime-configured line separator, which can change between runs, notably
+    // it has a different value on Windows. Make sure we account for that.
+    private static void assertXtendEquals(final String expected, final String actual) {
+        assertEquals(expected.replace("\n", StringConcatenation.DEFAULT_LINE_DELIMITER), actual);
+    }
 }