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