Skip to content

Commit

Permalink
refactor: RA-107 coins and base tests
Browse files Browse the repository at this point in the history
  • Loading branch information
shleger committed May 9, 2024
1 parent a33fe80 commit 48e2798
Show file tree
Hide file tree
Showing 8 changed files with 189 additions and 239 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,27 @@

import java.math.BigInteger;

import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.mockito.Mock;
import org.openapitools.jackson.nullable.JsonNullableModule;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.extension.ExtendWith;

import org.cardanofoundation.rosetta.ConfigurationMapper;
import org.cardanofoundation.rosetta.api.BaseMapperTest.BaseMappersConfig;
import org.cardanofoundation.rosetta.api.block.model.domain.ProtocolParams;
import org.cardanofoundation.rosetta.common.services.ProtocolParamService;

import static org.mockito.Mockito.when;

@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = {ConfigurationMapper.class, SpringMappersTestConfig.class})
@ContextConfiguration(classes = {ConfigurationMapper.class, BaseMappersConfig.class})
public class BaseMapperTest {
@MockBean
protected ProtocolParamService protocolParamService;
Expand All @@ -30,4 +35,16 @@ public void before() {
when(protocolParamService.getProtocolParameters()).thenReturn(protocolParams);
when(protocolParams.getPoolDeposit()).thenReturn(new BigInteger("500"));
}

@TestConfiguration
@ComponentScan(basePackages = {
"org.cardanofoundation.rosetta.api.block.mapper",
"org.cardanofoundation.rosetta.common.mapper"})
public static class BaseMappersConfig {

@Bean
public JsonNullableModule jsonNullableModule() {
return new JsonNullableModule();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.cardanofoundation.rosetta.api;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.web.servlet.MockMvc;
import com.fasterxml.jackson.databind.ObjectMapper;

@SpringBootTest
@AutoConfigureMockMvc
public class BaseSpringMvcTest extends TransactionsTestData {

@Autowired
protected ObjectMapper objectMapper;

@Autowired
protected MockMvc mockMvc;

}
Original file line number Diff line number Diff line change
@@ -1,43 +1,15 @@
package org.cardanofoundation.rosetta.api;

import java.io.File;
import java.io.IOException;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.boot.test.web.server.LocalServerPort;
import org.springframework.context.annotation.Profile;
import org.springframework.transaction.annotation.Transactional;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

import org.junit.jupiter.api.BeforeAll;

import org.cardanofoundation.rosetta.RosettaApiApplication;
import org.cardanofoundation.rosetta.testgenerator.common.TestConstants;
import org.cardanofoundation.rosetta.testgenerator.common.TransactionBlockDetails;

@Profile("test-integration")
@SpringBootTest(
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
classes = {RosettaApiApplication.class})
@Transactional
public abstract class IntegrationTest {

protected static Map<String, TransactionBlockDetails> generatedDataMap;

@Autowired
public TestRestTemplate restTemplate;

@LocalServerPort
protected int serverPort;

@BeforeAll
public static void init(@Autowired ObjectMapper objectMapper) throws IOException {
generatedDataMap = objectMapper.readValue(new File("." + TestConstants.FILE_SAVE_PATH),
new TypeReference<>() {
});
}
public abstract class IntegrationTest extends TransactionsTestData {
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.web.servlet.MockMvc;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

Expand All @@ -17,27 +16,14 @@
import org.cardanofoundation.rosetta.testgenerator.common.TransactionBlockDetails;

@SpringBootTest
@AutoConfigureMockMvc
public class SpringMvcTest {

@Autowired
protected ObjectMapper objectMapper;

@Autowired
protected MockMvc mockMvc;
abstract class TransactionsTestData {

protected static Map<String, TransactionBlockDetails> generatedDataMap;


@Deprecated
protected int serverPort;

@BeforeAll
public static void init() throws IOException {
generatedDataMap = new ObjectMapper().readValue(
public static void init(@Autowired ObjectMapper objectMapper) throws IOException {
generatedDataMap = objectMapper.readValue(
new File("." + TestConstants.FILE_SAVE_PATH),
new TypeReference<>() {
});
new TypeReference<>() {});
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import org.junit.jupiter.api.Test;

import org.cardanofoundation.rosetta.api.SpringMvcTest;
import org.cardanofoundation.rosetta.api.BaseSpringMvcTest;
import org.cardanofoundation.rosetta.api.block.mapper.BlockToBlockResponse;
import org.cardanofoundation.rosetta.api.block.mapper.BlockTxToBlockTxResponse;
import org.cardanofoundation.rosetta.api.block.model.domain.Block;
Expand All @@ -36,7 +36,7 @@
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;


class BlockApiImplTest extends SpringMvcTest {
class BlockApiImplTest extends BaseSpringMvcTest {

@MockBean
private BlockService blockService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

import org.cardanofoundation.rosetta.api.SpringMvcTest;
import org.cardanofoundation.rosetta.api.BaseSpringMvcTest;
import org.cardanofoundation.rosetta.common.util.Constants;
import org.cardanofoundation.rosetta.testgenerator.common.TestConstants;
import org.cardanofoundation.rosetta.testgenerator.common.TestTransactionNames;
Expand All @@ -26,7 +26,7 @@
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

class AccountBalanceApiTest extends SpringMvcTest {
class AccountBalanceApiTest extends BaseSpringMvcTest {

private final String upToBlockHash = generatedDataMap.get(
TestTransactionNames.SIMPLE_LOVELACE_FIRST_TRANSACTION.getName()).blockHash();
Expand All @@ -46,22 +46,6 @@ void accountBalance2Ada_Test() {
accountBalanceResponse.getBalances().getFirst().getCurrency().getSymbol());
}

private AccountBalanceResponse post(AccountBalanceRequest accountBalanceRequest) {
try {
var resp = mockMvc.perform(MockMvcRequestBuilders.post("/account/balance")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(accountBalanceRequest)))
.andDo(print())
.andExpect(status().isOk()) //200
.andReturn()
.getResponse()
.getContentAsString();
return objectMapper.readValue(resp, AccountBalanceResponse.class);
} catch (Exception e) {
throw new AssertionError(e);
}

}

@Test
void accountBalance2Lovelace_Test() {
Expand Down Expand Up @@ -292,4 +276,21 @@ private static void assertAdaCurrency(AccountBalanceResponse accountBalanceRespo
assertEquals(Constants.ADA_DECIMALS,
accountBalanceResponse.getBalances().getFirst().getCurrency().getDecimals());
}

private AccountBalanceResponse post(AccountBalanceRequest accountBalanceRequest) {
try {
var resp = mockMvc.perform(MockMvcRequestBuilders.post("/account/balance")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(accountBalanceRequest)))
.andDo(print())
.andExpect(status().isOk()) //200
.andReturn()
.getResponse()
.getContentAsString();
return objectMapper.readValue(resp, AccountBalanceResponse.class);
} catch (Exception e) {
throw new AssertionError(e);
}

}
}

0 comments on commit 48e2798

Please sign in to comment.