Revert "Move SchemaNodeIdentifier to yang-common"
[yangtools.git] / parser / 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.stmt.SchemaNodeIdentifier;
24 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Descendant;
25 import org.opendaylight.yangtools.yang.model.api.stmt.UniqueEffectiveStatement;
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 UniqueEffectiveStatement> uniqueConstraints = getListConstraints(context, WITHOUT_UNIQUE);
49         assertNotNull(uniqueConstraints);
50         assertTrue(uniqueConstraints.isEmpty());
51
52         Collection<? extends UniqueEffectiveStatement> simpleUniqueConstraints =
53             getListConstraints(context, SIMPLE_UNIQUE);
54         assertNotNull(simpleUniqueConstraints);
55         assertEquals(1, simpleUniqueConstraints.size());
56         Collection<Descendant> simpleUniqueConstraintTag = simpleUniqueConstraints.iterator().next().argument();
57         assertTrue(simpleUniqueConstraintTag.contains(L1_ID));
58         assertTrue(simpleUniqueConstraintTag.contains(C_L3_ID));
59
60         Collection<? extends UniqueEffectiveStatement> multipleUniqueConstraints =
61             getListConstraints(context, MULTIPLE_UNIQUE);
62         assertNotNull(multipleUniqueConstraints);
63         assertEquals(3, multipleUniqueConstraints.size());
64         boolean l1l2 = false;
65         boolean l1cl3 = false;
66         boolean cl3l2 = false;
67         for (UniqueEffectiveStatement uniqueConstraint : multipleUniqueConstraints) {
68             Collection<Descendant> uniqueConstraintTag = uniqueConstraint.argument();
69             if (uniqueConstraintTag.contains(L1_ID) && uniqueConstraintTag.contains(L2_ID)) {
70                 l1l2 = true;
71             } else if (uniqueConstraintTag.contains(L1_ID) && uniqueConstraintTag.contains(C_L3_ID)) {
72                 l1cl3 = true;
73             } else if (uniqueConstraintTag.contains(C_L3_ID) && uniqueConstraintTag.contains(L2_ID)) {
74                 cl3l2 = true;
75             }
76         }
77         assertTrue(l1l2 && l1cl3 && cl3l2);
78     }
79
80     @Test
81     public void testInvalid() throws Exception {
82         try {
83             StmtTestUtils.parseYangSources(new File(getClass().getResource("/bugs/bug5946/foo-invalid.yang").toURI()));
84             fail("Should fail due to invalid argument of unique constraint");
85         } catch (ReactorException e) {
86             assertTrue(e.getCause().getMessage().startsWith(
87                     "Unique statement argument '/simple-unique/l1' contains schema node identifier '/simple-unique/l1'"
88                             + " which is not in the descendant node identifier form."));
89         }
90     }
91
92     private static @NonNull Collection<? extends UniqueEffectiveStatement> getListConstraints(
93             final SchemaContext context, final QName listQName) {
94         DataSchemaNode dataChildByName = context.getDataChildByName(listQName);
95         assertTrue(dataChildByName instanceof ListSchemaNode);
96         return ((ListSchemaNode) dataChildByName).getUniqueConstraints();
97     }
98 }