92b4ae43cb38266b7c5e63972028d974ea28d09e
[yangtools.git] / data / yang-data-impl / src / test / java / org / opendaylight / yangtools / yang / data / impl / schema / nodes / AbstractImmutableNormalizedValueAttrNodeTest.java
1 /*
2  * Copyright (c) 2013 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.data.impl.schema.nodes;
9
10 import static org.junit.Assert.assertFalse;
11 import static org.junit.Assert.assertTrue;
12
13 import org.junit.Test;
14 import org.opendaylight.yangtools.yang.common.QName;
15 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
16 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
17
18 public class AbstractImmutableNormalizedValueAttrNodeTest {
19     private record TestValue(int value) {
20         // Simple enough
21     }
22
23     private static final QName ROOT_QNAME = QName.create("urn:test", "2014-03-13", "root");
24     private static final QName LEAF_QNAME = QName.create(ROOT_QNAME, "my-leaf");
25     private static final QName SAME_LEAF_QNAME = QName.create(ROOT_QNAME, "my-leaf");
26     private static final QName OTHER_LEAF_QNAME = QName.create(ROOT_QNAME, "my-other-leaf");
27
28     @Test
29     // This test is based on using different references; we're testing equals()
30     @SuppressWarnings({"RedundantStringConstructorCall", "EqualsWithItself"})
31     public void equalsByteTest() {
32         byte[] value = "test".getBytes();
33         byte[] equalValue = "test".getBytes();
34
35         LeafNode<byte[]> leafNode = ImmutableNodes.leafNode(LEAF_QNAME, value);
36         LeafNode<byte[]> equalLeafNode = ImmutableNodes.leafNode(SAME_LEAF_QNAME, equalValue);
37
38         assertTrue(leafNode.equals(leafNode));
39         assertTrue(leafNode.equals(equalLeafNode));
40         assertTrue(equalLeafNode.equals(leafNode));
41
42         TestValue[] value2 = new TestValue[] { new TestValue(1), new TestValue(2) };
43         TestValue[] equalValue2 = new TestValue[] { new TestValue(1), new TestValue(2) };
44
45         LeafNode<TestValue[]> leafNode2 = ImmutableNodes.leafNode(LEAF_QNAME, value2);
46         LeafNode<TestValue[]> equalLeafNode2 = ImmutableNodes.leafNode(SAME_LEAF_QNAME, equalValue2);
47
48         assertTrue(leafNode2.equals(leafNode2));
49         assertTrue(leafNode2.equals(equalLeafNode2));
50         assertTrue(equalLeafNode2.equals(leafNode2));
51
52         byte[][] value3 = new byte[][] { "test".getBytes(), "test2".getBytes() };
53         byte[][] equalValue3 = new byte[][] { "test".getBytes(), "test2".getBytes() };
54
55         LeafNode<byte[][]> leafNode3 = ImmutableNodes.leafNode(LEAF_QNAME,
56                 value3);
57         LeafNode<byte[][]> equalLeafNode3 = ImmutableNodes.leafNode(SAME_LEAF_QNAME, equalValue3);
58
59         assertTrue(leafNode3.equals(leafNode3));
60         assertTrue(leafNode3.equals(equalLeafNode3));
61         assertTrue(equalLeafNode3.equals(leafNode3));
62
63         TestValue[][] value4 = new TestValue[][] {
64             new TestValue[] { new TestValue(1), new TestValue(2) },
65             new TestValue[] { new TestValue(3), new TestValue(4) },
66         };
67         TestValue[][] equalValue4 = new TestValue[][] {
68             new TestValue[] { new TestValue(1), new TestValue(2) },
69             new TestValue[] { new TestValue(3), new TestValue(4) },
70         };
71
72         LeafNode<TestValue[][]> leafNode4 = ImmutableNodes.leafNode(LEAF_QNAME,value4);
73         LeafNode<TestValue[][]> equalLeafNode4 = ImmutableNodes.leafNode(SAME_LEAF_QNAME, equalValue4);
74
75         assertTrue(leafNode4.equals(leafNode4));
76         assertTrue(leafNode4.equals(equalLeafNode4));
77         assertTrue(equalLeafNode4.equals(leafNode4));
78
79         TestValue value6 = new TestValue(1);
80         TestValue equalValue6 = new TestValue(1);
81
82         LeafNode<TestValue> leafNode6 = ImmutableNodes.leafNode(LEAF_QNAME, value6);
83         LeafNode<TestValue> equalLeafNode6 = ImmutableNodes.leafNode(SAME_LEAF_QNAME, equalValue6);
84
85         assertTrue(leafNode6.equals(leafNode6));
86         assertTrue(leafNode6.equals(equalLeafNode6));
87         assertTrue(equalLeafNode6.equals(leafNode6));
88
89         String value5 = "test";
90         String equalValue5 = new String("test");
91
92         LeafNode<String> leafNode5 = ImmutableNodes.leafNode(LEAF_QNAME, value5);
93         LeafNode<String> equalLeafNode5 = ImmutableNodes.leafNode(SAME_LEAF_QNAME, equalValue5);
94
95         assertTrue(leafNode5.equals(leafNode5));
96         assertTrue(leafNode5.equals(equalLeafNode5));
97         assertTrue(equalLeafNode5.equals(leafNode5));
98     }
99
100     @Test
101     // We're testing equals()
102     @SuppressWarnings({"ObjectEqualsNull", "EqualsBetweenInconvertibleTypes"})
103     public void notEqualByteTest() {
104
105         byte[] value = "test".getBytes();
106         byte[] equalValue = "test".getBytes();
107
108         LeafNode<byte[]> leafNode = ImmutableNodes.leafNode(LEAF_QNAME, value);
109         LeafNode<byte[]> otherLeafNode = ImmutableNodes.leafNode(OTHER_LEAF_QNAME, equalValue);
110
111         assertFalse(leafNode.equals(null));
112         assertFalse(leafNode.equals(new Object()));
113         assertFalse(leafNode.equals(otherLeafNode));
114         assertFalse(otherLeafNode.equals(leafNode));
115
116         byte[] value1 = "test".getBytes();
117         byte[] otherValue1 = "test1".getBytes();
118
119         LeafNode<byte[]> leafNode1 = ImmutableNodes.leafNode(LEAF_QNAME, value1);
120         LeafNode<byte[]> otherLeafNode1 = ImmutableNodes.leafNode(SAME_LEAF_QNAME, otherValue1);
121
122         assertFalse(leafNode1.equals(otherLeafNode1));
123         assertFalse(otherLeafNode1.equals(leafNode1));
124
125         TestValue[] value2 = new TestValue[] { new TestValue(1), new TestValue(1) };
126         TestValue[] otherValue2 = new TestValue[] { new TestValue(1), new TestValue(2) };
127
128         LeafNode<TestValue[]> leafNode2 = ImmutableNodes.leafNode(LEAF_QNAME, value2);
129         LeafNode<TestValue[]> otherLeafNode2 = ImmutableNodes.leafNode(SAME_LEAF_QNAME, otherValue2);
130
131         assertFalse(leafNode2.equals(otherLeafNode2));
132         assertFalse(otherLeafNode2.equals(leafNode2));
133
134         byte[][] value3 = new byte[][] { "test".getBytes(), "test2".getBytes() };
135         byte[][] otherValue3 = new byte[][] { "test".getBytes(), "test3".getBytes() };
136
137         LeafNode<byte[][]> leafNode3 = ImmutableNodes.leafNode(LEAF_QNAME, value3);
138         LeafNode<byte[][]> otherLeafNode3 = ImmutableNodes.leafNode(SAME_LEAF_QNAME, otherValue3);
139
140         assertFalse(leafNode3.equals(otherLeafNode3));
141         assertFalse(otherLeafNode3.equals(leafNode3));
142
143         TestValue[][] value4 = new TestValue[][] {
144             new TestValue[] { new TestValue(1), new TestValue(2) },
145             new TestValue[] { new TestValue(3), new TestValue(4) },
146         };
147         TestValue[][] otherValue4 = new TestValue[][] {
148             new TestValue[] { new TestValue(1), new TestValue(2) },
149             new TestValue[] { new TestValue(3), new TestValue(5) },
150         };
151
152         LeafNode<TestValue[][]> leafNode4 = ImmutableNodes.leafNode(LEAF_QNAME, value4);
153         LeafNode<TestValue[][]> otherLeafNode4 = ImmutableNodes.leafNode(SAME_LEAF_QNAME, otherValue4);
154
155         assertFalse(leafNode4.equals(otherLeafNode4));
156         assertFalse(otherLeafNode4.equals(leafNode4));
157
158         TestValue value6 = new TestValue(1);
159         TestValue otherValue6 = new TestValue(2);
160
161         LeafNode<TestValue> leafNode6 = ImmutableNodes.leafNode(LEAF_QNAME, value6);
162         LeafNode<TestValue> otherLeafNode6 = ImmutableNodes.leafNode(SAME_LEAF_QNAME, otherValue6);
163
164         assertFalse(leafNode6.equals(otherLeafNode6));
165         assertFalse(otherLeafNode6.equals(leafNode6));
166
167         String value5 = "test";
168         String otherValue5 = "test2";
169
170         LeafNode<String> leafNode5 = ImmutableNodes.leafNode(LEAF_QNAME, value5);
171         LeafNode<String> otherLeafNode5 = ImmutableNodes.leafNode(SAME_LEAF_QNAME, otherValue5);
172
173         assertFalse(leafNode5.equals(otherLeafNode5));
174         assertFalse(otherLeafNode5.equals(leafNode5));
175         assertFalse(leafNode5.equals(leafNode));
176         assertFalse(leafNode5.equals(leafNode1));
177         assertFalse(leafNode5.equals(leafNode2));
178         assertFalse(leafNode5.equals(leafNode3));
179         assertFalse(leafNode5.equals(leafNode4));
180         assertFalse(leafNode5.equals(leafNode6));
181         assertFalse(leafNode.equals(leafNode5));
182         assertFalse(leafNode1.equals(leafNode5));
183         assertFalse(leafNode2.equals(leafNode5));
184         assertFalse(leafNode3.equals(leafNode5));
185         assertFalse(leafNode4.equals(leafNode5));
186         assertFalse(leafNode6.equals(leafNode5));
187
188         byte[] byteValue = new byte[] { 1, 1 };
189
190         LeafNode<byte[]> byteLeafNode = ImmutableNodes.leafNode(SAME_LEAF_QNAME, byteValue);
191         assertFalse(byteLeafNode.equals(leafNode2));
192         assertFalse(leafNode2.equals(byteLeafNode));
193     }
194
195     @Test
196     // We're testing equals()
197     @SuppressWarnings({"EqualsWithItself", "EqualsBetweenInconvertibleTypes"})
198     public void equalsOtherTypesTest() {
199
200         char[] valueChar = "test".toCharArray();
201         char[] equalValueChar = "test".toCharArray();
202
203         LeafNode<char[]> leafNodeChar = ImmutableNodes.leafNode(LEAF_QNAME, valueChar);
204         LeafNode<char[]> equalLeafNodeChar = ImmutableNodes.leafNode(SAME_LEAF_QNAME, equalValueChar);
205
206         assertTrue(leafNodeChar.equals(leafNodeChar));
207         assertTrue(leafNodeChar.equals(equalLeafNodeChar));
208         assertTrue(equalLeafNodeChar.equals(leafNodeChar));
209
210         boolean[] value = new boolean[] { true, false };
211         boolean[] equalValue = new boolean[] { true, false };
212
213         LeafNode<boolean[]> leafNode = ImmutableNodes.leafNode(LEAF_QNAME, value);
214         LeafNode<boolean[]> equalLeafNode = ImmutableNodes.leafNode(SAME_LEAF_QNAME, equalValue);
215
216         assertTrue(leafNode.equals(leafNode));
217         assertTrue(leafNode.equals(equalLeafNode));
218         assertTrue(equalLeafNode.equals(leafNode));
219
220         int[] value2 = new int[] { 1, 2 };
221         int[] equalValue2 = new int[] { 1, 2 };
222
223         LeafNode<int[]> leafNode2 = ImmutableNodes.leafNode(LEAF_QNAME, value2);
224         LeafNode<int[]> equalLeafNode2 = ImmutableNodes.leafNode(SAME_LEAF_QNAME, equalValue2);
225
226         assertTrue(leafNode2.equals(leafNode2));
227         assertTrue(leafNode2.equals(equalLeafNode2));
228         assertTrue(equalLeafNode2.equals(leafNode2));
229
230         short[] value3 = new short[] { 1, 2 };
231         short[] equalValue3 = new short[] { 1, 2 };
232
233         LeafNode<short[]> leafNode3 = ImmutableNodes.leafNode(LEAF_QNAME, value3);
234         LeafNode<short[]> equalLeafNode3 = ImmutableNodes.leafNode(SAME_LEAF_QNAME, equalValue3);
235
236         assertTrue(leafNode3.equals(leafNode3));
237         assertTrue(leafNode3.equals(equalLeafNode3));
238         assertTrue(equalLeafNode3.equals(leafNode3));
239
240         long[] value4 = new long[] { 1, 2 };
241         long[] equalValue4 = new long[] { 1, 2 };
242
243         LeafNode<long[]> leafNode4 = ImmutableNodes.leafNode(LEAF_QNAME, value4);
244         LeafNode<long[]> equalLeafNode4 = ImmutableNodes.leafNode(SAME_LEAF_QNAME, equalValue4);
245
246         assertTrue(leafNode4.equals(leafNode4));
247         assertTrue(leafNode4.equals(equalLeafNode4));
248         assertTrue(equalLeafNode4.equals(leafNode4));
249
250         double[] value6 = new double[] { 1, 2 };
251         double[] equalValue6 = new double[] { 1, 2 };
252
253         LeafNode<double[]> leafNode6 = ImmutableNodes.leafNode(LEAF_QNAME, value6);
254         LeafNode<double[]> equalLeafNode6 = ImmutableNodes.leafNode(SAME_LEAF_QNAME, equalValue6);
255
256         assertTrue(leafNode6.equals(leafNode6));
257         assertTrue(leafNode6.equals(equalLeafNode6));
258         assertTrue(equalLeafNode6.equals(leafNode6));
259
260         float[] value5 = new float[] { 1, 2 };
261         float[] equalValue5 = new float[] { 1, 2 };
262
263         LeafNode<float[]> leafNode5 = ImmutableNodes.leafNode(LEAF_QNAME, value5);
264         LeafNode<float[]> equalLeafNode5 = ImmutableNodes.leafNode(SAME_LEAF_QNAME, equalValue5);
265
266         assertTrue(leafNode5.equals(leafNode5));
267         assertTrue(leafNode5.equals(equalLeafNode5));
268         assertTrue(equalLeafNode5.equals(leafNode5));
269
270         assertFalse(leafNode.equals(leafNode5));
271         assertFalse(leafNode2.equals(leafNode5));
272         assertFalse(leafNode3.equals(leafNode5));
273         assertFalse(leafNode4.equals(leafNode5));
274         assertFalse(leafNodeChar.equals(leafNode5));
275         assertFalse(leafNode6.equals(leafNode5));
276
277         assertFalse(leafNode5.equals(leafNode));
278         assertFalse(leafNode5.equals(leafNode2));
279         assertFalse(leafNode5.equals(leafNode3));
280         assertFalse(leafNode5.equals(leafNode4));
281         assertFalse(leafNode5.equals(leafNodeChar));
282         assertFalse(leafNode5.equals(leafNode6));
283     }
284 }