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; |
| 19 | import junit.framework.TestCase; |
| 20 | import org.junit.Test; |
| 21 | |
| 22 | import static org.hamcrest.MatcherAssert.assertThat; |
| 23 | import static org.hamcrest.Matchers.is; |
| 24 | |
| 25 | /** |
| 26 | * Unit Tests for DatabseUpdate class. |
| 27 | */ |
| 28 | |
| 29 | public class DatabaseUpdateTest extends TestCase { |
| 30 | |
| 31 | private final DatabaseUpdate stats1 = DatabaseUpdate.newBuilder() |
| 32 | .withCurrentValue("1".getBytes()) |
| 33 | .withValue("2".getBytes()) |
| 34 | .withCurrentVersion(3) |
| 35 | .withKey("4") |
| 36 | .withTableName("5") |
| 37 | .withType(DatabaseUpdate.Type.PUT) |
| 38 | .build(); |
| 39 | |
| 40 | private final DatabaseUpdate stats2 = DatabaseUpdate.newBuilder() |
| 41 | .withCurrentValue("1".getBytes()) |
| 42 | .withValue("2".getBytes()) |
| 43 | .withCurrentVersion(3) |
| 44 | .withKey("4") |
| 45 | .withTableName("5") |
| 46 | .withType(DatabaseUpdate.Type.REMOVE) |
| 47 | .build(); |
| 48 | |
| 49 | private final DatabaseUpdate stats3 = DatabaseUpdate.newBuilder() |
| 50 | .withCurrentValue("1".getBytes()) |
| 51 | .withValue("2".getBytes()) |
| 52 | .withCurrentVersion(3) |
| 53 | .withKey("4") |
| 54 | .withTableName("5") |
| 55 | .withType(DatabaseUpdate.Type.REMOVE_IF_VALUE_MATCH) |
| 56 | .build(); |
| 57 | |
| 58 | private final DatabaseUpdate stats4 = DatabaseUpdate.newBuilder() |
| 59 | .withCurrentValue("1".getBytes()) |
| 60 | .withValue("2".getBytes()) |
| 61 | .withCurrentVersion(3) |
| 62 | .withKey("4") |
| 63 | .withTableName("5") |
| 64 | .withType(DatabaseUpdate.Type.REMOVE_IF_VERSION_MATCH) |
| 65 | .build(); |
| 66 | |
| 67 | private final DatabaseUpdate stats5 = DatabaseUpdate.newBuilder() |
| 68 | .withCurrentValue("1".getBytes()) |
| 69 | .withValue("2".getBytes()) |
| 70 | .withCurrentVersion(3) |
| 71 | .withKey("4") |
| 72 | .withTableName("5") |
| 73 | .withType(DatabaseUpdate.Type.PUT_IF_VALUE_MATCH) |
| 74 | .build(); |
| 75 | |
| 76 | private final DatabaseUpdate stats6 = DatabaseUpdate.newBuilder() |
| 77 | .withCurrentValue("1".getBytes()) |
| 78 | .withValue("2".getBytes()) |
| 79 | .withCurrentVersion(3) |
| 80 | .withKey("4") |
| 81 | .withTableName("5") |
| 82 | .withType(DatabaseUpdate.Type.PUT_IF_VERSION_MATCH) |
| 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")); |
| 94 | assertThat(stats1.tableName(), is("5")); |
| 95 | assertThat(stats1.type(), is(DatabaseUpdate.Type.PUT)); |
| 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 | |
| 127 | } |