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