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