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.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;
45 public class TypeProviderImplTest {
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);
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);
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);
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);
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);
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);
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);
105 public void testMethodsOfTypeProviderImpl() {
106 final SchemaContext schemaContext = YangParserTestUtils.parseYangResource("/base-yang-types.yang");
108 final AbstractTypeProvider typeProvider = new RuntimeTypeProvider(schemaContext);
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();
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();
122 assertNotNull(typeProvider);
124 // test getAdditionalTypes() method
125 assertFalse(typeProvider.getAdditionalTypes().isEmpty());
127 // test getConstructorPropertyName() method
128 assertTrue(typeProvider.getConstructorPropertyName(null).isEmpty());
129 assertEquals("value", typeProvider.getConstructorPropertyName(stringType));
131 // test getParamNameFromType() method
132 assertEquals("string", typeProvider.getParamNameFromType(stringType));
134 // test getTypeDefaultConstruction() method for string type
135 assertEquals("\"default value\"", typeProvider.getTypeDefaultConstruction(leafSchemaNode, "default value"));
138 final BinaryTypeDefinition binaryType = BaseTypes.binaryType();
140 reset(leafSchemaNode);
141 doReturn(binaryType).when(leafSchemaNode).getType();
142 doReturn(SchemaPath.ROOT).when(leafSchemaNode).getPath();
143 doReturn(QName.create("", "Cont1")).when(leafSchemaNode).getQName();
145 assertEquals("new byte[] {-45}", typeProvider.getTypeDefaultConstruction(leafSchemaNode, "01"));
148 final BooleanTypeDefinition booleanType = BaseTypes.booleanType();
150 reset(leafSchemaNode);
151 doReturn(booleanType).when(leafSchemaNode).getType();
152 doReturn(SchemaPath.ROOT).when(leafSchemaNode).getPath();
153 doReturn(QName.create("", "Cont1")).when(leafSchemaNode).getQName();
155 assertEquals("java.lang.Boolean.FALSE", typeProvider.getTypeDefaultConstruction(leafSchemaNode, "false"));
158 final DecimalTypeDefinition decimalType = BaseTypes.decimalTypeBuilder(refTypePath).setFractionDigits(4)
161 reset(leafSchemaNode);
162 doReturn(decimalType).when(leafSchemaNode).getType();
163 doReturn(SchemaPath.ROOT).when(leafSchemaNode).getPath();
164 doReturn(QName.create("", "Cont1")).when(leafSchemaNode).getQName();
166 assertEquals("new java.math.BigDecimal(\"111\")", typeProvider.getTypeDefaultConstruction(leafSchemaNode,
170 final EmptyTypeDefinition emptyType = BaseTypes.emptyType();
172 reset(leafSchemaNode);
173 doReturn(emptyType).when(leafSchemaNode).getType();
174 doReturn(SchemaPath.ROOT).when(leafSchemaNode).getPath();
175 doReturn(QName.create("", "Cont1")).when(leafSchemaNode).getQName();
177 assertEquals("java.lang.Boolean.valueOf(\"default value\")", typeProvider.getTypeDefaultConstruction(
178 leafSchemaNode, "default value"));
181 final EnumTypeDefinition enumType = BaseTypes.enumerationTypeBuilder(refTypePath).build();
183 reset(leafSchemaNode);
184 doReturn(enumType).when(leafSchemaNode).getType();
185 doReturn(SchemaPath.ROOT).when(leafSchemaNode).getPath();
186 doReturn(QName.create("", "Cont1")).when(leafSchemaNode).getQName();
189 assertEquals("\"default value\"", typeProvider.getTypeDefaultConstruction(leafSchemaNode, "default value"));
190 fail("Expected NoSuchElementException");
191 } catch (Exception e) {
192 assertTrue(e instanceof NoSuchElementException);
196 final IdentitySchemaNode identitySchemaNode = mock(IdentitySchemaNode.class);
197 final IdentityrefTypeBuilder identityRefBuilder = BaseTypes.identityrefTypeBuilder(refTypePath);
198 identityRefBuilder.addIdentity(identitySchemaNode);
199 final IdentityrefTypeDefinition identityRef = identityRefBuilder.build();
201 reset(leafSchemaNode);
202 doReturn(identityRef).when(leafSchemaNode).getType();
203 doReturn(SchemaPath.ROOT).when(leafSchemaNode).getPath();
204 doReturn(QName.create("", "Cont1")).when(leafSchemaNode).getQName();
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());