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.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;
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);
43 public void test() throws Exception {
44 SchemaContext context = StmtTestUtils.parseYangSources(new File(getClass()
45 .getResource("/bugs/bug5946/foo.yang").toURI()));
46 assertNotNull(context);
48 Collection<? extends UniqueEffectiveStatement> uniqueConstraints = getListConstraints(context, WITHOUT_UNIQUE);
49 assertNotNull(uniqueConstraints);
50 assertTrue(uniqueConstraints.isEmpty());
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));
60 Collection<? extends UniqueEffectiveStatement> multipleUniqueConstraints =
61 getListConstraints(context, MULTIPLE_UNIQUE);
62 assertNotNull(multipleUniqueConstraints);
63 assertEquals(3, multipleUniqueConstraints.size());
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)) {
71 } else if (uniqueConstraintTag.contains(L1_ID) && uniqueConstraintTag.contains(C_L3_ID)) {
73 } else if (uniqueConstraintTag.contains(C_L3_ID) && uniqueConstraintTag.contains(L2_ID)) {
77 assertTrue(l1l2 && l1cl3 && cl3l2);
81 public void testInvalid() throws Exception {
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."));
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();