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;
45 public class TypeProviderImplTest {
47 @Test (expected = YangValidationException.class)
48 public void testLeafRefRelativeSelfReference() throws Exception {
49 File relative = new File(getClass().getResource("/leafref/leafref-relative-invalid.yang").toURI());
51 final SchemaContext schemaContext = TestUtils.parseYangSources(relative);
52 final Module moduleRelative = schemaContext.findModuleByNamespace(new URI("urn:xml:ns:yang:lrr")).iterator().next();
53 final TypeProviderImpl typeProvider = new TypeProviderImpl(schemaContext);
55 final QName listNode = QName.create(moduleRelative.getQNameModule(), "neighbor");
56 final QName leafNode = QName.create(moduleRelative.getQNameModule(), "neighbor-id");
57 DataSchemaNode leafref = ((ListSchemaNode) moduleRelative.getDataChildByName(listNode))
58 .getDataChildByName(leafNode);
59 LeafSchemaNode leaf = (LeafSchemaNode) leafref;
60 TypeDefinition<?> leafType = leaf.getType();
61 Type leafrefResolvedType = typeProvider.javaTypeForSchemaDefinitionType(leafType, leaf);
64 @Test (expected = YangValidationException.class)
65 public void testLeafRefAbsoluteSelfReference() throws Exception {
66 File relative = new File(getClass().getResource("/leafref/leafref-absolute-invalid.yang").toURI());
68 final SchemaContext schemaContext = TestUtils.parseYangSources(relative);
69 final Module moduleRelative = schemaContext.findModuleByNamespace(new URI("urn:xml:ns:yang:lra")).iterator().next();
70 final TypeProviderImpl typeProvider = new TypeProviderImpl(schemaContext);
72 final QName listNode = QName.create(moduleRelative.getQNameModule(), "neighbor");
73 final QName leafNode = QName.create(moduleRelative.getQNameModule(), "neighbor-id");
74 DataSchemaNode leafref = ((ListSchemaNode) moduleRelative.getDataChildByName(listNode))
75 .getDataChildByName(leafNode);
76 LeafSchemaNode leaf = (LeafSchemaNode) leafref;
77 TypeDefinition<?> leafType = leaf.getType();
78 Type leafrefResolvedType = typeProvider.javaTypeForSchemaDefinitionType(leafType, leaf);
82 public void testLeafRefRelativeAndAbsoluteValidReference() throws Exception {
83 File valid = new File(getClass().getResource("/leafref/leafref-valid.yang").toURI());
85 final SchemaContext schemaContext = TestUtils.parseYangSources(valid);
86 final Module moduleValid = schemaContext.findModuleByNamespace(new URI("urn:xml:ns:yang:lrv")).iterator().next();
87 final TypeProviderImpl typeProvider = new TypeProviderImpl(schemaContext);
89 final QName listNode = QName.create(moduleValid.getQNameModule(), "neighbor");
90 final QName leaf1Node = QName.create(moduleValid.getQNameModule(), "neighbor-id");
91 DataSchemaNode leafrefRel = ((ListSchemaNode) moduleValid.getDataChildByName(listNode))
92 .getDataChildByName(leaf1Node);
93 LeafSchemaNode leafRel = (LeafSchemaNode) leafrefRel;
94 TypeDefinition<?> leafTypeRel = leafRel.getType();
95 Type leafrefRelResolvedType = typeProvider.javaTypeForSchemaDefinitionType(leafTypeRel, leafRel);
96 assertNotNull(leafrefRelResolvedType);
98 final QName leaf2Node = QName.create(moduleValid.getQNameModule(), "neighbor2-id");
99 DataSchemaNode leafrefAbs = ((ListSchemaNode) moduleValid.getDataChildByName(listNode))
100 .getDataChildByName(leaf2Node);
101 LeafSchemaNode leafAbs = (LeafSchemaNode) leafrefAbs;
102 TypeDefinition<?> leafTypeAbs = leafAbs.getType();
103 Type leafrefAbsResolvedType = typeProvider.javaTypeForSchemaDefinitionType(leafTypeAbs, leafAbs);
104 assertNotNull(leafrefAbsResolvedType);
108 public void testMethodsOfTypeProviderImpl() throws Exception {
109 final File abstractTopology = new File(getClass().getResource("/base-yang-types.yang").toURI());
111 final SchemaContext schemaContext = TestUtils.parseYangSources(abstractTopology);
113 final TypeProviderImpl typeProvider = new TypeProviderImpl(schemaContext);
115 final SchemaPath refTypePath = SchemaPath.create(true, QName.create("cont1"), QName.create("list1"));
116 final GeneratedTypeBuilderImpl refType = new GeneratedTypeBuilderImpl("org.opendaylight.yangtools.test", "TestType");
117 typeProvider.putReferencedType(refTypePath, refType);
118 final StringTypeDefinition stringType = BaseTypes.stringType();
120 final LeafSchemaNode leafSchemaNode = mock(LeafSchemaNode.class);
121 doReturn(stringType).when(leafSchemaNode).getType();
122 doReturn(SchemaPath.ROOT).when(leafSchemaNode).getPath();
123 doReturn(QName.create("Cont1")).when(leafSchemaNode).getQName();
126 assertNotNull(typeProvider);
128 // test getAdditionalTypes() method
129 assertFalse(typeProvider.getAdditionalTypes().isEmpty());
131 // test getConstructorPropertyName() method
132 assertTrue(typeProvider.getConstructorPropertyName(null).isEmpty());
133 assertEquals("value", typeProvider.getConstructorPropertyName(stringType));
135 // test getParamNameFromType() method
136 assertEquals("string", typeProvider.getParamNameFromType(stringType));
138 // test getTypeDefaultConstruction() method for string type
139 assertEquals("\"default value\"", typeProvider.getTypeDefaultConstruction(leafSchemaNode, "default value"));
142 final BinaryTypeDefinition binaryType = BaseTypes.binaryType();
144 reset(leafSchemaNode);
145 doReturn(binaryType).when(leafSchemaNode).getType();
146 doReturn(SchemaPath.ROOT).when(leafSchemaNode).getPath();
147 doReturn(QName.create("Cont1")).when(leafSchemaNode).getQName();
149 assertEquals("new byte[] {-45}", typeProvider.getTypeDefaultConstruction(leafSchemaNode, "01"));
152 final BooleanTypeDefinition booleanType = BaseTypes.booleanType();
154 reset(leafSchemaNode);
155 doReturn(booleanType).when(leafSchemaNode).getType();
156 doReturn(SchemaPath.ROOT).when(leafSchemaNode).getPath();
157 doReturn(QName.create("Cont1")).when(leafSchemaNode).getQName();
159 assertEquals("new java.lang.Boolean(\"false\")", typeProvider.getTypeDefaultConstruction(leafSchemaNode, "false"));
162 final DecimalTypeDefinition decimalType = BaseTypes.decimalTypeBuilder(refTypePath).setFractionDigits(4).build();
164 reset(leafSchemaNode);
165 doReturn(decimalType).when(leafSchemaNode).getType();
166 doReturn(SchemaPath.ROOT).when(leafSchemaNode).getPath();
167 doReturn(QName.create("Cont1")).when(leafSchemaNode).getQName();
169 assertEquals("new java.math.BigDecimal(\"111\")", typeProvider.getTypeDefaultConstruction(leafSchemaNode, "111"));
172 final EmptyTypeDefinition emptyType = BaseTypes.emptyType();
174 reset(leafSchemaNode);
175 doReturn(emptyType).when(leafSchemaNode).getType();
176 doReturn(SchemaPath.ROOT).when(leafSchemaNode).getPath();
177 doReturn(QName.create("Cont1")).when(leafSchemaNode).getQName();
179 assertEquals("new java.lang.Boolean(\"default value\")", typeProvider.getTypeDefaultConstruction(leafSchemaNode, "default value"));
182 final EnumTypeDefinition enumType = BaseTypes.enumerationTypeBuilder(refTypePath).build();
184 reset(leafSchemaNode);
185 doReturn(enumType).when(leafSchemaNode).getType();
186 doReturn(SchemaPath.ROOT).when(leafSchemaNode).getPath();
187 doReturn(QName.create("Cont1")).when(leafSchemaNode).getQName();
190 assertEquals("\"default value\"", typeProvider.getTypeDefaultConstruction(leafSchemaNode, "default value"));
191 fail("Expected NoSuchElementException");
192 } catch (Exception e) {
193 assertTrue( e instanceof NoSuchElementException);
197 final IdentitySchemaNode identitySchemaNode = mock(IdentitySchemaNode.class);
198 final IdentityrefTypeBuilder identityRefBuilder = BaseTypes.identityrefTypeBuilder(refTypePath);
199 identityRefBuilder.setIdentity(identitySchemaNode);
200 final IdentityrefTypeDefinition identityRef = identityRefBuilder.build();
202 reset(leafSchemaNode);
203 doReturn(identityRef).when(leafSchemaNode).getType();
204 doReturn(SchemaPath.ROOT).when(leafSchemaNode).getPath();
205 doReturn(QName.create("Cont1")).when(leafSchemaNode).getQName();
208 assertEquals("\"default value\"", typeProvider.getTypeDefaultConstruction(leafSchemaNode, "default value"));
209 fail("Expected UnsupportedOperationException");
210 } catch (Exception e) {
211 assertTrue(e instanceof UnsupportedOperationException);
212 assertEquals("Cannot get default construction for identityref type", e.getMessage());