2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.mdsal.binding.yang.types;
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;
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.GeneratedTypeBuilderImpl;
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;
44 public class TypeProviderImplTest {
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);
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);
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);
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);
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);
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);
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);
104 public void testMethodsOfTypeProviderImpl() {
105 final SchemaContext schemaContext = YangParserTestUtils.parseYangResource("/base-yang-types.yang");
107 final AbstractTypeProvider typeProvider = new RuntimeTypeProvider(schemaContext);
109 final SchemaPath refTypePath = SchemaPath.create(true, QName.create("", "cont1"), QName.create("", "list1"));
110 final GeneratedTypeBuilderImpl refType = new GeneratedTypeBuilderImpl("org.opendaylight.yangtools.test",
112 typeProvider.putReferencedType(refTypePath, refType);
113 final StringTypeDefinition stringType = BaseTypes.stringType();
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();
121 assertNotNull(typeProvider);
123 // test getAdditionalTypes() method
124 assertFalse(typeProvider.getAdditionalTypes().isEmpty());
126 // test getConstructorPropertyName() method
127 assertTrue(typeProvider.getConstructorPropertyName(null).isEmpty());
128 assertEquals("value", typeProvider.getConstructorPropertyName(stringType));
130 // test getParamNameFromType() method
131 assertEquals("string", typeProvider.getParamNameFromType(stringType));
133 // test getTypeDefaultConstruction() method for string type
134 assertEquals("\"default value\"", typeProvider.getTypeDefaultConstruction(leafSchemaNode, "default value"));
137 final BinaryTypeDefinition binaryType = BaseTypes.binaryType();
139 reset(leafSchemaNode);
140 doReturn(binaryType).when(leafSchemaNode).getType();
141 doReturn(SchemaPath.ROOT).when(leafSchemaNode).getPath();
142 doReturn(QName.create("", "Cont1")).when(leafSchemaNode).getQName();
144 assertEquals("new byte[] {-45}", typeProvider.getTypeDefaultConstruction(leafSchemaNode, "01"));
147 final BooleanTypeDefinition booleanType = BaseTypes.booleanType();
149 reset(leafSchemaNode);
150 doReturn(booleanType).when(leafSchemaNode).getType();
151 doReturn(SchemaPath.ROOT).when(leafSchemaNode).getPath();
152 doReturn(QName.create("", "Cont1")).when(leafSchemaNode).getQName();
154 assertEquals("java.lang.Boolean.FALSE", typeProvider.getTypeDefaultConstruction(leafSchemaNode, "false"));
157 final DecimalTypeDefinition decimalType = BaseTypes.decimalTypeBuilder(refTypePath).setFractionDigits(4)
160 reset(leafSchemaNode);
161 doReturn(decimalType).when(leafSchemaNode).getType();
162 doReturn(SchemaPath.ROOT).when(leafSchemaNode).getPath();
163 doReturn(QName.create("", "Cont1")).when(leafSchemaNode).getQName();
165 assertEquals("new java.math.BigDecimal(\"111\")", typeProvider.getTypeDefaultConstruction(leafSchemaNode,
169 final EmptyTypeDefinition emptyType = BaseTypes.emptyType();
171 reset(leafSchemaNode);
172 doReturn(emptyType).when(leafSchemaNode).getType();
173 doReturn(SchemaPath.ROOT).when(leafSchemaNode).getPath();
174 doReturn(QName.create("", "Cont1")).when(leafSchemaNode).getQName();
176 assertEquals("java.lang.Boolean.valueOf(\"default value\")", typeProvider.getTypeDefaultConstruction(
177 leafSchemaNode, "default value"));
180 final EnumTypeDefinition enumType = BaseTypes.enumerationTypeBuilder(refTypePath).build();
182 reset(leafSchemaNode);
183 doReturn(enumType).when(leafSchemaNode).getType();
184 doReturn(SchemaPath.ROOT).when(leafSchemaNode).getPath();
185 doReturn(QName.create("", "Cont1")).when(leafSchemaNode).getQName();
188 assertEquals("\"default value\"", typeProvider.getTypeDefaultConstruction(leafSchemaNode, "default value"));
189 fail("Expected NoSuchElementException");
190 } catch (Exception e) {
191 assertTrue(e instanceof NoSuchElementException);
195 final IdentitySchemaNode identitySchemaNode = mock(IdentitySchemaNode.class);
196 final IdentityrefTypeBuilder identityRefBuilder = BaseTypes.identityrefTypeBuilder(refTypePath);
197 identityRefBuilder.addIdentity(identitySchemaNode);
198 final IdentityrefTypeDefinition identityRef = identityRefBuilder.build();
200 reset(leafSchemaNode);
201 doReturn(identityRef).when(leafSchemaNode).getType();
202 doReturn(SchemaPath.ROOT).when(leafSchemaNode).getPath();
203 doReturn(QName.create("", "Cont1")).when(leafSchemaNode).getQName();
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());