blob: 6a0db9381e19a565dc5a58bf54e68727bb568d21 [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 Jampanicadd70b2016-02-08 13:45:43 -080016package org.onosproject.store.primitives.resources.impl;
Samanwita Pale00733a2015-06-23 13:57:11 -070017
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/**
Madan Jampanicadd70b2016-02-08 13:45:43 -080025 * Unit Tests for MapUpdate class.
Samanwita Pale00733a2015-06-23 13:57:11 -070026 */
27
Madan Jampanicadd70b2016-02-08 13:45:43 -080028public class MapUpdateTest {
Samanwita Pale00733a2015-06-23 13:57:11 -070029
Madan Jampanicadd70b2016-02-08 13:45:43 -080030 private final MapUpdate<String, byte[]> stats1 = MapUpdate.<String, byte[]>newBuilder()
Samanwita Pale00733a2015-06-23 13:57:11 -070031 .withCurrentValue("1".getBytes())
32 .withValue("2".getBytes())
33 .withCurrentVersion(3)
34 .withKey("4")
Madan Jampani7804c992015-07-20 13:20:19 -070035 .withMapName("5")
Madan Jampanicadd70b2016-02-08 13:45:43 -080036 .withType(MapUpdate.Type.PUT)
Samanwita Pale00733a2015-06-23 13:57:11 -070037 .build();
38
Madan Jampanicadd70b2016-02-08 13:45:43 -080039 private final MapUpdate<String, byte[]> stats2 = MapUpdate.<String, byte[]>newBuilder()
Samanwita Pale00733a2015-06-23 13:57:11 -070040 .withCurrentValue("1".getBytes())
41 .withValue("2".getBytes())
42 .withCurrentVersion(3)
43 .withKey("4")
Madan Jampani7804c992015-07-20 13:20:19 -070044 .withMapName("5")
Madan Jampanicadd70b2016-02-08 13:45:43 -080045 .withType(MapUpdate.Type.REMOVE)
Samanwita Pale00733a2015-06-23 13:57:11 -070046 .build();
47
Madan Jampanicadd70b2016-02-08 13:45:43 -080048 private final MapUpdate<String, byte[]> stats3 = MapUpdate.<String, byte[]>newBuilder()
Samanwita Pale00733a2015-06-23 13:57:11 -070049 .withCurrentValue("1".getBytes())
50 .withValue("2".getBytes())
51 .withCurrentVersion(3)
52 .withKey("4")
Madan Jampani7804c992015-07-20 13:20:19 -070053 .withMapName("5")
Madan Jampanicadd70b2016-02-08 13:45:43 -080054 .withType(MapUpdate.Type.REMOVE_IF_VALUE_MATCH)
Samanwita Pale00733a2015-06-23 13:57:11 -070055 .build();
56
Madan Jampanicadd70b2016-02-08 13:45:43 -080057 private final MapUpdate<String, byte[]> stats4 = MapUpdate.<String, byte[]>newBuilder()
Samanwita Pale00733a2015-06-23 13:57:11 -070058 .withCurrentValue("1".getBytes())
59 .withValue("2".getBytes())
60 .withCurrentVersion(3)
61 .withKey("4")
Madan Jampani7804c992015-07-20 13:20:19 -070062 .withMapName("5")
Madan Jampanicadd70b2016-02-08 13:45:43 -080063 .withType(MapUpdate.Type.REMOVE_IF_VERSION_MATCH)
Samanwita Pale00733a2015-06-23 13:57:11 -070064 .build();
65
Madan Jampanicadd70b2016-02-08 13:45:43 -080066 private final MapUpdate<String, byte[]> stats5 = MapUpdate.<String, byte[]>newBuilder()
Samanwita Pale00733a2015-06-23 13:57:11 -070067 .withCurrentValue("1".getBytes())
68 .withValue("2".getBytes())
69 .withCurrentVersion(3)
70 .withKey("4")
Madan Jampani7804c992015-07-20 13:20:19 -070071 .withMapName("5")
Madan Jampanicadd70b2016-02-08 13:45:43 -080072 .withType(MapUpdate.Type.PUT_IF_VALUE_MATCH)
Samanwita Pale00733a2015-06-23 13:57:11 -070073 .build();
74
Madan Jampanicadd70b2016-02-08 13:45:43 -080075 private final MapUpdate<String, byte[]> stats6 = MapUpdate.<String, byte[]>newBuilder()
Samanwita Pale00733a2015-06-23 13:57:11 -070076 .withCurrentValue("1".getBytes())
77 .withValue("2".getBytes())
78 .withCurrentVersion(3)
79 .withKey("4")
Madan Jampani7804c992015-07-20 13:20:19 -070080 .withMapName("5")
Madan Jampanicadd70b2016-02-08 13:45:43 -080081 .withType(MapUpdate.Type.PUT_IF_VERSION_MATCH)
Samanwita Pale00733a2015-06-23 13:57:11 -070082 .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"));
Madan Jampanicadd70b2016-02-08 13:45:43 -080094 assertThat(stats1.type(), is(MapUpdate.Type.PUT));
Samanwita Pale00733a2015-06-23 13:57:11 -070095 }
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}