2 * Copyright (c) 2017 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.yang.parser.stmt.rfc7950;
10 import static org.hamcrest.CoreMatchers.startsWith;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertTrue;
14 import java.util.Collection;
15 import java.util.List;
16 import org.junit.Test;
17 import org.opendaylight.yangtools.yang.common.QName;
18 import org.opendaylight.yangtools.yang.common.Revision;
19 import org.opendaylight.yangtools.yang.common.Uint32;
20 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
21 import org.opendaylight.yangtools.yang.model.api.Module;
22 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition;
23 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition.Bit;
24 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition;
25 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition.EnumPair;
26 import org.opendaylight.yangtools.yang.model.ri.type.BitBuilder;
27 import org.opendaylight.yangtools.yang.model.ri.type.EnumPairBuilder;
28 import org.opendaylight.yangtools.yang.stmt.AbstractYangTest;
30 public class Bug6887Test extends AbstractYangTest {
33 public void testRestrictedEnumeration() {
34 final var context = assertEffectiveModel("/rfc7950/bug6887/foo.yang");
36 final Module foo = context.findModule("foo", Revision.of("2017-01-26")).get();
37 final LeafSchemaNode myEnumerationLeaf = (LeafSchemaNode) foo.getDataChildByName(
38 QName.create(foo.getQNameModule(), "my-enumeration-leaf"));
40 EnumTypeDefinition enumerationType = (EnumTypeDefinition) myEnumerationLeaf.getType();
42 List<EnumPair> enums = enumerationType.getValues();
43 assertEquals(2, enums.size());
44 final EnumPair yellowEnum = createEnumPair("yellow", 2);
45 final EnumPair redEnum = createEnumPair("red", 3);
46 assertContainsEnums(enums, yellowEnum, redEnum);
48 enumerationType = enumerationType.getBaseType();
49 enums = enumerationType.getValues();
50 assertEquals(3, enums.size());
51 final EnumPair blackEnum = createEnumPair("black", 4);
52 assertContainsEnums(enums, yellowEnum, redEnum, blackEnum);
54 enumerationType = enumerationType.getBaseType();
55 enums = enumerationType.getValues();
56 assertEquals(4, enums.size());
57 final EnumPair whiteEnum = createEnumPair("white", 1);
58 assertContainsEnums(enums, whiteEnum, yellowEnum, redEnum, blackEnum);
60 final LeafSchemaNode myEnumerationLeaf2 = (LeafSchemaNode) foo.getDataChildByName(
61 QName.create(foo.getQNameModule(), "my-enumeration-leaf-2"));
63 enumerationType = (EnumTypeDefinition) myEnumerationLeaf2.getType();
64 enums = enumerationType.getValues();
65 assertEquals(3, enums.size());
66 assertContainsEnums(enums, yellowEnum, redEnum, blackEnum);
70 public void testInvalidRestrictedEnumeration() {
71 assertSourceException(startsWith("Enum 'purple' is not a subset of its base enumeration type "
72 + "(foo?revision=2017-02-02)my-derived-enumeration-type."), "/rfc7950/bug6887/foo-invalid.yang");
76 public void testInvalidRestrictedEnumeration2() {
77 assertInvalidEnumDefinitionException(startsWith("Enum 'magenta' is not a subset of its base enumeration type "
78 + "(foo?revision=2017-02-02)my-base-enumeration-type."), "/rfc7950/bug6887/foo-invalid-2.yang");
82 public void testInvalidRestrictedEnumeration3() {
83 assertInvalidEnumDefinitionException(startsWith("Value of enum 'red' must be the same as the value of "
84 + "corresponding enum in the base enumeration type (foo?revision=2017-02-02)"
85 + "my-derived-enumeration-type."), "/rfc7950/bug6887/foo-invalid-3.yang");
89 public void testInvalidRestrictedEnumeration4() {
90 assertInvalidEnumDefinitionException(startsWith("Value of enum 'black' must be the same as the value of "
91 + "corresponding enum in the base enumeration type (foo?revision=2017-02-02)"
92 + "my-base-enumeration-type."), "/rfc7950/bug6887/foo-invalid-4.yang");
96 public void testValidYang10EnumerationWithUnknownStatements() {
97 assertEffectiveModel("/rfc7950/bug6887/foo10-valid.yang");
101 public void testInvalidYang10RestrictedEnumeration() {
102 assertSourceException(startsWith("Restricted enumeration type is not allowed in YANG version 1 [at "),
103 "/rfc7950/bug6887/foo10-invalid.yang");
107 public void testInvalidYang10RestrictedEnumeration2() {
108 assertSourceException(startsWith("Restricted enumeration type is not allowed in YANG version 1 [at "),
109 "/rfc7950/bug6887/foo10-invalid-2.yang");
113 public void testRestrictedBits() {
114 final var context = assertEffectiveModel("/rfc7950/bug6887/bar.yang");
116 final Module bar = context.findModule("bar", Revision.of("2017-02-02")).get();
117 final LeafSchemaNode myBitsLeaf = (LeafSchemaNode) bar.getDataChildByName(
118 QName.create(bar.getQNameModule(), "my-bits-leaf"));
120 BitsTypeDefinition bitsType = (BitsTypeDefinition) myBitsLeaf.getType();
122 Collection<? extends Bit> bits = bitsType.getBits();
123 assertEquals(2, bits.size());
124 Bit bitB = createBit("bit-b", 2);
125 Bit bitC = createBit("bit-c", 3);
126 assertContainsBits(bits, bitB, bitC);
128 bitsType = bitsType.getBaseType();
129 bits = bitsType.getBits();
130 assertEquals(3, bits.size());
131 bitB = createBit("bit-b", 2);
132 bitC = createBit("bit-c", 3);
133 Bit bitD = createBit("bit-d", 4);
134 assertContainsBits(bits, bitB, bitC, bitD);
136 bitsType = bitsType.getBaseType();
137 bits = bitsType.getBits();
138 assertEquals(4, bits.size());
139 final Bit bitA = createBit("bit-a", 1);
140 bitB = createBit("bit-b", 2);
141 bitC = createBit("bit-c", 3);
142 bitD = createBit("bit-d", 4);
143 assertContainsBits(bits, bitA, bitB, bitC, bitD);
145 final LeafSchemaNode myBitsLeaf2 = (LeafSchemaNode) bar.getDataChildByName(
146 QName.create(bar.getQNameModule(), "my-bits-leaf-2"));
148 bitsType = (BitsTypeDefinition) myBitsLeaf2.getType();
149 bits = bitsType.getBits();
150 assertEquals(3, bits.size());
151 bitB = createBit("bit-b", 2);
152 bitC = createBit("bit-c", 3);
153 bitD = createBit("bit-d", 4);
154 assertContainsBits(bits, bitB, bitC, bitD);
158 public void testInvalidRestrictedBits() {
159 assertSourceException(startsWith("Bit 'bit-w' is not a subset of its base bits type "
160 + "(bar?revision=2017-02-02)my-derived-bits-type."), "/rfc7950/bug6887/bar-invalid.yang");
164 public void testInvalidRestrictedBits2() {
165 assertInvalidBitDefinitionException(startsWith("Bit 'bit-x' is not a subset of its base bits type "
166 + "(bar?revision=2017-02-02)my-base-bits-type."), "/rfc7950/bug6887/bar-invalid-2.yang");
170 public void testInvalidRestrictedBits3() {
171 assertInvalidBitDefinitionException(startsWith("Position of bit 'bit-c' must be the same as the position of "
172 + "corresponding bit in the base bits type (bar?revision=2017-02-02)my-derived-bits-type."),
173 "/rfc7950/bug6887/bar-invalid-3.yang");
177 public void testInvalidRestrictedBits4() {
178 assertInvalidBitDefinitionException(startsWith("Position of bit 'bit-d' must be the same as the position of "
179 + "corresponding bit in the base bits type (bar?revision=2017-02-02)my-base-bits-type."),
180 "/rfc7950/bug6887/bar-invalid-4.yang");
184 public void testValidYang10BitsWithUnknownStatements() {
185 assertEffectiveModel("/rfc7950/bug6887/bar10-valid.yang");
189 public void testInvalidYang10RestrictedBits() {
190 assertSourceException(startsWith("Restricted bits type is not allowed in YANG version 1 [at "),
191 "/rfc7950/bug6887/bar10-invalid.yang");
195 public void testInvalidYang10RestrictedBits2() {
196 assertSourceException(startsWith("Restricted bits type is not allowed in YANG version 1 [at "),
197 "/rfc7950/bug6887/bar10-invalid-2.yang");
200 private static EnumPair createEnumPair(final String name, final int value) {
201 return EnumPairBuilder.create(name, value).build();
204 private static void assertContainsEnums(final List<EnumPair> enumList, final EnumPair... enumPairs) {
205 for (final EnumPair enumPair : enumPairs) {
206 assertTrue(enumList.contains(enumPair));
210 private static Bit createBit(final String name, final long position) {
211 return BitBuilder.create(name, Uint32.valueOf(position)).build();
214 private static void assertContainsBits(final Collection<? extends Bit> bitList, final Bit... bits) {
215 for (final Bit bit : bits) {
216 assertTrue(bitList.contains(bit));