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