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 DataSchemaNode leafref = ((ListSchemaNode) moduleRelative.getDataChildByName("neighbor")).getDataChildByName("neighbor-id");
56 LeafSchemaNode leaf = (LeafSchemaNode) leafref;
57 TypeDefinition<?> leafType = leaf.getType();
58 Type leafrefResolvedType = typeProvider.javaTypeForSchemaDefinitionType(leafType, leaf);
61 @Test (expected = YangValidationException.class)
62 public void testLeafRefAbsoluteSelfReference() throws Exception {
63 File relative = new File(getClass().getResource("/leafref/leafref-absolute-invalid.yang").toURI());
65 final SchemaContext schemaContext = TestUtils.parseYangSources(relative);
66 final Module moduleRelative = schemaContext.findModuleByNamespace(new URI("urn:xml:ns:yang:lra")).iterator().next();
67 final TypeProviderImpl typeProvider = new TypeProviderImpl(schemaContext);
69 DataSchemaNode leafref = ((ListSchemaNode) moduleRelative.getDataChildByName("neighbor")).getDataChildByName("neighbor-id");
70 LeafSchemaNode leaf = (LeafSchemaNode) leafref;
71 TypeDefinition<?> leafType = leaf.getType();
72 Type leafrefResolvedType = typeProvider.javaTypeForSchemaDefinitionType(leafType, leaf);
76 public void testLeafRefRelativeAndAbsoluteValidReference() throws Exception {
77 File valid = new File(getClass().getResource("/leafref/leafref-valid.yang").toURI());
79 final SchemaContext schemaContext = TestUtils.parseYangSources(valid);
80 final Module moduleValid = schemaContext.findModuleByNamespace(new URI("urn:xml:ns:yang:lrv")).iterator().next();
81 final TypeProviderImpl typeProvider = new TypeProviderImpl(schemaContext);
83 DataSchemaNode leafrefRel = ((ListSchemaNode) moduleValid.getDataChildByName("neighbor")).getDataChildByName
85 LeafSchemaNode leafRel = (LeafSchemaNode) leafrefRel;
86 TypeDefinition<?> leafTypeRel = leafRel.getType();
87 Type leafrefRelResolvedType = typeProvider.javaTypeForSchemaDefinitionType(leafTypeRel, leafRel);
88 assertNotNull(leafrefRelResolvedType);
90 DataSchemaNode leafrefAbs = ((ListSchemaNode) moduleValid.getDataChildByName("neighbor")).getDataChildByName
92 LeafSchemaNode leafAbs = (LeafSchemaNode) leafrefAbs;
93 TypeDefinition<?> leafTypeAbs = leafAbs.getType();
94 Type leafrefAbsResolvedType = typeProvider.javaTypeForSchemaDefinitionType(leafTypeAbs, leafAbs);
95 assertNotNull(leafrefAbsResolvedType);
99 public void testMethodsOfTypeProviderImpl() throws Exception {
100 final File abstractTopology = new File(getClass().getResource("/base-yang-types.yang")
103 final SchemaContext schemaContext = TestUtils.parseYangSources(abstractTopology);
105 final TypeProviderImpl typeProvider = new TypeProviderImpl(schemaContext);
107 final SchemaPath refTypePath = SchemaPath.create(true, QName.create("cont1"), QName.create("list1"));
108 final GeneratedTypeBuilderImpl refType = new GeneratedTypeBuilderImpl("org.opendaylight.yangtools.test", "TestType");
109 typeProvider.putReferencedType(refTypePath, refType);
110 final StringTypeDefinition stringType = BaseTypes.stringType();
112 final LeafSchemaNode leafSchemaNode = mock(LeafSchemaNode.class);
113 doReturn(stringType).when(leafSchemaNode).getType();
114 doReturn(SchemaPath.ROOT).when(leafSchemaNode).getPath();
115 doReturn(QName.create("Cont1")).when(leafSchemaNode).getQName();
118 assertNotNull(typeProvider);
120 // test getAdditionalTypes() method
121 assertFalse(typeProvider.getAdditionalTypes().isEmpty());
123 // test getConstructorPropertyName() method
124 assertTrue(typeProvider.getConstructorPropertyName(null).isEmpty());
125 assertEquals("value", typeProvider.getConstructorPropertyName(stringType));
127 // test getParamNameFromType() method
128 assertEquals("string", typeProvider.getParamNameFromType(stringType));
130 // test getTypeDefaultConstruction() method for string type
131 assertEquals("\"default value\"", typeProvider.getTypeDefaultConstruction(leafSchemaNode, "default value"));
134 final BinaryTypeDefinition binaryType = BaseTypes.binaryType();
136 reset(leafSchemaNode);
137 doReturn(binaryType).when(leafSchemaNode).getType();
138 doReturn(SchemaPath.ROOT).when(leafSchemaNode).getPath();
139 doReturn(QName.create("Cont1")).when(leafSchemaNode).getQName();
141 assertEquals("new byte[] {-45}", typeProvider.getTypeDefaultConstruction(leafSchemaNode, "01"));
144 final BooleanTypeDefinition booleanType = BaseTypes.booleanType();
146 reset(leafSchemaNode);
147 doReturn(booleanType).when(leafSchemaNode).getType();
148 doReturn(SchemaPath.ROOT).when(leafSchemaNode).getPath();
149 doReturn(QName.create("Cont1")).when(leafSchemaNode).getQName();
151 assertEquals("new java.lang.Boolean(\"false\")", typeProvider.getTypeDefaultConstruction(leafSchemaNode, "false"));
154 final DecimalTypeDefinition decimalType = BaseTypes.decimalTypeBuilder(refTypePath).setFractionDigits(4).build();
156 reset(leafSchemaNode);
157 doReturn(decimalType).when(leafSchemaNode).getType();
158 doReturn(SchemaPath.ROOT).when(leafSchemaNode).getPath();
159 doReturn(QName.create("Cont1")).when(leafSchemaNode).getQName();
161 assertEquals("new java.math.BigDecimal(\"111\")", typeProvider.getTypeDefaultConstruction(leafSchemaNode, "111"));
164 final EmptyTypeDefinition emptyType = BaseTypes.emptyType();
166 reset(leafSchemaNode);
167 doReturn(emptyType).when(leafSchemaNode).getType();
168 doReturn(SchemaPath.ROOT).when(leafSchemaNode).getPath();
169 doReturn(QName.create("Cont1")).when(leafSchemaNode).getQName();
171 assertEquals("new java.lang.Boolean(\"default value\")", typeProvider.getTypeDefaultConstruction(leafSchemaNode, "default value"));
174 final EnumTypeDefinition enumType = BaseTypes.enumerationTypeBuilder(refTypePath).build();
176 reset(leafSchemaNode);
177 doReturn(enumType).when(leafSchemaNode).getType();
178 doReturn(SchemaPath.ROOT).when(leafSchemaNode).getPath();
179 doReturn(QName.create("Cont1")).when(leafSchemaNode).getQName();
182 assertEquals("\"default value\"", typeProvider.getTypeDefaultConstruction(leafSchemaNode, "default value"));
183 fail("Expected NoSuchElementException");
184 } catch (Exception e) {
185 assertTrue( e instanceof NoSuchElementException);
189 final IdentitySchemaNode identitySchemaNode = mock(IdentitySchemaNode.class);
190 final IdentityrefTypeBuilder identityRefBuilder = BaseTypes.identityrefTypeBuilder(refTypePath);
191 identityRefBuilder.setIdentity(identitySchemaNode);
192 final IdentityrefTypeDefinition identityRef = identityRefBuilder.build();
194 reset(leafSchemaNode);
195 doReturn(identityRef).when(leafSchemaNode).getType();
196 doReturn(SchemaPath.ROOT).when(leafSchemaNode).getPath();
197 doReturn(QName.create("Cont1")).when(leafSchemaNode).getQName();
200 assertEquals("\"default value\"", typeProvider.getTypeDefaultConstruction(leafSchemaNode, "default value"));
201 fail("Expected UnsupportedOperationException");
202 } catch (Exception e) {
203 assertTrue(e instanceof UnsupportedOperationException);
204 assertEquals("Cannot get default construction for identityref type", e.getMessage());