blob: 86c497c382eaef98dd754142584a10d3faa757c4 [file] [log] [blame]
samanwita pal74d32fd2015-06-25 09:22:09 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
samanwita pal74d32fd2015-06-25 09:22:09 -07003 *
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 */
16package org.onosproject.store.service;
17
18import com.google.common.testing.EqualsTester;
samanwita pal74d32fd2015-06-25 09:22:09 -070019import org.junit.Test;
20
21import static org.hamcrest.MatcherAssert.assertThat;
22import static org.hamcrest.Matchers.is;
23
24/**
25 * Versioned unit tests.
26 */
Sho SHIMIZU8dc81ea2015-09-10 10:59:43 -070027public class VersionedTest {
samanwita pal74d32fd2015-06-25 09:22:09 -070028
29 private final Versioned<Integer> stats1 = new Versioned<>(1, 2, 3);
30
31 private final Versioned<Integer> stats2 = new Versioned<>(1, 2);
32
33 /**
34 * Tests the creation of the MapEvent object.
35 */
36 @Test
37 public void testConstruction() {
38 assertThat(stats1.value(), is(1));
39 assertThat(stats1.version(), is(2L));
40 assertThat(stats1.creationTime(), is(3L));
41 }
42
43 /**
44 * Maps an Integer to a String - Utility function to test the map function.
45 * @param a Actual Integer parameter.
46 * @return String Mapped valued.
47 */
48 public static String transform(Integer a) {
49 return Integer.toString(a);
50 }
51
52 /**
53 * Tests the map function.
54 */
55 @Test
56 public void testMap() {
57 Versioned<String> tempObj = stats1.<String>map(VersionedTest::transform);
58 assertThat(tempObj.value(), is("1"));
59 }
60
61 /**
Madan Jampani7ffe53d2015-08-17 15:25:35 -070062 * Tests the valueOrElse method.
63 */
64 @Test
65 public void testOrElse() {
Sho SHIMIZUd88db6f2015-09-09 14:22:06 -070066 Versioned<String> vv = new Versioned<>("foo", 1);
Madan Jampani7ffe53d2015-08-17 15:25:35 -070067 Versioned<String> nullVV = null;
68 assertThat(Versioned.valueOrElse(vv, "bar"), is("foo"));
69 assertThat(Versioned.valueOrElse(nullVV, "bar"), is("bar"));
70 }
71
72 /**
samanwita pal74d32fd2015-06-25 09:22:09 -070073 * Tests the equals, hashCode and toString methods using Guava EqualsTester.
74 */
75 @Test
76 public void testEquals() {
77 new EqualsTester()
78 .addEqualityGroup(stats1, stats1)
79 .addEqualityGroup(stats2)
80 .testEquals();
81 }
82
Sho SHIMIZUd88db6f2015-09-09 14:22:06 -070083}