ee20186e6fcabad39e4e07f55f10fdeabf508676
[yangtools.git] / yang / yang-parser-rfc7950 / src / test / java / org / opendaylight / yangtools / yang / stmt / Bug5946Test.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.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertTrue;
13 import static org.junit.Assert.fail;
14
15 import java.io.File;
16 import java.util.Collection;
17 import org.eclipse.jdt.annotation.NonNull;
18 import org.junit.Test;
19 import org.opendaylight.yangtools.yang.common.QName;
20 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
21 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
22 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
23 import org.opendaylight.yangtools.yang.model.api.UniqueConstraint;
24 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
25 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Descendant;
26 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
27
28 public class Bug5946Test {
29     private static final String NS = "foo";
30     private static final String REV = "2016-05-26";
31     private static final QName L1 = QName.create(NS, REV, "l1");
32     private static final QName L2 = QName.create(NS, REV, "l2");
33     private static final QName L3 = QName.create(NS, REV, "l3");
34     private static final QName C = QName.create(NS, REV, "c");
35     private static final QName WITHOUT_UNIQUE = QName.create(NS, REV, "without-unique");
36     private static final QName SIMPLE_UNIQUE = QName.create(NS, REV, "simple-unique");
37     private static final QName MULTIPLE_UNIQUE = QName.create(NS, REV, "multiple-unique");
38     private static final SchemaNodeIdentifier L1_ID = SchemaNodeIdentifier.Descendant.of(L1);
39     private static final SchemaNodeIdentifier L2_ID = SchemaNodeIdentifier.Descendant.of(L2);
40     private static final SchemaNodeIdentifier C_L3_ID = SchemaNodeIdentifier.Descendant.of(C, L3);
41
42     @Test
43     public void test() throws Exception {
44         SchemaContext context = StmtTestUtils.parseYangSources(new File(getClass()
45                 .getResource("/bugs/bug5946/foo.yang").toURI()));
46         assertNotNull(context);
47
48         Collection<? extends UniqueConstraint> uniqueConstraints = getListConstraints(context, WITHOUT_UNIQUE);
49         assertNotNull(uniqueConstraints);
50         assertTrue(uniqueConstraints.isEmpty());
51
52         Collection<? extends UniqueConstraint> simpleUniqueConstraints = getListConstraints(context, SIMPLE_UNIQUE);
53         assertNotNull(simpleUniqueConstraints);
54         assertEquals(1, simpleUniqueConstraints.size());
55         Collection<Descendant> simpleUniqueConstraintTag = simpleUniqueConstraints.iterator().next().getTag();
56         assertTrue(simpleUniqueConstraintTag.contains(L1_ID));
57         assertTrue(simpleUniqueConstraintTag.contains(C_L3_ID));
58
59         Collection<? extends UniqueConstraint> multipleUniqueConstraints = getListConstraints(context, MULTIPLE_UNIQUE);
60         assertNotNull(multipleUniqueConstraints);
61         assertEquals(3, multipleUniqueConstraints.size());
62         boolean l1l2 = false;
63         boolean l1cl3 = false;
64         boolean cl3l2 = false;
65         for (UniqueConstraint uniqueConstraint : multipleUniqueConstraints) {
66             Collection<Descendant> uniqueConstraintTag = uniqueConstraint.getTag();
67             if (uniqueConstraintTag.contains(L1_ID) && uniqueConstraintTag.contains(L2_ID)) {
68                 l1l2 = true;
69             } else if (uniqueConstraintTag.contains(L1_ID) && uniqueConstraintTag.contains(C_L3_ID)) {
70                 l1cl3 = true;
71             } else if (uniqueConstraintTag.contains(C_L3_ID) && uniqueConstraintTag.contains(L2_ID)) {
72                 cl3l2 = true;
73             }
74         }
75         assertTrue(l1l2 && l1cl3 && cl3l2);
76     }
77
78     @Test
79     public void testInvalid() throws Exception {
80         try {
81             StmtTestUtils.parseYangSources(new File(getClass().getResource("/bugs/bug5946/foo-invalid.yang").toURI()));
82             fail("Should fail due to invalid argument of unique constraint");
83         } catch (ReactorException e) {
84             assertTrue(e.getCause().getMessage().startsWith(
85                     "Unique statement argument '/simple-unique/l1' contains schema node identifier '/simple-unique/l1'"
86                             + " which is not in the descendant node identifier form."));
87         }
88     }
89
90     private static @NonNull Collection<? extends UniqueConstraint> getListConstraints(final SchemaContext context,
91             final QName listQName) {
92         DataSchemaNode dataChildByName = context.getDataChildByName(listQName);
93         assertTrue(dataChildByName instanceof ListSchemaNode);
94         return ((ListSchemaNode) dataChildByName).getUniqueConstraints();
95     }
96 }