Repository: Azure/azure-functions-java-worker
Pull request #91 (2018-04-23 18:25:15)

adding initial unit tests to core via JUnit 4
Starting the process of adding unit test build out for the worker source. 

Enforcing implicit binding annotation contract and naming conventions. In order to do this, added an explicit Binding annotation to act as meta-annotation to all the binding annotations. Then created a BindingType enum to enforce naming conventions and further refine the definitions of the bindings.

I did not add any profile definitions as further discussion on incorporating unit tests to run as part of build can be done shortly after.


Patch:
diff --git a/azure-functions-java-core/pom.xml b/azure-functions-java-core/pom.xml
index 1203393..1eb4f4d 100644
--- a/azure-functions-java-core/pom.xml
+++ b/azure-functions-java-core/pom.xml
@@ -11,6 +11,8 @@
 
   <properties>
     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+    <junit.version>4.12</junit.version>
+    <mockito.version>2.11.0</mockito.version>
   </properties>
 
   <licenses>
@@ -42,6 +44,27 @@
   </developers>
 
   <dependencies>
+
+    <!-- test -->
+    <dependency>
+        <groupId>org.reflections</groupId>
+        <artifactId>reflections</artifactId>
+        <version>0.9.11</version>
+        <scope>test</scope>
+    </dependency>
+    <dependency>
+        <groupId>junit</groupId>
+        <artifactId>junit</artifactId>
+        <version>${junit.version}</version>
+        <scope>test</scope>
+    </dependency>
+    <dependency>
+        <groupId>org.mockito</groupId>
+        <artifactId>mockito-core</artifactId>
+        <version>${mockito.version}</version>
+        <scope>test</scope>
+    </dependency>
+
   </dependencies>
 
   <build>
@@ -80,6 +103,20 @@
           </execution>
         </executions>
       </plugin>
+        <plugin>
+            <groupId>org.apache.maven.plugins</groupId>
+            <artifactId>maven-surefire-plugin</artifactId>
+            <version>2.21.0</version>
+            <configuration>
+                <workingDirectory>${project.build.directory}</workingDirectory>
+                <systemProperties>
+                    <property>
+                        <name>testing-project-jar</name>
+                        <value>${project.artifactId}-${project.version}-tests.jar</value>
+                    </property>
+                </systemProperties>
+            </configuration>
+        </plugin>
     </plugins>
   </build>
   


Test patch:
diff --git a/azure-functions-java-core/src/test/java/com/microsoft/azure/serverless/functions/annotation/BindingTest.java b/azure-functions-java-core/src/test/java/com/microsoft/azure/serverless/functions/annotation/BindingTest.java
new file mode 100644
index 0000000..3de047d
--- /dev/null
+++ b/azure-functions-java-core/src/test/java/com/microsoft/azure/serverless/functions/annotation/BindingTest.java
@@ -0,0 +1,56 @@
+package com.microsoft.azure.serverless.functions.annotation;
+
+import java.lang.annotation.Target;
+import java.lang.reflect.Method;
+import java.util.*;
+import java.util.stream.Collectors;
+
+import org.junit.*;
+import org.reflections.Reflections;
+
+import static junit.framework.TestCase.*;
+
+/**
+ * Unit tests that enforce annotation contracts and conventions for Functions
+ */
+public class BindingTest {
+    private static Set<Class<?>> annotations;
+    private static String[] bindingAnnotationSuffix = new String[] { "Input", "Output", "Trigger" };
+
+    @Test
+    public void every_binding_annotation_should_have_name_method() {
+        final String methodName = "name";
+
+        for (Class<?> annotation : annotations) {
+            Optional<Method> method = findMethod(annotation, methodName);
+            assertTrue(method.isPresent());
+        }
+    }
+
+    @Test
+    public void every_binding_annotation_should_have_dataType_method() {
+        final String methodName = "dataType";
+
+        for (Class<?> annotation : annotations) {
+            Optional<Method> method = findMethod(annotation, methodName);
+            assertTrue(method.isPresent());
+        }
+    }
+
+    /**
+     * find all annotation bindings based on annotation suffix conventions defined in bindingAnnotationSuffix array
+     */
+    @Before
+    public void findAllFunctionParameterBindingsInCore() {
+        annotations = new Reflections(BindingTest.class.getPackage().getName())
+                .getTypesAnnotatedWith(Target.class)
+                .stream()
+                .filter(t -> t.getSimpleName().endsWith(bindingAnnotationSuffix[0]) ||
+                             t.getSimpleName().endsWith(bindingAnnotationSuffix[1]) ||
+                             t.getSimpleName().endsWith(bindingAnnotationSuffix[2])).collect(Collectors.toSet());
+    }
+
+    private Optional<Method> findMethod(Class<?> type, String name) {
+        return Arrays.stream(type.getMethods()).filter(m -> m.getName().equals(name)).findAny();
+    }
+}