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