Add tests for generateBindingHashCode() 54/91454/14
authorillia.ihushev <illia.ihushev@pantheon.tech>
Mon, 20 Jul 2020 16:11:36 +0000 (19:11 +0300)
committerRobert Varga <nite@hq.sk>
Mon, 3 Aug 2020 07:56:07 +0000 (07:56 +0000)
Check bindingHashCode() declaration correctness.

JIRA: MDSAL-471
Change-Id: Iebf4963c1069b824ef97d07c9a81bff08cf3cabd
Signed-off-by: illia.ihushev <illia.ihushev@pantheon.tech>
binding/mdsal-binding-java-api-generator/src/main/java/org/opendaylight/mdsal/binding/java/api/generator/InterfaceTemplate.xtend
binding/mdsal-binding-java-api-generator/src/test/java/org/opendaylight/mdsal/binding/java/api/generator/BuilderGeneratorTest.java

index 61dbcf89a4809d91825cf09caf31c7b433a3c0cb..62d1df8d5c358f7c01c64be62711173885e4e5d2 100644 (file)
@@ -17,6 +17,7 @@ import static org.opendaylight.mdsal.binding.spec.naming.BindingMapping.BINDING_
 import static org.opendaylight.mdsal.binding.spec.naming.BindingMapping.BINDING_TO_STRING_NAME
 import static org.opendaylight.mdsal.binding.spec.naming.BindingMapping.DATA_CONTAINER_IMPLEMENTED_INTERFACE_NAME
 
+import com.google.common.annotations.VisibleForTesting;
 import com.google.common.base.MoreObjects
 import java.util.List
 import java.util.Map.Entry
