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