Remove @Deprecated ClassLoaderUtils as it's now in yangtools.util
[mdsal.git] / binding / mdsal-binding-generator-impl / src / test / java / org / opendaylight / yangtools / sal / binding / yang / types / stmt / parser / retest / TypeProviderImplTest.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.sal.binding.yang.types.stmt.parser.retest;
9
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 java.io.File;
15 import java.io.FileNotFoundException;
16 import java.net.URI;
17 import java.net.URISyntaxException;
18 import java.util.NoSuchElementException;
19 import org.junit.Rule;
20 import org.junit.Test;
21 import org.junit.rules.ExpectedException;
22 import org.opendaylight.yangtools.binding.generator.util.generated.type.builder.GeneratedTypeBuilderImpl;
23 import org.opendaylight.yangtools.sal.binding.model.api.Type;
24 import org.opendaylight.yangtools.sal.binding.yang.types.TypeProviderImpl;
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.builder.impl.IdentitySchemaNodeBuilder;
44 import org.opendaylight.yangtools.yang.parser.builder.impl.LeafSchemaNodeBuilder;
45 import org.opendaylight.yangtools.yang.parser.builder.impl.ModuleBuilder;
46 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
47 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
48 import org.opendaylight.yangtools.yang.parser.util.YangValidationException;
49
50 public class TypeProviderImplTest {
51
52     @Rule
53     public ExpectedException expException = ExpectedException.none();
54
55     @Test (expected = YangValidationException.class)
56     public void testLeafRefRelativeSelfReference() throws Exception {
57         File relative = new File(getClass().getResource("/leafref/leafref-relative-invalid.yang").toURI());
58
59         final SchemaContext schemaContext = RetestUtils.parseYangSources(relative);
60         final Module moduleRelative = schemaContext.findModuleByNamespace(new URI("urn:xml:ns:yang:lrr")).iterator().next();
61         final TypeProviderImpl typeProvider = new TypeProviderImpl(schemaContext);
62
63         DataSchemaNode leafref = ((ListSchemaNode) moduleRelative.getDataChildByName("neighbor")).getDataChildByName("neighbor-id");
64         LeafSchemaNode leaf = (LeafSchemaNode) leafref;
65         TypeDefinition<?> leafType = leaf.getType();
66         Type leafrefResolvedType = typeProvider.javaTypeForSchemaDefinitionType(leafType, leaf);
67     }
68
69     @Test (expected = YangValidationException.class)
70     public void testLeafRefAbsoluteSelfReference() throws Exception {
71         File relative = new File(getClass().getResource("/leafref/leafref-absolute-invalid.yang").toURI());
72
73         final SchemaContext schemaContext = RetestUtils.parseYangSources(relative);
74         final Module moduleRelative = schemaContext.findModuleByNamespace(new URI("urn:xml:ns:yang:lra")).iterator().next();
75         final TypeProviderImpl typeProvider = new TypeProviderImpl(schemaContext);
76
77         DataSchemaNode leafref = ((ListSchemaNode) moduleRelative.getDataChildByName("neighbor")).getDataChildByName("neighbor-id");
78         LeafSchemaNode leaf = (LeafSchemaNode) leafref;
79         TypeDefinition<?> leafType = leaf.getType();
80         Type leafrefResolvedType = typeProvider.javaTypeForSchemaDefinitionType(leafType, leaf);
81     }
82
83     @Test
84     public void testLeafRefRelativeAndAbsoluteValidReference() throws URISyntaxException, SourceException, FileNotFoundException, ReactorException {
85         File valid = new File(getClass().getResource("/leafref/leafref-valid.yang").toURI());
86
87         final SchemaContext schemaContext = RetestUtils.parseYangSources(valid);
88         final Module moduleValid = schemaContext.findModuleByNamespace(new URI("urn:xml:ns:yang:lrv")).iterator().next();
89         final TypeProviderImpl typeProvider = new TypeProviderImpl(schemaContext);
90
91         DataSchemaNode leafrefRel = ((ListSchemaNode) moduleValid.getDataChildByName("neighbor")).getDataChildByName
92                 ("neighbor-id");
93         LeafSchemaNode leafRel = (LeafSchemaNode) leafrefRel;
94         TypeDefinition<?> leafTypeRel = leafRel.getType();
95         Type leafrefRelResolvedType = typeProvider.javaTypeForSchemaDefinitionType(leafTypeRel, leafRel);
96         assertNotNull(leafrefRelResolvedType);
97
98         DataSchemaNode leafrefAbs = ((ListSchemaNode) moduleValid.getDataChildByName("neighbor")).getDataChildByName
99                 ("neighbor2-id");
100         LeafSchemaNode leafAbs = (LeafSchemaNode) leafrefAbs;
101         TypeDefinition<?> leafTypeAbs = leafAbs.getType();
102         Type leafrefAbsResolvedType = typeProvider.javaTypeForSchemaDefinitionType(leafTypeAbs, leafAbs);
103         assertNotNull(leafrefAbsResolvedType);
104     }
105
106     @Test
107     public void testMethodsOfTypeProviderImpl() throws URISyntaxException, SourceException, FileNotFoundException, ReactorException {
108         final File abstractTopology = new File(getClass().getResource("/base-yang-types.yang")
109                 .toURI());
110
111         final SchemaContext schemaContext = RetestUtils.parseYangSources(abstractTopology);
112
113         final TypeProviderImpl typeProvider = new TypeProviderImpl(schemaContext);
114
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();
119         LeafSchemaNodeBuilder leafSchemaNodeBuilder = new LeafSchemaNodeBuilder("test-module", 111, QName.create("Cont1"), SchemaPath.ROOT);
120         leafSchemaNodeBuilder.setType(stringType);
121         LeafSchemaNode leafSchemaNode = leafSchemaNodeBuilder.build();
122
123         // test constructor
124         assertNotNull(typeProvider);
125
126         // test getAdditionalTypes() method
127         assertFalse(typeProvider.getAdditionalTypes().isEmpty());
128
129         // test getConstructorPropertyName() method
130         assertTrue(typeProvider.getConstructorPropertyName(null).isEmpty());
131         assertEquals("value", typeProvider.getConstructorPropertyName(stringType));
132
133         // test getParamNameFromType() method
134         assertEquals("string", typeProvider.getParamNameFromType(stringType));
135
136         // test getTypeDefaultConstruction() method for string type
137         assertEquals("\"default value\"", typeProvider.getTypeDefaultConstruction(leafSchemaNode, "default value"));
138
139         // binary type
140         final BinaryTypeDefinition binaryType = BaseTypes.binaryType();
141         leafSchemaNodeBuilder = new LeafSchemaNodeBuilder("test-module", 111, QName.create("Cont1"), SchemaPath.ROOT);
142         leafSchemaNodeBuilder.setType(binaryType);
143         leafSchemaNode = leafSchemaNodeBuilder.build();
144         assertEquals("new byte[] {-45}", typeProvider.getTypeDefaultConstruction(leafSchemaNode, "01"));
145
146         // boolean type
147         final BooleanTypeDefinition booleanType = BaseTypes.booleanType();
148         leafSchemaNodeBuilder = new LeafSchemaNodeBuilder("test-module", 111, QName.create("Cont1"), SchemaPath.ROOT);
149         leafSchemaNodeBuilder.setType(booleanType);
150         leafSchemaNode = leafSchemaNodeBuilder.build();
151         assertEquals("new java.lang.Boolean(\"false\")", typeProvider.getTypeDefaultConstruction(leafSchemaNode, "false"));
152
153         // decimal type
154         final DecimalTypeDefinition decimalType = BaseTypes.decimalTypeBuilder(refTypePath).setFractionDigits(4).build();
155         leafSchemaNodeBuilder = new LeafSchemaNodeBuilder("test-module", 111, QName.create("Cont1"), SchemaPath.ROOT);
156         leafSchemaNodeBuilder.setType(decimalType);
157         leafSchemaNode = leafSchemaNodeBuilder.build();
158         assertEquals("new java.math.BigDecimal(\"111\")", typeProvider.getTypeDefaultConstruction(leafSchemaNode, "111"));
159
160         // empty type
161         final EmptyTypeDefinition emptyType = BaseTypes.emptyType();
162         leafSchemaNodeBuilder = new LeafSchemaNodeBuilder("test-module", 111, QName.create("Cont1"), SchemaPath.ROOT);
163         leafSchemaNodeBuilder.setType(emptyType);
164         leafSchemaNode = leafSchemaNodeBuilder.build();
165         assertEquals("new java.lang.Boolean(\"default value\")", typeProvider.getTypeDefaultConstruction(leafSchemaNode, "default value"));
166
167         // enum type
168         expException.expect(NoSuchElementException.class);
169         final EnumTypeDefinition enumType =  BaseTypes.enumerationTypeBuilder(refTypePath).build();
170         leafSchemaNodeBuilder = new LeafSchemaNodeBuilder("test-module", 111, QName.create("Cont1"), SchemaPath.ROOT);
171         leafSchemaNodeBuilder.setType(enumType);
172         leafSchemaNode = leafSchemaNodeBuilder.build();
173         assertEquals("\"default value\"", typeProvider.getTypeDefaultConstruction(leafSchemaNode, "default value"));
174
175         // identityref type
176         expException.expect(UnsupportedOperationException.class);
177         expException.expectMessage("Cannot get default construction for identityref type");
178
179         final ModuleBuilder testModBuilder = new ModuleBuilder("test-module", "/test");
180         final IdentitySchemaNodeBuilder identityNodeBuilder = testModBuilder.addIdentity(QName.create("IdentityRefTest"), 111, SchemaPath.ROOT);
181         final IdentitySchemaNode identitySchemaNode = identityNodeBuilder.build();
182
183         final IdentityrefTypeBuilder identityRefBuilder = BaseTypes.identityrefTypeBuilder(refTypePath);
184         identityRefBuilder.setIdentity(identitySchemaNode);
185         final IdentityrefTypeDefinition identityRef =  identityRefBuilder.build();
186         leafSchemaNodeBuilder = new LeafSchemaNodeBuilder("test-module", 111, QName.create("Cont1"), SchemaPath.ROOT);
187         leafSchemaNodeBuilder.setType(identityRef);
188
189         leafSchemaNodeBuilder.setParent(identityNodeBuilder);
190         leafSchemaNode = leafSchemaNodeBuilder.build();
191         assertEquals("\"default value\"", typeProvider.getTypeDefaultConstruction(leafSchemaNode, "default value"));
192     }
193 }