Skip to content

Commit

Permalink
Improves code coverage
Browse files Browse the repository at this point in the history
Signed-off-by: Peter Alfonsi <[email protected]>
  • Loading branch information
Peter Alfonsi committed Apr 12, 2024
1 parent 5283470 commit 54e12a3
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 1 deletion.
11 changes: 10 additions & 1 deletion server/src/main/java/org/opensearch/common/cache/ICacheKey.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,16 @@ public boolean equals(Object o) {
return false;

Check warning on line 57 in server/src/main/java/org/opensearch/common/cache/ICacheKey.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/common/cache/ICacheKey.java#L57

Added line #L57 was not covered by tests
}
ICacheKey other = (ICacheKey) o;
return key.equals(other.key) && dimensions.equals(other.dimensions);
if (!dimensions.equals(other.dimensions)) {
return false;

Check warning on line 61 in server/src/main/java/org/opensearch/common/cache/ICacheKey.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/common/cache/ICacheKey.java#L61

Added line #L61 was not covered by tests
}
if (this.key == null && other.key == null) {
return true;
}
if (this.key == null || other.key == null) {
return false;
}
return this.key.equals(other.key);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,17 @@ public void testHashCodes() throws Exception {
ICacheKey<String> key1 = new ICacheKey<>("key", List.of("dimension_value"));
ICacheKey<String> key2 = new ICacheKey<>("key", List.of("dimension_value"));

ICacheKey<String> key3 = new ICacheKey<>(null, List.of("dimension_value"));
ICacheKey<String> key4 = new ICacheKey<>(null, List.of("dimension_value"));

assertEquals(key1, key2);
assertEquals(key1.hashCode(), key2.hashCode());

assertEquals(key3, key4);
assertEquals(key3.hashCode(), key4.hashCode());

assertNotEquals(key1, key3);
assertNotEquals("string", key3);
}

public void testNullInputs() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

package org.opensearch.common.cache.stats;

import org.opensearch.common.io.stream.BytesStreamOutput;
import org.opensearch.core.common.bytes.BytesReference;
import org.opensearch.core.common.io.stream.BytesStreamInput;
import org.opensearch.test.OpenSearchTestCase;

public class ImmutableCacheStatsTests extends OpenSearchTestCase {
public void testSerialization() throws Exception {
ImmutableCacheStats immutableCacheStats = new ImmutableCacheStats(1, 2, 3, 4, 5);
BytesStreamOutput os = new BytesStreamOutput();
immutableCacheStats.writeTo(os);
BytesStreamInput is = new BytesStreamInput(BytesReference.toBytes(os.bytes()));
ImmutableCacheStats deserialized = new ImmutableCacheStats(is);

assertEquals(immutableCacheStats, deserialized);
}

public void testAddSnapshots() throws Exception {
ImmutableCacheStats ics1 = new ImmutableCacheStats(1, 2, 3, 4, 5);
ImmutableCacheStats ics2 = new ImmutableCacheStats(6, 7, 8, 9, 10);
ImmutableCacheStats expected = new ImmutableCacheStats(7, 9, 11, 13, 15);
assertEquals(expected, ImmutableCacheStats.addSnapshots(ics1, ics2));
}

public void testEqualsAndHash() throws Exception {
ImmutableCacheStats ics1 = new ImmutableCacheStats(1, 2, 3, 4, 5);
ImmutableCacheStats ics2 = new ImmutableCacheStats(1, 2, 3, 4, 5);
ImmutableCacheStats ics3 = new ImmutableCacheStats(0, 2, 3, 4, 5);

assertEquals(ics1, ics2);
assertNotEquals(ics1, ics3);
assertNotEquals(ics1, null);
assertNotEquals(ics1, "string");

assertEquals(ics1.hashCode(), ics2.hashCode());
assertNotEquals(ics1.hashCode(), ics3.hashCode());
}
}

0 comments on commit 54e12a3

Please sign in to comment.