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