BUG-865: remove String-based getDataChildByName()
[mdsal.git] / binding / mdsal-binding-generator-impl / src / test / java / org / opendaylight / yangtools / sal / binding / yang / types / TypeProviderTest.java
index 0c96d747f8c691bca59a542c7a6cb7f6dc7dd635..02269bbecdf8730649147621cff39772a30b82bc 100644 (file)
@@ -1,17 +1,18 @@
-/**
- * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
+/*
+ * Copyright (c) 2016 Cisco Systems, Inc. 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.yangtools.sal.binding.yang.types;
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
+import static org.mockito.Mockito.doReturn;
 
-import java.math.BigInteger;
 import java.util.List;
 import java.util.Set;
 import org.junit.Before;
@@ -32,6 +33,7 @@ import org.opendaylight.yangtools.sal.binding.model.api.ParameterizedType;
 import org.opendaylight.yangtools.sal.binding.model.api.Restrictions;
 import org.opendaylight.yangtools.sal.binding.model.api.Type;
 import org.opendaylight.yangtools.sal.binding.model.api.type.builder.GeneratedTOBuilder;
+import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
@@ -44,13 +46,15 @@ import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
 import org.opendaylight.yangtools.yang.model.api.type.LeafrefTypeDefinition;
 import org.opendaylight.yangtools.yang.model.api.type.RangeConstraint;
 import org.opendaylight.yangtools.yang.model.api.type.UnionTypeDefinition;
+import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
+import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
 
 /**
  * Test suite for testing public methods in TypeProviderImpl class
  *
  * @see org.opendaylight.yangtools.sal.binding.yang.types.TypeProviderImpl
  *
- * @author Lukas Sedlak <lsedlak@cisco.com>
+ * @author Lukas Sedlak &lt;lsedlak@cisco.com&gt;
  */
 @RunWith(JUnit4.class)
 public class TypeProviderTest {
@@ -66,7 +70,7 @@ public class TypeProviderTest {
     private SchemaNode schemaNode;
 
     @Before
-    public void setUp() {
+    public void setUp() throws SourceException, ReactorException {
         MockitoAnnotations.initMocks(this);
         schemaContext = TypeProviderModel.createTestContext();
         assertNotNull(schemaContext);
@@ -130,24 +134,28 @@ public class TypeProviderTest {
     }
 
     private static LeafSchemaNode provideLeafNodeFromTopLevelContainer(final Module module, final String containerName, final String leafNodeName) {
-        final DataSchemaNode rootNode = module.getDataChildByName(containerName);
+        final QName containerNode = QName.create(module.getQNameModule(), containerName);
+        final DataSchemaNode rootNode = module.getDataChildByName(containerNode);
         assertNotNull("Container foo is not present in root of module "+ module.getName(), rootNode);
         assertTrue(rootNode instanceof DataNodeContainer);
 
+        final QName leafNode = QName.create(module.getQNameModule(), leafNodeName);
         final DataNodeContainer rootContainer = (DataNodeContainer) rootNode;
-        final DataSchemaNode node = rootContainer.getDataChildByName(leafNodeName);
+        final DataSchemaNode node = rootContainer.getDataChildByName(leafNode);
         assertNotNull(node);
         assertTrue(node instanceof LeafSchemaNode);
         return (LeafSchemaNode) node;
     }
 
     private static LeafListSchemaNode provideLeafListNodeFromTopLevelContainer(final Module module, final String containerName, final String leafListNodeName) {
-        final DataSchemaNode rootNode = module.getDataChildByName(containerName);
+        final QName containerNode = QName.create(module.getQNameModule(), containerName);
+        final DataSchemaNode rootNode = module.getDataChildByName(containerNode);
         assertNotNull("Container foo is not present in root of module " + module.getName(), rootNode);
         assertTrue(rootNode instanceof DataNodeContainer);
 
         final DataNodeContainer rootContainer = (DataNodeContainer) rootNode;
-        final DataSchemaNode node = rootContainer.getDataChildByName(leafListNodeName);
+        final QName leafListNode = QName.create(module.getQNameModule(), leafListNodeName);
+        final DataSchemaNode node = rootContainer.getDataChildByName(leafListNode);
         assertNotNull(node);
         assertTrue(node instanceof LeafListSchemaNode);
         return (LeafListSchemaNode) node;
@@ -190,8 +198,8 @@ public class TypeProviderTest {
 
         assertTrue(!rangeConstraints.isEmpty());
         final RangeConstraint constraint = rangeConstraints.get(0);
-        assertEquals(BigInteger.ONE, constraint.getMin());
-        assertEquals(BigInteger.valueOf(100), constraint.getMax());
+        assertEquals(1, constraint.getMin().intValue());
+        assertEquals(100, constraint.getMax().intValue());
     }
 
     @Test
@@ -253,8 +261,10 @@ public class TypeProviderTest {
         final Enumeration enumType = (Enumeration) result;
         final List<Enumeration.Pair> enumValues = enumType.getValues();
         assertTrue(!enumValues.isEmpty());
-        assertEquals("A", enumValues.get(0).getName());
-        assertEquals("B", enumValues.get(1).getName());
+        assertEquals("a", enumValues.get(0).getName());
+        assertEquals("b", enumValues.get(1).getName());
+        assertEquals("A", enumValues.get(0).getMappedName());
+        assertEquals("B", enumValues.get(1).getMappedName());
 
         leaf = provideLeafNodeFromTopLevelContainer(testTypeProviderModule, "foo", "resolve-direct-use-of-enum");
         leafType = leaf.getType();
@@ -276,7 +286,8 @@ public class TypeProviderTest {
         assertTrue(leafrefResolvedType1 instanceof GeneratedTransferObject);
 
         final Module module = resolveModule("test-type-provider-b");
-        final DataSchemaNode rootNode = module.getDataChildByName("id");
+        final QName leafNode = QName.create(module.getQNameModule(), "id");
+        final DataSchemaNode rootNode = module.getDataChildByName(leafNode);
         assertNotNull("leaf id is not present in root of module "+ module.getName(), rootNode);
         assertTrue(rootNode instanceof LeafSchemaNode);
         leaf = (LeafSchemaNode) rootNode;
@@ -295,7 +306,8 @@ public class TypeProviderTest {
 
         final Module module = resolveModule("test-type-provider-b");
 
-        final DataSchemaNode enumNode = module.getDataChildByName("enum");
+        final QName leafNode = QName.create(module.getQNameModule(), "enum");
+        final DataSchemaNode enumNode = module.getDataChildByName(leafNode);
         assertNotNull("leaf enum is not present in root of module " + module.getName(), enumNode);
         assertTrue(enumNode instanceof LeafSchemaNode);
         LeafSchemaNode leaf = (LeafSchemaNode) enumNode;
@@ -305,7 +317,8 @@ public class TypeProviderTest {
         assertNotNull(leafrefResolvedType1);
         assertTrue(leafrefResolvedType1 instanceof ReferencedTypeImpl);
 
-        final DataSchemaNode enumListNode = module.getDataChildByName("enums");
+        final QName leafListNode = QName.create(module.getQNameModule(), "enums");
+        final DataSchemaNode enumListNode = module.getDataChildByName(leafListNode);
         assertNotNull("leaf-list enums is not present in root of module "+ module.getName(), enumNode);
         assertTrue(enumListNode instanceof LeafListSchemaNode);
         LeafListSchemaNode leafList = (LeafListSchemaNode) enumListNode;
@@ -316,7 +329,7 @@ public class TypeProviderTest {
         assertTrue(leafrefResolvedType2 instanceof ParameterizedType);
     }
 
-    private void setReferencedTypeForTypeProvider(TypeProvider provider) {
+    private void setReferencedTypeForTypeProvider(final TypeProvider provider) {
         final LeafSchemaNode enumLeafNode = provideLeafNodeFromTopLevelContainer(testTypeProviderModule, "foo",
             "resolve-direct-use-of-enum");
         final TypeDefinition<?> enumLeafTypedef = enumLeafNode.getType();
@@ -339,7 +352,8 @@ public class TypeProviderTest {
         final TypeProvider provider = new TypeProviderImpl(schemaContext);
         final Module module = resolveModule("test-type-provider-b");
 
-        final DataSchemaNode condLeaf = module.getDataChildByName("conditional-leafref");
+        final QName leafrefNode = QName.create(module.getQNameModule(), "conditional-leafref");
+        final DataSchemaNode condLeaf = module.getDataChildByName(leafrefNode);
         assertNotNull("leaf conditional-leafref is not present in root of module "+ module.getName(), condLeaf);
         assertTrue(condLeaf instanceof LeafSchemaNode);
         LeafSchemaNode leaf = (LeafSchemaNode) condLeaf;
@@ -357,7 +371,8 @@ public class TypeProviderTest {
         final TypeProvider provider = new TypeProviderImpl(schemaContext);
         final Module module = resolveModule("test-type-provider-b");
 
-        final DataSchemaNode condLeaf = module.getDataChildByName("unreslovable-leafref");
+        final QName leafrefNode = QName.create(module.getQNameModule(), "unreslovable-leafref");
+        final DataSchemaNode condLeaf = module.getDataChildByName(leafrefNode);
         assertNotNull("leaf unreslovable-leafref is not present in root of module "+ module.getName(), condLeaf);
         assertTrue(condLeaf instanceof LeafSchemaNode);
         LeafSchemaNode leaf = (LeafSchemaNode) condLeaf;
@@ -387,6 +402,7 @@ public class TypeProviderTest {
         LeafSchemaNode leaf = provideLeafNodeFromTopLevelContainer(testTypeProviderModule, "bar", "leafref-value");
         TypeDefinition<?> leafType = leaf.getType();
         assertTrue(leafType instanceof LeafrefTypeDefinition);
+        doReturn(null).when(schemaNode).getPath();
         provider.provideTypeForLeafref((LeafrefTypeDefinition) leafType, schemaNode);
     }
 
@@ -688,15 +704,15 @@ public class TypeProviderTest {
 
         GeneratedTOBuilder builder = new GeneratedTOBuilderImpl("test.package", "TestBuilder");
 
-        provider.addUnitsToGenTO(builder, null);
+        TypeProviderImpl.addUnitsToGenTO(builder, null);
         GeneratedTransferObject genTO = builder.toInstance();
         assertTrue(genTO.getConstantDefinitions().isEmpty());
 
-        provider.addUnitsToGenTO(builder, "");
+        TypeProviderImpl.addUnitsToGenTO(builder, "");
         genTO = builder.toInstance();
         assertTrue(genTO.getConstantDefinitions().isEmpty());
 
-        provider.addUnitsToGenTO(builder, "125");
+        TypeProviderImpl.addUnitsToGenTO(builder, "125");
         genTO = builder.toInstance();
         assertTrue(!genTO.getConstantDefinitions().isEmpty());
         assertEquals(1, genTO.getConstantDefinitions().size());
@@ -878,7 +894,8 @@ public class TypeProviderTest {
             result);
 
         final Module module = resolveModule("test-type-provider");
-        DataSchemaNode rootNode = module.getDataChildByName("root-union-leaf");
+        final QName leafUnionNode = QName.create(module.getQNameModule(), "root-union-leaf");
+        DataSchemaNode rootNode = module.getDataChildByName(leafUnionNode);
         assertNotNull("leaf root-union-leaf is not present in root of module "+ module.getName(), rootNode);
         assertTrue(rootNode instanceof LeafSchemaNode);
         leaf = (LeafSchemaNode) rootNode;
@@ -895,7 +912,8 @@ public class TypeProviderTest {
         assertEquals("new org.opendaylight.yang.gen.v1.urn.opendaylight.org.test.base.yang.types.rev140914.YangBinary(new byte[] {-45, 23, -33, 125, -9, -33})",
             result);
 
-        rootNode = module.getDataChildByName("root-bits-leaf");
+        final QName leafBitsNode = QName.create(module.getQNameModule(), "root-bits-leaf");
+        rootNode = module.getDataChildByName(leafBitsNode);
         assertNotNull("leaf bits-leaf is not present in root of module "+ module.getName(), rootNode);
         assertTrue(rootNode instanceof LeafSchemaNode);
         leaf = (LeafSchemaNode) rootNode;