2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.parser.util;
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;
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;
29 public class RefineHolderTest {
31 private RefineHolderImpl rh;
32 private RefineHolderImpl rh1;
36 rh = new RefineHolderImpl("module", 2104, "name");
37 rh1 = new RefineHolderImpl("module", 2104, "name");
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")));
48 assertEquals("rh1 should equals to rh", rh, rh1);
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));
55 assertEquals("Wrong hash code", 1557537141, rh.hashCode());
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);
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");
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");
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);
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);
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);
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")));
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);
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");
170 private static URI getUri(final String uri) {
171 URI simpleUri = null;
172 boolean instantionated = false;
174 simpleUri = new URI(uri);
175 instantionated = true;
176 } catch (URISyntaxException e1) {
177 e1.printStackTrace();
178 assertTrue("Uri instance wasn't created.", instantionated);
184 public void testAddUnknownNodeBuilderEqualsBranch() {
185 URI simpleUri = null;
186 simpleUri = getUri("very:simple:URI");
187 assertNotNull("URI can't be null", simpleUri);
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<>();
197 SchemaPath schemaPathB = SchemaPath.create(qnamesB, true);
199 UnknownSchemaNodeBuilderImpl usnb = new UnknownSchemaNodeBuilderImpl("usnb", 151, new QName(simpleUri, "tst"), schemaPathB);
200 UnknownSchemaNodeBuilderImpl usnb1 = new UnknownSchemaNodeBuilderImpl("usnb", 151, new QName(simpleUri, "tst"), schemaPathB);
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);
210 public void testParentEqualsBranch() {
211 URI simpleUriA = getUri("very:simple:URI:a");
212 URI simpleUriB = getUri("very:simple:URI:b");
214 SchemaPath path = org.mockito.Mockito.mock(SchemaPath.class);
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,
220 usnbA.setParent(usnbAParent);
222 assertEquals("rh should equal to rh1", rh, rh1);
223 rh1.setParent(usnbB);
224 assertFalse("rh shouldn't equal to rh1", rh.equals(rh1));
226 assertEquals("rh should equal to rh1", rh, rh1);
228 assertFalse("rh shouldn't equal to rh1", rh.equals(rh1));
229 rh1.setParent(usnbA);
231 assertEquals("rh should equal to rh1", rh, rh1);