From fa57b077aa9bd41da5e4289be0462118cdfb621d Mon Sep 17 00:00:00 2001 From: Bilyana Gospodinova Date: Tue, 12 Nov 2024 10:14:39 +0200 Subject: [PATCH] Increase coverage Signed-off-by: Bilyana Gospodinova --- .../web3/state/MirrorNodeStateTest.java | 117 ++++++++++++++++++ .../state/utils/MapWritableStatesTest.java | 14 ++- 2 files changed, 128 insertions(+), 3 deletions(-) diff --git a/hedera-mirror-web3/src/test/java/com/hedera/mirror/web3/state/MirrorNodeStateTest.java b/hedera-mirror-web3/src/test/java/com/hedera/mirror/web3/state/MirrorNodeStateTest.java index 40cceb246a..8dfc1ebfdb 100644 --- a/hedera-mirror-web3/src/test/java/com/hedera/mirror/web3/state/MirrorNodeStateTest.java +++ b/hedera-mirror-web3/src/test/java/com/hedera/mirror/web3/state/MirrorNodeStateTest.java @@ -17,11 +17,15 @@ package com.hedera.mirror.web3.state; import static org.assertj.core.api.AssertionsForClassTypes.assertThat; +import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; +import com.hedera.hapi.node.base.FileID; +import com.hedera.hapi.node.state.file.File; +import com.hedera.mirror.web3.state.utils.ListWritableQueueState; import com.hedera.mirror.web3.state.utils.MapReadableKVState; import com.hedera.mirror.web3.state.utils.MapReadableStates; import com.hedera.mirror.web3.state.utils.MapWritableKVState; @@ -30,8 +34,13 @@ import com.hedera.node.app.service.contract.ContractService; import com.hedera.node.app.service.file.FileService; import com.hedera.node.app.service.token.TokenService; +import com.hedera.node.app.spi.validation.TruePredicate; import com.swirlds.state.StateChangeListener; import com.swirlds.state.StateChangeListener.StateType; +import com.swirlds.state.spi.KVChangeListener; +import com.swirlds.state.spi.QueueChangeListener; +import com.swirlds.state.spi.SingletonChangeListener; +import com.swirlds.state.spi.WritableSingletonStateBase; import com.swirlds.state.spi.WritableStates; import java.util.HashMap; import java.util.Map; @@ -46,6 +55,7 @@ import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; +@SuppressWarnings("unchecked") @ExtendWith(MockitoExtension.class) class MirrorNodeStateTest { @@ -302,6 +312,113 @@ void testCommit() { verify(mockMapWritableState, times(1)).commit(); } + @Test + void testCommitWithUpdateKVListener() { + final var state = buildTestState(); + final var writableKVState = new MapWritableKVState("FILES", new HashMap<>()); + final var backingStore = new HashMap(); + backingStore.put("FILES", writableKVState); + final var mapWritableStates = new MapWritableStates(backingStore); + Map writableStates = new ConcurrentHashMap<>(); + writableStates.put(FileService.NAME, mapWritableStates); + writableKVState.put(FileID.DEFAULT, File.DEFAULT); + + final var mockListener = mock(KVChangeListener.class); + writableKVState.registerListener(mockListener); + state.registerCommitListener(listener); + state.setWritableStates(writableStates); + state.commit(); + + verify(mockListener, times(1)).mapUpdateChange(any(), any()); + } + + @Test + void testCommitWithDeleteKVListener() { + final var state = buildTestState(); + final var writableKVState = new MapWritableKVState("FILES", new HashMap<>()); + final var backingStore = new HashMap(); + backingStore.put("FILES", writableKVState); + final var mapWritableStates = new MapWritableStates(backingStore); + Map writableStates = new ConcurrentHashMap<>(); + writableStates.put(FileService.NAME, mapWritableStates); + writableKVState.put(FileID.DEFAULT, File.DEFAULT); + writableKVState.remove(FileID.DEFAULT); + + final var mockListener = mock(KVChangeListener.class); + writableKVState.registerListener(mockListener); + state.registerCommitListener(listener); + state.setWritableStates(writableStates); + state.commit(); + + verify(mockListener, times(1)).mapDeleteChange(any()); + } + + @Test + void testCommitWithUpdateSingletonListener() { + final var state = buildTestState(); + final var ref = new AtomicReference<>(); + final var backingStore = new HashMap(); + final var writableState = new WritableSingletonStateBase<>("FILES", ref::get, ref::set); + backingStore.put("FILES", writableState); + final var mapWritableStates = new MapWritableStates(backingStore); + Map writableStates = new ConcurrentHashMap<>(); + writableStates.put(FileService.NAME, mapWritableStates); + writableState.put(1L); + + final var mockListener = mock(SingletonChangeListener.class); + writableState.registerListener(mockListener); + state.registerCommitListener(listener); + state.setWritableStates(writableStates); + state.commit(); + + verify(mockListener, times(1)).singletonUpdateChange(any()); + } + + @Test + void testCommitWithQueuePushListener() { + final var state = buildTestState(); + final var writableQueue = new ConcurrentLinkedDeque<>(); + final var backingStore = new HashMap(); + final var writableState = new ListWritableQueueState<>("FILES", writableQueue); + backingStore.put("FILES", writableState); + final var mapWritableStates = new MapWritableStates(backingStore); + Map writableStates = new ConcurrentHashMap<>(); + writableStates.put(FileService.NAME, mapWritableStates); + writableState.add(1L); + + final var mockListener = mock(QueueChangeListener.class); + writableState.registerListener(mockListener); + state.registerCommitListener(listener); + state.setWritableStates(writableStates); + state.commit(); + + verify(mockListener, times(1)).queuePushChange(any()); + } + + @Test + void testCommitWithQueuePopListener() { + final var state = buildTestState(); + final var backingStore = new HashMap(); + final var writableQueue = new ConcurrentLinkedDeque<>(); + final var writableState = new ListWritableQueueState<>("FILES", writableQueue); + backingStore.put("FILES", writableState); + writableQueue.push(1L); + final var mapWritableStates = new MapWritableStates(backingStore); + Map writableStates = new ConcurrentHashMap<>(); + writableStates.put(FileService.NAME, mapWritableStates); + writableState.add(1L); + writableState.peek(); + writableState.removeIf(TruePredicate.INSTANCE); + + final var mockListener = mock(QueueChangeListener.class); + writableState.registerListener(mockListener); + state.registerCommitListener(listener); + state.setWritableStates(writableStates); + state.commit(); + + verify(mockListener, times(1)).queuePopChange(); + } + @Test void testEqualsSameInstance() { assertThat(mirrorNodeState).isEqualTo(mirrorNodeState); diff --git a/hedera-mirror-web3/src/test/java/com/hedera/mirror/web3/state/utils/MapWritableStatesTest.java b/hedera-mirror-web3/src/test/java/com/hedera/mirror/web3/state/utils/MapWritableStatesTest.java index 0f2b6fb7a8..0905d44db6 100644 --- a/hedera-mirror-web3/src/test/java/com/hedera/mirror/web3/state/utils/MapWritableStatesTest.java +++ b/hedera-mirror-web3/src/test/java/com/hedera/mirror/web3/state/utils/MapWritableStatesTest.java @@ -18,6 +18,7 @@ import static org.assertj.core.api.AssertionsForClassTypes.assertThat; import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy; +import static org.mockito.Mockito.mock; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; @@ -144,16 +145,23 @@ void testHashCode() { @Test void testCommit() { - final Runnable onCommit = () -> {}; final var state = new MapWritableStates( - Map.of(KV_STATE_KEY, kvStateMock, SINGLETON_KEY, singletonStateMock, QUEUE_KEY, queueStateMock), - onCommit); + Map.of(KV_STATE_KEY, kvStateMock, SINGLETON_KEY, singletonStateMock, QUEUE_KEY, queueStateMock)); state.commit(); verify(kvStateMock, times(1)).commit(); verify(singletonStateMock, times(1)).commit(); verify(queueStateMock, times(1)).commit(); } + @Test + void testCommitWithListener() { + final Runnable onCommit = mock(Runnable.class); + final var state = new MapWritableStates(Map.of(KV_STATE_KEY, kvStateMock), onCommit); + state.commit(); + verify(kvStateMock, times(1)).commit(); + verify(onCommit, times(1)).run(); + } + @Test void testCommitUnknownValue() { final var state = new MapWritableStates(Map.of("other", new Object()));