blob: ad439d66d69f40f5e77e7d7bc1e106f326dce71f [file] [log] [blame]
Samanwita Pale00733a2015-06-23 13:57:11 -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;
Samanwita Pale00733a2015-06-23 13:57:11 -070019import org.junit.Test;
20
21import static org.hamcrest.MatcherAssert.assertThat;
22import static org.hamcrest.Matchers.is;
23
24/**
25 * Unit Tests for DatabseUpdate class.
26 */
27
Sho SHIMIZU8dc81ea2015-09-10 10:59:43 -070028public class DatabaseUpdateTest {
Samanwita Pale00733a2015-06-23 13:57:11 -070029
30 private final DatabaseUpdate stats1 = DatabaseUpdate.newBuilder()
31 .withCurrentValue("1".getBytes())
32 .withValue("2".getBytes())
33 .withCurrentVersion(3)
34 .withKey("4")
Madan Jampani7804c992015-07-20 13:20:19 -070035 .withMapName("5")
Samanwita Pale00733a2015-06-23 13:57:11 -070036 .withType(DatabaseUpdate.Type.PUT)
37 .build();
38
39 private final DatabaseUpdate stats2 = DatabaseUpdate.newBuilder()
40 .withCurrentValue("1".getBytes())
41 .withValue("2".getBytes())
42 .withCurrentVersion(3)
43 .withKey("4")
Madan Jampani7804c992015-07-20 13:20:19 -070044 .withMapName("5")
Samanwita Pale00733a2015-06-23 13:57:11 -070045 .withType(DatabaseUpdate.Type.REMOVE)
46 .build();
47
48 private final DatabaseUpdate stats3 = DatabaseUpdate.newBuilder()
49 .withCurrentValue("1".getBytes())
50 .withValue("2".getBytes())
51 .withCurrentVersion(3)
52 .withKey("4")
Madan Jampani7804c992015-07-20 13:20:19 -070053 .withMapName("5")
Samanwita Pale00733a2015-06-23 13:57:11 -070054 .withType(DatabaseUpdate.Type.REMOVE_IF_VALUE_MATCH)
55 .build();
56
57 private final DatabaseUpdate stats4 = DatabaseUpdate.newBuilder()
58 .withCurrentValue("1".getBytes())
59 .withValue("2".getBytes())
60 .withCurrentVersion(3)
61 .withKey("4")
Madan Jampani7804c992015-07-20 13:20:19 -070062 .withMapName("5")
Samanwita Pale00733a2015-06-23 13:57:11 -070063 .withType(DatabaseUpdate.Type.REMOVE_IF_VERSION_MATCH)
64 .build();
65
66 private final DatabaseUpdate stats5 = DatabaseUpdate.newBuilder()
67 .withCurrentValue("1".getBytes())
68 .withValue("2".getBytes())
69 .withCurrentVersion(3)
70 .withKey("4")
Madan Jampani7804c992015-07-20 13:20:19 -070071 .withMapName("5")
Samanwita Pale00733a2015-06-23 13:57:11 -070072 .withType(DatabaseUpdate.Type.PUT_IF_VALUE_MATCH)
73 .build();
74
75 private final DatabaseUpdate stats6 = DatabaseUpdate.newBuilder()
76 .withCurrentValue("1".getBytes())
77 .withValue("2".getBytes())
78 .withCurrentVersion(3)
79 .withKey("4")
Madan Jampani7804c992015-07-20 13:20:19 -070080 .withMapName("5")
Samanwita Pale00733a2015-06-23 13:57:11 -070081 .withType(DatabaseUpdate.Type.PUT_IF_VERSION_MATCH)
82 .build();
83
84 /**
85 * Tests the constructor for the class.
86 */
87 @Test
88 public void testConstruction() {
89 assertThat(stats1.currentValue(), is("1".getBytes()));
90 assertThat(stats1.value(), is("2".getBytes()));
91 assertThat(stats1.currentVersion(), is(3L));
92 assertThat(stats1.key(), is("4"));
Madan Jampani7804c992015-07-20 13:20:19 -070093 assertThat(stats1.mapName(), is("5"));
Samanwita Pale00733a2015-06-23 13:57:11 -070094 assertThat(stats1.type(), is(DatabaseUpdate.Type.PUT));
95 }
96
97 /**
98 * Tests the equals, hashCode and toString methods using Guava EqualsTester.
99 */
100 @Test
101 public void testEquals() {
102 new EqualsTester()
103 .addEqualityGroup(stats1, stats1)
104 .addEqualityGroup(stats2)
105 .testEquals();
106
107 new EqualsTester()
108 .addEqualityGroup(stats3, stats3)
109 .addEqualityGroup(stats4)
110 .testEquals();
111
112 new EqualsTester()
113 .addEqualityGroup(stats5, stats5)
114 .addEqualityGroup(stats6)
115 .testEquals();
116 }
117
118 /**
119 * Tests if the toString method returns a consistent value for hashing.
120 */
121 @Test
122 public void testToString() {
123 assertThat(stats1.toString(), is(stats1.toString()));
124 }
125
Sho SHIMIZU8dc81ea2015-09-10 10:59:43 -0700126}