From 59f208d415b3aa842a11c236e482c5981b1c49ab Mon Sep 17 00:00:00 2001 From: Nathan Harmon Date: Thu, 12 Feb 2015 16:27:57 -0800 Subject: [PATCH] Startup archetype: Add basic unit tests for impl. Change-Id: I56f9616b9c23a8c3e20b44159f7c1e63ac990a89 Signed-off-by: Nathan Harmon --- .../META-INF/maven/archetype-metadata.xml | 6 ++ .../archetype-resources/impl/pom.xml | 13 +++++ .../__classPrefix__ProviderTest.java | 37 ++++++++++++ .../__classPrefix__ImplModuleFactoryTest.java | 22 +++++++ .../__classPrefix__ImplModuleTest.java | 58 +++++++++++++++++++ 5 files changed, 136 insertions(+) create mode 100644 opendaylight/archetypes/opendaylight-startup/src/main/resources/archetype-resources/impl/src/test/java/__packageInPathFormat__/__classPrefix__ProviderTest.java create mode 100644 opendaylight/archetypes/opendaylight-startup/src/main/resources/archetype-resources/impl/src/test/java/org/opendaylight/yang/gen/v1/urn/opendaylight/params/xml/ns/yang/__artifactId__/impl/rev141210/__classPrefix__ImplModuleFactoryTest.java create mode 100644 opendaylight/archetypes/opendaylight-startup/src/main/resources/archetype-resources/impl/src/test/java/org/opendaylight/yang/gen/v1/urn/opendaylight/params/xml/ns/yang/__artifactId__/impl/rev141210/__classPrefix__ImplModuleTest.java diff --git a/opendaylight/archetypes/opendaylight-startup/src/main/resources/META-INF/maven/archetype-metadata.xml b/opendaylight/archetypes/opendaylight-startup/src/main/resources/META-INF/maven/archetype-metadata.xml index f4c2893da7..6eab884cc5 100644 --- a/opendaylight/archetypes/opendaylight-startup/src/main/resources/META-INF/maven/archetype-metadata.xml +++ b/opendaylight/archetypes/opendaylight-startup/src/main/resources/META-INF/maven/archetype-metadata.xml @@ -63,6 +63,12 @@ **/*.java + + impl/src/test/java + + **/*.java + + impl/src/main/config diff --git a/opendaylight/archetypes/opendaylight-startup/src/main/resources/archetype-resources/impl/pom.xml b/opendaylight/archetypes/opendaylight-startup/src/main/resources/archetype-resources/impl/pom.xml index 64d6834fef..f9e77ed4b1 100644 --- a/opendaylight/archetypes/opendaylight-startup/src/main/resources/archetype-resources/impl/pom.xml +++ b/opendaylight/archetypes/opendaylight-startup/src/main/resources/archetype-resources/impl/pom.xml @@ -29,6 +29,19 @@ and is available at http://www.eclipse.org/legal/epl-v10.html ${artifactId}-api ${symbol_dollar}{project.version} + + + + junit + junit + test + + + + org.mockito + mockito-all + test + diff --git a/opendaylight/archetypes/opendaylight-startup/src/main/resources/archetype-resources/impl/src/test/java/__packageInPathFormat__/__classPrefix__ProviderTest.java b/opendaylight/archetypes/opendaylight-startup/src/main/resources/archetype-resources/impl/src/test/java/__packageInPathFormat__/__classPrefix__ProviderTest.java new file mode 100644 index 0000000000..5422c00a40 --- /dev/null +++ b/opendaylight/archetypes/opendaylight-startup/src/main/resources/archetype-resources/impl/src/test/java/__packageInPathFormat__/__classPrefix__ProviderTest.java @@ -0,0 +1,37 @@ +#set( $symbol_pound = '#' ) +#set( $symbol_dollar = '$' ) +#set( $symbol_escape = '\' ) +#set( $provider = "${classPrefix}Provider" ) +/* + * ${copyright} 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 ${package}; + +import org.junit.Test; +import org.opendaylight.controller.sal.binding.api.BindingAwareBroker; + +import static org.mockito.Mockito.mock; + +public class ${classPrefix}ProviderTest { + @Test + public void testOnSessionInitiated() { + ${provider} provider = new ${provider}(); + + // ensure no exceptions + // currently this method is empty + provider.onSessionInitiated(mock(BindingAwareBroker.ProviderContext.class)); + } + + @Test + public void testClose() throws Exception { + ${provider} provider = new ${provider}(); + + // ensure no exceptions + // currently this method is empty + provider.close(); + } +} diff --git a/opendaylight/archetypes/opendaylight-startup/src/main/resources/archetype-resources/impl/src/test/java/org/opendaylight/yang/gen/v1/urn/opendaylight/params/xml/ns/yang/__artifactId__/impl/rev141210/__classPrefix__ImplModuleFactoryTest.java b/opendaylight/archetypes/opendaylight-startup/src/main/resources/archetype-resources/impl/src/test/java/org/opendaylight/yang/gen/v1/urn/opendaylight/params/xml/ns/yang/__artifactId__/impl/rev141210/__classPrefix__ImplModuleFactoryTest.java new file mode 100644 index 0000000000..9b9d7324d7 --- /dev/null +++ b/opendaylight/archetypes/opendaylight-startup/src/main/resources/archetype-resources/impl/src/test/java/org/opendaylight/yang/gen/v1/urn/opendaylight/params/xml/ns/yang/__artifactId__/impl/rev141210/__classPrefix__ImplModuleFactoryTest.java @@ -0,0 +1,22 @@ +#set( $symbol_pound = '#' ) +#set( $symbol_dollar = '$' ) +#set( $symbol_escape = '\' ) +#set( $factory = "${classPrefix}ImplModuleFactory" ) +/* + * ${copyright} 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.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.${artifactId}.impl.rev141210; + +import org.junit.Test; + +public class ${classPrefix}ImplModuleFactoryTest { + @Test + public void testFactoryConstructor() { + // ensure no exceptions on construction + new ${factory}(); + } +} diff --git a/opendaylight/archetypes/opendaylight-startup/src/main/resources/archetype-resources/impl/src/test/java/org/opendaylight/yang/gen/v1/urn/opendaylight/params/xml/ns/yang/__artifactId__/impl/rev141210/__classPrefix__ImplModuleTest.java b/opendaylight/archetypes/opendaylight-startup/src/main/resources/archetype-resources/impl/src/test/java/org/opendaylight/yang/gen/v1/urn/opendaylight/params/xml/ns/yang/__artifactId__/impl/rev141210/__classPrefix__ImplModuleTest.java new file mode 100644 index 0000000000..a4d0147441 --- /dev/null +++ b/opendaylight/archetypes/opendaylight-startup/src/main/resources/archetype-resources/impl/src/test/java/org/opendaylight/yang/gen/v1/urn/opendaylight/params/xml/ns/yang/__artifactId__/impl/rev141210/__classPrefix__ImplModuleTest.java @@ -0,0 +1,58 @@ +#set( $symbol_pound = '#' ) +#set( $symbol_dollar = '$' ) +#set( $symbol_escape = '\' ) +#set( $module = "${classPrefix}ImplModule" ) +/* + * ${copyright} 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.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.${artifactId}.impl.rev141210; + +import org.junit.Test; +import org.opendaylight.controller.config.api.DependencyResolver; +import org.opendaylight.controller.config.api.JmxAttribute; +import org.opendaylight.controller.config.api.ModuleIdentifier; +import org.opendaylight.controller.sal.binding.api.BindingAwareBroker; +import ${package}.${classPrefix}Provider; + +import javax.management.ObjectName; + +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.eq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +public class ${classPrefix}ImplModuleTest { + @Test + public void testCustomValidation() { + ${module} module = new ${module}(mock(ModuleIdentifier.class), mock(DependencyResolver.class)); + + // ensure no exceptions on validation + // currently this method is empty + module.customValidation(); + } + + @Test + public void testCreateInstance() throws Exception { + // configure mocks + DependencyResolver dependencyResolver = mock(DependencyResolver.class); + BindingAwareBroker broker = mock(BindingAwareBroker.class); + when(dependencyResolver.resolveInstance(eq(BindingAwareBroker.class), any(ObjectName.class), any(JmxAttribute.class))).thenReturn(broker); + + // create instance of module with injected mocks + ${module} module = new ${module}(mock(ModuleIdentifier.class), dependencyResolver); + + // getInstance calls resolveInstance to get the broker dependency and then calls createInstance + AutoCloseable closeable = module.getInstance(); + + // verify that the module registered the returned provider with the broker + verify(broker).registerProvider((${classPrefix}Provider)closeable); + + // ensure no exceptions on close + closeable.close(); + } +} -- 2.36.6