Skip to content

Commit

Permalink
fix: fixes for unit tests after moving to falling yaci not via REST b…
Browse files Browse the repository at this point in the history
…ut internal method invocation changes.
  • Loading branch information
Mateusz Czeladka committed Dec 1, 2023
1 parent c0d66f5 commit 5e77d53
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 83 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

import java.util.Optional;

import static org.zalando.problem.Status.INTERNAL_SERVER_ERROR;

@Service
@Slf4j
@RequiredArgsConstructor
Expand All @@ -40,6 +42,7 @@ public Either<Problem, ChainTip> getChainTip() {
return Either.left(Problem.builder()
.withTitle("CHAIN_TIP_NOT_FOUND")
.withDetail("Unable to get chain tip from backend service.")
.withStatus(INTERNAL_SERVER_ERROR)
.build()
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public Either<Problem, Optional<TransactionDetails>> getTransactionDetails(Strin

return TransactionDetails.builder()
.transactionsConfirmations(blockConfirmations)
.transactionHash(txn.getHash())
.finalityScore(TransactionDetails.FinalityScore.fromConfirmations(blockConfirmations))
.blockHash(block.getHash())
.absoluteSlot(txn.getSlot())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package org.cardano.foundation.voting.api;

import com.bloxbean.cardano.yaci.store.blocks.domain.Block;
import com.bloxbean.cardano.yaci.store.blocks.service.BlockService;
import com.bloxbean.cardano.yaci.store.transaction.domain.TransactionDetails;
import com.bloxbean.cardano.yaci.store.transaction.service.TransactionService;
import com.github.tomakehurst.wiremock.WireMockServer;
import com.github.tomakehurst.wiremock.client.WireMock;
import io.restassured.RestAssured;
Expand Down Expand Up @@ -28,16 +32,19 @@
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;

import java.util.List;
import java.util.Optional;

import static com.github.tomakehurst.wiremock.client.WireMock.*;
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.when;

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ComponentScan
@EnableJpaRepositories
@EntityScan
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@ActiveProfiles({"test", "dev--preprod"})
@SpringJUnitConfig(classes = VotingLedgerFollowerApp.class)
@SpringJUnitConfig(classes = { VotingLedgerFollowerApp.class, VotingLedgerFollowerAppTest.class } )
public class BaseTest {

@LocalServerPort
Expand All @@ -55,6 +62,12 @@ public class BaseTest {
@Autowired
private MerkleRootHashRepository merkleRootHashRepository;

@Autowired
private BlockService blockService;

@Autowired
private TransactionService transactionService;

private WireMockServer wireMockServer;

@BeforeAll
Expand All @@ -63,7 +76,7 @@ public void setUp() {
wireMockServer.start();

String accountResponse = """
{ "active": true,
{ "active": true,
"controlled_amount": "12695385",
"pool_id": "pool1pu5jlj4q9w9jlxeu370a3c9myx47md5j5m2str0naunn2q3lkdy",
"rewards_sum": "95385",
Expand All @@ -89,86 +102,6 @@ public void setUp() {
.willReturn(com.github.tomakehurst.wiremock.client.WireMock.aResponse()
.withStatus(404)));

String latestBlockResponse = "{" +
"\"time\": 1641338934," +
"\"height\": 15243593," +
"\"hash\": \"4ea1ba291e8eef538635a53e59fddba7810d1679631cc3aed7c8e6c4091a516a\"," +
"\"slot\": 412162133," +
"\"epoch\": 425," +
"\"epoch_slot\": 12," +
"\"slot_leader\": \"pool1pu5jlj4q9w9jlxeu370a3c9myx47md5j5m2str0naunn2qnikdy\"," +
"\"size\": 3," +
"\"tx_count\": 1," +
"\"output\": \"128314491794\"," +
"\"fees\": \"592661\"," +
"\"block_vrf\": \"vrf_vk1wf2k6lhujezqcfe00l6zetxpnmh9n6mwhpmhm0dvfh3fxgmdnrfqkms8ty\"," +
"\"op_cert\": \"da905277534faf75dae41732650568af545134ee08a3c0392dbefc8096ae177c\"," +
"\"op_cert_counter\": \"18\"," +
"\"previous_block\": \"43ebccb3ac72c7cebd0d9b755a4b08412c9f5dcb81b8a0ad1e3c197d29d47b05\"," +
"\"next_block\": \"8367f026cf4b03e116ff8ee5daf149b55ba5a6ec6dec04803b8dc317721d15fa\"," +
"\"confirmations\": 4698" +
"}";

wireMockServer.stubFor(
WireMock.get(urlEqualTo("/blocks/latest"))
.willReturn(com.github.tomakehurst.wiremock.client.WireMock.aResponse()
.withStatus(200)
.withHeader("Content-Type", "application/json")
.withBody(latestBlockResponse)));

wireMockServer.stubFor(
WireMock.get(urlEqualTo("/yaci-api/blocks/latest"))
.willReturn(com.github.tomakehurst.wiremock.client.WireMock.aResponse()
.withStatus(200)
.withHeader("Content-Type", "application/json")
.withBody(latestBlockResponse)));

String transactionResponse = "{" +
"\"hash\": \"1e043f100dce12d107f679685acd2fc0610e10f72a92d412794c9773d11d8477\"," +
"\"block\": \"356b7d7dbb696ccd12775c016941057a9dc70898d87a63fc752271bb46856940\"," +
"\"block_height\": 123456," +
"\"block_time\": 1635505891," +
"\"slot\": 42000000," +
"\"index\": 1," +
"\"output_amount\": [" +
"{" +
"\"unit\": \"lovelace\"," +
"\"quantity\": \"42000000\"" +
"}," +
"{" +
"\"unit\": \"b0d07d45fe9514f80213f4020e5a61241458be626841cde717cb38a76e7574636f696e\"," +
"\"quantity\": \"12\"" +
"}" +
"]," +
"\"fees\": \"182485\"," +
"\"deposit\": \"0\"," +
"\"size\": 433," +
"\"invalid_before\": null," +
"\"invalid_hereafter\": \"13885913\"," +
"\"utxo_count\": 4," +
"\"withdrawal_count\": 0," +
"\"mir_cert_count\": 0," +
"\"delegation_count\": 0," +
"\"stake_cert_count\": 0," +
"\"pool_update_count\": 0," +
"\"pool_retire_count\": 0," +
"\"asset_mint_or_burn_count\": 0," +
"\"redeemer_count\": 0," +
"\"valid_contract\": true" +
"}";

wireMockServer.stubFor(
WireMock.get(urlEqualTo("/yaci-api/txs/1e043f100dce12d107f679685acd2fc0610e10f72a92d412794c9773d11d8477"))
.willReturn(com.github.tomakehurst.wiremock.client.WireMock.aResponse()
.withStatus(200)
.withHeader("Content-Type", "application/json")
.withBody(transactionResponse)));

wireMockServer.stubFor(
WireMock.get(urlEqualTo("/yaci-api/txs/23ab9463463eb149054d22249433f1bfd5acbbf8af38cc64f3840b0491230880"))
.willReturn(com.github.tomakehurst.wiremock.client.WireMock.aResponse()
.withStatus(404)));

Event event = Event.builder()
.id("CF_TEST_EVENT_01")
.organisers("Cardano Foundation")
Expand Down Expand Up @@ -224,6 +157,29 @@ public void setUp() {
.build();

merkleRootHashRepository.save(merkleRootHash);

when(blockService.getLatestBlock()).thenReturn(Optional.empty());

Block block1 = Block.builder()
.hash("356b7d7dbb696ccd12775c016941057a9dc70898d87a63fc752271bb46856940")
.epochNumber(412)
.slot(412162133L)
.blockBodyHash("1e043f100dce12d107f679685acd2fc0610e10f72a92d412794c9773d11d8477")
.build();

when(blockService.getLatestBlock()).thenReturn(Optional.of(block1));

when(blockService.getBlockByNumber(412162133L)).thenReturn(Optional.of(block1));

when(transactionService.getTransaction(anyString())).thenReturn(Optional.empty());

var tx1 = TransactionDetails.builder()
.hash("1e043f100dce12d107f679685acd2fc0610e10f72a92d412794c9773d11d8477")
.blockHeight(412162133L)
.slot(412162133L)
.build();

when(transactionService.getTransaction("1e043f100dce12d107f679685acd2fc0610e10f72a92d412794c9773d11d8477")).thenReturn(Optional.of(tx1));
}

@AfterAll
Expand All @@ -234,4 +190,5 @@ public void tearDown() {
eventRepository.deleteAll();
merkleRootHashRepository.deleteAll();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package org.cardano.foundation.voting.api;

import com.bloxbean.cardano.yaci.store.blocks.service.BlockService;
import com.bloxbean.cardano.yaci.store.transaction.service.TransactionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import static org.mockito.Mockito.mock;

@Configuration
@Slf4j
public class VotingLedgerFollowerAppTest {

@Bean
@Primary
public BlockService blockService() {
log.info("Mocking Yaci's BlockService...");
return mock(BlockService.class);
}

@Bean
@Primary
public TransactionService transactionService() {
log.info("Mocking Yaci's TransactionService...");
return mock(TransactionService.class);
}

}

0 comments on commit 5e77d53

Please sign in to comment.