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
9 package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertFalse;
13 import static org.junit.Assert.assertNotEquals;
14 import static org.junit.Assert.assertNotNull;
15 import static org.junit.Assert.assertTrue;
17 import java.io.IOException;
18 import java.net.URISyntaxException;
19 import java.text.ParseException;
20 import org.junit.Test;
21 import org.opendaylight.yangtools.yang.common.QName;
22 import org.opendaylight.yangtools.yang.common.Revision;
23 import org.opendaylight.yangtools.yang.data.util.ConstraintDefinitions;
24 import org.opendaylight.yangtools.yang.model.api.ConstraintDefinition;
25 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
26 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
27 import org.opendaylight.yangtools.yang.model.api.Module;
28 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
29 import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException;
30 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
31 import org.opendaylight.yangtools.yang.parser.rfc6020.repo.YangStatementStreamSource;
32 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
33 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
34 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
36 public class ConstraintDefinitionsTest {
39 public void testConstraintDefinitions() throws ParseException, ReactorException, URISyntaxException, IOException,
40 YangSyntaxErrorException {
41 final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
43 reactor.addSource(YangStatementStreamSource.create(
44 YangTextSchemaSource.forResource("/constraint-definitions-test/foo.yang")));
45 final SchemaContext schemaContext = reactor.buildEffective();
46 assertNotNull(schemaContext);
48 final Module testModule = schemaContext.findModule("foo", Revision.valueOf("2016-09-20")).get();
49 final LeafSchemaNode mandatoryLeaf1 = (LeafSchemaNode) testModule.getDataChildByName(
50 QName.create(testModule.getQNameModule(), "mandatory-leaf-1"));
51 assertNotNull(mandatoryLeaf1);
52 ConstraintDefinition constraints1 = mandatoryLeaf1.getConstraints();
54 final LeafSchemaNode mandatoryLeaf2 = (LeafSchemaNode) testModule.getDataChildByName(
55 QName.create(testModule.getQNameModule(), "mandatory-leaf-2"));
56 assertNotNull(mandatoryLeaf2);
57 ConstraintDefinition constraints2 = mandatoryLeaf2.getConstraints();
59 assertEquals(ConstraintDefinitions.hashCode(constraints1), ConstraintDefinitions.hashCode(constraints2));
60 assertTrue(ConstraintDefinitions.equals(constraints1, constraints2));
62 assertTrue(ConstraintDefinitions.equals(constraints1, constraints1));
63 assertFalse(ConstraintDefinitions.equals(constraints1, "str"));
65 final LeafSchemaNode mandatoryLeaf3 = (LeafSchemaNode) testModule.getDataChildByName(
66 QName.create(testModule.getQNameModule(), "mandatory-leaf-3"));
67 assertNotNull(mandatoryLeaf3);
68 ConstraintDefinition constraints3 = mandatoryLeaf3.getConstraints();
70 assertNotEquals(ConstraintDefinitions.hashCode(constraints2), ConstraintDefinitions.hashCode(constraints3));
71 assertFalse(ConstraintDefinitions.equals(constraints2, constraints3));
73 final LeafSchemaNode mandatoryLeaf4 = (LeafSchemaNode) testModule.getDataChildByName(
74 QName.create(testModule.getQNameModule(), "mandatory-leaf-4"));
75 assertNotNull(mandatoryLeaf4);
76 ConstraintDefinition constraints4 = mandatoryLeaf4.getConstraints();
78 assertNotEquals(ConstraintDefinitions.hashCode(constraints3), ConstraintDefinitions.hashCode(constraints4));
79 assertFalse(ConstraintDefinitions.equals(constraints3, constraints4));
81 final LeafSchemaNode mandatoryLeaf5 = (LeafSchemaNode) testModule.getDataChildByName(
82 QName.create(testModule.getQNameModule(), "mandatory-leaf-5"));
83 assertNotNull(mandatoryLeaf5);
84 final ConstraintDefinition constraints5 = mandatoryLeaf5.getConstraints();
86 assertNotEquals(ConstraintDefinitions.hashCode(constraints4), ConstraintDefinitions.hashCode(constraints5));
87 assertFalse(ConstraintDefinitions.equals(constraints4, constraints5));
89 final LeafListSchemaNode constrainedLeafList1 = (LeafListSchemaNode) testModule.getDataChildByName(
90 QName.create(testModule.getQNameModule(), "constrained-leaf-list-1"));
91 assertNotNull(constrainedLeafList1);
92 constraints1 = constrainedLeafList1.getConstraints();
94 final LeafListSchemaNode constrainedLeafList2 = (LeafListSchemaNode) testModule.getDataChildByName(
95 QName.create(testModule.getQNameModule(), "constrained-leaf-list-2"));
96 assertNotNull(constrainedLeafList2);
97 constraints2 = constrainedLeafList2.getConstraints();
99 assertEquals(ConstraintDefinitions.hashCode(constraints1), ConstraintDefinitions.hashCode(constraints2));
100 assertTrue(ConstraintDefinitions.equals(constraints1, constraints2));
102 final LeafListSchemaNode constrainedLeafList3 = (LeafListSchemaNode) testModule.getDataChildByName(
103 QName.create(testModule.getQNameModule(), "constrained-leaf-list-3"));
104 assertNotNull(constrainedLeafList3);
105 constraints3 = constrainedLeafList3.getConstraints();
107 assertNotEquals(ConstraintDefinitions.hashCode(constraints2), ConstraintDefinitions.hashCode(constraints3));
108 assertFalse(ConstraintDefinitions.equals(constraints2, constraints3));
110 final LeafListSchemaNode constrainedLeafList4 = (LeafListSchemaNode) testModule.getDataChildByName(
111 QName.create(testModule.getQNameModule(), "constrained-leaf-list-4"));
112 assertNotNull(constrainedLeafList4);
113 constraints4 = constrainedLeafList4.getConstraints();
115 assertNotEquals(ConstraintDefinitions.hashCode(constraints3), ConstraintDefinitions.hashCode(constraints4));
116 assertFalse(ConstraintDefinitions.equals(constraints3, constraints4));
118 final String constraintsString = ConstraintDefinitions.toString(constraints4);
119 assertEquals("EffectiveConstraintDefinitionImpl{whenCondition=foo = 'bar', mustConstraints=[bar != 'foo'], "
120 + "mandatory=true, minElements=50, maxElements=100}", constraintsString);