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