Allow fluent use of BuildAction
[yangtools.git] / yang / yang-parser-impl / src / test / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / effective / ConstraintDefinitionsTest.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
9 package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective;
10
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;
16
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.rfc6020.YangInferencePipeline;
34
35 public class ConstraintDefinitionsTest {
36
37     @Test
38     public void testConstraintDefinitions() throws ParseException, ReactorException, URISyntaxException, IOException,
39             YangSyntaxErrorException {
40         final SchemaContext schemaContext = YangInferencePipeline.RFC6020_REACTOR.newBuild()
41                 .addSource(YangStatementStreamSource.create(
42                     YangTextSchemaSource.forResource("/constraint-definitions-test/foo.yang")))
43                 .buildEffective();
44         assertNotNull(schemaContext);
45
46         final Module testModule = schemaContext.findModule("foo", Revision.of("2016-09-20")).get();
47         final LeafSchemaNode mandatoryLeaf1 = (LeafSchemaNode) testModule.getDataChildByName(
48                 QName.create(testModule.getQNameModule(), "mandatory-leaf-1"));
49         assertNotNull(mandatoryLeaf1);
50         ConstraintDefinition constraints1 = mandatoryLeaf1.getConstraints();
51
52         final LeafSchemaNode mandatoryLeaf2 = (LeafSchemaNode) testModule.getDataChildByName(
53                 QName.create(testModule.getQNameModule(), "mandatory-leaf-2"));
54         assertNotNull(mandatoryLeaf2);
55         ConstraintDefinition constraints2 = mandatoryLeaf2.getConstraints();
56
57         assertEquals(ConstraintDefinitions.hashCode(constraints1), ConstraintDefinitions.hashCode(constraints2));
58         assertTrue(ConstraintDefinitions.equals(constraints1, constraints2));
59
60         assertTrue(ConstraintDefinitions.equals(constraints1, constraints1));
61         assertFalse(ConstraintDefinitions.equals(constraints1, "str"));
62
63         final LeafSchemaNode mandatoryLeaf3 = (LeafSchemaNode) testModule.getDataChildByName(
64                 QName.create(testModule.getQNameModule(), "mandatory-leaf-3"));
65         assertNotNull(mandatoryLeaf3);
66         ConstraintDefinition constraints3 = mandatoryLeaf3.getConstraints();
67
68         assertEquals(ConstraintDefinitions.hashCode(constraints2), ConstraintDefinitions.hashCode(constraints3));
69         assertTrue(ConstraintDefinitions.equals(constraints2, constraints3));
70
71         final LeafSchemaNode mandatoryLeaf4 = (LeafSchemaNode) testModule.getDataChildByName(
72                 QName.create(testModule.getQNameModule(), "mandatory-leaf-4"));
73         assertNotNull(mandatoryLeaf4);
74         ConstraintDefinition constraints4 = mandatoryLeaf4.getConstraints();
75
76         assertNotEquals(ConstraintDefinitions.hashCode(constraints3), ConstraintDefinitions.hashCode(constraints4));
77         assertFalse(ConstraintDefinitions.equals(constraints3, constraints4));
78
79         final LeafSchemaNode mandatoryLeaf5 = (LeafSchemaNode) testModule.getDataChildByName(
80                 QName.create(testModule.getQNameModule(), "mandatory-leaf-5"));
81         assertNotNull(mandatoryLeaf5);
82         final ConstraintDefinition constraints5 = mandatoryLeaf5.getConstraints();
83
84         assertNotEquals(ConstraintDefinitions.hashCode(constraints4), ConstraintDefinitions.hashCode(constraints5));
85         assertFalse(ConstraintDefinitions.equals(constraints4, constraints5));
86
87         final LeafListSchemaNode constrainedLeafList1 = (LeafListSchemaNode) testModule.getDataChildByName(
88                 QName.create(testModule.getQNameModule(), "constrained-leaf-list-1"));
89         assertNotNull(constrainedLeafList1);
90         constraints1 = constrainedLeafList1.getConstraints();
91
92         final LeafListSchemaNode constrainedLeafList2 = (LeafListSchemaNode) testModule.getDataChildByName(
93                 QName.create(testModule.getQNameModule(), "constrained-leaf-list-2"));
94         assertNotNull(constrainedLeafList2);
95         constraints2 = constrainedLeafList2.getConstraints();
96
97         assertEquals(ConstraintDefinitions.hashCode(constraints1), ConstraintDefinitions.hashCode(constraints2));
98         assertTrue(ConstraintDefinitions.equals(constraints1, constraints2));
99
100         final LeafListSchemaNode constrainedLeafList3 = (LeafListSchemaNode) testModule.getDataChildByName(
101                 QName.create(testModule.getQNameModule(), "constrained-leaf-list-3"));
102         assertNotNull(constrainedLeafList3);
103         constraints3 = constrainedLeafList3.getConstraints();
104
105         assertNotEquals(ConstraintDefinitions.hashCode(constraints2), ConstraintDefinitions.hashCode(constraints3));
106         assertFalse(ConstraintDefinitions.equals(constraints2, constraints3));
107
108         final LeafListSchemaNode constrainedLeafList4 = (LeafListSchemaNode) testModule.getDataChildByName(
109                 QName.create(testModule.getQNameModule(), "constrained-leaf-list-4"));
110         assertNotNull(constrainedLeafList4);
111         constraints4 = constrainedLeafList4.getConstraints();
112
113         assertNotEquals(ConstraintDefinitions.hashCode(constraints3), ConstraintDefinitions.hashCode(constraints4));
114         assertFalse(ConstraintDefinitions.equals(constraints3, constraints4));
115
116         final String constraintsString = ConstraintDefinitions.toString(constraints4);
117         assertEquals("EffectiveConstraintDefinitionImpl{whenCondition=foo = 'bar', mustConstraints=[bar != 'foo'], "
118                 + "minElements=50, maxElements=100}", constraintsString);
119     }
120 }