BUG-584: tests update, removed use of deprecated elements.
[yangtools.git] / yang / yang-parser-impl / src / test / java / org / opendaylight / yangtools / yang / parser / util / RefineHolderTest.java
1 /*
2  * Copyright (c) 2014 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.util;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertTrue;
14
15 import java.net.URI;
16 import java.net.URISyntaxException;
17 import java.util.ArrayList;
18 import java.util.Date;
19 import java.util.List;
20 import org.junit.Before;
21 import org.junit.Test;
22 import org.opendaylight.yangtools.yang.common.QName;
23 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
24 import org.opendaylight.yangtools.yang.parser.builder.impl.UnknownSchemaNodeBuilder;
25
26 public class RefineHolderTest {
27
28     private RefineHolder rh;
29     private RefineHolder rh1;
30
31     @Before
32     public void init() {
33         rh = new RefineHolder("module", 2104, "name");
34         rh1 = new RefineHolder("module", 2104, "name");
35     }
36
37
38     @Test
39     public void testRefineEquality() {
40         // hashCode method test
41         assertEquals("rh should equals to itsefl", rh, rh);
42         assertFalse("rh shouldn't equal to null", rh.equals(null));
43         assertFalse("rh shouldn't equal to object of other type", rh.equals(new String("str")));
44
45         assertEquals("rh1 should equals to rh", rh, rh1);
46
47         RefineHolder rh2 = new RefineHolder("module", 2104, null);
48         assertFalse("rh shouldn't equal to rh2", rh2.equals(rh1));
49         rh2 = new RefineHolder("module", 2104, "name2");
50         assertFalse("rh shouldn't equal to rh2", rh.equals(rh2));
51
52         assertEquals("Wrong hash code", 1557537141, rh.hashCode());
53     }
54
55     @Test
56     public void testConfigurationEqualsBranch() {
57         assertEquals("rh should equal to rh1", rh, rh1);
58         rh1.setConfiguration(false);
59         assertFalse("rh shouldn't equal to rh1", rh.equals(rh1));
60         rh.setConfiguration(false);
61         assertEquals("rh should equal to rh1", rh, rh1);
62         rh.setConfiguration(true);
63         assertFalse("rh shouldn't equal to rh1", rh.equals(rh1));
64         rh1.setConfiguration(true);
65     }
66
67     @Test
68     public void testDefaultStrEqualsBranch() {
69         assertEquals("rh should equal to rh1", rh, rh1);
70         rh1.setDefaultStr("default string1");
71         assertFalse("rh shouldn't equal to rh1", rh.equals(rh1));
72         rh.setDefaultStr("default string1");
73         assertEquals("rh should equal to rh1", rh, rh1);
74         rh.setDefaultStr("default string");
75         assertFalse("rh shouldn't equal to rh1", rh.equals(rh1));
76         rh1.setDefaultStr("default string");
77     }
78
79     @Test
80     public void testDescriptionEqualsBranch() {
81         assertEquals("rh should equal to rh1", rh, rh1);
82         rh1.setDescription("description1");
83         assertFalse("rh shouldn't equal to rh1", rh.equals(rh1));
84         rh.setDescription("description1");
85         assertEquals("rh should equal to rh1", rh, rh1);
86         rh.setDescription("description");
87         assertFalse("rh shouldn't equal to rh1", rh.equals(rh1));
88         rh1.setDescription("description");
89     }
90
91     @Test
92     public void testMandatoryEqualsBranch() {
93         assertEquals("rh should equal to rh1", rh, rh1);
94         rh1.setMandatory(false);
95         assertFalse("rh shouldn't equal to rh1", rh.equals(rh1));
96         rh.setMandatory(false);
97         assertEquals("rh should equal to rh1", rh, rh1);
98         rh.setMandatory(true);
99         assertFalse("rh shouldn't equal to rh1", rh.equals(rh1));
100         rh1.setMandatory(true);
101     }
102
103     @Test
104     public void testMaxElementsEqualsBranch() {
105         assertEquals("rh should equal to rh1", rh, rh1);
106         rh1.setMaxElements(5400);
107         assertFalse("rh shouldn't equal to rh1", rh.equals(rh1));
108         rh.setMaxElements(5400);
109         assertEquals("rh should equal to rh1", rh, rh1);
110         rh.setMaxElements(5435);
111         assertFalse("rh shouldn't equal to rh1", rh.equals(rh1));
112         rh1.setMaxElements(5435);
113     }
114
115     @Test
116     public void testMinElementsEqualsBranch() {
117         assertEquals("rh should equal to rh1", rh, rh1);
118         rh1.setMinElements(16);
119         assertFalse("rh shouldn't equal to rh1", rh.equals(rh1));
120         rh.setMinElements(16);
121         assertEquals("rh should equal to rh1", rh, rh1);
122         rh.setMinElements(159);
123         assertFalse("rh shouldn't equal to rh1", rh.equals(rh1));
124         rh1.setMinElements(159);
125     }
126
127     @Test
128     public void testMustEqualsBranch() {
129         assertEquals("rh should equal to rh1", rh, rh1);
130         rh1.setMust(new MustDefinitionImpl("mustStr1", "description1", "reference1", "errorAppTag1", "errorMessage1"));
131         assertFalse("rh shouldn't equal to rh1", rh.equals(rh1));
132         rh.setMust(new MustDefinitionImpl("mustStr1", "description1", "reference1", "errorAppTag1", "errorMessage1"));
133         assertEquals("rh should equal to rh1", rh, rh1);
134         rh.setMust(new MustDefinitionImpl("mustStr", "description", "reference", "errorAppTag", "errorMessage"));
135         assertFalse("rh shouldn't equal to rh1", rh.equals(rh1));
136         rh1.setMust(new MustDefinitionImpl("mustStr", "description", "reference", "errorAppTag", "errorMessage"));
137     }
138
139     @Test
140     public void testPresenceEqualsBranch() {
141         assertEquals("rh should equal to rh1", rh, rh1);
142         rh1.setPresence(false);
143         assertFalse("rh shouldn't equal to rh1", rh.equals(rh1));
144         rh.setPresence(false);
145         assertEquals("rh should equal to rh1", rh, rh1);
146         rh.setPresence(true);
147         assertFalse("rh shouldn't equal to rh1", rh.equals(rh1));
148         rh1.setPresence(true);
149     }
150
151     @Test
152     public void testReferenceEqualsBranch() {
153         assertEquals("rh should equal to rh1", rh, rh1);
154         rh1.setReference("reference1");
155         assertFalse("rh shouldn't equal to rh1", rh.equals(rh1));
156         rh.setReference("reference1");
157         assertEquals("rh should equal to rh1", rh, rh1);
158         rh.setReference("reference");
159         assertFalse("rh shouldn't equal to rh1", rh.equals(rh1));
160         rh1.setReference("reference");
161     }
162
163     private URI getUri(final String uri) {
164         URI simpleUri = null;
165         boolean instantionated = false;
166         try {
167             simpleUri = new URI(uri);
168             instantionated = true;
169         } catch (URISyntaxException e1) {
170             e1.printStackTrace();
171             assertTrue("Uri instance wasn't created.", instantionated);
172         }
173         return simpleUri;
174     }
175
176     @Test
177     public void testAddUnknownNodeBuilderEqualsBranch() {
178         URI simpleUri = null;
179         simpleUri = getUri("very:simple:URI");
180         assertNotNull("URI can't be null", simpleUri);
181
182         URI uriA = getUri("some:uriA");
183         assertNotNull("URI can't be null", simpleUri);
184         QName qnameA = QName.create(uriA, new Date(5000000), "some nameA");
185         QName qnameB = QName.create(uriA, new Date(6000000), "some nameB");
186         List<QName> qnamesA = new ArrayList<>();
187         List<QName> qnamesB = new ArrayList<>();
188         qnamesA.add(qnameA);
189         qnamesB.add(qnameB);
190         SchemaPath schemaPathB = SchemaPath.create(qnamesB, true);
191
192         UnknownSchemaNodeBuilder usnb = new UnknownSchemaNodeBuilder("usnb", 151, new QName(simpleUri, "tst"), schemaPathB);
193         UnknownSchemaNodeBuilder usnb1 = new UnknownSchemaNodeBuilder("usnb", 151, new QName(simpleUri, "tst"), schemaPathB);
194
195         assertEquals("rh should equal to rh1", rh, rh1);
196         rh1.addUnknownNodeBuilder(usnb);
197         assertFalse("rh shouldn't equal to rh1", rh.equals(rh1));
198         rh.addUnknownNodeBuilder(usnb1);
199         assertEquals("rh should equal to rh1", rh, rh1);
200     }
201
202     @Test
203     public void testParentEqualsBranch() {
204         URI simpleUriA = getUri("very:simple:URI:a");
205         URI simpleUriB = getUri("very:simple:URI:b");
206
207         SchemaPath path = org.mockito.Mockito.mock(SchemaPath.class);
208
209         UnknownSchemaNodeBuilder usnbA = new UnknownSchemaNodeBuilder("usnbA", 151, new QName(simpleUriA, "tst"), path);
210         UnknownSchemaNodeBuilder usnbB = new UnknownSchemaNodeBuilder("usnbB", 151, new QName(simpleUriB, "tst"), path);
211         UnknownSchemaNodeBuilder usnbAParent = new UnknownSchemaNodeBuilder("usnbAParent", 151, new QName(simpleUriA,
212                 "tst"), path);
213         usnbA.setParent(usnbAParent);
214
215         assertEquals("rh should equal to rh1", rh, rh1);
216         rh1.setParent(usnbB);
217         assertFalse("rh shouldn't equal to rh1", rh.equals(rh1));
218         rh.setParent(usnbB);
219         assertEquals("rh should equal to rh1", rh, rh1);
220         rh.setParent(usnbA);
221         assertFalse("rh shouldn't equal to rh1", rh.equals(rh1));
222         rh1.setParent(usnbA);
223
224         assertEquals("rh should equal to rh1", rh, rh1);
225     }
226
227 }