Skip to content

Commit

Permalink
Improve coverage
Browse files Browse the repository at this point in the history
Signed-off-by: Bilyana Gospodinova <[email protected]>
  • Loading branch information
bilyana-gospodinova committed Nov 12, 2024
1 parent 49cdcfb commit bdefa11
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,4 @@ public boolean equals(Object o) {
public int hashCode() {
return Objects.hash(getStateKey(), backingStore);
}

@Override
public String toString() {
return "MapReadableKVState{" + "backingStore=" + backingStore + '}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,6 @@ public long sizeOfDataSource() {
return backingStore.size();
}

@Override
public String toString() {
return "MapWritableKVState{" + "backingStore=" + backingStore + '}';
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,4 @@ public void commit() {
onCommit.run();
}
}

@Override
public String toString() {
return "MapWritableStates{" + "states=" + states + '}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,11 @@ void testReadFromDataSourceReturnsCorrectValue() {

@Test
void testIterateFromDataSourceReturnsEmptyIterator() {
final var backingStore = new HashMap<AccountID, Account>();
final var mapWritableKVState = new MapWritableKVState<>("ACCOUNTS", backingStore);
mapWritableKVState.putIntoDataSource(accountID, account);
assertThat(mapWritableKVState.iterateFromDataSource().next())
.isEqualTo(backingStore.keySet().iterator().next());
final var map = new HashMap<AccountID, Account>();
final var kvState = new MapWritableKVState<>("ACCOUNTS", map);
kvState.putIntoDataSource(accountID, account);
assertThat(kvState.iterateFromDataSource().next())
.isEqualTo(map.keySet().iterator().next());
}

@Test
Expand Down Expand Up @@ -116,4 +116,23 @@ void testEqualsDifferentType() {
void testEqualsWithNull() {
assertThat(mapWritableKVState).isNotEqualTo(null);
}

@Test
void testEqualsDifferentKeys() {
MapWritableKVState<AccountID, Account> other = new MapWritableKVState<>("ALIASES", backingStore);
assertThat(mapWritableKVState).isNotEqualTo(other);
}

@Test
void testEqualsDifferentValues() {
final var accountMapOther = Map.of(AccountID.newBuilder().accountNum(3L).build(), account);
MapWritableKVState<AccountID, Account> other = new MapWritableKVState<>("ACCOUNTS", accountMapOther);
assertThat(mapWritableKVState).isNotEqualTo(other);
}

@Test
void testHashCode() {
MapWritableKVState<AccountID, Account> other = new MapWritableKVState<>("ACCOUNTS", backingStore);
assertThat(mapWritableKVState).hasSameHashCodeAs(other);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

import com.swirlds.state.spi.WritableKVState;
import com.swirlds.state.spi.WritableQueueState;
import com.swirlds.state.spi.WritableSingletonState;
import com.swirlds.state.spi.WritableKVStateBase;
import com.swirlds.state.spi.WritableQueueStateBase;
import com.swirlds.state.spi.WritableSingletonStateBase;
import java.util.Map;
import java.util.Set;
import org.junit.jupiter.api.BeforeEach;
Expand All @@ -36,13 +38,13 @@ class MapWritableStatesTest {
private MapWritableStates states;

@Mock
private WritableKVState<String, String> kvStateMock;
private WritableKVStateBase<String, String> kvStateMock;

@Mock
private WritableSingletonState<String> singletonStateMock;
private WritableSingletonStateBase<String> singletonStateMock;

@Mock
private WritableQueueState<String> queueStateMock;
private WritableQueueStateBase<String> queueStateMock;

private static final String KV_STATE_KEY = "kvState";
private static final String SINGLETON_KEY = "singleton";
Expand Down Expand Up @@ -139,4 +141,22 @@ void testHashCode() {
Map.of(KV_STATE_KEY, kvStateMock, SINGLETON_KEY, singletonStateMock, QUEUE_KEY, queueStateMock));
assertThat(states).hasSameHashCodeAs(other);
}

@Test
void testCommit() {
final Runnable onCommit = () -> {};
final var state = new MapWritableStates(
Map.of(KV_STATE_KEY, kvStateMock, SINGLETON_KEY, singletonStateMock, QUEUE_KEY, queueStateMock),
onCommit);
state.commit();
verify(kvStateMock, times(1)).commit();
verify(singletonStateMock, times(1)).commit();
verify(queueStateMock, times(1)).commit();
}

@Test
void testCommitUnknownValue() {
final var state = new MapWritableStates(Map.of("other", new Object()));
assertThatThrownBy(state::commit).isInstanceOf(IllegalStateException.class);
}
}

0 comments on commit bdefa11

Please sign in to comment.