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
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.yangtools.sal.binding.yang.types;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertTrue;
14 import static org.junit.Assert.fail;
15 import static org.mockito.Mockito.doReturn;
16 import static org.mockito.Mockito.mock;
17 import static org.mockito.Mockito.reset;
18
19 import java.io.File;
20 import java.net.URI;
21 import java.util.NoSuchElementException;
22 import org.junit.Test;
23 import org.opendaylight.yangtools.binding.generator.util.generated.type.builder.GeneratedTypeBuilderImpl;
24 import org.opendaylight.yangtools.sal.binding.model.api.Type;
25 import org.opendaylight.yangtools.yang.common.QName;
26 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
27 import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode;
28 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
29 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
30 import org.opendaylight.yangtools.yang.model.api.Module;
31 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
32 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
33 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
34 import org.opendaylight.yangtools.yang.model.api.type.BinaryTypeDefinition;
35 import org.opendaylight.yangtools.yang.model.api.type.BooleanTypeDefinition;
36 import org.opendaylight.yangtools.yang.model.api.type.DecimalTypeDefinition;
37 import org.opendaylight.yangtools.yang.model.api.type.EmptyTypeDefinition;
38 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition;
39 import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition;
40 import org.opendaylight.yangtools.yang.model.api.type.StringTypeDefinition;
41 import org.opendaylight.yangtools.yang.model.util.type.BaseTypes;
42 import org.opendaylight.yangtools.yang.model.util.type.IdentityrefTypeBuilder;
43 import org.opendaylight.yangtools.yang.parser.util.YangValidationException;
44
45 public class TypeProviderImplTest {
46
47     @Test (expected = YangValidationException.class)
48     public void testLeafRefRelativeSelfReference() throws Exception {
49         File relative = new File(getClass().getResource("/leafref/leafref-relative-invalid.yang").toURI());
50
51         final SchemaContext schemaContext = TestUtils.parseYangSources(relative);
52         final Module moduleRelative = schemaContext.findModuleByNamespace(new URI("urn:xml:ns:yang:lrr")).iterator().next();
53         final TypeProviderImpl typeProvider = new TypeProviderImpl(schemaContext);
54
55         final QName listNode = QName.create(moduleRelative.getQNameModule(), "neighbor");
56         final QName leafNode = QName.create(moduleRelative.getQNameModule(), "neighbor-id");
57         DataSchemaNode leafref = ((ListSchemaNode) moduleRelative.getDataChildByName(listNode))
58                 .getDataChildByName(leafNode);
59         LeafSchemaNode leaf = (LeafSchemaNode) leafref;
60         TypeDefinition<?> leafType = leaf.getType();
61         Type leafrefResolvedType = typeProvider.javaTypeForSchemaDefinitionType(leafType, leaf);
62     }
63
64     @Test (expected = YangValidationException.class)
65     public void testLeafRefAbsoluteSelfReference() throws Exception {
66         File relative = new File(getClass().getResource("/leafref/leafref-absolute-invalid.yang").toURI());
67
68         final SchemaContext schemaContext = TestUtils.parseYangSources(relative);
69         final Module moduleRelative = schemaContext.findModuleByNamespace(new URI("urn:xml:ns:yang:lra")).iterator().next();
70         final TypeProviderImpl typeProvider = new TypeProviderImpl(schemaContext);
71
72         final QName listNode = QName.create(moduleRelative.getQNameModule(), "neighbor");
73         final QName leafNode = QName.create(moduleRelative.getQNameModule(), "neighbor-id");
74         DataSchemaNode leafref = ((ListSchemaNode) moduleRelative.getDataChildByName(listNode))
75                 .getDataChildByName(leafNode);
76         LeafSchemaNode leaf = (LeafSchemaNode) leafref;
77         TypeDefinition<?> leafType = leaf.getType();
78         Type leafrefResolvedType = typeProvider.javaTypeForSchemaDefinitionType(leafType, leaf);
79     }
80
81     @Test
82     public void testLeafRefRelativeAndAbsoluteValidReference() throws Exception {
83         File valid = new File(getClass().getResource("/leafref/leafref-valid.yang").toURI());
84
85         final SchemaContext schemaContext = TestUtils.parseYangSources(valid);
86         final Module moduleValid = schemaContext.findModuleByNamespace(new URI("urn:xml:ns:yang:lrv")).iterator().next();
87         final TypeProviderImpl typeProvider = new TypeProviderImpl(schemaContext);
88
89         final QName listNode = QName.create(moduleValid.getQNameModule(), "neighbor");
90         final QName leaf1Node = QName.create(moduleValid.getQNameModule(), "neighbor-id");
91         DataSchemaNode leafrefRel = ((ListSchemaNode) moduleValid.getDataChildByName(listNode))
92                 .getDataChildByName(leaf1Node);
93         LeafSchemaNode leafRel = (LeafSchemaNode) leafrefRel;
94         TypeDefinition<?> leafTypeRel = leafRel.getType();
95         Type leafrefRelResolvedType = typeProvider.javaTypeForSchemaDefinitionType(leafTypeRel, leafRel);
96         assertNotNull(leafrefRelResolvedType);
97
98         final QName leaf2Node = QName.create(moduleValid.getQNameModule(), "neighbor2-id");
99         DataSchemaNode leafrefAbs = ((ListSchemaNode) moduleValid.getDataChildByName(listNode))
100                 .getDataChildByName(leaf2Node);
101         LeafSchemaNode leafAbs = (LeafSchemaNode) leafrefAbs;
102         TypeDefinition<?> leafTypeAbs = leafAbs.getType();
103         Type leafrefAbsResolvedType = typeProvider.javaTypeForSchemaDefinitionType(leafTypeAbs, leafAbs);
104         assertNotNull(leafrefAbsResolvedType);
105     }
106
107     @Test
108     public void testMethodsOfTypeProviderImpl() throws Exception {
109         final File abstractTopology = new File(getClass().getResource("/base-yang-types.yang").toURI());
110
111         final SchemaContext schemaContext = TestUtils.parseYangSources(abstractTopology);
112
113         final TypeProviderImpl typeProvider = new TypeProviderImpl(schemaContext);
114
115         final SchemaPath refTypePath = SchemaPath.create(true, QName.create("cont1"), QName.create("list1"));
116         final GeneratedTypeBuilderImpl refType = new GeneratedTypeBuilderImpl("org.opendaylight.yangtools.test", "TestType");
117         typeProvider.putReferencedType(refTypePath, refType);
118         final StringTypeDefinition stringType = BaseTypes.stringType();
119
120         final LeafSchemaNode leafSchemaNode = mock(LeafSchemaNode.class);
121         doReturn(stringType).when(leafSchemaNode).getType();
122         doReturn(SchemaPath.ROOT).when(leafSchemaNode).getPath();
123         doReturn(QName.create("Cont1")).when(leafSchemaNode).getQName();
124
125         // test constructor
126         assertNotNull(typeProvider);
127
128         // test getAdditionalTypes() method
129         assertFalse(typeProvider.getAdditionalTypes().isEmpty());
130
131         // test getConstructorPropertyName() method
132         assertTrue(typeProvider.getConstructorPropertyName(null).isEmpty());
133         assertEquals("value", typeProvider.getConstructorPropertyName(stringType));
134
135         // test getParamNameFromType() method
136         assertEquals("string", typeProvider.getParamNameFromType(stringType));
137
138         // test getTypeDefaultConstruction() method for string type
139         assertEquals("\"default value\"", typeProvider.getTypeDefaultConstruction(leafSchemaNode, "default value"));
140
141         // binary type
142         final BinaryTypeDefinition binaryType = BaseTypes.binaryType();
143
144         reset(leafSchemaNode);
145         doReturn(binaryType).when(leafSchemaNode).getType();
146         doReturn(SchemaPath.ROOT).when(leafSchemaNode).getPath();
147         doReturn(QName.create("Cont1")).when(leafSchemaNode).getQName();
148
149         assertEquals("new byte[] {-45}", typeProvider.getTypeDefaultConstruction(leafSchemaNode, "01"));
150
151         // boolean type
152         final BooleanTypeDefinition booleanType = BaseTypes.booleanType();
153
154         reset(leafSchemaNode);
155         doReturn(booleanType).when(leafSchemaNode).getType();
156         doReturn(SchemaPath.ROOT).when(leafSchemaNode).getPath();
157         doReturn(QName.create("Cont1")).when(leafSchemaNode).getQName();
158
159         assertEquals("new java.lang.Boolean(\"false\")", typeProvider.getTypeDefaultConstruction(leafSchemaNode, "false"));
160
161         // decimal type
162         final DecimalTypeDefinition decimalType = BaseTypes.decimalTypeBuilder(refTypePath).setFractionDigits(4).build();
163
164         reset(leafSchemaNode);
165         doReturn(decimalType).when(leafSchemaNode).getType();
166         doReturn(SchemaPath.ROOT).when(leafSchemaNode).getPath();
167         doReturn(QName.create("Cont1")).when(leafSchemaNode).getQName();
168
169         assertEquals("new java.math.BigDecimal(\"111\")", typeProvider.getTypeDefaultConstruction(leafSchemaNode, "111"));
170
171         // empty type
172         final EmptyTypeDefinition emptyType = BaseTypes.emptyType();
173
174         reset(leafSchemaNode);
175         doReturn(emptyType).when(leafSchemaNode).getType();
176         doReturn(SchemaPath.ROOT).when(leafSchemaNode).getPath();
177         doReturn(QName.create("Cont1")).when(leafSchemaNode).getQName();
178
179         assertEquals("new java.lang.Boolean(\"default value\")", typeProvider.getTypeDefaultConstruction(leafSchemaNode, "default value"));
180
181         // enum type
182         final EnumTypeDefinition enumType =  BaseTypes.enumerationTypeBuilder(refTypePath).build();
183
184         reset(leafSchemaNode);
185         doReturn(enumType).when(leafSchemaNode).getType();
186         doReturn(SchemaPath.ROOT).when(leafSchemaNode).getPath();
187         doReturn(QName.create("Cont1")).when(leafSchemaNode).getQName();
188
189         try {
190             assertEquals("\"default value\"", typeProvider.getTypeDefaultConstruction(leafSchemaNode, "default value"));
191             fail("Expected NoSuchElementException");
192         } catch (Exception e) {
193             assertTrue( e instanceof NoSuchElementException);
194         }
195
196         // identityref type
197         final IdentitySchemaNode identitySchemaNode = mock(IdentitySchemaNode.class);
198         final IdentityrefTypeBuilder identityRefBuilder = BaseTypes.identityrefTypeBuilder(refTypePath);
199         identityRefBuilder.setIdentity(identitySchemaNode);
200         final IdentityrefTypeDefinition identityRef =  identityRefBuilder.build();
201
202         reset(leafSchemaNode);
203         doReturn(identityRef).when(leafSchemaNode).getType();
204         doReturn(SchemaPath.ROOT).when(leafSchemaNode).getPath();
205         doReturn(QName.create("Cont1")).when(leafSchemaNode).getQName();
206
207         try {
208             assertEquals("\"default value\"", typeProvider.getTypeDefaultConstruction(leafSchemaNode, "default value"));
209             fail("Expected UnsupportedOperationException");
210         } catch (Exception e) {
211             assertTrue(e instanceof UnsupportedOperationException);
212             assertEquals("Cannot get default construction for identityref type", e.getMessage());
213         }
214     }
215 }