Skip to content

Commit

Permalink
RA-105: Increasing code coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
Aleksandr Sologub committed May 20, 2024
1 parent db46c38 commit a3ac598
Show file tree
Hide file tree
Showing 13 changed files with 339 additions and 49 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=cardano-foundation_cardano-rosetta-java&metric=alert_status)](https://sonarcloud.io/summary/new_code?id=cardano-foundation_cardano-rosetta-java)
[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=cardano-foundation_cardano-rosetta-java&metric=alert_status)](https://sonarcloud.io/summary/new_code?id=cardano-foundation_cardano-rosetta-java) [![Coverage](https://sonarcloud.io/api/project_badges/measure?project=cardano-foundation_cardano-rosetta-java&metric=coverage)](https://sonarcloud.io/summary/new_code?id=cardano-foundation_cardano-rosetta-java)

## What the project is about?

Expand Down
18 changes: 0 additions & 18 deletions api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,6 @@
<properties>
<spotbugs-annotations.version>4.8.3</spotbugs-annotations.version>
<argLine/>
<test.exclusions>
**/org/openapitools/**,
**/model/**,
**/entity/**,
**/rosetta/config/**,
**/rosetta/common/enumeration/NetworkId.java,
**/rosetta/common/enumeration/OperationTypeStatus.java,
**/rosetta/common/enumeration/RewardType.java,
**/rosetta/common/enumeration/SyncStateType.java,
**/rosetta/common/enumeration/ScriptPurposetype.java,
**/rosetta/api/mempool/service/MempoolServiceImpl.java,
**/rosetta/common/exception/ServerException.java,
**/rosetta/common/exception/Error.java,
**/rosetta/common/exception/Details.java,
**/rosetta/common/exception/ApiException.java
</test.exclusions>
<sonar.sources>src/main/java</sonar.sources>
<sonar.coverage.exclusions>${test.exclusions}</sonar.coverage.exclusions>
</properties>

<packaging>jar</packaging>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,6 @@ public static ApiException missingChainCodeError() {
return new ApiException(RosettaErrorType.CHAIN_CODE_MISSING.toRosettaError(false));
}

public static ApiException cantBuildWitnessesSet() {
return new ApiException(RosettaErrorType.CANT_BUILD_WITNESSES_SET.toRosettaError(false));
}

public static ApiException outputsAreBiggerThanInputsError() {
return new ApiException(
RosettaErrorType.OUTPUTS_BIGGER_THAN_INPUTS_ERROR.toRosettaError(false));
Expand All @@ -81,10 +77,6 @@ public static ApiException invalidPoolRegistrationCertType() {
return new ApiException(RosettaErrorType.INVALID_POOL_CERT_TYPE.toRosettaError(false));
}

public static ApiException invalidPublicKeyFormat() {
return new ApiException(RosettaErrorType.INVALID_PUBLIC_KEY_FORMAT.toRosettaError(false));
}

public static ApiException publicKeyMissing() {
return new ApiException(RosettaErrorType.PUBLIC_KEY_MISSING.toRosettaError(false));
}
Expand Down Expand Up @@ -161,10 +153,6 @@ public static ApiException invalidPoolRegistrationParameters(String value) {
Details.builder().message(value).build()));
}

public static ApiException addressGenerationError() {
return new ApiException(RosettaErrorType.ADDRESS_GENERATION_ERROR.toRosettaError(false));
}

