205580e02d48a72f006e5ddf943c17c76f282d1f
[yangtools.git] / parser / yang-parser-rfc7950 / src / test / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / effective / ElementCountConstraintsTest.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.parser.stmt.rfc6020.effective;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotEquals;
12 import static org.junit.Assert.assertNotNull;
13
14 import java.io.IOException;
15 import java.net.URISyntaxException;
16 import org.junit.Test;
17 import org.opendaylight.yangtools.yang.common.QName;
18 import org.opendaylight.yangtools.yang.common.Revision;
19 import org.opendaylight.yangtools.yang.model.api.ElementCountConstraint;
20 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
21 import org.opendaylight.yangtools.yang.model.api.Module;
22 import org.opendaylight.yangtools.yang.parser.api.YangSyntaxErrorException;
23 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
24 import org.opendaylight.yangtools.yang.stmt.StmtTestUtils;
25
26 public class ElementCountConstraintsTest {
27     @Test
28     public void testElementCountConstraints()
29             throws YangSyntaxErrorException, ReactorException, URISyntaxException, IOException {
30         final var schemaContext = StmtTestUtils.parseYangSource("/constraint-definitions-test/foo.yang");
31         assertNotNull(schemaContext);
32
33         final Module testModule = schemaContext.findModule("foo", Revision.of("2016-09-20")).get();
34         final LeafListSchemaNode constrainedLeafList1 = (LeafListSchemaNode) testModule.getDataChildByName(
35                 QName.create(testModule.getQNameModule(), "constrained-leaf-list-1"));
36         ElementCountConstraint constraints1 = constrainedLeafList1.getElementCountConstraint().get();
37
38         final LeafListSchemaNode constrainedLeafList2 = (LeafListSchemaNode) testModule.getDataChildByName(
39                 QName.create(testModule.getQNameModule(), "constrained-leaf-list-2"));
40         ElementCountConstraint constraints2 = constrainedLeafList2.getElementCountConstraint().get();
41
42         assertEquals(constraints1.hashCode(), constraints2.hashCode());
43         assertEquals(constraints1, constraints2);
44
45         final LeafListSchemaNode constrainedLeafList3 = (LeafListSchemaNode) testModule.getDataChildByName(
46                 QName.create(testModule.getQNameModule(), "constrained-leaf-list-3"));
47         ElementCountConstraint constraints3 = constrainedLeafList3.getElementCountConstraint().get();
48
49         assertNotEquals(constraints2.hashCode(), constraints3.hashCode());
50         assertNotEquals(constraints2, constraints3);
51
52         final LeafListSchemaNode constrainedLeafList4 = (LeafListSchemaNode) testModule.getDataChildByName(
53                 QName.create(testModule.getQNameModule(), "constrained-leaf-list-4"));
54         ElementCountConstraint constraints4 = constrainedLeafList4.getElementCountConstraint().get();
55
56         assertNotEquals(constraints3.hashCode(), constraints4.hashCode());
57         assertNotEquals(constraints3, constraints4);
58
59         assertEquals("ElementCountConstraint{minElements=50, maxElements=100}", constraints4.toString());
60     }
61 }