Port yang-parser-rfc7950 to JUnit 5
[yangtools.git] / parser / yang-parser-rfc7950 / src / test / java / org / opendaylight / yangtools / yang / stmt / TypesResolutionTest.java
1 /*
2  * Copyright (c) 2016 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.yang.stmt;
9
10 import static org.hamcrest.CoreMatchers.startsWith;
11 import static org.junit.jupiter.api.Assertions.assertEquals;
12 import static org.junit.jupiter.api.Assertions.assertFalse;
13 import static org.junit.jupiter.api.Assertions.assertNotNull;
14 import static org.junit.jupiter.api.Assertions.assertTrue;
15
16 import com.google.common.collect.Iterables;
17 import com.google.common.collect.Range;
18 import java.util.Collection;
19 import java.util.Iterator;
20 import java.util.List;
21 import java.util.Optional;
22 import org.junit.jupiter.api.BeforeAll;
23 import org.junit.jupiter.api.Test;
24 import org.opendaylight.yangtools.yang.common.QName;
25 import org.opendaylight.yangtools.yang.common.Revision;
26 import org.opendaylight.yangtools.yang.common.Uint32;
27 import org.opendaylight.yangtools.yang.common.XMLNamespace;
28 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
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.Module;
32 import org.opendaylight.yangtools.yang.model.api.Status;
33 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
34 import org.opendaylight.yangtools.yang.model.api.stmt.UnrecognizedStatement;
35 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition;
36 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition.Bit;
37 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition;
38 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition.EnumPair;
39 import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition;
40 import org.opendaylight.yangtools.yang.model.api.type.InstanceIdentifierTypeDefinition;
41 import org.opendaylight.yangtools.yang.model.api.type.LengthConstraint;
42 import org.opendaylight.yangtools.yang.model.api.type.PatternConstraint;
43 import org.opendaylight.yangtools.yang.model.api.type.StringTypeDefinition;
44 import org.opendaylight.yangtools.yang.model.api.type.UnionTypeDefinition;
45
46 class TypesResolutionTest extends AbstractYangTest {
47     private static EffectiveModelContext CONTEXT;
48
49     @BeforeAll
50     static void beforeClass() {
51         CONTEXT = assertEffectiveModel(
52             "/types/custom-types-test@2012-04-04.yang",
53             "/ietf/iana-timezones@2012-07-09.yang",
54             "/ietf/ietf-inet-types@2010-09-24.yang",
55             "/ietf/ietf-yang-types@2010-09-24.yang");
56         assertEquals(4, CONTEXT.getModules().size());
57     }
58
59     @Test
60     void testIPVersion() {
61         Module tested = CONTEXT.findModules("ietf-inet-types").iterator().next();
62         Collection<? extends TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
63         assertEquals(14, typedefs.size());
64
65         TypeDefinition<?> type = TestUtils.findTypedef(typedefs, "ip-version");
66         assertTrue(type.getDescription().get().contains("This value represents the version of the IP protocol."));
67         assertTrue(type.getReference().get().contains("RFC 2460: Internet Protocol, Version 6 (IPv6) Specification"));
68
69         EnumTypeDefinition enumType = (EnumTypeDefinition) type.getBaseType();
70         List<EnumPair> values = enumType.getValues();
71         assertEquals(3, values.size());
72
73         EnumPair value0 = values.get(0);
74         assertEquals("unknown", value0.getName());
75         assertEquals(0, value0.getValue());
76         assertEquals(Optional.of("An unknown or unspecified version of the Internet protocol."),
77             value0.getDescription());
78
79         EnumPair value1 = values.get(1);
80         assertEquals("ipv4", value1.getName());
81         assertEquals(1, value1.getValue());
82         assertEquals(Optional.of("The IPv4 protocol as defined in RFC 791."), value1.getDescription());
83
84         EnumPair value2 = values.get(2);
85         assertEquals("ipv6", value2.getName());
86         assertEquals(2, value2.getValue());
87         assertEquals(Optional.of("The IPv6 protocol as defined in RFC 2460."), value2.getDescription());
88     }
89
90     @Test
91     void testEnumeration() {
92         Module tested = CONTEXT.findModules("custom-types-test").iterator().next();
93         Collection<? extends TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
94
95         TypeDefinition<?> type = TestUtils.findTypedef(typedefs, "ip-version");
96         EnumTypeDefinition enumType = (EnumTypeDefinition) type.getBaseType();
97         List<EnumPair> values = enumType.getValues();
98         assertEquals(4, values.size());
99
100         EnumPair value0 = values.get(0);
101         assertEquals("unknown", value0.getName());
102         assertEquals(0, value0.getValue());
103         assertEquals(Optional.of("An unknown or unspecified version of the Internet protocol."),
104             value0.getDescription());
105
106         EnumPair value1 = values.get(1);
107         assertEquals("ipv4", value1.getName());
108         assertEquals(19, value1.getValue());
109         assertEquals(Optional.of("The IPv4 protocol as defined in RFC 791."), value1.getDescription());
110
111         EnumPair value2 = values.get(2);
112         assertEquals("ipv6", value2.getName());
113         assertEquals(7, value2.getValue());
114         assertEquals(Optional.of("The IPv6 protocol as defined in RFC 2460."), value2.getDescription());
115
116         EnumPair value3 = values.get(3);
117         assertEquals("default", value3.getName());
118         assertEquals(20, value3.getValue());
119         assertEquals(Optional.of("default ip"), value3.getDescription());
120     }
121
122     @Test
123     void testIpAddress() {
124         Module tested = CONTEXT.findModules("ietf-inet-types").iterator().next();
125         Collection<? extends TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
126         TypeDefinition<?> type = TestUtils.findTypedef(typedefs, "ip-address");
127         UnionTypeDefinition baseType = (UnionTypeDefinition) type.getBaseType();
128         List<TypeDefinition<?>> unionTypes = baseType.getTypes();
129
130         StringTypeDefinition ipv4 = (StringTypeDefinition) unionTypes.get(0);
131         assertNotNull(ipv4.getBaseType());
132         String expectedPattern = "^(?:(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\\.){3}"
133             + "([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])" + "(%[\\p{N}\\p{L}]+)?)$";
134         assertEquals(expectedPattern, ipv4.getPatternConstraints().get(0).getJavaPatternString());
135
136         StringTypeDefinition ipv6 = (StringTypeDefinition) unionTypes.get(1);
137         assertNotNull(ipv6.getBaseType());
138         List<PatternConstraint> ipv6Patterns = ipv6.getPatternConstraints();
139         expectedPattern = "^(?:((:|[0-9a-fA-F]{0,4}):)([0-9a-fA-F]{0,4}:){0,5}"
140             + "((([0-9a-fA-F]{0,4}:)?(:|[0-9a-fA-F]{0,4}))|" + "(((25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9])\\.){3}"
141             + "(25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9])))" + "(%[\\p{N}\\p{L}]+)?)$";
142         assertEquals(expectedPattern, ipv6Patterns.get(0).getJavaPatternString());
143
144         expectedPattern = "^(?:(([^:]+:){6}(([^:]+:[^:]+)|(.*\\..*)))|" + "((([^:]+:)*[^:]+)?::(([^:]+:)*[^:]+)?)"
145             + "(%.+)?)$";
146         assertEquals(expectedPattern, ipv6Patterns.get(1).getJavaPatternString());
147     }
148
149     @Test
150     void testDomainName() {
151         Module tested = CONTEXT.findModules("ietf-inet-types").iterator().next();
152         Collection<? extends TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
153         StringTypeDefinition type = (StringTypeDefinition) TestUtils.findTypedef(typedefs, "domain-name");
154         assertNotNull(type.getBaseType());
155         List<PatternConstraint> patterns = type.getPatternConstraints();
156         assertEquals(1, patterns.size());
157         String expectedPattern = "^(?:((([a-zA-Z0-9_]([a-zA-Z0-9\\-_]){0,61})?[a-zA-Z0-9]\\.)*"
158             + "([a-zA-Z0-9_]([a-zA-Z0-9\\-_]){0,61})?[a-zA-Z0-9]\\.?)" + "|\\.)$";
159         assertEquals(expectedPattern, patterns.get(0).getJavaPatternString());
160
161         LengthConstraint lengths = type.getLengthConstraint().get();
162         assertEquals(1, lengths.getAllowedRanges().asRanges().size());
163         Range<Integer> length = lengths.getAllowedRanges().span();
164         assertEquals(Integer.valueOf(1), length.lowerEndpoint());
165         assertEquals(Integer.valueOf(253), length.upperEndpoint());
166     }
167
168     @Test
169     void testInstanceIdentifier1() {
170         Module tested = CONTEXT.findModules("custom-types-test").iterator().next();
171         LeafSchemaNode leaf = (LeafSchemaNode) tested.getDataChildByName(
172             QName.create(tested.getQNameModule(), "inst-id-leaf1"));
173         InstanceIdentifierTypeDefinition leafType = (InstanceIdentifierTypeDefinition) leaf.getType();
174         assertFalse(leafType.requireInstance());
175         assertEquals(1,
176             leaf.asEffectiveStatement().getDeclared().declaredSubstatements(UnrecognizedStatement.class).size());
177     }
178
179     @Test
180     void testInstanceIdentifier2() {
181         Module tested = CONTEXT.findModules("custom-types-test").iterator().next();
182         LeafSchemaNode leaf = (LeafSchemaNode) tested.getDataChildByName(
183             QName.create(tested.getQNameModule(), "inst-id-leaf2"));
184         InstanceIdentifierTypeDefinition leafType = (InstanceIdentifierTypeDefinition) leaf.getType();
185         assertFalse(leafType.requireInstance());
186     }
187
188     @Test
189     void testIdentity() {
190         Module tested = CONTEXT.findModules("custom-types-test").iterator().next();
191         Collection<? extends IdentitySchemaNode> identities = tested.getIdentities();
192         assertEquals(5, identities.size());
193         IdentitySchemaNode cryptoAlg = null;
194         IdentitySchemaNode cryptoBase = null;
195         IdentitySchemaNode cryptoId = null;
196         for (IdentitySchemaNode id : identities) {
197             if (id.getQName().getLocalName().equals("crypto-alg")) {
198                 cryptoAlg = id;
199             } else if ("crypto-base".equals(id.getQName().getLocalName())) {
200                 cryptoBase = id;
201             } else if ("crypto-id".equals(id.getQName().getLocalName())) {
202                 cryptoId = id;
203             }
204         }
205         assertNotNull(cryptoAlg);
206         IdentitySchemaNode baseIdentity = Iterables.getOnlyElement(cryptoAlg.getBaseIdentities());
207         assertEquals("crypto-base", baseIdentity.getQName().getLocalName());
208         assertEquals(0, CONTEXT.getDerivedIdentities(cryptoAlg).size());
209         assertEquals(0, baseIdentity.getBaseIdentities().size());
210
211         assertNotNull(cryptoBase);
212         assertTrue(cryptoBase.getBaseIdentities().isEmpty());
213         assertEquals(3, CONTEXT.getDerivedIdentities(cryptoBase).size());
214
215         assertNotNull(cryptoId);
216         assertEquals(1,
217             cryptoId.asEffectiveStatement().getDeclared().declaredSubstatements(UnrecognizedStatement.class).size());
218     }
219
220     @Test
221     void testBitsType1() {
222         Module tested = CONTEXT.findModules("custom-types-test").iterator().next();
223         LeafSchemaNode leaf = (LeafSchemaNode) tested.getDataChildByName(
224             QName.create(tested.getQNameModule(), "mybits"));
225         BitsTypeDefinition leafType = (BitsTypeDefinition) leaf.getType();
226         Iterator<? extends Bit> bits = leafType.getBits().iterator();
227
228         Bit bit1 = bits.next();
229         assertEquals("disable-nagle", bit1.getName());
230         assertEquals(Uint32.ZERO, bit1.getPosition());
231
232         Bit bit2 = bits.next();
233         assertEquals("auto-sense-speed", bit2.getName());
234         assertEquals(Uint32.ONE, bit2.getPosition());
235
236         Bit bit3 = bits.next();
237         assertEquals("only-10-Mb", bit3.getName());
238         assertEquals(Uint32.TWO, bit3.getPosition());
239
240         assertFalse(bits.hasNext());
241     }
242
243     @Test
244     void testBitsType2() {
245         Module tested = CONTEXT.findModules("custom-types-test").iterator().next();
246         Collection<? extends TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
247         TypeDefinition<?> testedType = TestUtils.findTypedef(typedefs, "access-operations-type");
248
249         BitsTypeDefinition bitsType = (BitsTypeDefinition) testedType.getBaseType();
250         Iterator<? extends Bit> bits = bitsType.getBits().iterator();
251
252         Bit bit0 = bits.next();
253         assertEquals("create", bit0.getName());
254         assertEquals(Uint32.ZERO, bit0.getPosition());
255
256         Bit bit1 = bits.next();
257         assertEquals("delete", bit1.getName());
258         assertEquals(Uint32.valueOf(365), bit1.getPosition());
259
260         Bit bit2 = bits.next();
261         assertEquals("read", bit2.getName());
262         assertEquals(Uint32.valueOf(500), bit2.getPosition());
263
264         Bit bit3 = bits.next();
265         assertEquals("update", bit3.getName());
266         assertEquals(Uint32.valueOf(501), bit3.getPosition());
267
268         Bit bit4 = bits.next();
269         assertEquals("exec", bit4.getName());
270         assertEquals(Uint32.valueOf(502), bit4.getPosition());
271
272         assertFalse(bits.hasNext());
273     }
274
275     @Test
276     void testIanaTimezones() {
277         Module tested = CONTEXT.findModules("iana-timezones").iterator().next();
278         Collection<? extends TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
279         TypeDefinition<?> testedType = TestUtils.findTypedef(typedefs, "iana-timezone");
280
281         String expectedDesc = "A timezone location as defined by the IANA timezone";
282         assertTrue(testedType.getDescription().get().contains(expectedDesc));
283         assertFalse(testedType.getReference().isPresent());
284         assertEquals(Status.CURRENT, testedType.getStatus());
285
286         QName testedTypeQName = testedType.getQName();
287         assertEquals(XMLNamespace.of("urn:ietf:params:xml:ns:yang:iana-timezones"), testedTypeQName.getNamespace());
288         assertEquals(Revision.ofNullable("2012-07-09"), testedTypeQName.getRevision());
289         assertEquals("iana-timezone", testedTypeQName.getLocalName());
290
291         EnumTypeDefinition enumType = (EnumTypeDefinition) testedType.getBaseType();
292         List<EnumPair> values = enumType.getValues();
293         // 0-414
294         assertEquals(415, values.size());
295
296         EnumPair enum168 = values.get(168);
297         assertEquals("America/Danmarkshavn", enum168.getName());
298         assertEquals(168, enum168.getValue());
299         assertEquals(Optional.of("east coast, north of Scoresbysund"), enum168.getDescription());
300
301         EnumPair enum374 = values.get(374);
302         assertEquals("America/Indiana/Winamac", enum374.getName());
303         assertEquals(374, enum374.getValue());
304         assertEquals(Optional.of("Eastern Time - Indiana - Pulaski County"), enum374.getDescription());
305     }
306
307     @Test
308     void testObjectId128() {
309         Module tested = CONTEXT.findModules("ietf-yang-types").iterator().next();
310         Collection<? extends TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
311         StringTypeDefinition testedType = (StringTypeDefinition) TestUtils.findTypedef(typedefs,
312             "object-identifier-128");
313
314         List<PatternConstraint> patterns = testedType.getPatternConstraints();
315         assertEquals(1, patterns.size());
316         PatternConstraint pattern = patterns.get(0);
317         assertEquals("^(?:\\d*(\\.\\d*){1,127})$", pattern.getJavaPatternString());
318
319         QName testedTypeQName = testedType.getQName();
320         assertEquals(XMLNamespace.of("urn:ietf:params:xml:ns:yang:ietf-yang-types"), testedTypeQName.getNamespace());
321         assertEquals(Revision.ofNullable("2010-09-24"), testedTypeQName.getRevision());
322         assertEquals("object-identifier-128", testedTypeQName.getLocalName());
323
324         StringTypeDefinition testedTypeBase = testedType.getBaseType();
325         patterns = testedTypeBase.getPatternConstraints();
326         assertEquals(1, patterns.size());
327
328         pattern = patterns.get(0);
329         assertEquals("^(?:(([0-1](\\.[1-3]?[0-9]))|(2\\.(0|([1-9]\\d*))))(\\.(0|([1-9]\\d*)))*)$",
330             pattern.getJavaPatternString());
331
332         QName testedTypeBaseQName = testedTypeBase.getQName();
333         assertEquals(XMLNamespace.of("urn:ietf:params:xml:ns:yang:ietf-yang-types"),
334             testedTypeBaseQName.getNamespace());
335         assertEquals(Revision.ofNullable("2010-09-24"), testedTypeBaseQName.getRevision());
336         assertEquals("object-identifier", testedTypeBaseQName.getLocalName());
337     }
338
339     @Test
340     void testIdentityref() {
341         Module tested = CONTEXT.findModules("custom-types-test").iterator().next();
342         Collection<? extends TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
343         TypeDefinition<?> testedType = TestUtils.findTypedef(typedefs, "service-type-ref");
344         IdentityrefTypeDefinition baseType = (IdentityrefTypeDefinition) testedType.getBaseType();
345         QName identity = baseType.getIdentities().iterator().next().getQName();
346         assertEquals(XMLNamespace.of("urn:custom.types.demo"), identity.getNamespace());
347         assertEquals(Revision.ofNullable("2012-04-16"), identity.getRevision());
348         assertEquals("service-type", identity.getLocalName());
349
350         LeafSchemaNode type = (LeafSchemaNode) tested.getDataChildByName(QName.create(tested.getQNameModule(), "type"));
351         assertNotNull(type);
352     }
353
354     @Test
355     void testUnionWithExt() {
356         assertEffectiveModel(
357             "/types/union-with-ext/extdef.yang",
358             "/types/union-with-ext/unionbug.yang",
359             "/ietf/ietf-inet-types@2010-09-24.yang");
360     }
361
362     @Test
363     void testUnionWithBits() {
364         assertEffectiveModel("/types/union-with-bits/union-bits-model.yang");
365     }
366
367     @Test
368     void testUnionInList() {
369         assertSourceException(startsWith("union is not a YANG statement or use of extension"),
370             "/types/union-in-list/unioninlisttest.yang");
371     }
372 }