Samanwita Pal | e00733a | 2015-06-23 13:57:11 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.store.service; |
| 17 | |
| 18 | import com.google.common.testing.EqualsTester; |
Samanwita Pal | e00733a | 2015-06-23 13:57:11 -0700 | [diff] [blame] | 19 | import org.junit.Test; |
| 20 | |
| 21 | import static org.hamcrest.MatcherAssert.assertThat; |
| 22 | import static org.hamcrest.Matchers.is; |
| 23 | |
| 24 | /** |
| 25 | * Unit Tests for DatabseUpdate class. |
| 26 | */ |
| 27 | |
Sho SHIMIZU | 8dc81ea | 2015-09-10 10:59:43 -0700 | [diff] [blame] | 28 | public class DatabaseUpdateTest { |
Samanwita Pal | e00733a | 2015-06-23 13:57:11 -0700 | [diff] [blame] | 29 | |
| 30 | private final DatabaseUpdate stats1 = DatabaseUpdate.newBuilder() |
| 31 | .withCurrentValue("1".getBytes()) |
| 32 | .withValue("2".getBytes()) |
| 33 | .withCurrentVersion(3) |
| 34 | .withKey("4") |
Madan Jampani | 7804c99 | 2015-07-20 13:20:19 -0700 | [diff] [blame] | 35 | .withMapName("5") |
Samanwita Pal | e00733a | 2015-06-23 13:57:11 -0700 | [diff] [blame] | 36 | .withType(DatabaseUpdate.Type.PUT) |
| 37 | .build(); |
| 38 | |
| 39 | private final DatabaseUpdate stats2 = DatabaseUpdate.newBuilder() |
| 40 | .withCurrentValue("1".getBytes()) |
| 41 | .withValue("2".getBytes()) |
| 42 | .withCurrentVersion(3) |
| 43 | .withKey("4") |
Madan Jampani | 7804c99 | 2015-07-20 13:20:19 -0700 | [diff] [blame] | 44 | .withMapName("5") |
Samanwita Pal | e00733a | 2015-06-23 13:57:11 -0700 | [diff] [blame] | 45 | .withType(DatabaseUpdate.Type.REMOVE) |
| 46 | .build(); |
| 47 | |
| 48 | private final DatabaseUpdate stats3 = DatabaseUpdate.newBuilder() |
| 49 | .withCurrentValue("1".getBytes()) |
| 50 | .withValue("2".getBytes()) |
| 51 | .withCurrentVersion(3) |
| 52 | .withKey("4") |
Madan Jampani | 7804c99 | 2015-07-20 13:20:19 -0700 | [diff] [blame] | 53 | .withMapName("5") |
Samanwita Pal | e00733a | 2015-06-23 13:57:11 -0700 | [diff] [blame] | 54 | .withType(DatabaseUpdate.Type.REMOVE_IF_VALUE_MATCH) |
| 55 | .build(); |
| 56 | |
| 57 | private final DatabaseUpdate stats4 = DatabaseUpdate.newBuilder() |
| 58 | .withCurrentValue("1".getBytes()) |
| 59 | .withValue("2".getBytes()) |
| 60 | .withCurrentVersion(3) |
| 61 | .withKey("4") |
Madan Jampani | 7804c99 | 2015-07-20 13:20:19 -0700 | [diff] [blame] | 62 | .withMapName("5") |
Samanwita Pal | e00733a | 2015-06-23 13:57:11 -0700 | [diff] [blame] | 63 | .withType(DatabaseUpdate.Type.REMOVE_IF_VERSION_MATCH) |
| 64 | .build(); |
| 65 | |
| 66 | private final DatabaseUpdate stats5 = DatabaseUpdate.newBuilder() |
| 67 | .withCurrentValue("1".getBytes()) |
| 68 | .withValue("2".getBytes()) |
| 69 | .withCurrentVersion(3) |
| 70 | .withKey("4") |
Madan Jampani | 7804c99 | 2015-07-20 13:20:19 -0700 | [diff] [blame] | 71 | .withMapName("5") |
Samanwita Pal | e00733a | 2015-06-23 13:57:11 -0700 | [diff] [blame] | 72 | .withType(DatabaseUpdate.Type.PUT_IF_VALUE_MATCH) |
| 73 | .build(); |
| 74 | |
| 75 | private final DatabaseUpdate stats6 = DatabaseUpdate.newBuilder() |
| 76 | .withCurrentValue("1".getBytes()) |
| 77 | .withValue("2".getBytes()) |
| 78 | .withCurrentVersion(3) |
| 79 | .withKey("4") |
Madan Jampani | 7804c99 | 2015-07-20 13:20:19 -0700 | [diff] [blame] | 80 | .withMapName("5") |
Samanwita Pal | e00733a | 2015-06-23 13:57:11 -0700 | [diff] [blame] | 81 | .withType(DatabaseUpdate.Type.PUT_IF_VERSION_MATCH) |
| 82 | .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 Jampani | 7804c99 | 2015-07-20 13:20:19 -0700 | [diff] [blame] | 93 | assertThat(stats1.mapName(), is("5")); |
Samanwita Pal | e00733a | 2015-06-23 13:57:11 -0700 | [diff] [blame] | 94 | assertThat(stats1.type(), is(DatabaseUpdate.Type.PUT)); |
| 95 | } |
| 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 SHIMIZU | 8dc81ea | 2015-09-10 10:59:43 -0700 | [diff] [blame] | 126 | } |