Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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<String> 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");
}
}
Original file line number Diff line number Diff line change
@@ -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<String> 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<Block> 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<Block> 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");
}
}
Original file line number Diff line number Diff line change
@@ -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}.
* <p>
* 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");
}
}
Loading
Loading