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.yangtools.sal.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;
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 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
46 public class TypeProviderImplTest {
48 @Test (expected = YangValidationException.class)
49 public void testLeafRefRelativeSelfReference() throws Exception {
50 File relative = new File(getClass().getResource("/leafref/leafref-relative-invalid.yang").toURI());
52 final SchemaContext schemaContext = YangParserTestUtils.parseYangSources(relative);
53 final Module moduleRelative = schemaContext.findModuleByNamespace(new URI("urn:xml:ns:yang:lrr")).iterator().next();
54 final TypeProviderImpl typeProvider = new TypeProviderImpl(schemaContext);
56 final QName listNode = QName.create(moduleRelative.getQNameModule(), "neighbor");
57 final QName leafNode = QName.create(moduleRelative.getQNameModule(), "neighbor-id");
58 DataSchemaNode leafref = ((ListSchemaNode) moduleRelative.getDataChildByName(listNode))
59 .getDataChildByName(leafNode);
60 LeafSchemaNode leaf = (LeafSchemaNode) leafref;
61 TypeDefinition<?> leafType = leaf.getType();
62 Type leafrefResolvedType = typeProvider.javaTypeForSchemaDefinitionType(leafType, leaf);
63 assertNotNull(leafrefResolvedType);
66 @Test (expected = YangValidationException.class)
67 public void testLeafRefAbsoluteSelfReference() throws Exception {
68 File relative = new File(getClass().getResource("/leafref/leafref-absolute-invalid.yang").toURI());
70 final SchemaContext schemaContext = YangParserTestUtils.parseYangSources(relative);
71 final Module moduleRelative = schemaContext.findModuleByNamespace(new URI("urn:xml:ns:yang:lra")).iterator().next();
72 final TypeProviderImpl typeProvider = new TypeProviderImpl(schemaContext);
74 final QName listNode = QName.create(moduleRelative.getQNameModule(), "neighbor");
75 final QName leafNode = QName.create(moduleRelative.getQNameModule(), "neighbor-id");
76 DataSchemaNode leafref = ((ListSchemaNode) moduleRelative.getDataChildByName(listNode))
77 .getDataChildByName(leafNode);
78 LeafSchemaNode leaf = (LeafSchemaNode) leafref;
79 TypeDefinition<?> leafType = leaf.getType();
80 Type leafrefResolvedType = typeProvider.javaTypeForSchemaDefinitionType(leafType, leaf);
81 assertNotNull(leafrefResolvedType);
85 public void testLeafRefRelativeAndAbsoluteValidReference() throws Exception {
86 File valid = new File(getClass().getResource("/leafref/leafref-valid.yang").toURI());
88 final SchemaContext schemaContext = YangParserTestUtils.parseYangSources(valid);
89 final Module moduleValid = schemaContext.findModuleByNamespace(new URI("urn:xml:ns:yang:lrv")).iterator().next();
90 final TypeProviderImpl typeProvider = new TypeProviderImpl(schemaContext);
92 final QName listNode = QName.create(moduleValid.getQNameModule(), "neighbor");
93 final QName leaf1Node = QName.create(moduleValid.getQNameModule(), "neighbor-id");
94 DataSchemaNode leafrefRel = ((ListSchemaNode) moduleValid.getDataChildByName(listNode))
95 .getDataChildByName(leaf1Node);
96 LeafSchemaNode leafRel = (LeafSchemaNode) leafrefRel;
97 TypeDefinition<?> leafTypeRel = leafRel.getType();
98 Type leafrefRelResolvedType = typeProvider.javaTypeForSchemaDefinitionType(leafTypeRel, leafRel);
99 assertNotNull(leafrefRelResolvedType);
101 final QName leaf2Node = QName.create(moduleValid.getQNameModule(), "neighbor2-id");
102 DataSchemaNode leafrefAbs = ((ListSchemaNode) moduleValid.getDataChildByName(listNode))
103 .getDataChildByName(leaf2Node);
104 LeafSchemaNode leafAbs = (LeafSchemaNode) leafrefAbs;
105 TypeDefinition<?> leafTypeAbs = leafAbs.getType();
106 Type leafrefAbsResolvedType = typeProvider.javaTypeForSchemaDefinitionType(leafTypeAbs, leafAbs);
107 assertNotNull(leafrefAbsResolvedType);
111 public void testMethodsOfTypeProviderImpl() throws Exception {
112 final File abstractTopology = new File(getClass().getResource("/base-yang-types.yang").toURI());
114 final SchemaContext schemaContext = YangParserTestUtils.parseYangSources(abstractTopology);
116 final TypeProviderImpl typeProvider = new TypeProviderImpl(schemaContext);
118 final SchemaPath refTypePath = SchemaPath.create(true, QName.create("cont1"), QName.create("list1"));
119 final GeneratedTypeBuilderImpl refType = new GeneratedTypeBuilderImpl("org.opendaylight.yangtools.test", "TestType");
120 typeProvider.putReferencedType(refTypePath, refType);
121 final StringTypeDefinition stringType = BaseTypes.stringType();
123 final LeafSchemaNode leafSchemaNode = mock(LeafSchemaNode.class);
124 doReturn(stringType).when(leafSchemaNode).getType();
125 doReturn(SchemaPath.ROOT).when(leafSchemaNode).getPath();
126 doReturn(QName.create("Cont1")).when(leafSchemaNode).getQName();
129 assertNotNull(typeProvider);
131 // test getAdditionalTypes() method
132 assertFalse(typeProvider.getAdditionalTypes().isEmpty());
134 // test getConstructorPropertyName() method
135 assertTrue(typeProvider.getConstructorPropertyName(null).isEmpty());
136 assertEquals("value", typeProvider.getConstructorPropertyName(stringType));
138 // test getParamNameFromType() method
139 assertEquals("string", typeProvider.getParamNameFromType(stringType));
141 // test getTypeDefaultConstruction() method for string type
142 assertEquals("\"default value\"", typeProvider.getTypeDefaultConstruction(leafSchemaNode, "default value"));
145 final BinaryTypeDefinition binaryType = BaseTypes.binaryType();
147 reset(leafSchemaNode);
148 doReturn(binaryType).when(leafSchemaNode).getType();
149 doReturn(SchemaPath.ROOT).when(leafSchemaNode).getPath();
150 doReturn(QName.create("Cont1")).when(leafSchemaNode).getQName();
152 assertEquals("new byte[] {-45}", typeProvider.getTypeDefaultConstruction(leafSchemaNode, "01"));
155 final BooleanTypeDefinition booleanType = BaseTypes.booleanType();
157 reset(leafSchemaNode);
158 doReturn(booleanType).when(leafSchemaNode).getType();
159 doReturn(SchemaPath.ROOT).when(leafSchemaNode).getPath();
160 doReturn(QName.create("Cont1")).when(leafSchemaNode).getQName();
162 assertEquals("new java.lang.Boolean(\"false\")", typeProvider.getTypeDefaultConstruction(leafSchemaNode, "false"));
165 final DecimalTypeDefinition decimalType = BaseTypes.decimalTypeBuilder(refTypePath).setFractionDigits(4).build();
167 reset(leafSchemaNode);
168 doReturn(decimalType).when(leafSchemaNode).getType();
169 doReturn(SchemaPath.ROOT).when(leafSchemaNode).getPath();
170 doReturn(QName.create("Cont1")).when(leafSchemaNode).getQName();
172 assertEquals("new java.math.BigDecimal(\"111\")", typeProvider.getTypeDefaultConstruction(leafSchemaNode, "111"));
175 final EmptyTypeDefinition emptyType = BaseTypes.emptyType();
177 reset(leafSchemaNode);
178 doReturn(emptyType).when(leafSchemaNode).getType();
179 doReturn(SchemaPath.ROOT).when(leafSchemaNode).getPath();
180 doReturn(QName.create("Cont1")).when(leafSchemaNode).getQName();
182 assertEquals("new java.lang.Boolean(\"default value\")", typeProvider.getTypeDefaultConstruction(leafSchemaNode, "default value"));
185 final EnumTypeDefinition enumType = BaseTypes.enumerationTypeBuilder(refTypePath).build();
187 reset(leafSchemaNode);
188 doReturn(enumType).when(leafSchemaNode).getType();
189 doReturn(SchemaPath.ROOT).when(leafSchemaNode).getPath();
190 doReturn(QName.create("Cont1")).when(leafSchemaNode).getQName();
193 assertEquals("\"default value\"", typeProvider.getTypeDefaultConstruction(leafSchemaNode, "default value"));
194 fail("Expected NoSuchElementException");
195 } catch (Exception e) {
196 assertTrue( e instanceof NoSuchElementException);
200 final IdentitySchemaNode identitySchemaNode = mock(IdentitySchemaNode.class);
201 final IdentityrefTypeBuilder identityRefBuilder = BaseTypes.identityrefTypeBuilder(refTypePath);
202 identityRefBuilder.setIdentity(identitySchemaNode);
203 final IdentityrefTypeDefinition identityRef = identityRefBuilder.build();
205 reset(leafSchemaNode);
206 doReturn(identityRef).when(leafSchemaNode).getType();
207 doReturn(SchemaPath.ROOT).when(leafSchemaNode).getPath();
208 doReturn(QName.create("Cont1")).when(leafSchemaNode).getQName();
211 assertEquals("\"default value\"", typeProvider.getTypeDefaultConstruction(leafSchemaNode, "default value"));
212 fail("Expected UnsupportedOperationException");
213 } catch (Exception e) {
214 assertTrue(e instanceof UnsupportedOperationException);
215 assertEquals("Cannot get default construction for identityref type", e.getMessage());