2 * Copyright (c) 2014 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;
15 import com.google.common.base.Optional;
18 import java.net.URISyntaxException;
19 import java.util.ArrayList;
20 import java.util.Arrays;
21 import java.util.NoSuchElementException;
22 import org.junit.Rule;
23 import org.junit.Test;
24 import org.junit.rules.ExpectedException;
25 import org.opendaylight.yangtools.binding.generator.util.generated.type.builder.GeneratedTypeBuilderImpl;
26 import org.opendaylight.yangtools.sal.binding.model.api.Type;
27 import org.opendaylight.yangtools.yang.common.QName;
28 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
29 import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode;
30 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
31 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
32 import org.opendaylight.yangtools.yang.model.api.Module;
33 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
34 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
35 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
36 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition;
37 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition.EnumPair;
38 import org.opendaylight.yangtools.yang.model.util.BinaryType;
39 import org.opendaylight.yangtools.yang.model.util.BooleanType;
40 import org.opendaylight.yangtools.yang.model.util.Decimal64;
41 import org.opendaylight.yangtools.yang.model.util.EmptyType;
42 import org.opendaylight.yangtools.yang.model.util.EnumerationType;
43 import org.opendaylight.yangtools.yang.model.util.IdentityrefType;
44 import org.opendaylight.yangtools.yang.model.util.StringType;
45 import org.opendaylight.yangtools.yang.parser.builder.impl.IdentitySchemaNodeBuilder;
46 import org.opendaylight.yangtools.yang.parser.builder.impl.LeafSchemaNodeBuilder;
47 import org.opendaylight.yangtools.yang.parser.builder.impl.ModuleBuilder;
48 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
49 import org.opendaylight.yangtools.yang.parser.util.YangValidationException;
51 public class TypeProviderImplTest {
54 public ExpectedException expException = ExpectedException.none();
56 @Test (expected = YangValidationException.class)
57 public void testLeafRefRelativeSelfReference() throws Exception {
58 File relative = new File(getClass().getResource("/leafref/leafref-relative-invalid.yang").toURI());
60 final YangParserImpl yangParser = new YangParserImpl();
61 final SchemaContext schemaContext = yangParser.parseFiles(Arrays.asList(relative));
62 final Module moduleRelative = schemaContext.findModuleByNamespace(new URI("urn:xml:ns:yang:lrr")).iterator().next();
63 final TypeProviderImpl typeProvider = new TypeProviderImpl(schemaContext);
65 DataSchemaNode leafref = ((ListSchemaNode) moduleRelative.getDataChildByName("neighbor")).getDataChildByName("neighbor-id");
66 LeafSchemaNode leaf = (LeafSchemaNode) leafref;
67 TypeDefinition<?> leafType = leaf.getType();
68 Type leafrefResolvedType = typeProvider.javaTypeForSchemaDefinitionType(leafType, leaf);
71 @Test (expected = YangValidationException.class)
72 public void testLeafRefAbsoluteSelfReference() throws Exception {
73 File relative = new File(getClass().getResource("/leafref/leafref-absolute-invalid.yang").toURI());
75 final YangParserImpl yangParser = new YangParserImpl();
76 final SchemaContext schemaContext = yangParser.parseFiles(Arrays.asList(relative));
77 final Module moduleRelative = schemaContext.findModuleByNamespace(new URI("urn:xml:ns:yang:lra")).iterator().next();
78 final TypeProviderImpl typeProvider = new TypeProviderImpl(schemaContext);
80 DataSchemaNode leafref = ((ListSchemaNode) moduleRelative.getDataChildByName("neighbor")).getDataChildByName("neighbor-id");
81 LeafSchemaNode leaf = (LeafSchemaNode) leafref;
82 TypeDefinition<?> leafType = leaf.getType();
83 Type leafrefResolvedType = typeProvider.javaTypeForSchemaDefinitionType(leafType, leaf);
87 public void testLeafRefRelativeAndAbsoluteValidReference() throws URISyntaxException {
88 File valid = new File(getClass().getResource("/leafref/leafref-valid.yang").toURI());
90 final YangParserImpl yangParser = new YangParserImpl();
91 final SchemaContext schemaContext = yangParser.parseFiles(Arrays.asList(valid));
92 final Module moduleValid = schemaContext.findModuleByNamespace(new URI("urn:xml:ns:yang:lrv")).iterator().next();
93 final TypeProviderImpl typeProvider = new TypeProviderImpl(schemaContext);
95 DataSchemaNode leafrefRel = ((ListSchemaNode) moduleValid.getDataChildByName("neighbor")).getDataChildByName
97 LeafSchemaNode leafRel = (LeafSchemaNode) leafrefRel;
98 TypeDefinition<?> leafTypeRel = leafRel.getType();
99 Type leafrefRelResolvedType = typeProvider.javaTypeForSchemaDefinitionType(leafTypeRel, leafRel);
100 assertNotNull(leafrefRelResolvedType);
102 DataSchemaNode leafrefAbs = ((ListSchemaNode) moduleValid.getDataChildByName("neighbor")).getDataChildByName
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 URISyntaxException {
112 final YangParserImpl yangParser = new YangParserImpl();
113 final File abstractTopology = new File(BaseYangTypes.class.getResource("/base-yang-types.yang")
115 final SchemaContext schemaContext = yangParser.parseFiles(Arrays.asList(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 StringType stringType = StringType.getInstance();
122 LeafSchemaNodeBuilder leafSchemaNodeBuilder = new LeafSchemaNodeBuilder("test-module", 111, QName.create("Cont1"), SchemaPath.ROOT);
123 leafSchemaNodeBuilder.setType(stringType);
124 LeafSchemaNode leafSchemaNode = leafSchemaNodeBuilder.build();
127 assertNotNull(typeProvider);
129 // test getAdditionalTypes() method
130 assertFalse(typeProvider.getAdditionalTypes().isEmpty());
132 // test getConstructorPropertyName() method
133 assertTrue(typeProvider.getConstructorPropertyName(null).isEmpty());
134 assertEquals("value", typeProvider.getConstructorPropertyName(stringType));
136 // test getParamNameFromType() method
137 assertEquals("string", typeProvider.getParamNameFromType(stringType));
139 // test getTypeDefaultConstruction() method for string type
140 assertEquals("\"default value\"", typeProvider.getTypeDefaultConstruction(leafSchemaNode, "default value"));
143 final BinaryType binaryType = BinaryType.getInstance();
144 leafSchemaNodeBuilder = new LeafSchemaNodeBuilder("test-module", 111, QName.create("Cont1"), SchemaPath.ROOT);
145 leafSchemaNodeBuilder.setType(binaryType);
146 leafSchemaNode = leafSchemaNodeBuilder.build();
147 assertEquals("new byte[] {-45}", typeProvider.getTypeDefaultConstruction(leafSchemaNode, "01"));
150 final BooleanType booleanType = BooleanType.getInstance();
151 leafSchemaNodeBuilder = new LeafSchemaNodeBuilder("test-module", 111, QName.create("Cont1"), SchemaPath.ROOT);
152 leafSchemaNodeBuilder.setType(booleanType);
153 leafSchemaNode = leafSchemaNodeBuilder.build();
154 assertEquals("new java.lang.Boolean(\"false\")", typeProvider.getTypeDefaultConstruction(leafSchemaNode, "false"));
157 final Decimal64 decimalType = Decimal64.create(refTypePath, 4);
158 leafSchemaNodeBuilder = new LeafSchemaNodeBuilder("test-module", 111, QName.create("Cont1"), SchemaPath.ROOT);
159 leafSchemaNodeBuilder.setType(decimalType);
160 leafSchemaNode = leafSchemaNodeBuilder.build();
161 assertEquals("new java.math.BigDecimal(\"111\")", typeProvider.getTypeDefaultConstruction(leafSchemaNode, "111"));
164 final EmptyType emptyType = EmptyType.getInstance();
165 leafSchemaNodeBuilder = new LeafSchemaNodeBuilder("test-module", 111, QName.create("Cont1"), SchemaPath.ROOT);
166 leafSchemaNodeBuilder.setType(emptyType);
167 leafSchemaNode = leafSchemaNodeBuilder.build();
168 assertEquals("new java.lang.Boolean(\"default value\")", typeProvider.getTypeDefaultConstruction(leafSchemaNode, "default value"));
171 expException.expect(NoSuchElementException.class);
172 final EnumerationType enumType = EnumerationType.create(refTypePath, new ArrayList<EnumTypeDefinition.EnumPair>(), Optional.<EnumPair> absent());
173 leafSchemaNodeBuilder = new LeafSchemaNodeBuilder("test-module", 111, QName.create("Cont1"), SchemaPath.ROOT);
174 leafSchemaNodeBuilder.setType(enumType);
175 leafSchemaNode = leafSchemaNodeBuilder.build();
176 assertEquals("\"default value\"", typeProvider.getTypeDefaultConstruction(leafSchemaNode, "default value"));
179 expException.expect(UnsupportedOperationException.class);
180 expException.expectMessage("Cannot get default construction for identityref type");
182 final ModuleBuilder testModBuilder = new ModuleBuilder("test-module", "/test");
183 final IdentitySchemaNodeBuilder identityNodeBuilder = testModBuilder.addIdentity(QName.create("IdentityRefTest"), 111, SchemaPath.ROOT);
184 final IdentitySchemaNode identitySchemaNode = identityNodeBuilder.build();
185 final IdentityrefType identityRef = IdentityrefType.create(refTypePath, identitySchemaNode);
186 leafSchemaNodeBuilder = new LeafSchemaNodeBuilder("test-module", 111, QName.create("Cont1"), SchemaPath.ROOT);
187 leafSchemaNodeBuilder.setType(identityRef);
189 leafSchemaNodeBuilder.setParent(identityNodeBuilder);
190 leafSchemaNode = leafSchemaNodeBuilder.build();
191 assertEquals("\"default value\"", typeProvider.getTypeDefaultConstruction(leafSchemaNode, "default value"));