samanwita pal

Added unit tests for Versioned and MultiValuedTimestamp classes.

Change-Id: I12c05c546a75aad6ddbe1d63aaa152a1ed642759
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 +}
...\ No newline at end of file ...\ No newline at end of file
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 +import org.junit.Test;
21 +
22 +import static org.hamcrest.MatcherAssert.assertThat;
23 +import static org.hamcrest.Matchers.is;
24 +
25 +/**
26 + * Versioned unit tests.
27 + */
28 +public class VersionedTest extends TestCase {
29 +
30 + private final Versioned<Integer> stats1 = new Versioned<>(1, 2, 3);
31 +
32 + private final Versioned<Integer> stats2 = new Versioned<>(1, 2);
33 +
34 + /**
35 + * Tests the creation of the MapEvent object.
36 + */
37 + @Test
38 + public void testConstruction() {
39 + assertThat(stats1.value(), is(1));
40 + assertThat(stats1.version(), is(2L));
41 + assertThat(stats1.creationTime(), is(3L));
42 + }
43 +
44 + /**
45 + * Maps an Integer to a String - Utility function to test the map function.
46 + * @param a Actual Integer parameter.
47 + * @return String Mapped valued.
48 + */
49 + public static String transform(Integer a) {
50 + return Integer.toString(a);
51 + }
52 +
53 + /**
54 + * Tests the map function.
55 + */
56 + @Test
57 + public void testMap() {
58 + Versioned<String> tempObj = stats1.<String>map(VersionedTest::transform);
59 + assertThat(tempObj.value(), is("1"));
60 + }
61 +
62 + /**
63 + * Tests the equals, hashCode and toString methods using Guava EqualsTester.
64 + */
65 + @Test
66 + public void testEquals() {
67 + new EqualsTester()
68 + .addEqualityGroup(stats1, stats1)
69 + .addEqualityGroup(stats2)
70 + .testEquals();
71 + }
72 +
73 +}
...\ No newline at end of file ...\ No newline at end of file