5d991a26ced52b8e0b551cfb5542974cc6300e10
[onosfw.git] /
1 /*
2  * Copyright 2015 Open Networking Laboratory
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.onosproject.store.service;
17
18 import com.google.common.testing.EqualsTester;
19 import junit.framework.TestCase;
20
21 import org.junit.Assert;
22 import org.junit.Test;
23
24 import java.lang.reflect.Constructor;
25 import java.util.Arrays;
26
27 import static org.hamcrest.MatcherAssert.assertThat;
28 import static org.hamcrest.Matchers.is;
29 import static org.hamcrest.Matchers.notNullValue;
30
31 /**
32  * MultiValuedTimestamp unit tests.
33  */
34 public class MultiValuedTimestampTest extends TestCase {
35
36     private final MultiValuedTimestamp<Integer, Integer> stats1 = new MultiValuedTimestamp<>(1, 3);
37
38     private final MultiValuedTimestamp<Integer, Integer> stats2 = new MultiValuedTimestamp<>(1, 2);
39
40
41     /**
42      * Tests the creation of the MapEvent object.
43      */
44     @Test
45     public void testConstruction() {
46         assertThat(stats1.value1(), is(1));
47         assertThat(stats1.value2(), is(3));
48     }
49
50     /**
51      * Tests the toCompare function.
52      */
53     @Test
54     public void testToCompare() {
55         assertThat(stats1.compareTo(stats2), is(1));
56     }
57
58     /**
59      * Tests the equals, hashCode and toString methods using Guava EqualsTester.
60      */
61     @Test
62     public void testEquals() {
63         new EqualsTester()
64                 .addEqualityGroup(stats1, stats1)
65                 .addEqualityGroup(stats2)
66                 .testEquals();
67     }
68
69     /**
70      * Tests that the empty argument list constructor for serialization
71      * is present and creates a proper object.
72      */
73     @Test
74     public void testSerializerConstructor() {
75         try {
76             Constructor[] constructors = MultiValuedTimestamp.class.getDeclaredConstructors();
77             assertThat(constructors, notNullValue());
78             Arrays.stream(constructors).filter(ctor ->
79                     ctor.getParameterTypes().length == 0)
80                     .forEach(noParamsCtor -> {
81                         try {
82                             noParamsCtor.setAccessible(true);
83                             MultiValuedTimestamp stats =
84                                     (MultiValuedTimestamp) noParamsCtor.newInstance();
85                             assertThat(stats, notNullValue());
86                         } catch (Exception e) {
87                             Assert.fail("Exception instantiating no parameters constructor");
88                         }
89                     });
90         } catch (Exception e) {
91             Assert.fail("Exception looking up constructors");
92         }
93     }
94 }