blob: b8ce0392dc428501551d46d5263eadb45fef560e [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 */
Madan Jampani74da78b2016-02-09 21:18:36 -080016package org.onosproject.store.primitives;
Samanwita Pale00733a2015-06-23 13:57:11 -070017
18import com.google.common.testing.EqualsTester;
Madan Jampani74da78b2016-02-09 21:18:36 -080019
Samanwita Pale00733a2015-06-23 13:57:11 -070020import org.junit.Test;
21
22import static org.hamcrest.MatcherAssert.assertThat;
23import static org.hamcrest.Matchers.is;
24
25/**
Madan Jampanicadd70b2016-02-08 13:45:43 -080026 * Unit Tests for MapUpdate class.
Samanwita Pale00733a2015-06-23 13:57:11 -070027 */
28
Madan Jampanicadd70b2016-02-08 13:45:43 -080029public class MapUpdateTest {
Samanwita Pale00733a2015-06-23 13:57:11 -070030
Madan Jampanicadd70b2016-02-08 13:45:43 -080031 private final MapUpdate<String, byte[]> stats1 = MapUpdate.<String, byte[]>newBuilder()
Samanwita Pale00733a2015-06-23 13:57:11 -070032 .withCurrentValue("1".getBytes())
33 .withValue("2".getBytes())
34 .withCurrentVersion(3)
35 .withKey("4")
Madan Jampani7804c992015-07-20 13:20:19 -070036 .withMapName("5")
Madan Jampanicadd70b2016-02-08 13:45:43 -080037 .withType(MapUpdate.Type.PUT)
Samanwita Pale00733a2015-06-23 13:57:11 -070038 .build();
39
Madan Jampanicadd70b2016-02-08 13:45:43 -080040 private final MapUpdate<String, byte[]> stats2 = MapUpdate.<String, byte[]>newBuilder()
Samanwita Pale00733a2015-06-23 13:57:11 -070041 .withCurrentValue("1".getBytes())
42 .withValue("2".getBytes())
43 .withCurrentVersion(3)
44 .withKey("4")
Madan Jampani7804c992015-07-20 13:20:19 -070045 .withMapName("5")
Madan Jampanicadd70b2016-02-08 13:45:43 -080046 .withType(MapUpdate.Type.REMOVE)
Samanwita Pale00733a2015-06-23 13:57:11 -070047 .build();
48
Madan Jampanicadd70b2016-02-08 13:45:43 -080049 private final MapUpdate<String, byte[]> stats3 = MapUpdate.<String, byte[]>newBuilder()
Samanwita Pale00733a2015-06-23 13:57:11 -070050 .withCurrentValue("1".getBytes())
51 .withValue("2".getBytes())
52 .withCurrentVersion(3)
53 .withKey("4")
Madan Jampani7804c992015-07-20 13:20:19 -070054 .withMapName("5")
Madan Jampanicadd70b2016-02-08 13:45:43 -080055 .withType(MapUpdate.Type.REMOVE_IF_VALUE_MATCH)
Samanwita Pale00733a2015-06-23 13:57:11 -070056 .build();
57
Madan Jampanicadd70b2016-02-08 13:45:43 -080058 private final MapUpdate<String, byte[]> stats4 = MapUpdate.<String, byte[]>newBuilder()
Samanwita Pale00733a2015-06-23 13:57:11 -070059 .withCurrentValue("1".getBytes())
60 .withValue("2".getBytes())
61 .withCurrentVersion(3)
62 .withKey("4")
Madan Jampani7804c992015-07-20 13:20:19 -070063 .withMapName("5")
Madan Jampanicadd70b2016-02-08 13:45:43 -080064 .withType(MapUpdate.Type.REMOVE_IF_VERSION_MATCH)
Samanwita Pale00733a2015-06-23 13:57:11 -070065 .build();
66
Madan Jampanicadd70b2016-02-08 13:45:43 -080067 private final MapUpdate<String, byte[]> stats5 = MapUpdate.<String, byte[]>newBuilder()
Samanwita Pale00733a2015-06-23 13:57:11 -070068 .withCurrentValue("1".getBytes())
69 .withValue("2".getBytes())
70 .withCurrentVersion(3)
71 .withKey("4")
Madan Jampani7804c992015-07-20 13:20:19 -070072 .withMapName("5")
Madan Jampanicadd70b2016-02-08 13:45:43 -080073 .withType(MapUpdate.Type.PUT_IF_VALUE_MATCH)
Samanwita Pale00733a2015-06-23 13:57:11 -070074 .build();
75
Madan Jampanicadd70b2016-02-08 13:45:43 -080076 private final MapUpdate<String, byte[]> stats6 = MapUpdate.<String, byte[]>newBuilder()
Samanwita Pale00733a2015-06-23 13:57:11 -070077 .withCurrentValue("1".getBytes())
78 .withValue("2".getBytes())
79 .withCurrentVersion(3)
80 .withKey("4")
Madan Jampani7804c992015-07-20 13:20:19 -070081 .withMapName("5")
Madan Jampanicadd70b2016-02-08 13:45:43 -080082 .withType(MapUpdate.Type.PUT_IF_VERSION_MATCH)
Samanwita Pale00733a2015-06-23 13:57:11 -070083 .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"));
Madan Jampanicadd70b2016-02-08 13:45:43 -080095 assertThat(stats1.type(), is(MapUpdate.Type.PUT));
Samanwita Pale00733a2015-06-23 13:57:11 -070096 }
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
Sho SHIMIZU8dc81ea2015-09-10 10:59:43 -0700127}