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