1 package org.olsr.v1.info.api.dto;
2 
3 import static org.hamcrest.core.IsEqual.equalTo;
4 import static org.hamcrest.core.IsNull.notNullValue;
5 import static org.junit.Assert.assertThat;
6 
7 import java.net.InetAddress;
8 import java.net.UnknownHostException;
9 import java.util.Set;
10 import java.util.TreeSet;
11 
12 import org.junit.After;
13 import org.junit.Before;
14 import org.junit.Test;
15 
16 public class TestJsonInfoInterfaceConfiguration {
17   private JsonInfoInterfaceConfiguration impl = null;
18 
19   @Before
setUp()20   public void setUp() {
21     this.impl = new JsonInfoInterfaceConfiguration();
22   }
23 
24   @After
tearDown()25   public void tearDown() {
26     this.impl = null;
27   }
28 
29   @Test(timeout = 8000)
testGettersAndSetters()30   public void testGettersAndSetters() throws UnknownHostException {
31     /* initial */
32     assertThat(this.impl.getIpv4Broadcast(), equalTo(""));
33     assertThat(this.impl.getIpv6Multicast(), equalTo(""));
34     assertThat(this.impl.getIpv4Source(), equalTo(""));
35     assertThat(this.impl.getIpv6Source(), equalTo(""));
36     assertThat(Integer.valueOf(this.impl.getIpv6SourcePrefixLength()), equalTo(Integer.valueOf(0)));
37     assertThat(this.impl.getMode(), equalTo(""));
38     assertThat(Integer.valueOf(this.impl.getWeightValue()), equalTo(Integer.valueOf(0)));
39     assertThat(Boolean.valueOf(this.impl.getWeightFixed()), equalTo(Boolean.FALSE));
40     assertThat(this.impl.getHello(), notNullValue());
41     assertThat(this.impl.getTc(), notNullValue());
42     assertThat(this.impl.getMid(), notNullValue());
43     assertThat(this.impl.getHna(), notNullValue());
44     assertThat(this.impl.getLinkQualityMultipliers(), notNullValue());
45     assertThat(Integer.valueOf(this.impl.getLinkQualityMultipliers().size()), equalTo(Integer.valueOf(0)));
46     assertThat(Integer.valueOf(this.impl.getLinkQualityMultipliersCount()), equalTo(Integer.valueOf(0)));
47     assertThat(Boolean.valueOf(this.impl.getAutoDetectChanges()), equalTo(Boolean.FALSE));
48 
49     /* set */
50     final InetAddress addr1 = InetAddress.getByName("127.0.0.1");
51     final InetAddress addr2 = InetAddress.getByName("127.0.0.2");
52     final InetAddress addr3 = InetAddress.getByName("127.0.0.3");
53     final InetAddress addr4 = InetAddress.getByName("127.0.0.4");
54     final JsonInfoMessageParameters hello = new JsonInfoMessageParameters();
55     hello.setEmissionInterval(1.0);
56     final JsonInfoMessageParameters tc = new JsonInfoMessageParameters();
57     tc.setEmissionInterval(2.0);
58     final JsonInfoMessageParameters mid = new JsonInfoMessageParameters();
59     mid.setEmissionInterval(3.0);
60     final JsonInfoMessageParameters hna = new JsonInfoMessageParameters();
61     hna.setEmissionInterval(4.0);
62     final Set<JsonInfoLinkQualityMultiplier> linkQualityMultipliers = new TreeSet<>();
63     final JsonInfoLinkQualityMultiplier entry = new JsonInfoLinkQualityMultiplier();
64     entry.setMultiplier(1.0);
65     linkQualityMultipliers.add(entry);
66 
67     this.impl.setIpv4Broadcast(addr1);
68     this.impl.setIpv6Multicast(addr2);
69     this.impl.setIpv4Source(addr3);
70     this.impl.setIpv6Source(addr4);
71     this.impl.setIpv6SourcePrefixLength(1);
72     this.impl.setMode("mode");
73     this.impl.setWeightValue(2);
74     this.impl.setWeightFixed(true);
75     this.impl.setHello(hello);
76     this.impl.setTc(tc);
77     this.impl.setMid(mid);
78     this.impl.setHna(hna);
79     this.impl.setLinkQualityMultipliers(linkQualityMultipliers);
80     this.impl.setLinkQualityMultipliersCount(3);
81     this.impl.setAutoDetectChanges(true);
82 
83     /* get */
84     assertThat(this.impl.getIpv4Broadcast(), equalTo(addr1.getHostAddress()));
85     assertThat(this.impl.getIpv6Multicast(), equalTo(addr2.getHostAddress()));
86     assertThat(this.impl.getIpv4Source(), equalTo(addr3.getHostAddress()));
87     assertThat(this.impl.getIpv6Source(), equalTo(addr4.getHostAddress()));
88     assertThat(Integer.valueOf(this.impl.getIpv6SourcePrefixLength()), equalTo(Integer.valueOf(1)));
89     assertThat(this.impl.getMode(), equalTo("mode"));
90     assertThat(Integer.valueOf(this.impl.getWeightValue()), equalTo(Integer.valueOf(2)));
91     assertThat(Boolean.valueOf(this.impl.getWeightFixed()), equalTo(Boolean.TRUE));
92     assertThat(this.impl.getHello(), equalTo(hello));
93     assertThat(this.impl.getTc(), equalTo(tc));
94     assertThat(this.impl.getMid(), equalTo(mid));
95     assertThat(this.impl.getHna(), equalTo(hna));
96     assertThat(this.impl.getLinkQualityMultipliers(), equalTo(linkQualityMultipliers));
97     assertThat(Integer.valueOf(this.impl.getLinkQualityMultipliers().size()), equalTo(Integer.valueOf(1)));
98     assertThat(Integer.valueOf(this.impl.getLinkQualityMultipliersCount()), equalTo(Integer.valueOf(3)));
99     assertThat(Boolean.valueOf(this.impl.getAutoDetectChanges()), equalTo(Boolean.TRUE));
100 
101     this.impl.setLinkQualityMultipliers(null);
102     assertThat(this.impl.getLinkQualityMultipliers(), notNullValue());
103   }
104 
105   @Test(timeout = 8000)
testEquals()106   public void testEquals() throws UnknownHostException {
107     boolean r;
108     JsonInfoInterfaceConfiguration other;
109     final InetAddress addr = InetAddress.getByName("127.0.0.1");
110     final JsonInfoMessageParameters mp = new JsonInfoMessageParameters();
111     mp.setEmissionInterval(123.321);
112 
113     r = this.impl.equals(this.impl);
114     assertThat(Boolean.valueOf(r), equalTo(Boolean.TRUE));
115 
116     other = null;
117     r = this.impl.equals(other);
118     assertThat(Boolean.valueOf(r), equalTo(Boolean.FALSE));
119 
120     final Object otherObj = new Object();
121     r = this.impl.equals(otherObj);
122     assertThat(Boolean.valueOf(r), equalTo(Boolean.FALSE));
123 
124     other = new JsonInfoInterfaceConfiguration();
125     r = this.impl.equals(other);
126     assertThat(Boolean.valueOf(r), equalTo(Boolean.TRUE));
127 
128     /* ipv4Broadcast */
129 
130     String savedIp = this.impl.getIpv4Broadcast();
131 
132     this.impl.setIpv4Broadcast(null);
133     other.setIpv4Broadcast(null);
134     r = this.impl.equals(other);
135     assertThat(Boolean.valueOf(r), equalTo(Boolean.TRUE));
136 
137     this.impl.setIpv4Broadcast(null);
138     other.setIpv4Broadcast(addr);
139     r = this.impl.equals(other);
140     assertThat(Boolean.valueOf(r), equalTo(Boolean.FALSE));
141 
142     this.impl.setIpv4Broadcast(addr);
143     other.setIpv4Broadcast(null);
144     r = this.impl.equals(other);
145     assertThat(Boolean.valueOf(r), equalTo(Boolean.FALSE));
146 
147     this.impl.setIpv4Broadcast(addr);
148     other.setIpv4Broadcast(addr);
149     r = this.impl.equals(other);
150     assertThat(Boolean.valueOf(r), equalTo(Boolean.TRUE));
151 
152     this.impl.setIpv4Broadcast(InetAddress.getByName(savedIp));
153     other.setIpv4Broadcast(InetAddress.getByName(savedIp));
154 
155     /* ipv6Multicast */
156 
157     savedIp = this.impl.getIpv6Multicast();
158 
159     this.impl.setIpv6Multicast(null);
160     other.setIpv6Multicast(null);
161     r = this.impl.equals(other);
162     assertThat(Boolean.valueOf(r), equalTo(Boolean.TRUE));
163 
164     this.impl.setIpv6Multicast(null);
165     other.setIpv6Multicast(addr);
166     r = this.impl.equals(other);
167     assertThat(Boolean.valueOf(r), equalTo(Boolean.FALSE));
168 
169     this.impl.setIpv6Multicast(addr);
170     other.setIpv6Multicast(null);
171     r = this.impl.equals(other);
172     assertThat(Boolean.valueOf(r), equalTo(Boolean.FALSE));
173 
174     this.impl.setIpv6Multicast(addr);
175     other.setIpv6Multicast(addr);
176     r = this.impl.equals(other);
177     assertThat(Boolean.valueOf(r), equalTo(Boolean.TRUE));
178 
179     this.impl.setIpv6Multicast(InetAddress.getByName(savedIp));
180     other.setIpv6Multicast(InetAddress.getByName(savedIp));
181 
182     /* ipv4Source */
183 
184     savedIp = this.impl.getIpv4Source();
185 
186     this.impl.setIpv4Source(null);
187     other.setIpv4Source(null);
188     r = this.impl.equals(other);
189     assertThat(Boolean.valueOf(r), equalTo(Boolean.TRUE));
190 
191     this.impl.setIpv4Source(null);
192     other.setIpv4Source(addr);
193     r = this.impl.equals(other);
194     assertThat(Boolean.valueOf(r), equalTo(Boolean.FALSE));
195 
196     this.impl.setIpv4Source(addr);
197     other.setIpv4Source(null);
198     r = this.impl.equals(other);
199     assertThat(Boolean.valueOf(r), equalTo(Boolean.FALSE));
200 
201     this.impl.setIpv4Source(addr);
202     other.setIpv4Source(addr);
203     r = this.impl.equals(other);
204     assertThat(Boolean.valueOf(r), equalTo(Boolean.TRUE));
205 
206     this.impl.setIpv4Source(InetAddress.getByName(savedIp));
207     other.setIpv4Source(InetAddress.getByName(savedIp));
208 
209     /* ipv6Source */
210 
211     savedIp = this.impl.getIpv6Source();
212 
213     this.impl.setIpv6Source(null);
214     other.setIpv6Source(null);
215     r = this.impl.equals(other);
216     assertThat(Boolean.valueOf(r), equalTo(Boolean.TRUE));
217 
218     this.impl.setIpv6Source(null);
219     other.setIpv6Source(addr);
220     r = this.impl.equals(other);
221     assertThat(Boolean.valueOf(r), equalTo(Boolean.FALSE));
222 
223     this.impl.setIpv6Source(addr);
224     other.setIpv6Source(null);
225     r = this.impl.equals(other);
226     assertThat(Boolean.valueOf(r), equalTo(Boolean.FALSE));
227 
228     this.impl.setIpv6Source(addr);
229     other.setIpv6Source(addr);
230     r = this.impl.equals(other);
231     assertThat(Boolean.valueOf(r), equalTo(Boolean.TRUE));
232 
233     this.impl.setIpv6Source(InetAddress.getByName(savedIp));
234     other.setIpv6Source(InetAddress.getByName(savedIp));
235 
236     /* ipv6SourcePrefixLength */
237 
238     int savedInt = this.impl.getIpv6SourcePrefixLength();
239 
240     this.impl.setIpv6SourcePrefixLength(1);
241     other.setIpv6SourcePrefixLength(2);
242     r = this.impl.equals(other);
243     assertThat(Boolean.valueOf(r), equalTo(Boolean.FALSE));
244 
245     this.impl.setIpv6SourcePrefixLength(3);
246     other.setIpv6SourcePrefixLength(3);
247     r = this.impl.equals(other);
248     assertThat(Boolean.valueOf(r), equalTo(Boolean.TRUE));
249 
250     this.impl.setIpv6SourcePrefixLength(savedInt);
251     other.setIpv6SourcePrefixLength(savedInt);
252 
253     /* mode */
254 
255     final String savedString = this.impl.getMode();
256 
257     this.impl.setMode(null);
258     other.setMode(null);
259     r = this.impl.equals(other);
260     assertThat(Boolean.valueOf(r), equalTo(Boolean.TRUE));
261 
262     this.impl.setMode(null);
263     other.setMode("mode");
264     r = this.impl.equals(other);
265     assertThat(Boolean.valueOf(r), equalTo(Boolean.FALSE));
266 
267     this.impl.setMode("mode");
268     other.setMode(null);
269     r = this.impl.equals(other);
270     assertThat(Boolean.valueOf(r), equalTo(Boolean.FALSE));
271 
272     this.impl.setMode("mode");
273     other.setMode("mode");
274     r = this.impl.equals(other);
275     assertThat(Boolean.valueOf(r), equalTo(Boolean.TRUE));
276 
277     this.impl.setMode(savedString);
278     other.setMode(savedString);
279 
280     /* weightValue */
281 
282     savedInt = this.impl.getWeightValue();
283 
284     this.impl.setWeightValue(1);
285     other.setWeightValue(2);
286     r = this.impl.equals(other);
287     assertThat(Boolean.valueOf(r), equalTo(Boolean.FALSE));
288 
289     this.impl.setWeightValue(3);
290     other.setWeightValue(3);
291     r = this.impl.equals(other);
292     assertThat(Boolean.valueOf(r), equalTo(Boolean.TRUE));
293 
294     this.impl.setWeightValue(savedInt);
295     other.setWeightValue(savedInt);
296 
297     /* weightFixed */
298 
299     boolean savedBoolean = this.impl.getWeightFixed();
300 
301     this.impl.setWeightFixed(false);
302     other.setWeightFixed(true);
303     r = this.impl.equals(other);
304     assertThat(Boolean.valueOf(r), equalTo(Boolean.FALSE));
305 
306     this.impl.setWeightFixed(true);
307     other.setWeightFixed(true);
308     r = this.impl.equals(other);
309     assertThat(Boolean.valueOf(r), equalTo(Boolean.TRUE));
310 
311     this.impl.setWeightFixed(savedBoolean);
312     other.setWeightFixed(savedBoolean);
313 
314     /* hello */
315 
316     JsonInfoMessageParameters savedMP = this.impl.getHello();
317 
318     this.impl.setHello(null);
319     other.setHello(null);
320     r = this.impl.equals(other);
321     assertThat(Boolean.valueOf(r), equalTo(Boolean.TRUE));
322 
323     this.impl.setHello(null);
324     other.setHello(mp);
325     r = this.impl.equals(other);
326     assertThat(Boolean.valueOf(r), equalTo(Boolean.FALSE));
327 
328     this.impl.setHello(mp);
329     other.setHello(null);
330     r = this.impl.equals(other);
331     assertThat(Boolean.valueOf(r), equalTo(Boolean.FALSE));
332 
333     this.impl.setHello(mp);
334     other.setHello(mp);
335     r = this.impl.equals(other);
336     assertThat(Boolean.valueOf(r), equalTo(Boolean.TRUE));
337 
338     this.impl.setHello(savedMP);
339     other.setHello(savedMP);
340 
341     /* tc */
342 
343     savedMP = this.impl.getTc();
344 
345     this.impl.setTc(null);
346     other.setTc(null);
347     r = this.impl.equals(other);
348     assertThat(Boolean.valueOf(r), equalTo(Boolean.TRUE));
349 
350     this.impl.setTc(null);
351     other.setTc(mp);
352     r = this.impl.equals(other);
353     assertThat(Boolean.valueOf(r), equalTo(Boolean.FALSE));
354 
355     this.impl.setTc(mp);
356     other.setTc(null);
357     r = this.impl.equals(other);
358     assertThat(Boolean.valueOf(r), equalTo(Boolean.FALSE));
359 
360     this.impl.setTc(mp);
361     other.setTc(mp);
362     r = this.impl.equals(other);
363     assertThat(Boolean.valueOf(r), equalTo(Boolean.TRUE));
364 
365     this.impl.setTc(savedMP);
366     other.setTc(savedMP);
367 
368     /* mid */
369 
370     savedMP = this.impl.getMid();
371 
372     this.impl.setMid(null);
373     other.setMid(null);
374     r = this.impl.equals(other);
375     assertThat(Boolean.valueOf(r), equalTo(Boolean.TRUE));
376 
377     this.impl.setMid(null);
378     other.setMid(mp);
379     r = this.impl.equals(other);
380     assertThat(Boolean.valueOf(r), equalTo(Boolean.FALSE));
381 
382     this.impl.setMid(mp);
383     other.setMid(null);
384     r = this.impl.equals(other);
385     assertThat(Boolean.valueOf(r), equalTo(Boolean.FALSE));
386 
387     this.impl.setMid(mp);
388     other.setMid(mp);
389     r = this.impl.equals(other);
390     assertThat(Boolean.valueOf(r), equalTo(Boolean.TRUE));
391 
392     this.impl.setMid(savedMP);
393     other.setMid(savedMP);
394 
395     /* hna */
396 
397     savedMP = this.impl.getHna();
398 
399     this.impl.setHna(null);
400     other.setHna(null);
401     r = this.impl.equals(other);
402     assertThat(Boolean.valueOf(r), equalTo(Boolean.TRUE));
403 
404     this.impl.setHna(null);
405     other.setHna(mp);
406     r = this.impl.equals(other);
407     assertThat(Boolean.valueOf(r), equalTo(Boolean.FALSE));
408 
409     this.impl.setHna(mp);
410     other.setHna(null);
411     r = this.impl.equals(other);
412     assertThat(Boolean.valueOf(r), equalTo(Boolean.FALSE));
413 
414     this.impl.setHna(mp);
415     other.setHna(mp);
416     r = this.impl.equals(other);
417     assertThat(Boolean.valueOf(r), equalTo(Boolean.TRUE));
418 
419     this.impl.setHna(savedMP);
420     other.setHna(savedMP);
421 
422     /* linkQualityMultipliers */
423 
424     final Set<JsonInfoLinkQualityMultiplier> lqm = new TreeSet<>();
425     final JsonInfoLinkQualityMultiplier e = new JsonInfoLinkQualityMultiplier();
426     e.setMultiplier(10.1);
427     lqm.add(e);
428 
429     final Set<JsonInfoLinkQualityMultiplier> savedLQM = this.impl.getLinkQualityMultipliers();
430 
431     this.impl.setLinkQualityMultipliers(null);
432     other.setLinkQualityMultipliers(null);
433     r = this.impl.equals(other);
434     assertThat(Boolean.valueOf(r), equalTo(Boolean.TRUE));
435 
436     this.impl.setLinkQualityMultipliers(null);
437     other.setLinkQualityMultipliers(lqm);
438     r = this.impl.equals(other);
439     assertThat(Boolean.valueOf(r), equalTo(Boolean.FALSE));
440 
441     this.impl.setLinkQualityMultipliers(lqm);
442     other.setLinkQualityMultipliers(null);
443     r = this.impl.equals(other);
444     assertThat(Boolean.valueOf(r), equalTo(Boolean.FALSE));
445 
446     this.impl.setLinkQualityMultipliers(lqm);
447     other.setLinkQualityMultipliers(lqm);
448     r = this.impl.equals(other);
449     assertThat(Boolean.valueOf(r), equalTo(Boolean.TRUE));
450 
451     this.impl.setLinkQualityMultipliers(savedLQM);
452     other.setLinkQualityMultipliers(savedLQM);
453 
454     /* linkQualityMultipliersCount */
455 
456     savedInt = this.impl.getLinkQualityMultipliersCount();
457 
458     this.impl.setLinkQualityMultipliersCount(1);
459     other.setLinkQualityMultipliersCount(2);
460     r = this.impl.equals(other);
461     assertThat(Boolean.valueOf(r), equalTo(Boolean.FALSE));
462 
463     this.impl.setLinkQualityMultipliersCount(3);
464     other.setLinkQualityMultipliersCount(3);
465     r = this.impl.equals(other);
466     assertThat(Boolean.valueOf(r), equalTo(Boolean.TRUE));
467 
468     this.impl.setLinkQualityMultipliersCount(savedInt);
469     other.setLinkQualityMultipliersCount(savedInt);
470 
471     /* autoDetectChanges */
472 
473     savedBoolean = this.impl.getAutoDetectChanges();
474 
475     this.impl.setAutoDetectChanges(false);
476     other.setAutoDetectChanges(true);
477     r = this.impl.equals(other);
478     assertThat(Boolean.valueOf(r), equalTo(Boolean.FALSE));
479 
480     this.impl.setAutoDetectChanges(true);
481     other.setAutoDetectChanges(true);
482     r = this.impl.equals(other);
483     assertThat(Boolean.valueOf(r), equalTo(Boolean.TRUE));
484 
485     this.impl.setAutoDetectChanges(savedBoolean);
486     other.setAutoDetectChanges(savedBoolean);
487   }
488 
489   @Test(timeout = 8000)
testHashCode()490   public void testHashCode() throws UnknownHostException {
491     int r = this.impl.hashCode();
492     assertThat(Integer.valueOf(r), equalTo(Integer.valueOf(123757567)));
493 
494     /* set */
495     final InetAddress addr1 = InetAddress.getByName("127.0.0.1");
496     final InetAddress addr2 = InetAddress.getByName("127.0.0.2");
497     final InetAddress addr3 = InetAddress.getByName("127.0.0.3");
498     final InetAddress addr4 = InetAddress.getByName("127.0.0.4");
499     final JsonInfoMessageParameters hello = new JsonInfoMessageParameters();
500     hello.setEmissionInterval(1.0);
501     final JsonInfoMessageParameters tc = new JsonInfoMessageParameters();
502     tc.setEmissionInterval(2.0);
503     final JsonInfoMessageParameters mid = new JsonInfoMessageParameters();
504     mid.setEmissionInterval(3.0);
505     final JsonInfoMessageParameters hna = new JsonInfoMessageParameters();
506     hna.setEmissionInterval(4.0);
507     final Set<JsonInfoLinkQualityMultiplier> linkQualityMultipliers = new TreeSet<>();
508     final JsonInfoLinkQualityMultiplier entry = new JsonInfoLinkQualityMultiplier();
509     entry.setMultiplier(1.0);
510     linkQualityMultipliers.add(entry);
511 
512     this.impl.setIpv4Broadcast(addr1);
513     this.impl.setIpv6Multicast(addr2);
514     this.impl.setIpv4Source(addr3);
515     this.impl.setIpv6Source(addr4);
516     this.impl.setIpv6SourcePrefixLength(1);
517     this.impl.setMode("mode");
518     this.impl.setWeightValue(2);
519     this.impl.setWeightFixed(true);
520     this.impl.setHello(hello);
521     this.impl.setTc(tc);
522     this.impl.setMid(mid);
523     this.impl.setHna(hna);
524     this.impl.setLinkQualityMultipliers(linkQualityMultipliers);
525     this.impl.setLinkQualityMultipliersCount(3);
526     this.impl.setAutoDetectChanges(true);
527 
528     r = this.impl.hashCode();
529     assertThat(Integer.valueOf(r), equalTo(Integer.valueOf(-867257189)));
530 
531     this.impl.setLinkQualityMultipliers(null);
532 
533     r = this.impl.hashCode();
534     assertThat(Integer.valueOf(r), equalTo(Integer.valueOf(-934240998)));
535   }
536 
537   @Test(timeout = 8000)
testToString()538   public void testToString() {
539     final String r = this.impl.toString();
540     assertThat(r, notNullValue());
541   }
542 }