BUG-865: remove String-based getDataChildByName()
[mdsal.git] / binding / mdsal-binding-generator-impl / src / test / java / org / opendaylight / yangtools / sal / binding / yang / types / TypeProviderImplTest.java
index 1742adcceea3d61801974eeffd8426d5d775cc64..ae7056fef263082b28efad6098872cae2875fcca 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2014 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,
@@ -11,17 +11,15 @@ import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.reset;
 
-import com.google.common.base.Optional;
 import java.io.File;
 import java.net.URI;
-import java.net.URISyntaxException;
-import java.util.ArrayList;
-import java.util.Arrays;
 import java.util.NoSuchElementException;
-import org.junit.Rule;
 import org.junit.Test;
-import org.junit.rules.ExpectedException;
 import org.opendaylight.yangtools.binding.generator.util.generated.type.builder.GeneratedTypeBuilderImpl;
 import org.opendaylight.yangtools.sal.binding.model.api.Type;
 import org.opendaylight.yangtools.yang.common.QName;
@@ -33,36 +31,31 @@ import org.opendaylight.yangtools.yang.model.api.Module;
 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
+import org.opendaylight.yangtools.yang.model.api.type.BinaryTypeDefinition;
+import org.opendaylight.yangtools.yang.model.api.type.BooleanTypeDefinition;
+import org.opendaylight.yangtools.yang.model.api.type.DecimalTypeDefinition;
+import org.opendaylight.yangtools.yang.model.api.type.EmptyTypeDefinition;
 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition;
