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