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