Merge "BUG-865: removed use of deprecated code in yang-parser-impl tests."
[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 com.google.common.base.Optional;
16 import java.net.URI;
17 import java.net.URISyntaxException;
18 import java.util.ArrayList;
19 import java.util.Date;
20 import java.util.List;
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(MustDefinitionImpl.create("mustStr1", Optional.of("description1"), Optional.of("reference1"),
135                 Optional.of("errorAppTag1"), Optional.of("errorMessage1")));
136         assertFalse("rh shouldn't equal to rh1", rh.equals(rh1));
137         rh.setMust(MustDefinitionImpl.create("mustStr1", Optional.of("description1"), Optional.of("reference1"),
138                 Optional.of("errorAppTag1"), Optional.of("errorMessage1")));
139         assertEquals("rh should equal to rh1", rh, rh1);
140         rh.setMust(MustDefinitionImpl.create("mustStr", Optional.of("description"), Optional.of("reference"),
141                 Optional.of("errorAppTag"), Optional.of("errorMessage")));
142         assertFalse("rh shouldn't equal to rh1", rh.equals(rh1));
143         rh1.setMust(MustDefinitionImpl.create("mustStr", Optional.of("description"), Optional.of("reference"),
144                 Optional.of("errorAppTag"), Optional.of("errorMessage")));
145     }
146
147     @Test
148     public void testPresenceEqualsBranch() {
149         assertEquals("rh should equal to rh1", rh, rh1);
150         rh1.setPresence(false);
151         assertFalse("rh shouldn't equal to rh1", rh.equals(rh1));
152         rh.setPresence(false);
153         assertEquals("rh should equal to rh1", rh, rh1);
154         rh.setPresence(true);
155         assertFalse("rh shouldn't equal to rh1", rh.equals(rh1));
156         rh1.setPresence(true);
157     }
158
159     @Test
160     public void testReferenceEqualsBranch() {
161         assertEquals("rh should equal to rh1", rh, rh1);
162         rh1.setReference("reference1");
163         assertFalse("rh shouldn't equal to rh1", rh.equals(rh1));
164         rh.setReference("reference1");
165         assertEquals("rh should equal to rh1", rh, rh1);
166         rh.setReference("reference");
167         assertFalse("rh shouldn't equal to rh1", rh.equals(rh1));
168         rh1.setReference("reference");
169     }
170
171     private URI getUri(final String uri) {
172         URI simpleUri = null;
173         boolean instantionated = false;
174         try {
175             simpleUri = new URI(uri);
176             instantionated = true;
177         } catch (URISyntaxException e1) {
178             e1.printStackTrace();
179             assertTrue("Uri instance wasn't created.", instantionated);
180         }
181         return simpleUri;
182     }
183
184     @Test
185     public void testAddUnknownNodeBuilderEqualsBranch() {
186         URI simpleUri = null;
187         simpleUri = getUri("very:simple:URI");
188         assertNotNull("URI can't be null", simpleUri);
189
190         URI uriA = getUri("some:uriA");
191         assertNotNull("URI can't be null", simpleUri);
192         QName qnameA = QName.create(uriA, new Date(5000000), "some nameA");
193         QName qnameB = QName.create(uriA, new Date(6000000), "some nameB");
194         List<QName> qnamesA = new ArrayList<>();
195         List<QName> qnamesB = new ArrayList<>();
196         qnamesA.add(qnameA);
197         qnamesB.add(qnameB);
198         SchemaPath schemaPathB = SchemaPath.create(qnamesB, true);
199
200         UnknownSchemaNodeBuilderImpl usnb = new UnknownSchemaNodeBuilderImpl("usnb", 151, new QName(simpleUri, "tst"), schemaPathB);
201         UnknownSchemaNodeBuilderImpl usnb1 = new UnknownSchemaNodeBuilderImpl("usnb", 151, new QName(simpleUri, "tst"), schemaPathB);
202
203         assertEquals("rh should equal to rh1", rh, rh1);
204         rh1.addUnknownNodeBuilder(usnb);
205         assertFalse("rh shouldn't equal to rh1", rh.equals(rh1));
206         rh.addUnknownNodeBuilder(usnb1);
207         assertEquals("rh should equal to rh1", rh, rh1);
208     }
209
210     @Test
211     public void testParentEqualsBranch() {
212         URI simpleUriA = getUri("very:simple:URI:a");
213         URI simpleUriB = getUri("very:simple:URI:b");
214
215         SchemaPath path = org.mockito.Mockito.mock(SchemaPath.class);
216
217         UnknownSchemaNodeBuilderImpl usnbA = new UnknownSchemaNodeBuilderImpl("usnbA", 151, new QName(simpleUriA, "tst"), path);
218         UnknownSchemaNodeBuilderImpl usnbB = new UnknownSchemaNodeBuilderImpl("usnbB", 151, new QName(simpleUriB, "tst"), path);
219         UnknownSchemaNodeBuilderImpl usnbAParent = new UnknownSchemaNodeBuilderImpl("usnbAParent", 151, new QName(simpleUriA,
220                 "tst"), path);
221         usnbA.setParent(usnbAParent);
222
223         assertEquals("rh should equal to rh1", rh, rh1);
224         rh1.setParent(usnbB);
225         assertFalse("rh shouldn't equal to rh1", rh.equals(rh1));
226         rh.setParent(usnbB);
227         assertEquals("rh should equal to rh1", rh, rh1);
228         rh.setParent(usnbA);
229         assertFalse("rh shouldn't equal to rh1", rh.equals(rh1));
230         rh1.setParent(usnbA);
231
232         assertEquals("rh should equal to rh1", rh, rh1);
233     }
234
235 }