Samanwita Pal | e00733a | 2015-06-23 13:57:11 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
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 | .withCurrentValue("1".getBytes()) |
| 33 | .withValue("2".getBytes()) |
| 34 | .withCurrentVersion(3) |
| 35 | .withKey("4") |
Madan Jampani | 7804c99 | 2015-07-20 13:20:19 -0700 | [diff] [blame] | 36 | .withMapName("5") |
Madan Jampani | cadd70b | 2016-02-08 13:45:43 -0800 | [diff] [blame] | 37 | .withType(MapUpdate.Type.PUT) |
Samanwita Pal | e00733a | 2015-06-23 13:57:11 -0700 | [diff] [blame] | 38 | .build(); |
| 39 | |
Madan Jampani | cadd70b | 2016-02-08 13:45:43 -0800 | [diff] [blame] | 40 | private final MapUpdate<String, byte[]> stats2 = MapUpdate.<String, byte[]>newBuilder() |
Samanwita Pal | e00733a | 2015-06-23 13:57:11 -0700 | [diff] [blame] | 41 | .withCurrentValue("1".getBytes()) |
| 42 | .withValue("2".getBytes()) |
| 43 | .withCurrentVersion(3) |
| 44 | .withKey("4") |
Madan Jampani | 7804c99 | 2015-07-20 13:20:19 -0700 | [diff] [blame] | 45 | .withMapName("5") |
Madan Jampani | cadd70b | 2016-02-08 13:45:43 -0800 | [diff] [blame] | 46 | .withType(MapUpdate.Type.REMOVE) |
Samanwita Pal | e00733a | 2015-06-23 13:57:11 -0700 | [diff] [blame] | 47 | .build(); |
| 48 | |
Madan Jampani | cadd70b | 2016-02-08 13:45:43 -0800 | [diff] [blame] | 49 | private final MapUpdate<String, byte[]> stats3 = MapUpdate.<String, byte[]>newBuilder() |
Samanwita Pal | e00733a | 2015-06-23 13:57:11 -0700 | [diff] [blame] | 50 | .withCurrentValue("1".getBytes()) |
| 51 | .withValue("2".getBytes()) |
| 52 | .withCurrentVersion(3) |
| 53 | .withKey("4") |
Madan Jampani | 7804c99 | 2015-07-20 13:20:19 -0700 | [diff] [blame] | 54 | .withMapName("5") |
Madan Jampani | cadd70b | 2016-02-08 13:45:43 -0800 | [diff] [blame] | 55 | .withType(MapUpdate.Type.REMOVE_IF_VALUE_MATCH) |
Samanwita Pal | e00733a | 2015-06-23 13:57:11 -0700 | [diff] [blame] | 56 | .build(); |
| 57 | |
Madan Jampani | cadd70b | 2016-02-08 13:45:43 -0800 | [diff] [blame] | 58 | private final MapUpdate<String, byte[]> stats4 = MapUpdate.<String, byte[]>newBuilder() |
Samanwita Pal | e00733a | 2015-06-23 13:57:11 -0700 | [diff] [blame] | 59 | .withCurrentValue("1".getBytes()) |
| 60 | .withValue("2".getBytes()) |
| 61 | .withCurrentVersion(3) |
| 62 | .withKey("4") |
Madan Jampani | 7804c99 | 2015-07-20 13:20:19 -0700 | [diff] [blame] | 63 | .withMapName("5") |
Madan Jampani | cadd70b | 2016-02-08 13:45:43 -0800 | [diff] [blame] | 64 | .withType(MapUpdate.Type.REMOVE_IF_VERSION_MATCH) |
Samanwita Pal | e00733a | 2015-06-23 13:57:11 -0700 | [diff] [blame] | 65 | .build(); |
| 66 | |
Madan Jampani | cadd70b | 2016-02-08 13:45:43 -0800 | [diff] [blame] | 67 | private final MapUpdate<String, byte[]> stats5 = MapUpdate.<String, byte[]>newBuilder() |
Samanwita Pal | e00733a | 2015-06-23 13:57:11 -0700 | [diff] [blame] | 68 | .withCurrentValue("1".getBytes()) |
| 69 | .withValue("2".getBytes()) |
| 70 | .withCurrentVersion(3) |
| 71 | .withKey("4") |
Madan Jampani | 7804c99 | 2015-07-20 13:20:19 -0700 | [diff] [blame] | 72 | .withMapName("5") |
Madan Jampani | cadd70b | 2016-02-08 13:45:43 -0800 | [diff] [blame] | 73 | .withType(MapUpdate.Type.PUT_IF_VALUE_MATCH) |
Samanwita Pal | e00733a | 2015-06-23 13:57:11 -0700 | [diff] [blame] | 74 | .build(); |
| 75 | |
Madan Jampani | cadd70b | 2016-02-08 13:45:43 -0800 | [diff] [blame] | 76 | private final MapUpdate<String, byte[]> stats6 = MapUpdate.<String, byte[]>newBuilder() |
Samanwita Pal | e00733a | 2015-06-23 13:57:11 -0700 | [diff] [blame] | 77 | .withCurrentValue("1".getBytes()) |
| 78 | .withValue("2".getBytes()) |
| 79 | .withCurrentVersion(3) |
| 80 | .withKey("4") |
Madan Jampani | 7804c99 | 2015-07-20 13:20:19 -0700 | [diff] [blame] | 81 | .withMapName("5") |
Madan Jampani | cadd70b | 2016-02-08 13:45:43 -0800 | [diff] [blame] | 82 | .withType(MapUpdate.Type.PUT_IF_VERSION_MATCH) |
Samanwita Pal | e00733a | 2015-06-23 13:57:11 -0700 | [diff] [blame] | 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 Jampani | 7804c99 | 2015-07-20 13:20:19 -0700 | [diff] [blame] | 94 | assertThat(stats1.mapName(), is("5")); |
Madan Jampani | cadd70b | 2016-02-08 13:45:43 -0800 | [diff] [blame] | 95 | assertThat(stats1.type(), is(MapUpdate.Type.PUT)); |
Samanwita Pal | e00733a | 2015-06-23 13:57:11 -0700 | [diff] [blame] | 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 | |
Sho SHIMIZU | 8dc81ea | 2015-09-10 10:59:43 -0700 | [diff] [blame] | 127 | } |