blob: 024bff74c2db8010b66151ec0255992c64a44fa1 [file] [log] [blame]
Samanwita Pale00733a2015-06-23 13:57:11 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Samanwita Pale00733a2015-06-23 13:57:11 -07003 *
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 .withValue("2".getBytes())
Jordan Halterman5f97a302017-04-26 23:41:31 -070033 .withVersion(3)
Samanwita Pale00733a2015-06-23 13:57:11 -070034 .withKey("4")
Jordan Halterman5f97a302017-04-26 23:41:31 -070035 .withType(MapUpdate.Type.PUT_IF_VERSION_MATCH)
Samanwita Pale00733a2015-06-23 13:57:11 -070036 .build();
37
Madan Jampanicadd70b2016-02-08 13:45:43 -080038 private final MapUpdate<String, byte[]> stats2 = MapUpdate.<String, byte[]>newBuilder()
Jordan Halterman5f97a302017-04-26 23:41:31 -070039 .withType(MapUpdate.Type.VERSION_MATCH)
40 .withVersion(10)
Samanwita Pale00733a2015-06-23 13:57:11 -070041 .build();
42
Madan Jampanicadd70b2016-02-08 13:45:43 -080043 private final MapUpdate<String, byte[]> stats3 = MapUpdate.<String, byte[]>newBuilder()
Samanwita Pale00733a2015-06-23 13:57:11 -070044 .withValue("2".getBytes())
Jordan Halterman5f97a302017-04-26 23:41:31 -070045 .withVersion(3)
Samanwita Pale00733a2015-06-23 13:57:11 -070046 .withKey("4")
Madan Jampanicadd70b2016-02-08 13:45:43 -080047 .withType(MapUpdate.Type.REMOVE_IF_VERSION_MATCH)
Samanwita Pale00733a2015-06-23 13:57:11 -070048 .build();
49
Jordan Halterman5f97a302017-04-26 23:41:31 -070050 private final MapUpdate<String, byte[]> stats4 = MapUpdate.<String, byte[]>newBuilder()
Samanwita Pale00733a2015-06-23 13:57:11 -070051 .withValue("2".getBytes())
Jordan Halterman5f97a302017-04-26 23:41:31 -070052 .withVersion(3)
Samanwita Pale00733a2015-06-23 13:57:11 -070053 .withKey("4")
Madan Jampanicadd70b2016-02-08 13:45:43 -080054 .withType(MapUpdate.Type.PUT_IF_VERSION_MATCH)
Samanwita Pale00733a2015-06-23 13:57:11 -070055 .build();
56
57 /**
58 * Tests the constructor for the class.
59 */
60 @Test
61 public void testConstruction() {
Samanwita Pale00733a2015-06-23 13:57:11 -070062 assertThat(stats1.value(), is("2".getBytes()));
Jordan Halterman5f97a302017-04-26 23:41:31 -070063 assertThat(stats1.version(), is(3L));
Samanwita Pale00733a2015-06-23 13:57:11 -070064 assertThat(stats1.key(), is("4"));
Jordan Halterman5f97a302017-04-26 23:41:31 -070065 assertThat(stats1.type(), is(MapUpdate.Type.PUT_IF_VERSION_MATCH));
Samanwita Pale00733a2015-06-23 13:57:11 -070066 }
67
68 /**
69 * Tests the equals, hashCode and toString methods using Guava EqualsTester.
70 */
71 @Test
72 public void testEquals() {
73 new EqualsTester()
74 .addEqualityGroup(stats1, stats1)
75 .addEqualityGroup(stats2)
76 .testEquals();
77
78 new EqualsTester()
79 .addEqualityGroup(stats3, stats3)
80 .addEqualityGroup(stats4)
81 .testEquals();
Samanwita Pale00733a2015-06-23 13:57:11 -070082 }
83
84 /**
85 * Tests if the toString method returns a consistent value for hashing.
86 */
87 @Test
88 public void testToString() {
89 assertThat(stats1.toString(), is(stats1.toString()));
90 }
91
Sho SHIMIZU8dc81ea2015-09-10 10:59:43 -070092}