blob: 8a401db2f7397c0113ecca391b3c83491d3b38c6 [file] [log] [blame]
samanwita pal74d32fd2015-06-25 09:22:09 -07001/*
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 */
16package org.onosproject.store.service;
17
18import com.google.common.testing.EqualsTester;
19import junit.framework.TestCase;
20import org.junit.Test;
21
22import static org.hamcrest.MatcherAssert.assertThat;
23import static org.hamcrest.Matchers.is;
24
25/**
26 * Versioned unit tests.
27 */
28public 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 /**
Madan Jampani7ffe53d2015-08-17 15:25:35 -070063 * Tests the valueOrElse method.
64 */
65 @Test
66 public void testOrElse() {
Sho SHIMIZUd88db6f2015-09-09 14:22:06 -070067 Versioned<String> vv = new Versioned<>("foo", 1);
Madan Jampani7ffe53d2015-08-17 15:25:35 -070068 Versioned<String> nullVV = null;
69 assertThat(Versioned.valueOrElse(vv, "bar"), is("foo"));
70 assertThat(Versioned.valueOrElse(nullVV, "bar"), is("bar"));
71 }
72
73 /**
samanwita pal74d32fd2015-06-25 09:22:09 -070074 * Tests the equals, hashCode and toString methods using Guava EqualsTester.
75 */
76 @Test
77 public void testEquals() {
78 new EqualsTester()
79 .addEqualityGroup(stats1, stats1)
80 .addEqualityGroup(stats2)
81 .testEquals();
82 }
83
Sho SHIMIZUd88db6f2015-09-09 14:22:06 -070084}