Further yang-parser-impl checkstyle fixes
[yangtools.git] / yang / yang-parser-impl / src / test / java / org / opendaylight / yangtools / yang / parser / stmt / rfc7950 / Bug6887Test.java
1 /*
2  * Copyright (c) 2017 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
9 package org.opendaylight.yangtools.yang.parser.stmt.rfc7950;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertTrue;
14 import static org.junit.Assert.fail;
15
16 import com.google.common.collect.ImmutableList;
17 import com.google.common.collect.Iterables;
18 import java.util.Date;
19 import java.util.List;
20 import org.junit.Test;
21 import org.opendaylight.yangtools.yang.common.QName;
22 import org.opendaylight.yangtools.yang.common.QNameModule;
23 import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
24 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
25 import org.opendaylight.yangtools.yang.model.api.Module;
26 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
27 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
28 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition;
29 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition.Bit;
30 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition;
31 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition.EnumPair;
32 import org.opendaylight.yangtools.yang.model.util.type.BitBuilder;
33 import org.opendaylight.yangtools.yang.model.util.type.EnumPairBuilder;
34 import org.opendaylight.yangtools.yang.model.util.type.InvalidBitDefinitionException;
35 import org.opendaylight.yangtools.yang.model.util.type.InvalidEnumDefinitionException;
36 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
37 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
38 import org.opendaylight.yangtools.yang.stmt.StmtTestUtils;
39
40 public class Bug6887Test {
41
42     @Test
43     public void testRestrictedEnumeration() throws Exception {
44         final SchemaContext schemaContext = StmtTestUtils.parseYangSource("/rfc7950/bug6887/foo.yang");
45         assertNotNull(schemaContext);
46
47         final Date revision = SimpleDateFormatUtil.getRevisionFormat().parse("2017-01-26");
48
49         final Module foo = schemaContext.findModuleByName("foo", revision);
50         assertNotNull(foo);
51
52         final LeafSchemaNode myEnumerationLeaf = (LeafSchemaNode) foo.getDataChildByName(
53                 QName.create(foo.getQNameModule(), "my-enumeration-leaf"));
54         assertNotNull(myEnumerationLeaf);
55
56         EnumTypeDefinition enumerationType = (EnumTypeDefinition) myEnumerationLeaf.getType();
57
58         List<EnumPair> enums = enumerationType.getValues();
59         assertEquals(2, enums.size());
60         final EnumPair yellowEnum = createEnumPair("yellow", 2);
61         final EnumPair redEnum = createEnumPair("red", 3);
62         assertContainsEnums(enums, yellowEnum, redEnum);
63
64         enumerationType = enumerationType.getBaseType();
65         enums = enumerationType.getValues();
66         assertEquals(3, enums.size());
67         final EnumPair blackEnum = createEnumPair("black", 4);
68         assertContainsEnums(enums, yellowEnum, redEnum, blackEnum);
69
70         enumerationType = enumerationType.getBaseType();
71         enums = enumerationType.getValues();
72         assertEquals(4, enums.size());
73         final EnumPair whiteEnum = createEnumPair("white", 1);
74         assertContainsEnums(enums, whiteEnum, yellowEnum, redEnum, blackEnum);
75
76         final LeafSchemaNode myEnumerationLeaf2 = (LeafSchemaNode) foo.getDataChildByName(
77                 QName.create(foo.getQNameModule(), "my-enumeration-leaf-2"));
78         assertNotNull(myEnumerationLeaf2);
79
80         enumerationType = (EnumTypeDefinition) myEnumerationLeaf2.getType();
81         enums = enumerationType.getValues();
82         assertEquals(3, enums.size());
83         assertContainsEnums(enums, yellowEnum, redEnum, blackEnum);
84     }
85
86     @Test
87     public void testInvalidRestrictedEnumeration() throws Exception {
88         try {
89             StmtTestUtils.parseYangSource("/rfc7950/bug6887/foo-invalid.yang");
90             fail("An exception should have been thrown.");
91         } catch (final ReactorException ex) {
92             final Throwable cause = ex.getCause();
93             assertTrue(cause instanceof SourceException);
94             assertTrue(cause.getMessage().startsWith("Enum 'purple' is not a subset of its base enumeration type "
95                     + "(foo?revision=2017-02-02)my-derived-enumeration-type."));
96         }
97     }
98
99     @Test
100     public void testInvalidRestrictedEnumeration2() throws Exception {
101         try {
102             StmtTestUtils.parseYangSource("/rfc7950/bug6887/foo-invalid-2.yang");
103             fail("An exception should have been thrown.");
104         } catch (final ReactorException ex) {
105             final Throwable cause = ex.getCause();
106             assertTrue(cause instanceof InvalidEnumDefinitionException);
107             assertTrue(cause.getMessage().startsWith("Enum 'magenta' is not a subset of its base enumeration type "
108                     + "(foo?revision=2017-02-02)my-base-enumeration-type."));
109         }
110     }
111
112     @Test
113     public void testInvalidRestrictedEnumeration3() throws Exception {
114         try {
115             StmtTestUtils.parseYangSource("/rfc7950/bug6887/foo-invalid-3.yang");
116             fail("An exception should have been thrown.");
117         } catch (final ReactorException ex) {
118             final Throwable cause = ex.getCause();
119             assertTrue(cause instanceof InvalidEnumDefinitionException);
120             assertTrue(cause.getMessage().startsWith("Value of enum 'red' must be the same as the value of "
121                     + "corresponding enum in the base enumeration type (foo?revision=2017-02-02)"
122                     + "my-derived-enumeration-type."));
123         }
124     }
125
126     @Test
127     public void testInvalidRestrictedEnumeration4() throws Exception {
128         try {
129             StmtTestUtils.parseYangSource("/rfc7950/bug6887/foo-invalid-4.yang");
130             fail("An exception should have been thrown.");
131         } catch (final ReactorException ex) {
132             final Throwable cause = ex.getCause();
133             assertTrue(cause instanceof InvalidEnumDefinitionException);
134             assertTrue(cause.getMessage().startsWith("Value of enum 'black' must be the same as the value of "
135                     + "corresponding enum in the base enumeration type (foo?revision=2017-02-02)"
136                     + "my-base-enumeration-type."));
137         }
138     }
139
140     @Test
141     public void testValidYang10EnumerationWithUnknownStatements() throws Exception {
142         final SchemaContext schemaContext = StmtTestUtils.parseYangSource("/rfc7950/bug6887/foo10-valid.yang");
143         assertNotNull(schemaContext);
144     }
145
146     @Test
147     public void testInvalidYang10RestrictedEnumeration() throws Exception {
148         try {
149             StmtTestUtils.parseYangSource("/rfc7950/bug6887/foo10-invalid.yang");
150             fail("An exception should have been thrown.");
151         } catch (final ReactorException ex) {
152             final Throwable cause = ex.getCause();
153             assertTrue(cause instanceof SourceException);
154             assertTrue(cause.getMessage().startsWith(
155                 "Restricted enumeration type is allowed only in YANG 1.1 version."));
156         }
157     }
158
159     @Test
160     public void testInvalidYang10RestrictedEnumeration2() throws Exception {
161         try {
162             StmtTestUtils.parseYangSource("/rfc7950/bug6887/foo10-invalid-2.yang");
163             fail("An exception should have been thrown.");
164         } catch (final ReactorException ex) {
165             final Throwable cause = ex.getCause();
166             assertTrue(cause instanceof SourceException);
167             assertTrue(cause.getMessage().startsWith(
168                 "Restricted enumeration type is allowed only in YANG 1.1 version."));
169         }
170     }
171
172     @Test
173     public void testRestrictedBits() throws Exception {
174         final SchemaContext schemaContext = StmtTestUtils.parseYangSource("/rfc7950/bug6887/bar.yang");
175         assertNotNull(schemaContext);
176
177         final Date revision = SimpleDateFormatUtil.getRevisionFormat().parse("2017-02-02");
178
179         final Module bar = schemaContext.findModuleByName("bar", revision);
180         assertNotNull(bar);
181
182         final LeafSchemaNode myBitsLeaf = (LeafSchemaNode) bar.getDataChildByName(
183                 QName.create(bar.getQNameModule(), "my-bits-leaf"));
184         assertNotNull(myBitsLeaf);
185
186         BitsTypeDefinition bitsType = (BitsTypeDefinition) myBitsLeaf.getType();
187
188         List<Bit> bits = bitsType.getBits();
189         assertEquals(2, bits.size());
190         Bit bitB = createBit(createSchemaPath(true, bar.getQNameModule(), ImmutableList.of("my-bits-leaf",
191                 "my-derived-bits-type", "bit-b")), 2);
192         Bit bitC = createBit(createSchemaPath(true, bar.getQNameModule(), ImmutableList.of("my-bits-leaf",
193                 "my-derived-bits-type", "bit-c")), 3);
194         assertContainsBits(bits, bitB, bitC);
195
196         bitsType = bitsType.getBaseType();
197         bits = bitsType.getBits();
198         assertEquals(3, bits.size());
199         bitB = createBit(createSchemaPath(true, bar.getQNameModule(), ImmutableList.of("my-derived-bits-type",
200                 "my-base-bits-type", "bit-b")), 2);
201         bitC = createBit(createSchemaPath(true, bar.getQNameModule(), ImmutableList.of("my-derived-bits-type",
202                 "my-base-bits-type", "bit-c")), 3);
203         Bit bitD = createBit(createSchemaPath(true, bar.getQNameModule(), ImmutableList.of("my-derived-bits-type",
204                 "my-base-bits-type", "bit-d")), 4);
205         assertContainsBits(bits, bitB, bitC, bitD);
206
207         bitsType = bitsType.getBaseType();
208         bits = bitsType.getBits();
209         assertEquals(4, bits.size());
210         final Bit bitA = createBit(createSchemaPath(true, bar.getQNameModule(), ImmutableList.of("my-base-bits-type",
211                 "bits", "bit-a")), 1);
212         bitB = createBit(createSchemaPath(true, bar.getQNameModule(), ImmutableList.of("my-base-bits-type",
213                 "bits", "bit-b")), 2);
214         bitC = createBit(createSchemaPath(true, bar.getQNameModule(), ImmutableList.of("my-base-bits-type",
215                 "bits", "bit-c")), 3);
216         bitD = createBit(createSchemaPath(true, bar.getQNameModule(), ImmutableList.of("my-base-bits-type",
217                 "bits", "bit-d")), 4);
218         assertContainsBits(bits, bitA, bitB, bitC, bitD);
219
220         final LeafSchemaNode myBitsLeaf2 = (LeafSchemaNode) bar.getDataChildByName(
221                 QName.create(bar.getQNameModule(), "my-bits-leaf-2"));
222         assertNotNull(myBitsLeaf2);
223
224         bitsType = (BitsTypeDefinition) myBitsLeaf2.getType();
225         bits = bitsType.getBits();
226         assertEquals(3, bits.size());
227         bitB = createBit(createSchemaPath(true, bar.getQNameModule(), ImmutableList.of("my-derived-bits-type",
228                 "my-base-bits-type", "bit-b")), 2);
229         bitC = createBit(createSchemaPath(true, bar.getQNameModule(), ImmutableList.of("my-derived-bits-type",
230                 "my-base-bits-type", "bit-c")), 3);
231         bitD = createBit(createSchemaPath(true, bar.getQNameModule(), ImmutableList.of("my-derived-bits-type",
232                 "my-base-bits-type", "bit-d")), 4);
233         assertContainsBits(bits, bitB, bitC, bitD);
234     }
235
236     @Test
237     public void testInvalidRestrictedBits() throws Exception {
238         try {
239             StmtTestUtils.parseYangSource("/rfc7950/bug6887/bar-invalid.yang");
240             fail("An exception should have been thrown.");
241         } catch (final ReactorException ex) {
242             final Throwable cause = ex.getCause();
243             assertTrue(cause instanceof SourceException);
244             assertTrue(cause.getMessage().startsWith("Bit 'bit-w' is not a subset of its base bits type "
245                     + "(bar?revision=2017-02-02)my-derived-bits-type."));
246         }
247     }
248
249     @Test
250     public void testInvalidRestrictedBits2() throws Exception {
251         try {
252             StmtTestUtils.parseYangSource("/rfc7950/bug6887/bar-invalid-2.yang");
253             fail("An exception should have been thrown.");
254         } catch (final ReactorException ex) {
255             final Throwable cause = ex.getCause();
256             assertTrue(cause instanceof InvalidBitDefinitionException);
257             assertTrue(cause.getMessage().startsWith("Bit 'bit-x' is not a subset of its base bits type "
258                     + "(bar?revision=2017-02-02)my-base-bits-type."));
259         }
260     }
261
262     @Test
263     public void testInvalidRestrictedBits3() throws Exception {
264         try {
265             StmtTestUtils.parseYangSource("/rfc7950/bug6887/bar-invalid-3.yang");
266             fail("An exception should have been thrown.");
267         } catch (final ReactorException ex) {
268             final Throwable cause = ex.getCause();
269             assertTrue(cause instanceof InvalidBitDefinitionException);
270             assertTrue(cause.getMessage().startsWith("Position of bit 'bit-c' must be the same as the position of "
271                     + "corresponding bit in the base bits type (bar?revision=2017-02-02)my-derived-bits-type."));
272         }
273     }
274
275     @Test
276     public void testInvalidRestrictedBits4() throws Exception {
277         try {
278             StmtTestUtils.parseYangSource("/rfc7950/bug6887/bar-invalid-4.yang");
279             fail("An exception should have been thrown.");
280         } catch (final ReactorException ex) {
281             final Throwable cause = ex.getCause();
282             assertTrue(cause instanceof InvalidBitDefinitionException);
283             assertTrue(cause.getMessage().startsWith("Position of bit 'bit-d' must be the same as the position of "
284                     + "corresponding bit in the base bits type (bar?revision=2017-02-02)my-base-bits-type."));
285         }
286     }
287
288     @Test
289     public void testValidYang10BitsWithUnknownStatements() throws Exception {
290         final SchemaContext schemaContext = StmtTestUtils.parseYangSource("/rfc7950/bug6887/bar10-valid.yang");
291         assertNotNull(schemaContext);
292     }
293
294     @Test
295     public void testInvalidYang10RestrictedBits() throws Exception {
296         try {
297             StmtTestUtils.parseYangSource("/rfc7950/bug6887/bar10-invalid.yang");
298             fail("An exception should have been thrown.");
299         } catch (final ReactorException ex) {
300             final Throwable cause = ex.getCause();
301             assertTrue(cause instanceof SourceException);
302             assertTrue(cause.getMessage().startsWith("Restricted bits type is allowed only in YANG 1.1 version."));
303         }
304     }
305
306     @Test
307     public void testInvalidYang10RestrictedBits2() throws Exception {
308         try {
309             StmtTestUtils.parseYangSource("/rfc7950/bug6887/bar10-invalid-2.yang");
310             fail("An exception should have been thrown.");
311         } catch (final ReactorException ex) {
312             final Throwable cause = ex.getCause();
313             assertTrue(cause instanceof SourceException);
314             assertTrue(cause.getMessage().startsWith("Restricted bits type is allowed only in YANG 1.1 version."));
315         }
316     }
317
318     private static EnumPair createEnumPair(final String name, final int value) {
319         return EnumPairBuilder.create(name, value).build();
320     }
321
322     private static void assertContainsEnums(final List<EnumPair> enumList, final EnumPair... enumPairs) {
323         for (final EnumPair enumPair : enumPairs) {
324             assertTrue(enumList.contains(enumPair));
325         }
326     }
327
328     private static Bit createBit(final SchemaPath path, final long position) {
329         return BitBuilder.create(path, position).build();
330     }
331
332     private static void assertContainsBits(final List<Bit> bitList, final Bit... bits) {
333         for (final Bit bit : bits) {
334             assertTrue(bitList.contains(bit));
335         }
336     }
337
338     private static SchemaPath createSchemaPath(final boolean absolute, final QNameModule qnameModule,
339             final Iterable<String> localNames) {
340         return SchemaPath.create(Iterables.transform(localNames,
341             localName -> QName.create(qnameModule, localName)), true);
342     }
343 }