-import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition.EnumPair;
-import org.opendaylight.yangtools.yang.model.util.BinaryType;
-import org.opendaylight.yangtools.yang.model.util.BooleanType;
-import org.opendaylight.yangtools.yang.model.util.Decimal64;
-import org.opendaylight.yangtools.yang.model.util.EmptyType;
-import org.opendaylight.yangtools.yang.model.util.EnumerationType;
-import org.opendaylight.yangtools.yang.model.util.IdentityrefType;
-import org.opendaylight.yangtools.yang.model.util.StringType;
-import org.opendaylight.yangtools.yang.parser.builder.impl.IdentitySchemaNodeBuilder;
-import org.opendaylight.yangtools.yang.parser.builder.impl.LeafSchemaNodeBuilder;
-import org.opendaylight.yangtools.yang.parser.builder.impl.ModuleBuilder;
-import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
+import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition;
+import org.opendaylight.yangtools.yang.model.api.type.StringTypeDefinition;
+import org.opendaylight.yangtools.yang.model.util.type.BaseTypes;
+import org.opendaylight.yangtools.yang.model.util.type.IdentityrefTypeBuilder;
 import org.opendaylight.yangtools.yang.parser.util.YangValidationException;
 
 public class TypeProviderImplTest {
 
-    @Rule
-    public ExpectedException expException = ExpectedException.none();
-
     @Test (expected = YangValidationException.class)
     public void testLeafRefRelativeSelfReference() throws Exception {
         File relative = new File(getClass().getResource("/leafref/leafref-relative-invalid.yang").toURI());
 
-        final YangParserImpl yangParser = new YangParserImpl();
-        final SchemaContext schemaContext = yangParser.parseFiles(Arrays.asList(relative));
+        final SchemaContext schemaContext = TestUtils.parseYangSources(relative);
         final Module moduleRelative = schemaContext.findModuleByNamespace(new URI("urn:xml:ns:yang:lrr")).iterator().next();
         final TypeProviderImpl typeProvider = new TypeProviderImpl(schemaContext);
 
-        DataSchemaNode leafref = ((ListSchemaNode) moduleRelative.getDataChildByName("neighbor")).getDataChildByName("neighbor-id");
+        final QName listNode = QName.create(moduleRelative.getQNameModule(), "neighbor");
+        final QName leafNode = QName.create(moduleRelative.getQNameModule(), "neighbor-id");
+        DataSchemaNode leafref = ((ListSchemaNode) moduleRelative.getDataChildByName(listNode))
+                .getDataChildByName(leafNode);
         LeafSchemaNode leaf = (LeafSchemaNode) leafref;
         TypeDefinition<?> leafType = leaf.getType();
         Type leafrefResolvedType = typeProvider.javaTypeForSchemaDefinitionType(leafType, leaf);
@@ -72,35 +65,39 @@ public class TypeProviderImplTest {
     public void testLeafRefAbsoluteSelfReference() throws Exception {
         File relative = new File(getClass().getResource("/leafref/leafref-absolute-invalid.yang").toURI());
 
-        final YangParserImpl yangParser = new YangParserImpl();
-        final SchemaContext schemaContext = yangParser.parseFiles(Arrays.asList(relative));
+        final SchemaContext schemaContext = TestUtils.parseYangSources(relative);
         final Module moduleRelative = schemaContext.findModuleByNamespace(new URI("urn:xml:ns:yang:lra")).iterator().next();
         final TypeProviderImpl typeProvider = new TypeProviderImpl(schemaContext);
 
-        DataSchemaNode leafref = ((ListSchemaNode) moduleRelative.getDataChildByName("neighbor")).getDataChildByName("neighbor-id");
+        final QName listNode = QName.create(moduleRelative.getQNameModule(), "neighbor");
+        final QName leafNode = QName.create(moduleRelative.getQNameModule(), "neighbor-id");
+        DataSchemaNode leafref = ((ListSchemaNode) moduleRelative.getDataChildByName(listNode))
+                .getDataChildByName(leafNode);
         LeafSchemaNode leaf = (LeafSchemaNode) leafref;
         TypeDefinition<?> leafType = leaf.getType();
         Type leafrefResolvedType = typeProvider.javaTypeForSchemaDefinitionType(leafType, leaf);
     }
 
     @Test
-    public void testLeafRefRelativeAndAbsoluteValidReference() throws URISyntaxException {
+    public void testLeafRefRelativeAndAbsoluteValidReference() throws Exception {
         File valid = new File(getClass().getResource("/leafref/leafref-valid.yang").toURI());
 
-        final YangParserImpl yangParser = new YangParserImpl();
-        final SchemaContext schemaContext = yangParser.parseFiles(Arrays.asList(valid));
+        final SchemaContext schemaContext = TestUtils.parseYangSources(valid);
         final Module moduleValid = schemaContext.findModuleByNamespace(new URI("urn:xml:ns:yang:lrv")).iterator().next();
         final TypeProviderImpl typeProvider = new TypeProviderImpl(schemaContext);
 
-        DataSchemaNode leafrefRel = ((ListSchemaNode) moduleValid.getDataChildByName("neighbor")).getDataChildByName
-                ("neighbor-id");
+        final QName listNode = QName.create(moduleValid.getQNameModule(), "neighbor");
+        final QName leaf1Node = QName.create(moduleValid.getQNameModule(), "neighbor-id");
+        DataSchemaNode leafrefRel = ((ListSchemaNode) moduleValid.getDataChildByName(listNode))
+                .getDataChildByName(leaf1Node);
         LeafSchemaNode leafRel = (LeafSchemaNode) leafrefRel;
         TypeDefinition<?> leafTypeRel = leafRel.getType();
         Type leafrefRelResolvedType = typeProvider.javaTypeForSchemaDefinitionType(leafTypeRel, leafRel);
         assertNotNull(leafrefRelResolvedType);
 
-        DataSchemaNode leafrefAbs = ((ListSchemaNode) moduleValid.getDataChildByName("neighbor")).getDataChildByName
-                ("neighbor2-id");
+        final QName leaf2Node = QName.create(moduleValid.getQNameModule(), "neighbor2-id");
+        DataSchemaNode leafrefAbs = ((ListSchemaNode) moduleValid.getDataChildByName(listNode))
+                .getDataChildByName(leaf2Node);
         LeafSchemaNode leafAbs = (LeafSchemaNode) leafrefAbs;
         TypeDefinition<?> leafTypeAbs = leafAbs.getType();
         Type leafrefAbsResolvedType = typeProvider.javaTypeForSchemaDefinitionType(leafTypeAbs, leafAbs);
@@ -108,20 +105,22 @@ public class TypeProviderImplTest {
     }
 
     @Test
-    public void testMethodsOfTypeProviderImpl() throws URISyntaxException {
-        final YangParserImpl yangParser = new YangParserImpl();
-        final File abstractTopology = new File(BaseYangTypes.class.getResource("/base-yang-types.yang")
-                .toURI());
-        final SchemaContext schemaContext = yangParser.parseFiles(Arrays.asList(abstractTopology));
+    public void testMethodsOfTypeProviderImpl() throws Exception {
+        final File abstractTopology = new File(getClass().getResource("/base-yang-types.yang").toURI());
+
+        final SchemaContext schemaContext = TestUtils.parseYangSources(abstractTopology);
+
         final TypeProviderImpl typeProvider = new TypeProviderImpl(schemaContext);
 
         final SchemaPath refTypePath = SchemaPath.create(true, QName.create("cont1"), QName.create("list1"));
         final GeneratedTypeBuilderImpl refType = new GeneratedTypeBuilderImpl("org.opendaylight.yangtools.test", "TestType");
         typeProvider.putReferencedType(refTypePath, refType);
-        final StringType stringType = StringType.getInstance();
-        LeafSchemaNodeBuilder leafSchemaNodeBuilder = new LeafSchemaNodeBuilder("test-module", 111, QName.create("Cont1"), SchemaPath.ROOT);
-        leafSchemaNodeBuilder.setType(stringType);
-        LeafSchemaNode leafSchemaNode = leafSchemaNodeBuilder.build();
+        final StringTypeDefinition stringType = BaseTypes.stringType();
+
+        final LeafSchemaNode leafSchemaNode = mock(LeafSchemaNode.class);
+        doReturn(stringType).when(leafSchemaNode).getType();
+        doReturn(SchemaPath.ROOT).when(leafSchemaNode).getPath();
+        doReturn(QName.create("Cont1")).when(leafSchemaNode).getQName();
 
         // test constructor
         assertNotNull(typeProvider);
@@ -140,54 +139,77 @@ public class TypeProviderImplTest {
         assertEquals("\"default value\"", typeProvider.getTypeDefaultConstruction(leafSchemaNode, "default value"));
 
         // binary type
-        final BinaryType binaryType = BinaryType.getInstance();
-        leafSchemaNodeBuilder = new LeafSchemaNodeBuilder("test-module", 111, QName.create("Cont1"), SchemaPath.ROOT);
-        leafSchemaNodeBuilder.setType(binaryType);
-        leafSchemaNode = leafSchemaNodeBuilder.build();
+        final BinaryTypeDefinition binaryType = BaseTypes.binaryType();
+
+        reset(leafSchemaNode);
+        doReturn(binaryType).when(leafSchemaNode).getType();
+        doReturn(SchemaPath.ROOT).when(leafSchemaNode).getPath();
+        doReturn(QName.create("Cont1")).when(leafSchemaNode).getQName();
+
         assertEquals("new byte[] {-45}", typeProvider.getTypeDefaultConstruction(leafSchemaNode, "01"));
 
         // boolean type
-        final BooleanType booleanType = BooleanType.getInstance();
-        leafSchemaNodeBuilder = new LeafSchemaNodeBuilder("test-module", 111, QName.create("Cont1"), SchemaPath.ROOT);
-        leafSchemaNodeBuilder.setType(booleanType);
-        leafSchemaNode = leafSchemaNodeBuilder.build();
+        final BooleanTypeDefinition booleanType = BaseTypes.booleanType();
+
+        reset(leafSchemaNode);
+        doReturn(booleanType).when(leafSchemaNode).getType();
+        doReturn(SchemaPath.ROOT).when(leafSchemaNode).getPath();
+        doReturn(QName.create("Cont1")).when(leafSchemaNode).getQName();
+
         assertEquals("new java.lang.Boolean(\"false\")", typeProvider.getTypeDefaultConstruction(leafSchemaNode, "false"));
 
         // decimal type
-        final Decimal64 decimalType = Decimal64.create(refTypePath, 4);
-        leafSchemaNodeBuilder = new LeafSchemaNodeBuilder("test-module", 111, QName.create("Cont1"), SchemaPath.ROOT);
-        leafSchemaNodeBuilder.setType(decimalType);
-        leafSchemaNode = leafSchemaNodeBuilder.build();
+        final DecimalTypeDefinition decimalType = BaseTypes.decimalTypeBuilder(refTypePath).setFractionDigits(4).build();
+
+        reset(leafSchemaNode);
+        doReturn(decimalType).when(leafSchemaNode).getType();
+        doReturn(SchemaPath.ROOT).when(leafSchemaNode).getPath();
+        doReturn(QName.create("Cont1")).when(leafSchemaNode).getQName();
+
         assertEquals("new java.math.BigDecimal(\"111\")", typeProvider.getTypeDefaultConstruction(leafSchemaNode, "111"));
 
         // empty type
-        final EmptyType emptyType = EmptyType.getInstance();
-        leafSchemaNodeBuilder = new LeafSchemaNodeBuilder("test-module", 111, QName.create("Cont1"), SchemaPath.ROOT);
-        leafSchemaNodeBuilder.setType(emptyType);
-        leafSchemaNode = leafSchemaNodeBuilder.build();
+        final EmptyTypeDefinition emptyType = BaseTypes.emptyType();
+
+        reset(leafSchemaNode);
+        doReturn(emptyType).when(leafSchemaNode).getType();
+        doReturn(SchemaPath.ROOT).when(leafSchemaNode).getPath();
+        doReturn(QName.create("Cont1")).when(leafSchemaNode).getQName();
+
         assertEquals("new java.lang.Boolean(\"default value\")", typeProvider.getTypeDefaultConstruction(leafSchemaNode, "default value"));
 
         // enum type
-        expException.expect(NoSuchElementException.class);
-        final EnumerationType enumType = EnumerationType.create(refTypePath, new ArrayList<EnumTypeDefinition.EnumPair>(), Optional.<EnumPair> absent());
-        leafSchemaNodeBuilder = new LeafSchemaNodeBuilder("test-module", 111, QName.create("Cont1"), SchemaPath.ROOT);
-        leafSchemaNodeBuilder.setType(enumType);
-        leafSchemaNode = leafSchemaNodeBuilder.build();
-        assertEquals("\"default value\"", typeProvider.getTypeDefaultConstruction(leafSchemaNode, "default value"));
+        final EnumTypeDefinition enumType =  BaseTypes.enumerationTypeBuilder(refTypePath).build();
+
+        reset(leafSchemaNode);
+        doReturn(enumType).when(leafSchemaNode).getType();
+        doReturn(SchemaPath.ROOT).when(leafSchemaNode).getPath();
+        doReturn(QName.create("Cont1")).when(leafSchemaNode).getQName();
+
+        try {
+            assertEquals("\"default value\"", typeProvider.getTypeDefaultConstruction(leafSchemaNode, "default value"));
+            fail("Expected NoSuchElementException");
+        } catch (Exception e) {
+            assertTrue( e instanceof NoSuchElementException);
+        }
 
         // identityref type
-        expException.expect(UnsupportedOperationException.class);
-        expException.expectMessage("Cannot get default construction for identityref type");
-
-        final ModuleBuilder testModBuilder = new ModuleBuilder("test-module", "/test");
-        final IdentitySchemaNodeBuilder identityNodeBuilder = testModBuilder.addIdentity(QName.create("IdentityRefTest"), 111, SchemaPath.ROOT);
-        final IdentitySchemaNode identitySchemaNode = identityNodeBuilder.build();
-        final IdentityrefType identityRef = IdentityrefType.create(refTypePath, identitySchemaNode);
-        leafSchemaNodeBuilder = new LeafSchemaNodeBuilder("test-module", 111, QName.create("Cont1"), SchemaPath.ROOT);
-        leafSchemaNodeBuilder.setType(identityRef);
-
-        leafSchemaNodeBuilder.setParent(identityNodeBuilder);
-        leafSchemaNode = leafSchemaNodeBuilder.build();
-        assertEquals("\"default value\"", typeProvider.getTypeDefaultConstruction(leafSchemaNode, "default value"));
+        final IdentitySchemaNode identitySchemaNode = mock(IdentitySchemaNode.class);
+        final IdentityrefTypeBuilder identityRefBuilder = BaseTypes.identityrefTypeBuilder(refTypePath);
+        identityRefBuilder.setIdentity(identitySchemaNode);
+        final IdentityrefTypeDefinition identityRef =  identityRefBuilder.build();
+
+        reset(leafSchemaNode);
+        doReturn(identityRef).when(leafSchemaNode).getType();
+        doReturn(SchemaPath.ROOT).when(leafSchemaNode).getPath();
+        doReturn(QName.create("Cont1")).when(leafSchemaNode).getQName();
+
+        try {
+            assertEquals("\"default value\"", typeProvider.getTypeDefaultConstruction(leafSchemaNode, "default value"));
+            fail("Expected UnsupportedOperationException");
+        } catch (Exception e) {
+            assertTrue(e instanceof UnsupportedOperationException);
+            assertEquals("Cannot get default construction for identityref type", e.getMessage());
+        }
     }
 }