Skip to content

Commit

Permalink
feat: implementing integration tests, fixing sonar issues
Browse files Browse the repository at this point in the history
  • Loading branch information
BerezinD authored Mar 28, 2024
1 parent 97d2987 commit 976dcfd
Show file tree
Hide file tree
Showing 12 changed files with 402 additions and 188 deletions.
38 changes: 18 additions & 20 deletions api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
<parent>
<artifactId>base</artifactId>
<groupId>org.cardanofoundation.rosetta-java</groupId>
<version>1.0.0-SNAPSHOT</version>
<version>${revision}</version>
</parent>

<artifactId>api</artifactId>
<name>api</name>
<description>Rosetta API implementation for Cardano.</description>
<properties>
<spotbugs-annotations.version>4.8.3</spotbugs-annotations.version>
<argLine/>
</properties>

<packaging>jar</packaging>
Expand Down Expand Up @@ -158,7 +159,7 @@
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.20.2</version>
<version>${version.assertj-core}</version>
<scope>test</scope>
</dependency>

Expand All @@ -168,7 +169,7 @@
</dependency>
<dependency>
<artifactId>hibernate-jpamodelgen</artifactId>
<groupId>org.hibernate</groupId>
<groupId>org.hibernate.orm</groupId>
<version>6.2.0.Final</version>
<scope>provided</scope>
</dependency>
Expand Down Expand Up @@ -236,23 +237,16 @@
<artifactId>modelmapper-spring</artifactId>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${assertj-core.version}</version>
<groupId>${project.groupId}</groupId>
<artifactId>test-data-generator</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.cardanofoundation.rosetta-java</groupId>
<artifactId>test-data-generator</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>2.2.224</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down Expand Up @@ -377,12 +371,16 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.0</version>
<version>${version.maven-surefire-plugin}</version>
<configuration>
<argLine>-XX:+EnableDynamicAgentLoading</argLine>
<argLine>${argLine}</argLine>
</configuration>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.11</version>
<version>${version.jacoco-maven-plugin}</version>
<configuration>
<excludes>
<exclude>**/org/openapitools/**</exclude>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,34 +1,39 @@
package org.cardanofoundation.rosetta.api;

import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.File;
import java.io.IOException;
import org.cardanofoundation.rosetta.RosettaApiApplication;
import org.cardanofoundation.rosetta.testgenerator.common.TestConstants;
import org.cardanofoundation.rosetta.testgenerator.common.GeneratedTestDataDTO;
import org.junit.jupiter.api.BeforeAll;

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 org.springframework.web.client.RestTemplate;
import com.fasterxml.jackson.databind.ObjectMapper;

import org.junit.jupiter.api.BeforeAll;

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

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

protected static RestTemplate restTemplate;
protected static GeneratedTestDataDTO generatedTestData;

@Autowired
public TestRestTemplate restTemplate;

@LocalServerPort
protected int serverPort;

@BeforeAll
public static void init() throws IOException {
restTemplate = new RestTemplate();
ObjectMapper objectMapper = new ObjectMapper();
generatedTestData = objectMapper.readValue(new File("." + TestConstants.FILE_SAVE_PATH), GeneratedTestDataDTO.class);
public static void init(@Autowired ObjectMapper objectMapper) throws IOException {
generatedTestData = objectMapper.readValue(new File("." + TestConstants.FILE_SAVE_PATH),
GeneratedTestDataDTO.class);
}
}
Original file line number Diff line number Diff line change
@@ -1,45 +1,141 @@
package org.cardanofoundation.rosetta.api.data.account;

import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.HashMap;

import org.cardanofoundation.rosetta.api.IntegrationTest;
import org.cardanofoundation.rosetta.api.account.service.AccountService;
import org.cardanofoundation.rosetta.testgenerator.common.TestConstants;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.ResponseEntity;
import org.jetbrains.annotations.NotNull;
import org.openapitools.client.model.AccountBalanceRequest;
import org.openapitools.client.model.AccountBalanceResponse;
import org.openapitools.client.model.AccountCoinsRequest;
import org.openapitools.client.model.AccountCoinsResponse;
import org.openapitools.client.model.AccountIdentifier;
import org.springframework.beans.factory.annotation.Autowired;
import org.openapitools.client.model.Error;
import org.openapitools.client.model.NetworkIdentifier;

import org.junit.jupiter.api.Test;

import org.cardanofoundation.rosetta.api.IntegrationTest;
import org.cardanofoundation.rosetta.api.account.controller.AccountApiImplementation;
import org.cardanofoundation.rosetta.testgenerator.common.TestConstants;

import static org.cardanofoundation.rosetta.testgenerator.common.TestConstants.URL;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

public class AccountApiTest extends IntegrationTest {
class AccountApiTest extends IntegrationTest {

@Autowired
private AccountService accountService;
private AccountApiImplementation accountApi;

@Test
void accountBalance2Ada_Test() {
ResponseEntity<AccountBalanceResponse> response = restTemplate.postForEntity(
getAccountBalanceUrl(), getAccountBalanceRequest(), AccountBalanceResponse.class);
AccountBalanceResponse accountBalanceResponse = response.getBody();

assertEquals(HttpStatusCode.valueOf(200), response.getStatusCode());
assertNotNull(accountBalanceResponse);
assertEquals(TestConstants.ACCOUNT_BALANCE_ADA_AMOUNT,
accountBalanceResponse.getBalances().getFirst().getValue());
assertEquals(TestConstants.ADA_SYMBOL,
accountBalanceResponse.getBalances().getFirst().getCurrency().getSymbol());
}

@Test
public void accountBalance2Ada_Test() {
AccountBalanceResponse accountBalance = accountService.getAccountBalance(
AccountBalanceRequest.builder()
.accountIdentifier(AccountIdentifier.builder()
.address(TestConstants.TEST_ACCOUNT_ADDRESS)
.build())
.build());
void accountBalanceException_Test() {
AccountBalanceRequest accountBalanceRequest = getAccountBalanceRequest();
accountBalanceRequest.getAccountIdentifier().setAddress("invalid_address");
ResponseEntity<Error> response = restTemplate.postForEntity(
getAccountBalanceUrl(), accountBalanceRequest, Error.class);
Error accountBalanceError = response.getBody();

assertEquals(TestConstants.ACCOUNT_BALANCE_ADA_AMOUNT, accountBalance.getBalances().get(0).getValue());
assertEquals(HttpStatusCode.valueOf(500), response.getStatusCode());
assertNotNull(accountBalanceError);
assertEquals("Provided address is invalid", accountBalanceError.getMessage());
assertEquals("invalid_address",
((HashMap<String, String>) accountBalanceError.getDetails()).get("message"));
assertEquals(4015, accountBalanceError.getCode());
}

@Test
public void accountCoins2Ada_Test() {
AccountCoinsResponse accountCoins = accountService.getAccountCoins(AccountCoinsRequest.builder()
void accountCoins2Ada_Test() {
ResponseEntity<AccountCoinsResponse> response = restTemplate.postForEntity(
getAccountCoinsUrl(), getAccountCoinsRequest(TestConstants.TEST_ACCOUNT_ADDRESS),
AccountCoinsResponse.class);
AccountCoinsResponse accountCoinsResponse = response.getBody();

assertEquals(HttpStatusCode.valueOf(200), response.getStatusCode());
assertNotNull(accountCoinsResponse);
assertEquals(1, accountCoinsResponse.getCoins().size());
assertEquals(TestConstants.ACCOUNT_BALANCE_ADA_AMOUNT,
accountCoinsResponse.getCoins().getFirst().getAmount().getValue());
}

@Test
void accountCoins2Lovelace_Test() {
AccountCoinsRequest accountCoinsRequest = getAccountCoinsRequest(
TestConstants.RECEIVER_1);
ResponseEntity<AccountCoinsResponse> response = restTemplate.postForEntity(
getAccountCoinsUrl(), accountCoinsRequest, AccountCoinsResponse.class);
AccountCoinsResponse accountCoinsResponse = response.getBody();

assertEquals(HttpStatusCode.valueOf(200), response.getStatusCode());
assertNotNull(accountCoinsResponse);
}

@Test
void accountCoinsException_Test() {
AccountCoinsRequest accountCoinsRequest = getAccountCoinsRequest(
TestConstants.TEST_ACCOUNT_ADDRESS);
accountCoinsRequest.getAccountIdentifier().setAddress("invalid_address");
ResponseEntity<Error> response = restTemplate.postForEntity(
getAccountCoinsUrl(), accountCoinsRequest, Error.class);
Error accountCoinsError = response.getBody();

assertEquals(HttpStatusCode.valueOf(500), response.getStatusCode());
assertNotNull(accountCoinsError);
assertEquals("Provided address is invalid", accountCoinsError.getMessage());
assertEquals("invalid_address",
((HashMap<String, String>) accountCoinsError.getDetails()).get("message"));
assertEquals(4015, accountCoinsError.getCode());
}

@NotNull
private String getAccountBalanceUrl() {
String accountBalancePath = "/account/balance";
return URL + serverPort + accountBalancePath;
}

@NotNull
private String getAccountCoinsUrl() {
String accountCoinsPath = "/account/coins";
return URL + serverPort + accountCoinsPath;
}

private AccountCoinsRequest getAccountCoinsRequest(String accountAddress) {
return AccountCoinsRequest.builder()
.networkIdentifier(NetworkIdentifier.builder()
.blockchain(TestConstants.TEST_BLOCKCHAIN)
.network(TestConstants.TEST_NETWORK)
.build())
.accountIdentifier(AccountIdentifier.builder()
.address(TestConstants.TEST_ACCOUNT_ADDRESS)
.address(accountAddress)
.build())
.build());
assertEquals(1, accountCoins.getCoins().size());
assertEquals(
TestConstants.ACCOUNT_BALANCE_ADA_AMOUNT, accountCoins.getCoins().get(0).getAmount().getValue());
.includeMempool(true)
.build();
}

private AccountBalanceRequest getAccountBalanceRequest() {
return AccountBalanceRequest.builder()
.networkIdentifier(NetworkIdentifier.builder()
.blockchain(TestConstants.TEST_BLOCKCHAIN)
.network(TestConstants.TEST_NETWORK)
.build())
.accountIdentifier(AccountIdentifier.builder()
.address(TestConstants.TEST_ACCOUNT_ADDRESS)
.build())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
package org.cardanofoundation.rosetta.api.data.block;

import static org.junit.jupiter.api.Assertions.assertEquals;
import org.springframework.beans.factory.annotation.Autowired;

import org.junit.jupiter.api.Test;

import org.cardanofoundation.rosetta.api.IntegrationTest;
import org.cardanofoundation.rosetta.api.account.model.domain.Utxo;
import org.cardanofoundation.rosetta.api.block.model.domain.Block;
import org.cardanofoundation.rosetta.api.block.service.BlockService;
import org.cardanofoundation.rosetta.testgenerator.common.TestConstants;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;

public class BlockApiTest extends IntegrationTest {
import static org.junit.jupiter.api.Assertions.assertEquals;

class BlockApiTest extends IntegrationTest {

@Autowired
@SuppressWarnings("unused")
private BlockService blockService;

@Test
public void getBlockWithTransaction_Test() {
void getBlockWithTransaction_Test() {
Block block = blockService.findBlock(generatedTestData.getTopUpBlockNumber(),
generatedTestData.getTopUpBlockHash());

Expand Down
Loading

0 comments on commit 976dcfd

Please sign in to comment.