Adjust to yangtools-2.0.0 changes
[controller.git] / opendaylight / config / yang-jmx-generator / src / test / java / org / opendaylight / controller / config / yangjmxgenerator / ModuleMXBeanEntryTest.java
index 8ca2bb5..d7fb213 100644 (file)
@@ -7,8 +7,30 @@
  */
 package org.opendaylight.controller.config.yangjmxgenerator;
 
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.CoreMatchers.isA;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.mock;
+
 import com.google.common.collect.Sets;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.util.Collection;
+import java.util.Collections;
 import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+import java.util.regex.Matcher;
+import javax.management.openmbean.ArrayType;
+import javax.management.openmbean.CompositeType;
+import javax.management.openmbean.SimpleType;
 import org.junit.Before;
 import org.junit.Test;
 import org.opendaylight.controller.config.yangjmxgenerator.attribute.AttributeIfc;
@@ -18,106 +40,60 @@ import org.opendaylight.controller.config.yangjmxgenerator.attribute.ListAttribu
 import org.opendaylight.controller.config.yangjmxgenerator.attribute.ListDependenciesAttribute;
 import org.opendaylight.controller.config.yangjmxgenerator.attribute.TOAttribute;
 import org.opendaylight.controller.config.yangjmxgenerator.attribute.TypedAttribute;
-import org.opendaylight.yangtools.binding.generator.util.Types;
-import org.opendaylight.yangtools.sal.binding.model.api.Type;
-import org.opendaylight.yangtools.sal.binding.yang.types.TypeProviderImpl;
+import org.opendaylight.mdsal.binding.model.api.Type;
+import org.opendaylight.mdsal.binding.model.util.Types;
+import org.opendaylight.mdsal.binding.yang.types.TypeProviderImpl;
 import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.common.Revision;
 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode;
 import org.opendaylight.yangtools.yang.model.api.RevisionAwareXPath;
 
