From db6c1694d683cba4de1ca440e19626806fbd9dd1 Mon Sep 17 00:00:00 2001 From: dudiedri Date: Mon, 14 Sep 2026 01:57:03 +0300 Subject: [PATCH] Cover the service implementations offline Governance, account, block and pool had only live integration suites. The mainnet ones are excluded from the coverage job, so GovernanceServiceImpl reported 5.5% despite being exercised nightly, and none of those suites can assert what request was actually sent. Adds MockWebServer suites for all four. They check that each method reaches the endpoint it should, that bulk calls POST their ids in the body, that epoch filters reach the query or body, that a null Options does not NPE, and that invalid input is rejected before any request leaves the client - asserted with getRequestCount() == 0 rather than assumed. GovernanceServiceImpl 5.5% -> 98.2% overall lines 81.8% -> 92.3% overall branches 71.2% -> 78.8% 17 tests, all offline, adding roughly 8 seconds to the suite. Co-Authored-By: Claude Opus 5 --- .../account/AccountServiceOfflineTest.java | 121 ++++++++++++++ .../api/block/BlockServiceOfflineTest.java | 103 ++++++++++++ .../GovernanceServiceOfflineTest.java | 155 ++++++++++++++++++ .../api/pool/PoolServiceOfflineTest.java | 133 +++++++++++++++ 4 files changed, 512 insertions(+) create mode 100644 src/test/java/rest/koios/client/backend/api/account/AccountServiceOfflineTest.java create mode 100644 src/test/java/rest/koios/client/backend/api/block/BlockServiceOfflineTest.java create mode 100644 src/test/java/rest/koios/client/backend/api/governance/GovernanceServiceOfflineTest.java create mode 100644 src/test/java/rest/koios/client/backend/api/pool/PoolServiceOfflineTest.java diff --git a/src/test/java/rest/koios/client/backend/api/account/AccountServiceOfflineTest.java b/src/test/java/rest/koios/client/backend/api/account/AccountServiceOfflineTest.java new file mode 100644 index 0000000..107d860 --- /dev/null +++ b/src/test/java/rest/koios/client/backend/api/account/AccountServiceOfflineTest.java @@ -0,0 +1,121 @@ +package rest.koios.client.backend.api.account; + +import okhttp3.mockwebserver.MockResponse; +import okhttp3.mockwebserver.MockWebServer; +import okhttp3.mockwebserver.RecordedRequest; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import rest.koios.client.backend.api.base.exception.ApiException; +import rest.koios.client.backend.factory.BackendFactory; +import rest.koios.client.backend.factory.options.Options; + +import java.io.IOException; +import java.util.Collections; +import java.util.List; + +/** + * Exercises {@link AccountService} against a local {@link MockWebServer}, so request shape can be + * asserted without depending on a live instance. + */ +class AccountServiceOfflineTest { + + private static final String STAKE = + "stake1uyrx65wjqjgeeksd8hptmcgl5jfyrqkfq0xe8xlp367kphsckq250"; + private static final List STAKES = Collections.singletonList(STAKE); + + private MockWebServer server; + private AccountService accountService; + + @BeforeEach + void setup() throws IOException { + server = new MockWebServer(); + server.start(); + accountService = BackendFactory + .getCustomRPCService(server.url("/api/v1/").toString()).getAccountService(); + } + + @AfterEach + void tearDown() throws IOException { + server.shutdown(); + } + + private void enqueue(int count) { + for (int i = 0; i < count; i++) { + server.enqueue(new MockResponse().setResponseCode(200) + .setHeader("Content-Type", "application/json").setBody("[]")); + } + } + + @Test + void everyEndpointIsReachedTest() throws ApiException, InterruptedException { + enqueue(13); + + accountService.getAccountList(Options.EMPTY); + Assertions.assertTrue(server.takeRequest().getPath().startsWith("/api/v1/account_list")); + + accountService.getAccountInformation(STAKES, Options.EMPTY); + Assertions.assertTrue(server.takeRequest().getPath().startsWith("/api/v1/account_info")); + + accountService.getCachedAccountInformation(STAKES, Options.EMPTY); + Assertions.assertTrue(server.takeRequest().getPath().startsWith("/api/v1/account_info_cached")); + + accountService.getAccountUTxOs(STAKES, false, Options.EMPTY); + Assertions.assertTrue(server.takeRequest().getPath().startsWith("/api/v1/account_utxos")); + + accountService.getAccountTxs(STAKE, null, Options.EMPTY); + Assertions.assertTrue(server.takeRequest().getPath().startsWith("/api/v1/account_txs")); + + accountService.getAccountRewards(STAKES, null, Options.EMPTY); + Assertions.assertTrue(server.takeRequest().getPath().startsWith("/api/v1/account_rewards")); + + accountService.getAccountUpdates(STAKES, Options.EMPTY); + Assertions.assertTrue(server.takeRequest().getPath().startsWith("/api/v1/account_updates")); + + accountService.getAccountAddresses(STAKES, false, false, Options.EMPTY); + Assertions.assertTrue(server.takeRequest().getPath().startsWith("/api/v1/account_addresses")); + + accountService.getAccountAssets(STAKES, null, Options.EMPTY); + Assertions.assertTrue(server.takeRequest().getPath().startsWith("/api/v1/account_assets")); + + accountService.getAccountHistory(STAKES, null, Options.EMPTY); + Assertions.assertTrue(server.takeRequest().getPath().startsWith("/api/v1/account_history")); + + accountService.getAccountStakeHistory(STAKES, Options.EMPTY); + Assertions.assertTrue(server.takeRequest().getPath().startsWith("/api/v1/account_stake_history")); + + accountService.getAccountRewardHistory(STAKES, null, Options.EMPTY); + Assertions.assertTrue(server.takeRequest().getPath().startsWith("/api/v1/account_reward_history")); + + accountService.getAccountUpdateHistory(STAKES, Options.EMPTY); + Assertions.assertTrue(server.takeRequest().getPath().startsWith("/api/v1/account_update_history")); + + Assertions.assertEquals(13, server.getRequestCount()); + } + + @Test + void stakeAddressesArePostedInTheBodyTest() throws ApiException, InterruptedException { + enqueue(1); + accountService.getAccountInformation(STAKES, Options.EMPTY); + RecordedRequest request = server.takeRequest(); + Assertions.assertEquals("POST", request.getMethod()); + Assertions.assertTrue(request.getBody().readUtf8().contains(STAKE)); + } + + @Test + void epochFilterIsAppliedWhenGivenTest() throws ApiException, InterruptedException { + enqueue(1); + accountService.getAccountRewards(STAKES, 500, Options.EMPTY); + Assertions.assertTrue(server.takeRequest().getBody().readUtf8().contains("500")); + } + + @Test + void invalidInputIsRejectedBeforeSendingTest() { + Assertions.assertThrows(ApiException.class, + () -> accountService.getAccountRewards(STAKES, -1, Options.EMPTY)); + Assertions.assertThrows(ApiException.class, + () -> accountService.getAccountTxs("not-a-stake-address", null, Options.EMPTY)); + Assertions.assertEquals(0, server.getRequestCount(), "nothing should have been sent"); + } +} diff --git a/src/test/java/rest/koios/client/backend/api/block/BlockServiceOfflineTest.java b/src/test/java/rest/koios/client/backend/api/block/BlockServiceOfflineTest.java new file mode 100644 index 0000000..f0dfd0c --- /dev/null +++ b/src/test/java/rest/koios/client/backend/api/block/BlockServiceOfflineTest.java @@ -0,0 +1,103 @@ +package rest.koios.client.backend.api.block; + +import okhttp3.mockwebserver.MockResponse; +import okhttp3.mockwebserver.MockWebServer; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import rest.koios.client.backend.api.base.Result; +import rest.koios.client.backend.api.base.exception.ApiException; +import rest.koios.client.backend.api.block.model.Block; +import rest.koios.client.backend.factory.BackendFactory; +import rest.koios.client.backend.factory.options.Options; + +import java.io.IOException; +import java.util.Collections; +import java.util.List; + +class BlockServiceOfflineTest { + + private static final String BLOCK_HASH = + "88265b6b01bd9516009285eabaddedea18d42f6c778e5942b200afec232aca1e"; + private static final List HASHES = Collections.singletonList(BLOCK_HASH); + + private MockWebServer server; + private BlockService blockService; + + @BeforeEach + void setup() throws IOException { + server = new MockWebServer(); + server.start(); + blockService = BackendFactory + .getCustomRPCService(server.url("/api/v1/").toString()).getBlockService(); + } + + @AfterEach + void tearDown() throws IOException { + server.shutdown(); + } + + private void enqueue(int count, String body) { + for (int i = 0; i < count; i++) { + server.enqueue(new MockResponse().setResponseCode(200) + .setHeader("Content-Type", "application/json").setBody(body)); + } + } + + @Test + void everyEndpointIsReachedTest() throws ApiException, InterruptedException { + enqueue(6, "[]"); + + blockService.getBlockList(Options.EMPTY); + Assertions.assertTrue(server.takeRequest().getPath().startsWith("/api/v1/blocks")); + + blockService.getBlocksInformation(HASHES, Options.EMPTY); + Assertions.assertTrue(server.takeRequest().getPath().startsWith("/api/v1/block_info")); + + blockService.getBlockTransactions(HASHES, Options.EMPTY); + Assertions.assertTrue(server.takeRequest().getPath().startsWith("/api/v1/block_txs")); + + blockService.getBlockTransactionsCbor(HASHES, Options.EMPTY); + Assertions.assertTrue(server.takeRequest().getPath().startsWith("/api/v1/block_tx_cbor")); + + blockService.getBlockTransactionsInfo(HASHES, true, true, true, true, true, true, true, Options.EMPTY); + Assertions.assertTrue(server.takeRequest().getPath().startsWith("/api/v1/block_tx_info")); + + blockService.getBlockInformation(BLOCK_HASH); + Assertions.assertTrue(server.takeRequest().getPath().startsWith("/api/v1/block_info")); + } + + @Test + void latestBlockReturnsTheFirstRowTest() throws ApiException, InterruptedException { + server.enqueue(new MockResponse().setResponseCode(200) + .setHeader("Content-Type", "application/json") + .setBody("[{\"hash\":\"" + BLOCK_HASH + "\",\"block_height\":13917803,\"epoch_no\":655}]")); + + Result result = blockService.getLatestBlock(); + + Assertions.assertTrue(result.isSuccessful()); + Assertions.assertEquals(BLOCK_HASH, result.getValue().getHash()); + String path = server.takeRequest().getPath(); + Assertions.assertTrue(path.startsWith("/api/v1/blocks"), path); + Assertions.assertTrue(path.contains("limit=1"), path); + } + + @Test + void emptyListYieldsASyntheticNotFoundTest() throws ApiException { + enqueue(1, "[]"); + + Result result = blockService.getLatestBlock(); + + Assertions.assertFalse(result.isSuccessful()); + Assertions.assertEquals(404, result.getCode()); + Assertions.assertTrue(result.isSynthetic()); + } + + @Test + void invalidBlockHashIsRejectedBeforeSendingTest() { + Assertions.assertThrows(ApiException.class, + () -> blockService.getBlockInformation("not-hex")); + Assertions.assertEquals(0, server.getRequestCount(), "nothing should have been sent"); + } +} diff --git a/src/test/java/rest/koios/client/backend/api/governance/GovernanceServiceOfflineTest.java b/src/test/java/rest/koios/client/backend/api/governance/GovernanceServiceOfflineTest.java new file mode 100644 index 0000000..5f7922e --- /dev/null +++ b/src/test/java/rest/koios/client/backend/api/governance/GovernanceServiceOfflineTest.java @@ -0,0 +1,155 @@ +package rest.koios.client.backend.api.governance; + +import okhttp3.mockwebserver.MockResponse; +import okhttp3.mockwebserver.MockWebServer; +import okhttp3.mockwebserver.RecordedRequest; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import rest.koios.client.backend.api.base.exception.ApiException; +import rest.koios.client.backend.factory.BackendFactory; +import rest.koios.client.backend.factory.options.Options; + +import java.io.IOException; + +/** + * Exercises {@link GovernanceService} against a local {@link MockWebServer}. + *

+ * Governance only had a mainnet integration suite, which the coverage job excludes and which + * cannot assert request shape. These run offline, so they are deterministic and check that each + * call reaches the endpoint it should with the parameters it should. + */ +class GovernanceServiceOfflineTest { + + private MockWebServer server; + private GovernanceService governanceService; + + @BeforeEach + void setup() throws IOException { + server = new MockWebServer(); + server.start(); + governanceService = BackendFactory + .getCustomRPCService(server.url("/api/v1/").toString()).getGovernanceService(); + } + + @AfterEach + void tearDown() throws IOException { + server.shutdown(); + } + + private void enqueueEmptyArray() { + server.enqueue(new MockResponse() + .setResponseCode(200) + .setHeader("Content-Type", "application/json") + .setBody("[]")); + } + + private RecordedRequest take() throws InterruptedException { + return server.takeRequest(); + } + + @Test + void everyEndpointIsReachedTest() throws ApiException, InterruptedException { + for (int i = 0; i < 15; i++) { + enqueueEmptyArray(); + } + + governanceService.getDRepsEpochSummary(500, Options.EMPTY); + Assertions.assertTrue(take().getPath().startsWith("/api/v1/drep_epoch_summary")); + + governanceService.getDRepsList(Options.EMPTY); + Assertions.assertTrue(take().getPath().startsWith("/api/v1/drep_list")); + + governanceService.getDRepsUpdates("drep1abc", Options.EMPTY); + Assertions.assertTrue(take().getPath().startsWith("/api/v1/drep_updates")); + + governanceService.getDRepsVotingPowerHistory("drep1abc", null, Options.EMPTY); + Assertions.assertTrue(take().getPath().startsWith("/api/v1/drep_voting_power_history")); + + governanceService.getDRepsVotes("drep1abc", Options.EMPTY); + Assertions.assertTrue(take().getPath().startsWith("/api/v1/drep_votes")); + + governanceService.getDRepsDelegators("drep1abc", Options.EMPTY); + Assertions.assertTrue(take().getPath().startsWith("/api/v1/drep_delegators")); + + governanceService.getCommitteeInformation(Options.EMPTY); + Assertions.assertTrue(take().getPath().startsWith("/api/v1/committee_info")); + + governanceService.getCommitteeVotes("cc_hot1abc", Options.EMPTY); + Assertions.assertTrue(take().getPath().startsWith("/api/v1/committee_votes")); + + governanceService.getProposalList(Options.EMPTY); + Assertions.assertTrue(take().getPath().startsWith("/api/v1/proposal_list")); + + governanceService.getVoterProposals("drep1abc", Options.EMPTY); + Assertions.assertTrue(take().getPath().startsWith("/api/v1/voter_proposal_list")); + + governanceService.getProposalVotingSummary("gov_action1abc", Options.EMPTY); + Assertions.assertTrue(take().getPath().startsWith("/api/v1/proposal_voting_summary")); + + governanceService.getProposalVotes("gov_action1abc", Options.EMPTY); + Assertions.assertTrue(take().getPath().startsWith("/api/v1/proposal_votes")); + + governanceService.getVoteList(Options.EMPTY); + Assertions.assertTrue(take().getPath().startsWith("/api/v1/vote_list")); + + governanceService.getPoolsVotingPowerHistory("pool1abc", null, Options.EMPTY); + Assertions.assertTrue(take().getPath().startsWith("/api/v1/pool_voting_power_history")); + + governanceService.getPoolVotes("pool1abc", Options.EMPTY); + Assertions.assertTrue(take().getPath().startsWith("/api/v1/pool_votes")); + + Assertions.assertEquals(15, server.getRequestCount()); + } + + @Test + void drepsInfoAndMetadataPostTheIdsTest() throws ApiException, InterruptedException { + enqueueEmptyArray(); + governanceService.getDRepsInfo(java.util.Arrays.asList("drep1abc", "drep1def"), Options.EMPTY); + RecordedRequest info = take(); + Assertions.assertEquals("POST", info.getMethod()); + Assertions.assertTrue(info.getPath().startsWith("/api/v1/drep_info")); + Assertions.assertTrue(info.getBody().readUtf8().contains("drep1abc")); + + enqueueEmptyArray(); + governanceService.getDRepsMetadata(java.util.Collections.singletonList("drep1abc"), Options.EMPTY); + RecordedRequest metadata = take(); + Assertions.assertEquals("POST", metadata.getMethod()); + Assertions.assertTrue(metadata.getPath().startsWith("/api/v1/drep_metadata")); + } + + @Test + void epochFilterIsAppliedWhenGivenTest() throws ApiException, InterruptedException { + enqueueEmptyArray(); + governanceService.getDRepsVotingPowerHistory("drep1abc", 500, Options.EMPTY); + String path = take().getPath(); + Assertions.assertTrue(path.contains("epoch_no"), path); + Assertions.assertTrue(path.contains("500"), path); + + enqueueEmptyArray(); + governanceService.getPoolsVotingPowerHistory("pool1abc", 500, Options.EMPTY); + String poolPath = take().getPath(); + Assertions.assertTrue(poolPath.contains("epoch_no"), poolPath); + } + + @Test + void nullOptionsAreHandledTest() throws ApiException, InterruptedException { + enqueueEmptyArray(); + governanceService.getDRepsVotingPowerHistory("drep1abc", 500, null); + Assertions.assertTrue(take().getPath().contains("epoch_no")); + + enqueueEmptyArray(); + governanceService.getPoolsVotingPowerHistory("pool1abc", 500, null); + Assertions.assertTrue(take().getPath().contains("epoch_no")); + } + + @Test + void invalidEpochIsRejectedBeforeSendingTest() { + Assertions.assertThrows(ApiException.class, + () -> governanceService.getDRepsVotingPowerHistory("drep1abc", -1, Options.EMPTY)); + Assertions.assertThrows(ApiException.class, + () -> governanceService.getPoolsVotingPowerHistory("pool1abc", -1, Options.EMPTY)); + Assertions.assertEquals(0, server.getRequestCount(), "nothing should have been sent"); + } +} diff --git a/src/test/java/rest/koios/client/backend/api/pool/PoolServiceOfflineTest.java b/src/test/java/rest/koios/client/backend/api/pool/PoolServiceOfflineTest.java new file mode 100644 index 0000000..fcd1c3b --- /dev/null +++ b/src/test/java/rest/koios/client/backend/api/pool/PoolServiceOfflineTest.java @@ -0,0 +1,133 @@ +package rest.koios.client.backend.api.pool; + +import okhttp3.mockwebserver.MockResponse; +import okhttp3.mockwebserver.MockWebServer; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import rest.koios.client.backend.api.base.exception.ApiException; +import rest.koios.client.backend.factory.BackendFactory; +import rest.koios.client.backend.factory.options.Options; + +import java.io.IOException; +import java.util.Collections; +import java.util.List; + +class PoolServiceOfflineTest { + + private static final String POOL = "pool1z5uqdk7dzdxaae5633fqfcu2eqzy3a3rgtuvy087fdld7yws0xt"; + private static final List POOLS = Collections.singletonList(POOL); + + private MockWebServer server; + private PoolService poolService; + + @BeforeEach + void setup() throws IOException { + server = new MockWebServer(); + server.start(); + poolService = BackendFactory + .getCustomRPCService(server.url("/api/v1/").toString()).getPoolService(); + } + + @AfterEach + void tearDown() throws IOException { + server.shutdown(); + } + + private void enqueue(int count) { + for (int i = 0; i < count; i++) { + server.enqueue(new MockResponse().setResponseCode(200) + .setHeader("Content-Type", "application/json").setBody("[]")); + } + } + + @Test + void everyEndpointIsReachedTest() throws ApiException, InterruptedException { + enqueue(16); + + poolService.getPoolList(Options.EMPTY); + Assertions.assertTrue(server.takeRequest().getPath().startsWith("/api/v1/pool_list")); + + poolService.getPoolInformation(POOLS, Options.EMPTY); + Assertions.assertTrue(server.takeRequest().getPath().startsWith("/api/v1/pool_info")); + + poolService.getPoolStakeSnapshot(POOL, Options.EMPTY); + Assertions.assertTrue(server.takeRequest().getPath().startsWith("/api/v1/pool_stake_snapshot")); + + poolService.getPoolDelegatorsList(POOL, Options.EMPTY); + Assertions.assertTrue(server.takeRequest().getPath().startsWith("/api/v1/pool_delegators")); + + poolService.getPoolDelegatorsHistory(POOL, null, Options.EMPTY); + Assertions.assertTrue(server.takeRequest().getPath().startsWith("/api/v1/pool_delegators_history")); + + poolService.getPoolBlocks(POOL, Options.EMPTY); + Assertions.assertTrue(server.takeRequest().getPath().startsWith("/api/v1/pool_blocks")); + + poolService.getPoolOwnerHistory(POOLS, Options.EMPTY); + Assertions.assertTrue(server.takeRequest().getPath().startsWith("/api/v1/pool_owner_history")); + + poolService.getPoolHistory(POOL, Options.EMPTY); + Assertions.assertTrue(server.takeRequest().getPath().startsWith("/api/v1/pool_history")); + + poolService.getPoolUpdatesByPoolBech32(POOL, Options.EMPTY); + Assertions.assertTrue(server.takeRequest().getPath().startsWith("/api/v1/pool_updates")); + + poolService.getPoolUpdates(Options.EMPTY); + Assertions.assertTrue(server.takeRequest().getPath().startsWith("/api/v1/pool_updates")); + + poolService.getPoolRegistrations(null, Options.EMPTY); + Assertions.assertTrue(server.takeRequest().getPath().startsWith("/api/v1/pool_registrations")); + + poolService.getPoolRetirements(null, Options.EMPTY); + Assertions.assertTrue(server.takeRequest().getPath().startsWith("/api/v1/pool_retirements")); + + poolService.getPoolRelays(Options.EMPTY); + Assertions.assertTrue(server.takeRequest().getPath().startsWith("/api/v1/pool_relays")); + + poolService.getPoolGroups(Options.EMPTY); + Assertions.assertTrue(server.takeRequest().getPath().startsWith("/api/v1/pool_groups")); + + poolService.getPoolMetadata(Options.EMPTY); + Assertions.assertTrue(server.takeRequest().getPath().startsWith("/api/v1/pool_metadata")); + + poolService.getPoolCalidusKeys(Options.EMPTY); + Assertions.assertTrue(server.takeRequest().getPath().startsWith("/api/v1/pool_calidus_keys")); + + Assertions.assertEquals(16, server.getRequestCount()); + } + + @Test + void poolIdsArePostedInTheBodyTest() throws ApiException, InterruptedException { + enqueue(1); + poolService.getPoolInformation(POOLS, Options.EMPTY); + Assertions.assertTrue(server.takeRequest().getBody().readUtf8().contains(POOL)); + + enqueue(1); + poolService.getPoolMetadata(POOLS, Options.EMPTY); + Assertions.assertTrue(server.takeRequest().getBody().readUtf8().contains(POOL)); + } + + @Test + void epochScopedCallsCarryTheEpochTest() throws ApiException, InterruptedException { + enqueue(3); + + poolService.getPoolBlocksByEpoch(POOL, 500, Options.EMPTY); + Assertions.assertTrue(server.takeRequest().getPath().contains("500")); + + poolService.getPoolRegistrations(500, Options.EMPTY); + Assertions.assertTrue(server.takeRequest().getPath().contains("500")); + + poolService.getPoolRetirements(500, Options.EMPTY); + Assertions.assertTrue(server.takeRequest().getPath().contains("500")); + } + + @Test + void invalidInputIsRejectedBeforeSendingTest() { + Assertions.assertThrows(ApiException.class, + () -> poolService.getPoolDelegatorsList("not-a-pool", Options.EMPTY)); + Assertions.assertThrows(ApiException.class, + () -> poolService.getPoolBlocksByEpoch(POOL, -1, Options.EMPTY)); + Assertions.assertEquals(0, server.getRequestCount(), "nothing should have been sent"); + } +}