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 com.google.common.collect.Lists;
18 import com.google.common.io.ByteSource;
19 import com.google.common.io.Files;
21 import java.io.IOException;
22 import java.net.URISyntaxException;
23 import java.text.ParseException;
24 import org.junit.Test;
25 import org.opendaylight.yangtools.yang.common.QName;
26 import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
27 import org.opendaylight.yangtools.yang.data.util.ConstraintDefinitions;
28 import org.opendaylight.yangtools.yang.model.api.ConstraintDefinition;
29 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
30 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
31 import org.opendaylight.yangtools.yang.model.api.Module;
32 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
33 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
34 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
35 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
37 public class ConstraintDefinitionsTest {
40 public void testConstraintDefinitions() throws ParseException, ReactorException, URISyntaxException, IOException {
41 final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
43 final File yangFile = new File(getClass().getResource("/constraint-definitions-test/foo.yang").toURI());
44 assertNotNull(yangFile);
46 final ByteSource yangByteSource = Files.asByteSource(yangFile);
47 assertNotNull(yangByteSource);
49 final SchemaContext schemaContext = reactor.buildEffective(Lists.newArrayList(yangByteSource));
50 assertNotNull(schemaContext);
52 final Module testModule = schemaContext.findModuleByName(
53 "foo", SimpleDateFormatUtil.getRevisionFormat().parse("2016-09-20"));
54 assertNotNull(testModule);
56 final LeafSchemaNode mandatoryLeaf1 = (LeafSchemaNode) testModule.getDataChildByName(
57 QName.create(testModule.getQNameModule(), "mandatory-leaf-1"));
58 assertNotNull(mandatoryLeaf1);
59 ConstraintDefinition constraints1 = mandatoryLeaf1.getConstraints();
61 final LeafSchemaNode mandatoryLeaf2 = (LeafSchemaNode) testModule.getDataChildByName(
62 QName.create(testModule.getQNameModule(), "mandatory-leaf-2"));
63 assertNotNull(mandatoryLeaf2);
64 ConstraintDefinition constraints2 = mandatoryLeaf2.getConstraints();
66 assertEquals(ConstraintDefinitions.hashCode(constraints1), ConstraintDefinitions.hashCode(constraints2));
67 assertTrue(ConstraintDefinitions.equals(constraints1, constraints2));
69 assertTrue(ConstraintDefinitions.equals(constraints1, constraints1));
70 assertFalse(ConstraintDefinitions.equals(constraints1, "str"));
72 final LeafSchemaNode mandatoryLeaf3 = (LeafSchemaNode) testModule.getDataChildByName(
73 QName.create(testModule.getQNameModule(), "mandatory-leaf-3"));
74 assertNotNull(mandatoryLeaf3);
75 ConstraintDefinition constraints3 = mandatoryLeaf3.getConstraints();
77 assertNotEquals(ConstraintDefinitions.hashCode(constraints2), ConstraintDefinitions.hashCode(constraints3));
78 assertFalse(ConstraintDefinitions.equals(constraints2, constraints3));
80 final LeafSchemaNode mandatoryLeaf4 = (LeafSchemaNode) testModule.getDataChildByName(
81 QName.create(testModule.getQNameModule(), "mandatory-leaf-4"));
82 assertNotNull(mandatoryLeaf4);
83 ConstraintDefinition constraints4 = mandatoryLeaf4.getConstraints();
85 assertNotEquals(ConstraintDefinitions.hashCode(constraints3), ConstraintDefinitions.hashCode(constraints4));
86 assertFalse(ConstraintDefinitions.equals(constraints3, constraints4));
88 final LeafSchemaNode mandatoryLeaf5 = (LeafSchemaNode) testModule.getDataChildByName(
89 QName.create(testModule.getQNameModule(), "mandatory-leaf-5"));
90 assertNotNull(mandatoryLeaf5);
91 final ConstraintDefinition constraints5 = mandatoryLeaf5.getConstraints();
93 assertNotEquals(ConstraintDefinitions.hashCode(constraints4), ConstraintDefinitions.hashCode(constraints5));
94 assertFalse(ConstraintDefinitions.equals(constraints4, constraints5));
96 final LeafListSchemaNode constrainedLeafList1 = (LeafListSchemaNode) testModule.getDataChildByName(
97 QName.create(testModule.getQNameModule(), "constrained-leaf-list-1"));
98 assertNotNull(constrainedLeafList1);
99 constraints1 = constrainedLeafList1.getConstraints();
101 final LeafListSchemaNode constrainedLeafList2 = (LeafListSchemaNode) testModule.getDataChildByName(
102 QName.create(testModule.getQNameModule(), "constrained-leaf-list-2"));
103 assertNotNull(constrainedLeafList2);
104 constraints2 = constrainedLeafList2.getConstraints();
106 assertEquals(ConstraintDefinitions.hashCode(constraints1), ConstraintDefinitions.hashCode(constraints2));
107 assertTrue(ConstraintDefinitions.equals(constraints1, constraints2));
109 final LeafListSchemaNode constrainedLeafList3 = (LeafListSchemaNode) testModule.getDataChildByName(
110 QName.create(testModule.getQNameModule(), "constrained-leaf-list-3"));
111 assertNotNull(constrainedLeafList3);
112 constraints3 = constrainedLeafList3.getConstraints();
114 assertNotEquals(ConstraintDefinitions.hashCode(constraints2), ConstraintDefinitions.hashCode(constraints3));
115 assertFalse(ConstraintDefinitions.equals(constraints2, constraints3));
117 final LeafListSchemaNode constrainedLeafList4 = (LeafListSchemaNode) testModule.getDataChildByName(
118 QName.create(testModule.getQNameModule(), "constrained-leaf-list-4"));
119 assertNotNull(constrainedLeafList4);
120 constraints4 = constrainedLeafList4.getConstraints();
122 assertNotEquals(ConstraintDefinitions.hashCode(constraints3), ConstraintDefinitions.hashCode(constraints4));
123 assertFalse(ConstraintDefinitions.equals(constraints3, constraints4));
125 final String constraintsString = ConstraintDefinitions.toString(constraints4);
126 assertEquals("EffectiveConstraintDefinitionImpl{whenCondition=foo = 'bar', mustConstraints=[bar != 'foo'], " +
127 "mandatory=true, minElements=50, maxElements=100}", constraintsString);