@@ -220,7 +221,8 @@ class InterfaceTemplate extends BaseTemplate {
         }
     '''
 
-    def private generateBindingHashCode() '''
+    @VisibleForTesting
+    def generateBindingHashCode() '''
         «val augmentable = analyzeType»
         /**
          * Default implementation of {@link «Object.importedName»#hashCode()} contract for this interface.
index b2317e15f6db8132225dde649ea632b15e8d5b52..08c0482d83750b2dc18f7449dae34dda48b3a7c4 100644 (file)
@@ -34,6 +34,121 @@ public class BuilderGeneratorTest {
         assertEquals("", new BuilderGenerator().generate(mock(Type.class)));
     }
 
+    @Test
+    public void builderTemplateGenerateHashcodeWithPropertyTest() {
+        final GeneratedType genType = mockGenType("get" + TEST);
+
+        assertEquals("/**\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"
+                + " * results across all implementations.\n"
+                + " *\n"
+                + " * @param obj Object for which to generate hashCode() result.\n"
+                + " * @return Hash code value of data modeled by this interface.\n"
+                + " * @throws NullPointerException if {@code obj} is null\n"
+                + " */\n"
+                + "static int bindingHashCode(final test.test obj) {\n"
+                + "    final int prime = 31;\n"
+                + "    int result = 1;\n"
+                + "    result = prime * result + Objects.hashCode(obj.getTest());\n"
+                + "    return result;\n"
+                + "}\n", genHashCode(genType).toString());
+    }
+
+    @Test
+    public void builderTemplateGenerateHashCodeWithMorePropertiesTest() throws Exception {
+        assertEquals("/**\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"
+                + " * results across all implementations.\n"
+                + " *\n"
+                + " * @param obj Object for which to generate hashCode() result.\n"
+                + " * @return Hash code value of data modeled by this interface.\n"
+                + " * @throws NullPointerException if {@code obj} is null\n"
+                + " */\n"
+                + "static int bindingHashCode(final test.test obj) {\n"
+                + "    final int prime = 31;\n"
+                + "    int result = 1;\n"
+                + "    result = prime * result + Objects.hashCode(obj.getTest1());\n"
+                + "    result = prime * result + Objects.hashCode(obj.getTest2());\n"
+                + "    return result;\n"
+                + "}\n", genHashCode(mockGenTypeMoreMeth("get" + TEST)).toString());
+    }
+
+    @Test
+    public void builderTemplateGenerateHashCodeWithoutPropertyWithAugmentTest() throws Exception {
+        assertEquals("/**\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"
+                + " * results across all implementations.\n"
+                + " *\n"
+                + " * @param <T$$> implementation type, which has to also implement AugmentationHolder interface\n"
+                + " *              contract.\n"
+                + " * @param obj Object for which to generate hashCode() result.\n"
+                + " * @return Hash code value of data modeled by this interface.\n"
+                + " * @throws NullPointerException if {@code obj} is null\n"
+                + " */\n"
+                + "static <T$$ extends test.test & AugmentationHolder<?>> int bindingHashCode(final @NonNull T$$ obj) {"
+                + "\n"
+                + "    final int prime = 31;\n"
+                + "    int result = 1;\n"
+                + "    result = prime * result + CodeHelpers.hashAugmentations(obj);\n"
+                + "    return result;\n"
+                + "}\n", genHashCode(mockAugment(mockGenType(TEST))).toString());
+    }
+
+    @Test
+    public void builderTemplateGenerateHashCodeWithPropertyWithAugmentTest() throws Exception {
+        assertEquals("/**\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"
+                + " * results across all implementations.\n"
+                + " *\n"
+                + " * @param <T$$> implementation type, which has to also implement AugmentationHolder interface\n"
+                + " *              contract.\n"
+                + " * @param obj Object for which to generate hashCode() result.\n"
+                + " * @return Hash code value of data modeled by this interface.\n"
+                + " * @throws NullPointerException if {@code obj} is null\n"
+                + " */\n"
+                + "static <T$$ extends test.test & AugmentationHolder<?>> int bindingHashCode(final @NonNull T$$ obj) {"
+                + "\n"
+                + "    final int prime = 31;\n"
+                + "    int result = 1;\n"
+                + "    result = prime * result + Objects.hashCode(obj.getTest());\n"
+                + "    result = prime * result + CodeHelpers.hashAugmentations(obj);\n"
+                + "    return result;\n"
+                + "}\n", genHashCode(mockAugment(mockGenType("get" + TEST))).toString());
+    }
+
+    @Test
+    public void builderTemplateGenerateHashCodeWithMorePropertiesWithAugmentTest() throws Exception {
+        assertEquals("/**\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"
+                + " * results across all implementations.\n"
+                + " *\n"
+                + " * @param <T$$> implementation type, which has to also implement AugmentationHolder interface\n"
+                + " *              contract.\n"
+                + " * @param obj Object for which to generate hashCode() result.\n"
+                + " * @return Hash code value of data modeled by this interface.\n"
+                + " * @throws NullPointerException if {@code obj} is null\n"
+                + " */\n"
+                + "static <T$$ extends test.test & AugmentationHolder<?>> int bindingHashCode(final @NonNull T$$ obj) {"
+                + "\n"
+                + "    final int prime = 31;\n"
+                + "    int result = 1;\n"
+                + "    result = prime * result + Objects.hashCode(obj.getTest1());\n"
+                + "    result = prime * result + Objects.hashCode(obj.getTest2());\n"
+                + "    result = prime * result + CodeHelpers.hashAugmentations(obj);\n"
+                + "    return result;\n"
+                + "}\n", genHashCode(mockAugment(mockGenTypeMoreMeth("get" + TEST))).toString());
+    }
+
     @Test
     public void builderTemplateGenerateToStringWithPropertyTest() {
         final GeneratedType genType = mockGenType("get" + TEST);
@@ -215,6 +330,10 @@ public class BuilderGeneratorTest {
         return new InterfaceTemplate(genType).generateBindingToString();
     }
 
+    private static CharSequence genHashCode(final GeneratedType genType) {
+        return new InterfaceTemplate(genType).generateBindingHashCode();
+    }
+
     private static GeneratedType mockGenType(final String methodeName) {
         final GeneratedType genType = spy(GeneratedType.class);
         doReturn(TYPE_NAME).when(genType).getIdentifier();
@@ -236,6 +355,7 @@ public class BuilderGeneratorTest {
         doReturn(methodeName).when(methSign).getName();
         final Type methType = mock(Type.class);
         doReturn(TYPE_NAME).when(methType).getIdentifier();
+        doReturn(TEST).when(methType).getName();
         doReturn(methType).when(methSign).getReturnType();
         doReturn(ValueMechanics.NORMAL).when(methSign).getMechanics();
         return methSign;