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.hamcrest.CoreMatchers.startsWith;
11 import static org.junit.jupiter.api.Assertions.assertEquals;
12 import static org.junit.jupiter.api.Assertions.assertInstanceOf;
13 import static org.junit.jupiter.api.Assertions.assertNotNull;
14 import static org.junit.jupiter.api.Assertions.assertTrue;
16 import java.util.Collection;
17 import org.eclipse.jdt.annotation.NonNull;
18 import org.junit.jupiter.api.Test;
19 import org.opendaylight.yangtools.yang.common.QName;
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.stmt.SchemaNodeIdentifier;
23 import org.opendaylight.yangtools.yang.model.api.stmt.UniqueEffectiveStatement;
25 class Bug5946Test extends AbstractYangTest {
26 private static final String NS = "foo";
27 private static final String REV = "2016-05-26";
28 private static final QName L1 = QName.create(NS, REV, "l1");
29 private static final QName L2 = QName.create(NS, REV, "l2");
30 private static final QName L3 = QName.create(NS, REV, "l3");
31 private static final QName C = QName.create(NS, REV, "c");
32 private static final QName WITHOUT_UNIQUE = QName.create(NS, REV, "without-unique");
33 private static final QName SIMPLE_UNIQUE = QName.create(NS, REV, "simple-unique");
34 private static final QName MULTIPLE_UNIQUE = QName.create(NS, REV, "multiple-unique");
35 private static final SchemaNodeIdentifier L1_ID = SchemaNodeIdentifier.Descendant.of(L1);
36 private static final SchemaNodeIdentifier L2_ID = SchemaNodeIdentifier.Descendant.of(L2);
37 private static final SchemaNodeIdentifier C_L3_ID = SchemaNodeIdentifier.Descendant.of(C, L3);
40 void test() throws Exception {
41 final var context = assertEffectiveModel("/bugs/bug5946/foo.yang");
43 var uniqueConstraints = getListConstraints(context, WITHOUT_UNIQUE);
44 assertNotNull(uniqueConstraints);
45 assertTrue(uniqueConstraints.isEmpty());
47 var simpleUniqueConstraints = getListConstraints(context, SIMPLE_UNIQUE);
48 assertNotNull(simpleUniqueConstraints);
49 assertEquals(1, simpleUniqueConstraints.size());
50 var simpleUniqueConstraintTag = simpleUniqueConstraints.iterator().next().argument();
51 assertTrue(simpleUniqueConstraintTag.contains(L1_ID));
52 assertTrue(simpleUniqueConstraintTag.contains(C_L3_ID));
54 var multipleUniqueConstraints = getListConstraints(context, MULTIPLE_UNIQUE);
55 assertNotNull(multipleUniqueConstraints);
56 assertEquals(3, multipleUniqueConstraints.size());
58 boolean l1cl3 = false;
59 boolean cl3l2 = false;
60 for (var uniqueConstraint : multipleUniqueConstraints) {
61 var uniqueConstraintTag = uniqueConstraint.argument();
62 if (uniqueConstraintTag.contains(L1_ID) && uniqueConstraintTag.contains(L2_ID)) {
64 } else if (uniqueConstraintTag.contains(L1_ID) && uniqueConstraintTag.contains(C_L3_ID)) {
66 } else if (uniqueConstraintTag.contains(C_L3_ID) && uniqueConstraintTag.contains(L2_ID)) {
70 assertTrue(l1l2 && l1cl3 && cl3l2);
74 void testInvalid() throws Exception {
75 assertSourceException(
76 startsWith("Unique statement argument '/simple-unique/l1' contains schema node identifier "
77 + "'/simple-unique/l1' which is not in the descendant node identifier form."),
78 "/bugs/bug5946/foo-invalid.yang");
81 private static @NonNull Collection<? extends @NonNull UniqueEffectiveStatement> getListConstraints(
82 final SchemaContext context, final QName listQName) {
83 return assertInstanceOf(ListSchemaNode.class, context.getDataChildByName(listQName)).getUniqueConstraints();