-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implementing integration tests, fixing sonar issues
- Loading branch information
Showing
12 changed files
with
402 additions
and
188 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 16 additions & 11 deletions
27
api/src/test/java/org/cardanofoundation/rosetta/api/IntegrationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
142 changes: 119 additions & 23 deletions
142
api/src/test/java/org/cardanofoundation/rosetta/api/data/account/AccountApiTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
12 changes: 7 additions & 5 deletions
12
api/src/test/java/org/cardanofoundation/rosetta/api/data/block/BlockApiTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.