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