Bug 6880: [Yang 1.1] Allow leaf-lists to have default values
[yangtools.git] / yang / yang-parser-impl / src / test / java / org / opendaylight / yangtools / yang / parser / stmt / rfc7950 / Bug6880Test.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
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 java.io.FileNotFoundException;
17 import java.net.URISyntaxException;
18 import java.util.Collection;
19 import org.junit.Test;
20 import org.opendaylight.yangtools.yang.common.QName;
21 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
22 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
23 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
24 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
25 import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil;
26 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
27 import org.opendaylight.yangtools.yang.parser.spi.meta.SomeModifiersUnresolvedException;
28 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
29 import org.opendaylight.yangtools.yang.stmt.StmtTestUtils;
30
31 public class Bug6880Test {
32     private static final String FOO_NS = "foo";
33     private static final String FOO_REV = "1970-01-01";
34
35     @Test
36     public void valid10Test() throws ReactorException, SourceException, FileNotFoundException, URISyntaxException {
37         final SchemaContext schemaContext = StmtTestUtils.parseYangSource("/rfc7950/bug6880/foo.yang");
38         assertNotNull(schemaContext);
39
40         final SchemaNode findDataSchemaNode = SchemaContextUtil.findDataSchemaNode(schemaContext,
41                 SchemaPath.create(true, QName.create(FOO_NS, FOO_REV, "my-leaf-list")));
42         assertTrue(findDataSchemaNode instanceof LeafListSchemaNode);
43         final LeafListSchemaNode myLeafList = (LeafListSchemaNode) findDataSchemaNode;
44
45         final Collection<String> defaults = myLeafList.getDefaults();
46         assertEquals(2, defaults.size());
47         assertTrue(defaults.contains("my-default-value-1") && defaults.contains("my-default-value-2"));
48     }
49
50     @Test
51     public void invalid10Test() throws ReactorException, SourceException, FileNotFoundException, URISyntaxException {
52         try {
53             StmtTestUtils.parseYangSource("/rfc7950/bug6880/invalid10.yang");
54             fail("Test should fail due to invalid Yang 1.0");
55         } catch (final SomeModifiersUnresolvedException e) {
56             assertTrue(e.getCause().getMessage().startsWith("DEFAULT is not valid for LEAF_LIST"));
57         }
58     }
59 }