e896f1c34f91fa7307111671ee6bace75231b729
[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 java.util.Arrays;
21 import org.junit.Test;
22 import org.opendaylight.yangtools.yang.common.QName;
23 import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
24 import org.opendaylight.yangtools.yang.data.util.ConstraintDefinitions;
25 import org.opendaylight.yangtools.yang.model.api.ConstraintDefinition;
26 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
27 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
28 import org.opendaylight.yangtools.yang.model.api.Module;
29 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
30 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
31 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
32 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
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         final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
40
41         final YangTextSchemaSource source = YangTextSchemaSource.forResource("/constraint-definitions-test/foo.yang");
42
43         final SchemaContext schemaContext = reactor.buildEffective(Arrays.asList(source));
44         assertNotNull(schemaContext);
45
46         final Module testModule = schemaContext.findModuleByName("foo",
47             SimpleDateFormatUtil.getRevisionFormat().parse("2016-09-20"));
48         assertNotNull(testModule);
49
50         final LeafSchemaNode mandatoryLeaf1 = (LeafSchemaNode) testModule.getDataChildByName(
51                 QName.create(testModule.getQNameModule(), "mandatory-leaf-1"));
52         assertNotNull(mandatoryLeaf1);
53         ConstraintDefinition constraints1 = mandatoryLeaf1.getConstraints();
54
55         final LeafSchemaNode mandatoryLeaf2 = (LeafSchemaNode) testModule.getDataChildByName(
56                 QName.create(testModule.getQNameModule(), "mandatory-leaf-2"));
57         assertNotNull(mandatoryLeaf2);
58         ConstraintDefinition constraints2 = mandatoryLeaf2.getConstraints();
59
60         assertEquals(ConstraintDefinitions.hashCode(constraints1), ConstraintDefinitions.hashCode(constraints2));
61         assertTrue(ConstraintDefinitions.equals(constraints1, constraints2));
62
63         assertTrue(ConstraintDefinitions.equals(constraints1, constraints1));
64         assertFalse(ConstraintDefinitions.equals(constraints1, "str"));
65
66         final LeafSchemaNode mandatoryLeaf3 = (LeafSchemaNode) testModule.getDataChildByName(
67                 QName.create(testModule.getQNameModule(), "mandatory-leaf-3"));
68         assertNotNull(mandatoryLeaf3);
69         ConstraintDefinition constraints3 = mandatoryLeaf3.getConstraints();
70
71         assertNotEquals(ConstraintDefinitions.hashCode(constraints2), ConstraintDefinitions.hashCode(constraints3));
72         assertFalse(ConstraintDefinitions.equals(constraints2, constraints3));
73
74         final LeafSchemaNode mandatoryLeaf4 = (LeafSchemaNode) testModule.getDataChildByName(
75                 QName.create(testModule.getQNameModule(), "mandatory-leaf-4"));
76         assertNotNull(mandatoryLeaf4);
77         ConstraintDefinition constraints4 = mandatoryLeaf4.getConstraints();
78
79         assertNotEquals(ConstraintDefinitions.hashCode(constraints3), ConstraintDefinitions.hashCode(constraints4));
80         assertFalse(ConstraintDefinitions.equals(constraints3, constraints4));
81
82         final LeafSchemaNode mandatoryLeaf5 = (LeafSchemaNode) testModule.getDataChildByName(
83                 QName.create(testModule.getQNameModule(), "mandatory-leaf-5"));
84         assertNotNull(mandatoryLeaf5);
85         final ConstraintDefinition constraints5 = mandatoryLeaf5.getConstraints();
86
87         assertNotEquals(ConstraintDefinitions.hashCode(constraints4), ConstraintDefinitions.hashCode(constraints5));
88         assertFalse(ConstraintDefinitions.equals(constraints4, constraints5));
89
90         final LeafListSchemaNode constrainedLeafList1 = (LeafListSchemaNode) testModule.getDataChildByName(
91                 QName.create(testModule.getQNameModule(), "constrained-leaf-list-1"));
92         assertNotNull(constrainedLeafList1);
93         constraints1 = constrainedLeafList1.getConstraints();
94
95         final LeafListSchemaNode constrainedLeafList2 = (LeafListSchemaNode) testModule.getDataChildByName(
96                 QName.create(testModule.getQNameModule(), "constrained-leaf-list-2"));
97         assertNotNull(constrainedLeafList2);
98         constraints2 = constrainedLeafList2.getConstraints();
99
100         assertEquals(ConstraintDefinitions.hashCode(constraints1), ConstraintDefinitions.hashCode(constraints2));
101         assertTrue(ConstraintDefinitions.equals(constraints1, constraints2));
102
103         final LeafListSchemaNode constrainedLeafList3 = (LeafListSchemaNode) testModule.getDataChildByName(
104                 QName.create(testModule.getQNameModule(), "constrained-leaf-list-3"));
105         assertNotNull(constrainedLeafList3);
106         constraints3 = constrainedLeafList3.getConstraints();
107
108         assertNotEquals(ConstraintDefinitions.hashCode(constraints2), ConstraintDefinitions.hashCode(constraints3));
109         assertFalse(ConstraintDefinitions.equals(constraints2, constraints3));
110
111         final LeafListSchemaNode constrainedLeafList4 = (LeafListSchemaNode) testModule.getDataChildByName(
112                 QName.create(testModule.getQNameModule(), "constrained-leaf-list-4"));
113         assertNotNull(constrainedLeafList4);
114         constraints4 = constrainedLeafList4.getConstraints();
115
116         assertNotEquals(ConstraintDefinitions.hashCode(constraints3), ConstraintDefinitions.hashCode(constraints4));
117         assertFalse(ConstraintDefinitions.equals(constraints3, constraints4));
118
119         final String constraintsString = ConstraintDefinitions.toString(constraints4);
120         assertEquals("EffectiveConstraintDefinitionImpl{whenCondition=foo = 'bar', mustConstraints=[bar != 'foo'], "
121                 + "mandatory=true, minElements=50, maxElements=100}", constraintsString);
122     }
123 }