public static ApiException transactionInputsParametersMissingError(String error) {
return new ApiException(
RosettaErrorType.TRANSACTION_INPUTS_PARAMETERS_MISSING_ERROR.toRosettaError(false,
Expand Down Expand Up @@ -232,15 +220,6 @@ public static ApiException invalidPolicyIdError(String details) {
Details.builder().message(details).build()));
}

public static ApiException submitRejected(String detail) {
return new ApiException(RosettaErrorType.SEND_TRANSACTION_ERROR.toRosettaError(true,
Details.builder().message(detail).build()));
}

public static ApiException outputTooLow() {
return unspecifiedError("The outputAmount is too low. Try with more funds.");
}

public static ApiException deserializationError(String details) {
return new ApiException(
RosettaErrorType.TRANSACTION_INPUT_DESERIALIZATION_ERROR.toRosettaError(false,
Expand All @@ -251,10 +230,6 @@ public static ApiException invalidNetworkError() {
return new ApiException(RosettaErrorType.INVALID_NETWORK.toRosettaError(false));
}

public static ApiException cantDecodeMemPoolTransaction() {
return new ApiException(RosettaErrorType.CANT_DECODE_MEMPOOL_TRANSACTION.toRosettaError(false));
}

public static ApiException sendTransactionError(String error) {
return new ApiException(
RosettaErrorType.SEND_TRANSACTION_ERROR.toRosettaError(false, null, error));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ private static void addTokenBundleToMap(List<TokenBundleItem> tokenBundle, Map m
Map tokenBundleMap = new Map();

addTokensToMap(tokenBundleItem.getTokens(), tokenBundleMap);
putStringDataItemToMap(tokenBundleMap,Constants.POLICYID,tokenBundleItem.getPolicyId());
putStringDataItemToMap(tokenBundleMap, Constants.POLICYID, tokenBundleItem.getPolicyId());
tokenBundleArray.add(tokenBundleMap);
});
metadataMap.put(key(Constants.TOKENBUNDLE), tokenBundleArray);
Expand Down Expand Up @@ -365,7 +365,7 @@ private static void addAccountIdentifierToMap(AccountIdentifier account, Map ope
.ifPresent(accountIdentifier -> {
co.nstant.in.cbor.model.Map accountIdentifierMap = new co.nstant.in.cbor.model.Map();
// fill map
putStringDataItemToMap(accountIdentifierMap,Constants.ADDRESS,account.getAddress());
putStringDataItemToMap(accountIdentifierMap, Constants.ADDRESS, account.getAddress());
addSubAccountToMap(account, accountIdentifierMap);
operationMap.put(key(Constants.ACCOUNT), accountIdentifierMap);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.mockito.MockedStatic;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;
import org.openapitools.client.model.Operation;
import org.openapitools.client.model.PublicKey;

import org.junit.jupiter.api.BeforeEach;
Expand Down Expand Up @@ -79,6 +80,30 @@ void calculateFeeTest() {
assertEquals(0L, l);
}

@Test
void outputMoreThanInputTest() {
List<BigInteger> inputAmounts = List.of(BigInteger.valueOf(-5L));
List<BigInteger> outputAmounts = List.of(BigInteger.valueOf(2L));
List<BigInteger> withdrawalAmounts = List.of(BigInteger.valueOf(7L));
Map<String, Double> depositsSumMap = Map.of(Constants.KEY_REFUNDS_SUM, (double) 6L,
Constants.KEY_DEPOSITS_SUM, (double) 2L, Constants.POOL_DEPOSITS_SUM, (double) 2L);
ApiException exception = assertThrows(ApiException.class,
() -> cardanoService.calculateFee(inputAmounts, outputAmounts, withdrawalAmounts, depositsSumMap));

assertEquals(4010, exception.getError().getCode());
assertEquals("The transaction you are trying to build has more outputs than inputs", exception.getError().getMessage());
}

@SuppressWarnings("java:S5778")
@Test
void convertRosettaOperationsWOOperationType() {
ApiException exception = assertThrows(ApiException.class,
() -> cardanoService.convertRosettaOperations(NetworkIdentifierType.CARDANO_MAINNET_NETWORK, List.of(new Operation())));

assertEquals(4019, exception.getError().getCode());
assertEquals("Provided operation type is invalid", exception.getError().getMessage());
}

@Test
void extractTransactionRosettaTxTest() {
String packedRosettaTransaction = "827901d4383461343030383138323538323037663639306539666234313034383732636136613361353961373636363663633236303737303064323236323839313832643933626564646266393536363138303130313832383235383164363066306233656133363935343261353834383239306237323736343862353833386235646161323535396465626435356466636161316632303161303031653834383038323538316436303564643239333565396262333530383331353062633834326661376661303935623734306335636436363031376532623633313531336532316130353932343937393032316130303032616533643033316130333638313930366131303038313832353832303337363939653262386432333835613031623538393439333638336266326435356635396331646463316637306137303466623936356331613930666633303935383430303935656366376431363731333564653064343831353433306662343037396466383563353865316564633930346363383461323864323361633336393839616631373939653463633662343865383738303131366532383263303365343630353738626337663766353266663663613161616632653138336162643835303166356636a26a6f7065726174696f6e7381a6746f7065726174696f6e5f6964656e746966696572a165696e64657800676163636f756e74a16761646472657373783f616464725f746573743176707761397936376e776534707163347030797939376e6c357a326d7773783965346e717a6c33747676323338637367716b616c6c66616d6f756e74a26863757272656e6379a26673796d626f6c6341444168646563696d616c73066576616c7565692d39353634383832326b636f696e5f6368616e6765a26f636f696e5f6964656e746966696572a16a6964656e7469666965727842376636393065396662343130343837326361366133613539613736363636636332363037373030643232363238393138326439336265646462663935363631383a316b636f696e5f616374696f6e6a636f696e5f7370656e746673746174757360647479706565696e707574767472616e73616374696f6e4d6574616461746148657860";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import org.openapitools.client.model.ConstructionDeriveRequest;
import org.openapitools.client.model.ConstructionDeriveResponse;
import org.openapitools.client.model.NetworkIdentifier;

import org.junit.jupiter.api.Test;

import org.cardanofoundation.rosetta.api.IntegrationTest;
import org.cardanofoundation.rosetta.common.exception.ApiException;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

class DeriveApiTest extends IntegrationTest {

Expand All @@ -37,4 +40,17 @@ void deriveAddressTest() throws IOException {
assertEquals(address, constructionDeriveResponse.getAccountIdentifier().getAddress());
}

@Test
void deriveAddressTestWithInvalidNetworkConfigurationTest() throws IOException {
ConstructionDeriveRequest deriveRequest = getDeriveRequest(
"testdata/construction/derive/derive_request.json");
deriveRequest.setNetworkIdentifier(new NetworkIdentifier());
ApiException exception = assertThrows(ApiException. class,
() -> constructionApiService.constructionDeriveService(
deriveRequest));
assertEquals(4000, exception.getError().getCode());
assertEquals("Invalid Network configuration", exception.getError().getMessage());

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package org.cardanofoundation.rosetta.common.mapper;

import java.util.List;

import co.nstant.in.cbor.model.Map;
import org.mockito.junit.jupiter.MockitoExtension;
import org.openapitools.client.model.AccountIdentifier;
import org.openapitools.client.model.AccountIdentifierMetadata;
import org.openapitools.client.model.Amount;
import org.openapitools.client.model.Currency;
import org.openapitools.client.model.CurrencyMetadata;
import org.openapitools.client.model.Operation;
import org.openapitools.client.model.OperationIdentifier;
import org.openapitools.client.model.OperationMetadata;
import org.openapitools.client.model.PoolMargin;
import org.openapitools.client.model.PoolMetadata;
import org.openapitools.client.model.PoolRegistrationParams;
import org.openapitools.client.model.Relay;
import org.openapitools.client.model.SubAccountIdentifier;
import org.openapitools.client.model.TokenBundleItem;

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

import org.cardanofoundation.rosetta.common.util.Constants;

import static org.cardanofoundation.rosetta.common.mapper.OperationToCborMap.convertToCborMap;
import static org.junit.jupiter.api.Assertions.assertEquals;

@ExtendWith(MockitoExtension.class)
class OperationToCborMapTest {

@Test
void convertToCborMapAndViseVersaTest() {
//given
AccountIdentifier accountIdentifier = new AccountIdentifier("address", new SubAccountIdentifier("address", null),
new AccountIdentifierMetadata());
PoolRegistrationParams poolRegistrationParams = PoolRegistrationParams
.builder()
.poolOwners(List.of("owner"))
.margin(new PoolMargin("numerator", "denominator"))
.relays(List.of(new Relay("ipv4", "ipv6", "name", 8080, "type")))
.poolMetadata(new PoolMetadata("url", "hash"))
.build();
OperationMetadata operationMetadata = OperationMetadata.builder()
.poolRegistrationParams(poolRegistrationParams)
.refundAmount(new Amount("2", new Currency(Constants.ADA, 2, new CurrencyMetadata("policyId")), new Object()))
.tokenBundle(List.of(new TokenBundleItem("tokenBunlePolicyId", List.of(new Amount()))))
.build();
Operation operation = Operation
.builder()
.operationIdentifier(new OperationIdentifier())
.account(accountIdentifier)
.type("poolRegistration")
.metadata(operationMetadata)
.build();
//when
Map map = convertToCborMap(operation);
Operation opr = CborMapToOperation.cborMapToOperation(map);
//then
assertEquals(4, map.getKeys().size());
assertEquals(operation.getAccount().getAddress(), opr.getAccount().getAddress());
assertEquals(operation.getAccount().getSubAccount(), opr.getAccount().getSubAccount());
assertEquals(operation.getOperationIdentifier(), opr.getOperationIdentifier());
assertEquals(operation.getMetadata().getPoolRegistrationParams().getPoolOwners(), opr.getMetadata().getPoolRegistrationParams().getPoolOwners());
assertEquals(operation.getMetadata().getPoolRegistrationParams().getRelays().getFirst().getDnsName(),
opr.getMetadata().getPoolRegistrationParams().getRelays().getFirst().getDnsName());
assertEquals(operation.getMetadata().getPoolRegistrationParams().getMargin(), opr.getMetadata().getPoolRegistrationParams().getMargin());
assertEquals(operation.getMetadata().getPoolRegistrationParams().getPoolMetadata(), opr.getMetadata().getPoolRegistrationParams().getPoolMetadata());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.cardanofoundation.rosetta.common.mapper;

import java.util.List;

import org.mockito.junit.jupiter.MockitoExtension;
import org.openapitools.client.model.Operation;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

import org.cardanofoundation.rosetta.common.enumeration.NetworkIdentifierType;

import static org.cardanofoundation.rosetta.common.mapper.TransactionDataToOperations.*;

@ExtendWith(MockitoExtension.class)
class TransactionDataToOperationsTest {

@Test
void getPoolSignersFromOperationWOPoolSigners() {
//given
Operation operation = Operation
.builder()
.type("poolRegistration")
.build();
//when
List<String> poolSigners = getPoolSigners(NetworkIdentifierType.CARDANO_MAINNET_NETWORK, operation);
//then
Assertions.assertEquals(0, poolSigners.size());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

class CborEncodeUtilTest {


@Test
void encodeSimpleInputOutputTest()
throws IOException, CborException, AddressExcepion, CborSerializationException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import co.nstant.in.cbor.CborException;
import com.bloxbean.cardano.client.exception.CborSerializationException;
import com.bloxbean.cardano.client.spec.UnitInterval;
import com.bloxbean.cardano.client.transaction.spec.MultiAsset;
import com.bloxbean.cardano.client.transaction.spec.TransactionInput;
import com.bloxbean.cardano.client.transaction.spec.cert.PoolRegistration;
import com.bloxbean.cardano.client.transaction.spec.cert.SingleHostAddr;
import org.openapitools.client.model.Operation;
Expand All @@ -23,13 +25,19 @@
import org.cardanofoundation.rosetta.common.exception.ApiException;

import static org.cardanofoundation.rosetta.common.util.ParseConstructionUtil.addRelayToPoolReLayOfTypeSingleHostAddr;
import static org.cardanofoundation.rosetta.common.util.ParseConstructionUtil.parseAsset;
import static org.cardanofoundation.rosetta.common.util.ParseConstructionUtil.parsePoolCertToOperation;
import static org.cardanofoundation.rosetta.common.util.ParseConstructionUtil.parsePoolRegistration;
import static org.cardanofoundation.rosetta.common.util.ParseConstructionUtil.parseTokenAsset;
import static org.cardanofoundation.rosetta.common.util.ParseConstructionUtil.parseVoteMetadataToOperation;
import static org.cardanofoundation.rosetta.common.util.ParseConstructionUtil.parseWithdrawalToOperation;
import static org.cardanofoundation.rosetta.common.util.ParseConstructionUtil.transactionInputToOperation;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

@SuppressWarnings("java:S5778")
class ParseConstructionUtilTest {

@Test
Expand Down Expand Up @@ -65,7 +73,7 @@ void parsePoolRegiStrationTest() {
@Test
void parseVoteMetadataToOperationWOVoteRegMetadataTest() {
//when //then
ApiException exception = Assertions.assertThrows(ApiException.class,
ApiException exception = assertThrows(ApiException.class,
() -> parseVoteMetadataToOperation(1L,""));

assertEquals("Missing vote registration metadata", exception.getError().getMessage());
Expand All @@ -91,4 +99,44 @@ void addRelayToPoolReLayOfTypeSingleHostAddrTest() {
assertEquals("SINGLE_HOST_ADDR", relays.getFirst().getIpv4());
}

@Test
void parseAssetWOAssetTest() {
//when //then
ApiException exception = assertThrows(ApiException.class,
() -> parseAsset(List.of(), ""));
assertEquals("Asset value is required for token asset", exception.getError().getMessage());
assertEquals(4022, exception.getError().getCode());
}

@Test
void parseAssetWithEmptyAssetTest() {
//when //then
ApiException exception = assertThrows(ApiException.class,
() -> parseTokenAsset(List.of(new MultiAsset()), ""));
assertEquals("Assets are required for output operation token bundle", exception.getError().getMessage());
assertEquals(4021, exception.getError().getCode());
}

@Test
void withdrawalToOperationTest() {
//when //then
Operation operation = parseWithdrawalToOperation("value", "hex", 1L, "address");
assertEquals("withdrawal", operation.getType());
assertEquals("", operation.getStatus());
assertEquals("address", operation.getAccount().getAddress());
assertEquals("value", operation.getAmount().getValue());
assertEquals("hex", operation.getMetadata().getStakingCredential().getHexBytes());
}

@Test
void transactionInputToOperationTest() {
//when //then
Operation operation = transactionInputToOperation(new TransactionInput("id", 1), 1L);
assertEquals("input", operation.getType());
assertEquals("", operation.getStatus());
assertEquals(1L, operation.getOperationIdentifier().getIndex());
assertEquals("id:1", operation.getCoinChange().getCoinIdentifier().getIdentifier());
assertEquals("coin_spent", operation.getCoinChange().getCoinAction().getValue());
}

}

0 comments on commit a3ac598

Please sign in to comment.