Skip to content

Commit

Permalink
chore: adding more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Kammerlo committed Aug 14, 2024
1 parent 7b81e6b commit 0b0415b
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package org.cardanofoundation.rosetta.api.search.controller;

import java.util.Collections;

import lombok.SneakyThrows;

import org.springframework.http.MediaType;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.openapitools.client.model.NetworkIdentifier;
import org.openapitools.client.model.SearchTransactionsRequest;

import org.junit.jupiter.api.Test;

import org.cardanofoundation.rosetta.api.BaseSpringMvcSetup;
import org.cardanofoundation.rosetta.api.network.service.NetworkService;
import org.cardanofoundation.rosetta.api.search.service.SearchService;
import org.cardanofoundation.rosetta.testgenerator.common.TestConstants;

import static org.mockito.ArgumentMatchers.any;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

class SearchControllerTest extends BaseSpringMvcSetup {

@Mock
SearchService service;
@Mock
NetworkService networkService;

@SneakyThrows
@Test
void searchControllerNetworkIsMissing() {
SearchTransactionsRequest request = new SearchTransactionsRequest();
Mockito.when(service.searchTransaction(any(), any(), any())).thenReturn(Collections.emptyList());
mockMvc.perform(post("/search/transactions")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(request)))
.andExpect(status().is5xxServerError());
}

@SneakyThrows
@Test
void searchControllerOK() {
SearchTransactionsRequest request = SearchTransactionsRequest.builder()
.networkIdentifier(NetworkIdentifier.builder()
.blockchain(TestConstants.TEST_BLOCKCHAIN)
.network(TestConstants.TEST_NETWORK)
.build())
.build();
Mockito.when(service.searchTransaction(any(), any(), any())).thenReturn(Collections.emptyList());

mockMvc.perform(post("/search/transactions")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(request)))
.andExpect(status().isOk())
.andExpect(jsonPath("$.total_count").value(10))
.andExpect(jsonPath("$.next_offset").value(10));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

import org.cardanofoundation.rosetta.api.BaseMapperSetup;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;

class BlockTransactionsToSearchTransactionsResponseTest extends BaseMapperSetup {

Expand All @@ -38,11 +38,11 @@ void mapToSearchResponse_test_Ok() {
SearchTransactionsResponse searchTransactionsResponse = my.mapToSearchTransactionsResponse(
blockTransactions, 2L);

assertThat(searchTransactionsResponse.getTransactions().size() == 1);
assertThat(searchTransactionsResponse.getNextOffset() == 2L);
assertThat(searchTransactionsResponse.getTotalCount() == 1);
assertThat(searchTransactionsResponse.getTransactions().get(0).getBlockIdentifier().getHash().equals("hash"));
assertThat(searchTransactionsResponse.getTransactions().get(0).getBlockIdentifier().getIndex().equals(1L));
assertEquals(1,searchTransactionsResponse.getTransactions().size());
assertEquals(2L, searchTransactionsResponse.getNextOffset());
assertEquals(1, searchTransactionsResponse.getTotalCount());
assertEquals("hash", searchTransactionsResponse.getTransactions().getFirst().getBlockIdentifier().getHash());
assertEquals(1L, searchTransactionsResponse.getTransactions().getFirst().getBlockIdentifier().getIndex());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import org.cardanofoundation.rosetta.testgenerator.common.TestConstants;
import org.cardanofoundation.rosetta.testgenerator.common.TransactionBlockDetails;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;

class SearchControllerIntTest extends IntegrationTest {

Expand All @@ -28,7 +28,7 @@ void searchAddressTransactions() {
.build();

List<BlockTransaction> blockTransactions = service.searchTransaction(req, 0L, 5L);
assertThat(blockTransactions.size() == 5);
assertEquals(5, blockTransactions.size());
}

@Test
Expand All @@ -40,13 +40,14 @@ void searchByTxHash() {
.hash(stakeKeyDeregistration.txHash())
.build())
.build();

List<BlockTransaction> blockTransactions = service.searchTransaction(req, 0L, 10L);
assertThat(blockTransactions.size() == 1);
assertEquals(1, blockTransactions.size());
BlockTransaction tx = blockTransactions.getFirst();
assertThat(tx.getBlockIdentifier().getHash().equals(stakeKeyDeregistration.blockHash()));
assertThat(tx.getBlockIdentifier().getIndex() == stakeKeyDeregistration.blockNumber());
assertThat(tx.getTransaction().getTransactionIdentifier().getHash().equals(stakeKeyDeregistration.txHash()));
assertThat(tx.getTransaction().getOperations().size() == 4);
assertEquals(stakeKeyDeregistration.blockHash(), tx.getBlockIdentifier().getHash());
assertEquals(stakeKeyDeregistration.blockNumber(), tx.getBlockIdentifier().getIndex());
assertEquals(stakeKeyDeregistration.txHash(), tx.getTransaction().getTransactionIdentifier().getHash());
assertEquals(4, tx.getTransaction().getOperations().size());
}

@Test
Expand All @@ -60,7 +61,7 @@ void searchByUtxo() {
.build();

List<BlockTransaction> blockTransactions = service.searchTransaction(req, 0L, 10L);
assertThat(blockTransactions.size() == 1);
assertEquals(1, blockTransactions.size());
String identifier = blockTransactions.getFirst().getTransaction().getOperations().getFirst()
.getCoinChange().getCoinIdentifier().getIdentifier();
req = SearchTransactionsRequest.builder()
Expand All @@ -70,7 +71,7 @@ void searchByUtxo() {
.build();

blockTransactions = service.searchTransaction(req, 0L, 10L);
assertThat(blockTransactions.size() == 2);
assertEquals(2, blockTransactions.size());
}

}

0 comments on commit 0b0415b

Please sign in to comment.