1742adcceea3d61801974eeffd8426d5d775cc64
[mdsal.git] / binding / mdsal-binding-generator-impl / src / test / java / org / opendaylight / yangtools / sal / binding / yang / types / 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;
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
15 import com.google.common.base.Optional;
16 import java.io.File;
17 import java.net.URI;
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;
50
51 public class TypeProviderImplTest {
52
53     @Rule
54     public ExpectedException expException = ExpectedException.none();
55
56     @Test (expected = YangValidationException.class)
57     public void testLeafRefRelativeSelfReference() throws Exception {
58         File relative = new File(getClass().getResource("/leafref/leafref-relative-invalid.yang").toURI());
59
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);
64
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);
69     }
70
71     @Test (expected = YangValidationException.class)
72     public void testLeafRefAbsoluteSelfReference() throws Exception {
73         File relative = new File(getClass().getResource("/leafref/leafref-absolute-invalid.yang").toURI());
74
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);
79
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);
84     }
85
86     @Test
87     public void testLeafRefRelativeAndAbsoluteValidReference() throws URISyntaxException {
88         File valid = new File(getClass().getResource("/leafref/leafref-valid.yang").toURI());
89
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);
94
95         DataSchemaNode leafrefRel = ((ListSchemaNode) moduleValid.getDataChildByName("neighbor")).getDataChildByName
96                 ("neighbor-id");
97         LeafSchemaNode leafRel = (LeafSchemaNode) leafrefRel;
98         TypeDefinition<?> leafTypeRel = leafRel.getType();
99         Type leafrefRelResolvedType = typeProvider.javaTypeForSchemaDefinitionType(leafTypeRel, leafRel);
100         assertNotNull(leafrefRelResolvedType);
101
102         DataSchemaNode leafrefAbs = ((ListSchemaNode) moduleValid.getDataChildByName("neighbor")).getDataChildByName
103                 ("neighbor2-id");
104         LeafSchemaNode leafAbs = (LeafSchemaNode) leafrefAbs;
105         TypeDefinition<?> leafTypeAbs = leafAbs.getType();
106         Type leafrefAbsResolvedType = typeProvider.javaTypeForSchemaDefinitionType(leafTypeAbs, leafAbs);
107         assertNotNull(leafrefAbsResolvedType);
108     }
109
110     @Test
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")
114                 .toURI());
115         final SchemaContext schemaContext = yangParser.parseFiles(Arrays.asList(abstractTopology));
116         final TypeProviderImpl typeProvider = new TypeProviderImpl(schemaContext);
117
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();
125
126         // test constructor
127         assertNotNull(typeProvider);
128
129         // test getAdditionalTypes() method
130         assertFalse(typeProvider.getAdditionalTypes().isEmpty());
131
132         // test getConstructorPropertyName() method
133         assertTrue(typeProvider.getConstructorPropertyName(null).isEmpty());
134         assertEquals("value", typeProvider.getConstructorPropertyName(stringType));
135
136         // test getParamNameFromType() method
137         assertEquals("string", typeProvider.getParamNameFromType(stringType));
138
139         // test getTypeDefaultConstruction() method for string type
140         assertEquals("\"default value\"", typeProvider.getTypeDefaultConstruction(leafSchemaNode, "default value"));
141
142         // binary type
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"));
148
149         // boolean type
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"));
155
156         // decimal type
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"));
162
163         // empty type
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"));
169
170         // enum type
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"));
177
178         // identityref type
179         expException.expect(UnsupportedOperationException.class);
180         expException.expectMessage("Cannot get default construction for identityref type");
181
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);
188
189         leafSchemaNodeBuilder.setParent(identityNodeBuilder);
190         leafSchemaNode = leafSchemaNodeBuilder.build();
191         assertEquals("\"default value\"", typeProvider.getTypeDefaultConstruction(leafSchemaNode, "default value"));
192     }
193 }