Samanwita Pal | e00733a | 2015-06-23 13:57:11 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Samanwita Pal | e00733a | 2015-06-23 13:57:11 -0700 | [diff] [blame] | 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 Jampani | 74da78b | 2016-02-09 21:18:36 -0800 | [diff] [blame] | 16 | package org.onosproject.store.primitives; |
Samanwita Pal | e00733a | 2015-06-23 13:57:11 -0700 | [diff] [blame] | 17 | |
| 18 | import com.google.common.testing.EqualsTester; |
Madan Jampani | 74da78b | 2016-02-09 21:18:36 -0800 | [diff] [blame] | 19 | |
Samanwita Pal | e00733a | 2015-06-23 13:57:11 -0700 | [diff] [blame] | 20 | import org.junit.Test; |
| 21 | |
| 22 | import static org.hamcrest.MatcherAssert.assertThat; |
| 23 | import static org.hamcrest.Matchers.is; |
| 24 | |
| 25 | /** |
Madan Jampani | cadd70b | 2016-02-08 13:45:43 -0800 | [diff] [blame] | 26 | * Unit Tests for MapUpdate class. |
Samanwita Pal | e00733a | 2015-06-23 13:57:11 -0700 | [diff] [blame] | 27 | */ |
| 28 | |
Madan Jampani | cadd70b | 2016-02-08 13:45:43 -0800 | [diff] [blame] | 29 | public class MapUpdateTest { |
Samanwita Pal | e00733a | 2015-06-23 13:57:11 -0700 | [diff] [blame] | 30 | |
Madan Jampani | cadd70b | 2016-02-08 13:45:43 -0800 | [diff] [blame] | 31 | private final MapUpdate<String, byte[]> stats1 = MapUpdate.<String, byte[]>newBuilder() |
Samanwita Pal | e00733a | 2015-06-23 13:57:11 -0700 | [diff] [blame] | 32 | .withValue("2".getBytes()) |
Jordan Halterman | 5f97a30 | 2017-04-26 23:41:31 -0700 | [diff] [blame] | 33 | .withVersion(3) |
Samanwita Pal | e00733a | 2015-06-23 13:57:11 -0700 | [diff] [blame] | 34 | .withKey("4") |
Jordan Halterman | 5f97a30 | 2017-04-26 23:41:31 -0700 | [diff] [blame] | 35 | .withType(MapUpdate.Type.PUT_IF_VERSION_MATCH) |
Samanwita Pal | e00733a | 2015-06-23 13:57:11 -0700 | [diff] [blame] | 36 | .build(); |
| 37 | |
Madan Jampani | cadd70b | 2016-02-08 13:45:43 -0800 | [diff] [blame] | 38 | private final MapUpdate<String, byte[]> stats2 = MapUpdate.<String, byte[]>newBuilder() |
Jordan Halterman | 5f97a30 | 2017-04-26 23:41:31 -0700 | [diff] [blame] | 39 | .withType(MapUpdate.Type.VERSION_MATCH) |
| 40 | .withVersion(10) |
Samanwita Pal | e00733a | 2015-06-23 13:57:11 -0700 | [diff] [blame] | 41 | .build(); |
| 42 | |
Madan Jampani | cadd70b | 2016-02-08 13:45:43 -0800 | [diff] [blame] | 43 | private final MapUpdate<String, byte[]> stats3 = MapUpdate.<String, byte[]>newBuilder() |
Samanwita Pal | e00733a | 2015-06-23 13:57:11 -0700 | [diff] [blame] | 44 | .withValue("2".getBytes()) |
Jordan Halterman | 5f97a30 | 2017-04-26 23:41:31 -0700 | [diff] [blame] | 45 | .withVersion(3) |
Samanwita Pal | e00733a | 2015-06-23 13:57:11 -0700 | [diff] [blame] | 46 | .withKey("4") |
Madan Jampani | cadd70b | 2016-02-08 13:45:43 -0800 | [diff] [blame] | 47 | .withType(MapUpdate.Type.REMOVE_IF_VERSION_MATCH) |
Samanwita Pal | e00733a | 2015-06-23 13:57:11 -0700 | [diff] [blame] | 48 | .build(); |
| 49 | |
Jordan Halterman | 5f97a30 | 2017-04-26 23:41:31 -0700 | [diff] [blame] | 50 | private final MapUpdate<String, byte[]> stats4 = MapUpdate.<String, byte[]>newBuilder() |
Samanwita Pal | e00733a | 2015-06-23 13:57:11 -0700 | [diff] [blame] | 51 | .withValue("2".getBytes()) |
Jordan Halterman | 5f97a30 | 2017-04-26 23:41:31 -0700 | [diff] [blame] | 52 | .withVersion(3) |
Samanwita Pal | e00733a | 2015-06-23 13:57:11 -0700 | [diff] [blame] | 53 | .withKey("4") |
Madan Jampani | cadd70b | 2016-02-08 13:45:43 -0800 | [diff] [blame] | 54 | .withType(MapUpdate.Type.PUT_IF_VERSION_MATCH) |
Samanwita Pal | e00733a | 2015-06-23 13:57:11 -0700 | [diff] [blame] | 55 | .build(); |
| 56 | |
| 57 | /** |
| 58 | * Tests the constructor for the class. |
| 59 | */ |
| 60 | @Test |
| 61 | public void testConstruction() { |
Samanwita Pal | e00733a | 2015-06-23 13:57:11 -0700 | [diff] [blame] | 62 | assertThat(stats1.value(), is("2".getBytes())); |
Jordan Halterman | 5f97a30 | 2017-04-26 23:41:31 -0700 | [diff] [blame] | 63 | assertThat(stats1.version(), is(3L)); |
Samanwita Pal | e00733a | 2015-06-23 13:57:11 -0700 | [diff] [blame] | 64 | assertThat(stats1.key(), is("4")); |
Jordan Halterman | 5f97a30 | 2017-04-26 23:41:31 -0700 | [diff] [blame] | 65 | assertThat(stats1.type(), is(MapUpdate.Type.PUT_IF_VERSION_MATCH)); |
Samanwita Pal | e00733a | 2015-06-23 13:57:11 -0700 | [diff] [blame] | 66 | } |
| 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 Pal | e00733a | 2015-06-23 13:57:11 -0700 | [diff] [blame] | 82 | } |
| 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 SHIMIZU | 8dc81ea | 2015-09-10 10:59:43 -0700 | [diff] [blame] | 92 | } |