-import javax.management.openmbean.ArrayType;
-import javax.management.openmbean.CompositeType;
-import javax.management.openmbean.SimpleType;
-import java.net.URI;
-import java.net.URISyntaxException;
-import java.text.ParseException;
-import java.text.SimpleDateFormat;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.Date;
-import java.util.HashSet;
-import java.util.Map;
-import java.util.Set;
-import java.util.regex.Matcher;
-
-import static org.hamcrest.CoreMatchers.is;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertNull;
-import static org.junit.Assert.assertThat;
-import static org.junit.Assert.assertTrue;
-import static org.mockito.Mockito.doReturn;
-import static org.mockito.Mockito.mock;
-
 public class ModuleMXBeanEntryTest extends AbstractYangTest {
-    public static final String EVENTBUS_MXB_NAME = "eventbus";
-    public static final String ASYNC_EVENTBUS_MXB_NAME = "async-eventbus";
-    public static final String THREADFACTORY_NAMING_MXB_NAME = "threadfactory-naming";
-    public static final String THREADPOOL_DYNAMIC_MXB_NAME = "threadpool-dynamic";
-    public static final String THREADPOOL_REGISTRY_IMPL_NAME = "threadpool-registry-impl";
-
-    public static final String BGP_LISTENER_IMPL_MXB_NAME = "bgp-listener-impl";
 
     public static final String PACKAGE_NAME = "pack2";
 
     protected static final URI THREADS_NAMESPACE;
-    protected static final Date THREADS_REVISION_DATE;
+    protected static final Revision THREADS_REVISION_DATE = Revision.of("2013-04-09");
 
     static {
         try {
-            THREADS_NAMESPACE = new URI(ConfigConstants.CONFIG_NAMESPACE
-                    + ":threads");
-        } catch (URISyntaxException e) {
-            throw new Error(e);
-        }
-        SimpleDateFormat revisionFormat = new SimpleDateFormat("yyyy-MM-dd");
-        try {
-            THREADS_REVISION_DATE = revisionFormat.parse("2013-04-09");
-        } catch (ParseException e) {
-            throw new Error(e);
+            THREADS_NAMESPACE = new URI(ConfigConstants.CONFIG_NAMESPACE + ":threads");
+        } catch (final URISyntaxException e) {
+            throw new ExceptionInInitializerError(e);
         }
     }
 
     protected Map<QName, ServiceInterfaceEntry> modulesToSIEs;
 
-    protected Map<String /* identity local name */, ModuleMXBeanEntry> loadThreadsJava() {
-        Map<String /* identity local name */, ModuleMXBeanEntry> namesToMBEs = ModuleMXBeanEntry
-                .create(threadsJavaModule, modulesToSIEs, context, new TypeProviderWrapper(new TypeProviderImpl
-                        (context)), PACKAGE_NAME);
-        assertNotNull(namesToMBEs);
-        Set<String> expectedMXBeanNames = Sets.newHashSet(EVENTBUS_MXB_NAME,
-                ASYNC_EVENTBUS_MXB_NAME, THREADFACTORY_NAMING_MXB_NAME,
-                THREADPOOL_DYNAMIC_MXB_NAME, THREADPOOL_REGISTRY_IMPL_NAME);
-        assertThat(namesToMBEs.keySet(), is(expectedMXBeanNames));
-        return namesToMBEs;
-    }
 
     @Before
     public void setUp() {
-        Map<IdentitySchemaNode, ServiceInterfaceEntry> identitiesToSIs = new HashMap<>();
-        modulesToSIEs = ServiceInterfaceEntry.create(threadsModule,
-                "packages.sis",identitiesToSIs);
+        this.modulesToSIEs = loadThreadsServiceInterfaceEntries("packages.sis");
+    }
+
+
+    protected Map<String /* identity local name */, ModuleMXBeanEntry> loadThreadsJava() {
+        return loadThreadsJava(this.modulesToSIEs, PACKAGE_NAME);
     }
 
     @Test
     public void test_jmxImplModule() {
-        Map<IdentitySchemaNode, ServiceInterfaceEntry> identitiesToSIs = new HashMap<>();
-        Map<QName, ServiceInterfaceEntry> modulesToSIEs = ServiceInterfaceEntry
-                .create(threadsModule, PACKAGE_NAME,identitiesToSIs);
-        modulesToSIEs.putAll(ServiceInterfaceEntry.create(jmxModule,
+        final Map<IdentitySchemaNode, ServiceInterfaceEntry> identitiesToSIs = new HashMap<>();
+        final Map<QName, ServiceInterfaceEntry> modulesToSIEs = ServiceInterfaceEntry
+                .create(this.threadsModule, PACKAGE_NAME,identitiesToSIs);
+        modulesToSIEs.putAll(ServiceInterfaceEntry.create(this.jmxModule,
                 PACKAGE_NAME,identitiesToSIs));
-        Map<String /* identity local name */, ModuleMXBeanEntry> namesToMBEs = ModuleMXBeanEntry
-                .create(jmxImplModule, modulesToSIEs, context, new TypeProviderWrapper(new TypeProviderImpl(context))
-                        , PACKAGE_NAME);
-        Map<String, AttributeIfc> attributes = namesToMBEs.get("impl-netconf")
+        final Map<String /* identity local name */, ModuleMXBeanEntry> namesToMBEs = ModuleMXBeanEntry
+                .create(this.jmxImplModule, modulesToSIEs, this.context, new TypeProviderWrapper(new TypeProviderImpl(this.context))
+                , PACKAGE_NAME);
+        final Map<String, AttributeIfc> attributes = namesToMBEs.get("impl-netconf")
                 .getAttributes();
 
         assertCorrectAttributesSize(namesToMBEs, attributes);
 
         //
-        DependencyAttribute threadFactoryAttribute = (DependencyAttribute) attributes
+        final DependencyAttribute threadFactoryAttribute = (DependencyAttribute) attributes
                 .get("thread-factory");
         assertNotNull(threadFactoryAttribute);
         assertFalse(threadFactoryAttribute.getDependency().isMandatory());
@@ -129,13 +105,13 @@ public class ModuleMXBeanEntryTest extends AbstractYangTest {
                 is("threadFactory"));
         assertThat(threadFactoryAttribute.getUpperCaseCammelCase(),
                 is("ThreadFactory"));
-        assertThat(threadFactoryAttribute.getOpenType(), is(SimpleType.class));
+        assertThat(threadFactoryAttribute.getOpenType(), isA(SimpleType.class));
         assertNull(threadFactoryAttribute.getNullableDefault());
         assertNull(threadFactoryAttribute.getNullableDescription());
         assertThat(threadFactoryAttribute.getType().getName(), is("ObjectName"));
     }
 
-    private void assertCorrectAttributesSize(Map<String, ModuleMXBeanEntry> namesToMBEs, Map<String, AttributeIfc> attributes) {
+    private void assertCorrectAttributesSize(final Map<String, ModuleMXBeanEntry> namesToMBEs, final Map<String, AttributeIfc> attributes) {
         assertEquals(14, attributes.size());
         assertEquals(1, namesToMBEs.get("impl-netconf").getRuntimeBeans().size());
         assertEquals(2, namesToMBEs.get("impl-netconf").getRuntimeBeans().iterator().next().getAttributes().size());
@@ -146,15 +122,27 @@ public class ModuleMXBeanEntryTest extends AbstractYangTest {
     }
 
     protected RuntimeBeanEntry findFirstByYangName(
-            Collection<RuntimeBeanEntry> runtimeBeans, String yangName) {
-        for (RuntimeBeanEntry rb : runtimeBeans) {
-            if (yangName.equals(rb.getYangName()))
+            final Collection<RuntimeBeanEntry> runtimeBeans, final String yangName) {
+        for (final RuntimeBeanEntry rb : runtimeBeans) {
+            if (yangName.equals(rb.getYangName())) {
                 return rb;
+            }
         }
         throw new IllegalArgumentException("Yang name not found:" + yangName
                 + " in " + runtimeBeans);
     }
 
+    protected RuntimeBeanEntry findFirstByNamePrefix(final Collection<RuntimeBeanEntry> runtimeBeans, final String namePrefix) {
+        for (final RuntimeBeanEntry rb : runtimeBeans) {
+            if (namePrefix.equals(rb.getJavaNamePrefix())) {
+                return rb;
+            }
+        }
+
+        throw new IllegalArgumentException("Name prefix not found:" + namePrefix
+            + " in " + runtimeBeans);
+    }
+
     @Test
     public void testGetWhenConditionMatcher() {
         assertMatches("config",
@@ -165,10 +153,10 @@ public class ModuleMXBeanEntryTest extends AbstractYangTest {
                 "/config:modules/config:module/config:type=\"threadpool-dynamic\"");
     }
 
-    private void assertMatches(String prefix, String input) {
-        RevisionAwareXPath whenConstraint = mock(RevisionAwareXPath.class);
+    private void assertMatches(final String prefix, final String input) {
+        final RevisionAwareXPath whenConstraint = mock(RevisionAwareXPath.class);
         doReturn(input).when(whenConstraint).toString();
-        Matcher output = ModuleMXBeanEntryBuilder.getWhenConditionMatcher(prefix,
+        final Matcher output = ModuleMXBeanEntryBuilder.getWhenConditionMatcher(prefix,
                 whenConstraint);
         assertTrue(output.matches());
         assertEquals("threadpool-dynamic", output.group(1));
@@ -176,46 +164,46 @@ public class ModuleMXBeanEntryTest extends AbstractYangTest {
 
     @Test
     public void testThreadsJava() {
-        Map<String /* identity local name */, ModuleMXBeanEntry> namesToMBEs = loadThreadsJava();
+        final Map<String /* identity local name */, ModuleMXBeanEntry> namesToMBEs = loadThreadsJava();
 
         { // check threadpool-dynamic
-            ModuleMXBeanEntry dynamicThreadPool = namesToMBEs
+            final ModuleMXBeanEntry dynamicThreadPool = namesToMBEs
                     .get(THREADPOOL_DYNAMIC_MXB_NAME);
-            Map<String, AttributeIfc> attributes = dynamicThreadPool
+            final Map<String, AttributeIfc> attributes = dynamicThreadPool
                     .getAttributes();
             // core-size, keepalive, maximum-size
             // threadfactory
-            Set<String> longAttribs = Sets.newHashSet("core-size",
+            final Set<String> longAttribs = Sets.newHashSet("core-size",
                     "maximum-size");
-            for (String longAttrib : longAttribs) {
+            for (final String longAttrib : longAttribs) {
 
-                TypedAttribute attribute = (TypedAttribute) attributes
+                final TypedAttribute attribute = (TypedAttribute) attributes
                         .get(longAttrib);
                 assertThat("Failed to check " + longAttrib,
                         attribute.getType(),
                         is((Type) Types.typeForClass(Long.class)));
             }
             // check dependency on thread factory
-            QName threadfactoryQName = new QName(THREADS_NAMESPACE,
+            final QName threadfactoryQName = QName.create(THREADS_NAMESPACE,
                     THREADS_REVISION_DATE, "threadfactory");
-            ServiceInterfaceEntry threadFactorySIEntry = modulesToSIEs
+            final ServiceInterfaceEntry threadFactorySIEntry = this.modulesToSIEs
                     .get(threadfactoryQName);
             assertNotNull(threadFactorySIEntry);
-            boolean expectedMandatory = true;
-            TypedAttribute actualThreadFactory = (TypedAttribute) attributes
+            final boolean expectedMandatory = true;
+            final TypedAttribute actualThreadFactory = (TypedAttribute) attributes
                     .get("threadfactory");
 
-            DataSchemaNode mockedDataSchemaNode = mock(DataSchemaNode.class);
+            final DataSchemaNode mockedDataSchemaNode = mock(DataSchemaNode.class);
             doReturn(Collections.emptyList()).when(mockedDataSchemaNode)
-                    .getUnknownSchemaNodes();
+            .getUnknownSchemaNodes();
             doReturn(threadfactoryQName).when(mockedDataSchemaNode).getQName();
-            AttributeIfc expectedDependencyAttribute = new DependencyAttribute(
+            final AttributeIfc expectedDependencyAttribute = new DependencyAttribute(
                     mockedDataSchemaNode, threadFactorySIEntry,
                     expectedMandatory, "threadfactory description");
             assertThat(actualThreadFactory, is(expectedDependencyAttribute));
             assertThat(
                     dynamicThreadPool
-                            .getFullyQualifiedName("DynamicThreadPoolModuleMXBean"),
+                    .getFullyQualifiedName("DynamicThreadPoolModuleMXBean"),
                     is(PACKAGE_NAME + ".DynamicThreadPoolModuleMXBean"));
             assertThat(dynamicThreadPool.getNullableDescription(),
                     is("threadpool-dynamic description"));
@@ -225,14 +213,14 @@ public class ModuleMXBeanEntryTest extends AbstractYangTest {
                     is(THREADPOOL_DYNAMIC_MXB_NAME));
 
             // check root runtime bean
-            Collection<RuntimeBeanEntry> runtimeBeans = dynamicThreadPool
+            final Collection<RuntimeBeanEntry> runtimeBeans = dynamicThreadPool
                     .getRuntimeBeans();
             assertThat(runtimeBeans.size(), is(1));
-            RuntimeBeanEntry rootRB = findFirstByYangName(runtimeBeans,
+            final RuntimeBeanEntry rootRB = findFirstByYangName(runtimeBeans,
                     THREADPOOL_DYNAMIC_MXB_NAME);
             assertThat(rootRB.isRoot(), is(true));
             assertThat(rootRB.getAttributes().size(), is(1));
-            JavaAttribute attribute = (JavaAttribute) rootRB.getAttributes()
+            final JavaAttribute attribute = (JavaAttribute) rootRB.getAttributes()
                     .iterator().next();
             assertThat(attribute.getAttributeYangName(), is("created-sessions"));
             assertThat(rootRB.getYangName(), is(THREADPOOL_DYNAMIC_MXB_NAME));
@@ -240,20 +228,20 @@ public class ModuleMXBeanEntryTest extends AbstractYangTest {
                     is(Long.class.getName()));
         }
         {// check threadfactory-naming
-            ModuleMXBeanEntry threadFactoryNaming = namesToMBEs
+            final ModuleMXBeanEntry threadFactoryNaming = namesToMBEs
                     .get(THREADFACTORY_NAMING_MXB_NAME);
-            Collection<RuntimeBeanEntry> runtimeBeans = threadFactoryNaming
+            final Collection<RuntimeBeanEntry> runtimeBeans = threadFactoryNaming
                     .getRuntimeBeans();
             assertThat(runtimeBeans.size(), is(4));
             {
-                RuntimeBeanEntry threadRB = findFirstByYangName(runtimeBeans,
+                final RuntimeBeanEntry threadRB = findFirstByYangName(runtimeBeans,
                         "thread");
                 assertNotNull(threadRB);
                 assertFalse(threadRB.isRoot());
                 assertEquals("name", threadRB.getKeyYangName().get());
                 assertEquals("Name", threadRB.getKeyJavaName().get());
                 assertThat(threadRB.getAttributes().size(), is(1));
-                AttributeIfc threadNameAttr = threadRB.getAttributes()
+                final AttributeIfc threadNameAttr = threadRB.getAttributes()
                         .iterator().next();
                 assertThat(threadNameAttr.getAttributeYangName(), is("name"));
                 assertTrue(threadNameAttr instanceof JavaAttribute);
@@ -262,47 +250,47 @@ public class ModuleMXBeanEntryTest extends AbstractYangTest {
                 assertThat(threadRB.getRpcs().size(), is(2));
             }
             {
-                RuntimeBeanEntry streamRB = findFirstByYangName(runtimeBeans,
-                        "stream");
+                final RuntimeBeanEntry streamRB = findFirstByNamePrefix(runtimeBeans,
+                        "ThreadStream");
                 assertNotNull(streamRB);
                 assertFalse(streamRB.getKeyYangName().isPresent());
                 assertFalse(streamRB.getKeyJavaName().isPresent());
-                Map<String, AttributeIfc> attributeMap = streamRB
+                final Map<String, AttributeIfc> attributeMap = streamRB
                         .getYangPropertiesToTypesMap();
                 assertEquals(4, attributeMap.size());
 
-                TOAttribute toAttr = (TOAttribute) attributeMap.get("peer");
+                final TOAttribute toAttr = (TOAttribute) attributeMap.get("peer");
                 assertNotNull(toAttr);
                 assertThat(toAttr.getAttributeYangName(), is("peer"));
                 assertThat(toAttr.getLowerCaseCammelCase(), is("peer"));
                 assertThat(toAttr.getUpperCaseCammelCase(), is("Peer"));
-                assertThat(toAttr.getOpenType(), is(CompositeType.class));
-                Set<String> propsExpected = new HashSet<String>(2);
+                assertThat(toAttr.getOpenType(), isA(CompositeType.class));
+                Set<String> propsExpected = new HashSet<>(2);
                 propsExpected.add("port");
                 propsExpected.add("core-size");
                 assertThat(toAttr.getYangPropertiesToTypesMap().keySet(),
                         is(propsExpected));
-                propsExpected = new HashSet<String>(2);
+                propsExpected = new HashSet<>(2);
                 propsExpected.add("Port");
                 propsExpected.add("CoreSize");
                 assertThat(
                         toAttr.getCapitalizedPropertiesToTypesMap().keySet(),
                         is(propsExpected));
-                propsExpected = new HashSet<String>(2);
+                propsExpected = new HashSet<>(2);
                 propsExpected.add("port");
                 propsExpected.add("coreSize");
                 assertThat(toAttr.getJmxPropertiesToTypesMap().keySet(),
                         is(propsExpected));
 
-                JavaAttribute timestampAttr = (JavaAttribute) attributeMap
+                final JavaAttribute timestampAttr = (JavaAttribute) attributeMap
                         .get("timestamp");
                 assertNotNull(timestampAttr);
 
-                JavaAttribute stateAttr = (JavaAttribute) attributeMap
+                final JavaAttribute stateAttr = (JavaAttribute) attributeMap
                         .get("state");
                 assertNotNull(stateAttr);
 
-                ListAttribute innerStream = (ListAttribute) attributeMap
+                final ListAttribute innerStream = (ListAttribute) attributeMap
                         .get("inner-stream-list");
                 assertNotNull(innerStream);
                 assertThat(innerStream.getAttributeYangName(),
@@ -311,19 +299,19 @@ public class ModuleMXBeanEntryTest extends AbstractYangTest {
                         is("innerStreamList"));
                 assertThat(innerStream.getUpperCaseCammelCase(),
                         is("InnerStreamList"));
-                assertThat(innerStream.getOpenType(), is(ArrayType.class));
+                assertThat(innerStream.getOpenType(), isA(ArrayType.class));
 
             }
 
         }
         { // test multiple dependencies
-            ModuleMXBeanEntry threadPoolRegistry = namesToMBEs.get(THREADPOOL_REGISTRY_IMPL_NAME);
-            Map<String, AttributeIfc> attributes = threadPoolRegistry.getAttributes();
+            final ModuleMXBeanEntry threadPoolRegistry = namesToMBEs.get(THREADPOOL_REGISTRY_IMPL_NAME);
+            final Map<String, AttributeIfc> attributes = threadPoolRegistry.getAttributes();
             assertEquals(1, attributes.size());
-            AttributeIfc threadpoolsAttr = attributes.get("threadpools");
+            final AttributeIfc threadpoolsAttr = attributes.get("threadpools");
             assertNotNull(threadpoolsAttr);
             assertTrue(threadpoolsAttr instanceof ListDependenciesAttribute);
-            ListDependenciesAttribute threadpools = (ListDependenciesAttribute) threadpoolsAttr;
+            final ListDependenciesAttribute threadpools = (ListDependenciesAttribute) threadpoolsAttr;
         }
     }
 

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.