Skip to content

Commit

Permalink
test(case): optimize case
Browse files Browse the repository at this point in the history
  • Loading branch information
forfreeday committed Jan 15, 2024
1 parent 32f210d commit 1225ace
Show file tree
Hide file tree
Showing 9 changed files with 457 additions and 98 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package org.tron.core.actuator;

import com.google.protobuf.ByteString;
import java.util.List;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.tron.common.BaseTest;
import org.tron.common.utils.ByteArray;
import org.tron.core.Constant;
import org.tron.core.Wallet;
import org.tron.core.capsule.AccountCapsule;
import org.tron.core.capsule.TransactionCapsule;
import org.tron.core.config.args.Args;
import org.tron.protos.Protocol.AccountType;
import org.tron.protos.contract.BalanceContract.TransferContract;

public class ActuatorFactoryTest extends BaseTest {

private static final String OWNER_ADDRESS = Wallet.getAddressPreFixString()
+ "548794500882809695a8a687866e76d4271a1abc";
private static final String TO_ADDRESS = Wallet.getAddressPreFixString()
+ "abd4b9367799eaa3197fecb144eb71de1e049abc";

static {
Args.setParam(
new String[] {
"--output-directory", dbPath()
},
Constant.TEST_CONF
);
}

private TransferContract getContract(long count, String owneraddress, String toaddress) {
return TransferContract.newBuilder()
.setOwnerAddress(ByteString.copyFrom(ByteArray.fromHexString(owneraddress)))
.setToAddress(ByteString.copyFrom(ByteArray.fromHexString(toaddress)))
.setAmount(count)
.build();
}

@Before
public void createCapsule() {
AccountCapsule ownerCapsule =
new AccountCapsule(
ByteString.copyFromUtf8("owner"),
ByteString.copyFrom(ByteArray.fromHexString(OWNER_ADDRESS)),
AccountType.Normal,
10000L);
AccountCapsule toAccountCapsule =
new AccountCapsule(
ByteString.copyFromUtf8("toAccount"),
ByteString.copyFrom(ByteArray.fromHexString(TO_ADDRESS)),
AccountType.Normal,
10L);
dbManager.getAccountStore().put(ownerCapsule.getAddress().toByteArray(), ownerCapsule);
dbManager.getAccountStore().put(toAccountCapsule.getAddress().toByteArray(), toAccountCapsule);
}


@Test
public void testCreateActuator() {
TransferContract contract = getContract(10L, OWNER_ADDRESS, TO_ADDRESS);
TransactionCapsule trx = new TransactionCapsule(contract,
chainBaseManager.getAccountStore());
List<Actuator> actList = ActuatorFactory.createActuator(trx, chainBaseManager);

Assert.assertEquals(1, actList.size());
}

}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,18 +1,28 @@
package org.tron.core.services.http;

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.tron.common.utils.client.utils.HttpMethed.createRequest;

import com.alibaba.fastjson.JSONObject;
import com.google.protobuf.ByteString;

import java.io.UnsupportedEncodingException;
import javax.annotation.Resource;

import org.apache.http.client.methods.HttpPost;

import org.junit.Assert;
import org.junit.Test;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.tron.common.BaseTest;
import org.tron.common.utils.ByteArray;
import org.tron.core.Constant;
import org.tron.core.capsule.ContractCapsule;
import org.tron.core.config.args.Args;
import org.tron.protos.contract.SmartContractOuterClass;

public class ClearABIServletTest extends BaseTest {

Expand All @@ -27,11 +37,39 @@ public class ClearABIServletTest extends BaseTest {
@Resource
private ClearABIServlet clearABIServlet;

private static final String SMART_CONTRACT_NAME = "smart_contract_test";
private static String CONTRACT_ADDRESS = "A0B4750E2CD76E19DCA331BF5D089B71C3C2798548";
private static String OWNER_ADDRESS;
private static final long SOURCE_ENERGY_LIMIT = 10L;



private SmartContractOuterClass.SmartContract.Builder createContract(
String contractAddress, String contractName) {
OWNER_ADDRESS =
"A099357684BC659F5166046B56C95A0E99F1265CBD";
SmartContractOuterClass.SmartContract.Builder builder =
SmartContractOuterClass.SmartContract.newBuilder();
builder.setName(contractName);
builder.setOriginAddress(ByteString.copyFrom(ByteArray.fromHexString(OWNER_ADDRESS)));
builder.setContractAddress(ByteString.copyFrom(ByteArray.fromHexString(contractAddress)));
builder.setOriginEnergyLimit(SOURCE_ENERGY_LIMIT);
return builder;
}

@Test
public void testClear() {
String jsonParam = "{\n"
+ " \"owner_address\": \"41a7d8a35b260395c14aa456297662092ba3b76fc0\",\n"
+ " \"contract_address\": \"417bcb781f4743afaacf9f9528f3ea903b3782339f\"\n"
public void testClearABI() {
chainBaseManager.getDynamicPropertiesStore()
.saveAllowTvmConstantinople(1);
SmartContractOuterClass.SmartContract.Builder contract =
createContract(CONTRACT_ADDRESS, SMART_CONTRACT_NAME);
chainBaseManager.getContractStore().put(
ByteArray.fromHexString(CONTRACT_ADDRESS),
new ContractCapsule(contract.build()));

String jsonParam = "{"
+ " \"owner_address\": \"A099357684BC659F5166046B56C95A0E99F1265CBD\","
+ " \"contract_address\": \"A0B4750E2CD76E19DCA331BF5D089B71C3C2798548\""
+ "}";
MockHttpServletRequest request = createRequest(HttpPost.METHOD_NAME);
request.setContentType("application/json");
Expand All @@ -40,6 +78,15 @@ public void testClear() {
MockHttpServletResponse response = new MockHttpServletResponse();
clearABIServlet.doPost(request, response);
Assert.assertEquals(200, response.getStatus());
try {
String contentAsString = response.getContentAsString();
JSONObject result = JSONObject.parseObject(contentAsString);
assertTrue(result.containsKey("raw_data"));
assertTrue(result.containsKey("txID"));
} catch (UnsupportedEncodingException e) {
fail(e.getMessage());
}

}

}
Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@
package org.tron.core.services.http;

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.junit.Assert.fail;
import static org.tron.common.utils.client.utils.HttpMethed.createRequest;

import com.alibaba.fastjson.JSONObject;
import com.google.protobuf.ByteString;

import java.io.UnsupportedEncodingException;
import javax.annotation.Resource;

import org.apache.http.client.methods.HttpPost;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.tron.common.BaseTest;
import org.tron.common.utils.ByteArray;
import org.tron.core.Constant;
import org.tron.core.capsule.AccountCapsule;
import org.tron.core.config.args.Args;
import org.tron.protos.Protocol;


public class CreateAccountServletTest extends BaseTest {
Expand All @@ -28,11 +37,23 @@ public class CreateAccountServletTest extends BaseTest {
@Resource
private CreateAccountServlet createAccountServlet;

@Before
public void init() {
AccountCapsule accountCapsule = new AccountCapsule(
ByteString.copyFrom(ByteArray
.fromHexString("A099357684BC659F5166046B56C95A0E99F1265CD1")),
ByteString.copyFromUtf8("owner"),
Protocol.AccountType.forNumber(1));

chainBaseManager.getAccountStore().put(accountCapsule.createDbKey(),
accountCapsule);
}

@Test
public void testCreate() {
String jsonParam = "{"
+ "\"owner_address\": \"41d1e7a6bc354106cb410e65ff8b181c600ff14292\","
+ "\"account_address\": \"41e552f6487585c2b58bc2c9bb4492bc1f17132cd0\""
+ "\"owner_address\": \"A099357684BC659F5166046B56C95A0E99F1265CD1\","
+ "\"account_address\": \"A0B4750E2CD76E19DCA331BF5D089B71C3C2798541\""
+ "}";
MockHttpServletRequest request = createRequest(HttpPost.METHOD_NAME);
request.setContentType("application/json");
Expand All @@ -42,5 +63,13 @@ public void testCreate() {
createAccountServlet.doPost(request, response);

Assert.assertEquals(200, response.getStatus());
try {
String contentAsString = response.getContentAsString();
JSONObject result = JSONObject.parseObject(contentAsString);
Assert.assertTrue(result.containsKey("raw_data"));
Assert.assertTrue(result.containsKey("txID"));
} catch (UnsupportedEncodingException e) {
fail(e.getMessage());
}
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@
package org.tron.core.services.http;

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.junit.Assert.fail;
import static org.tron.common.utils.client.utils.HttpMethed.createRequest;

import com.alibaba.fastjson.JSONObject;
import com.google.protobuf.ByteString;

import java.io.UnsupportedEncodingException;
import javax.annotation.Resource;

import org.apache.http.client.methods.HttpPost;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.tron.common.BaseTest;
import org.tron.common.utils.ByteArray;
import org.tron.core.Constant;
import org.tron.core.capsule.AccountCapsule;
import org.tron.core.config.args.Args;
import org.tron.protos.Protocol;

public class CreateAssetIssueServletTest extends BaseTest {

Expand All @@ -24,14 +33,26 @@ public class CreateAssetIssueServletTest extends BaseTest {
);
}


@Resource
private CreateAssetIssueServlet createAssetIssueServlet;

@Before
public void init() {
AccountCapsule accountCapsule = new AccountCapsule(
ByteString.copyFrom(ByteArray
.fromHexString("A099357684BC659F5166046B56C95A0E99F1265CD1")),
ByteString.copyFromUtf8("owner"),
Protocol.AccountType.forNumber(1));
accountCapsule.setBalance(10000000000L);

chainBaseManager.getAccountStore().put(accountCapsule.createDbKey(),
accountCapsule);
}

@Test
public void testCreate() {
String jsonParam = "{"
+ " \"owner_address\": \"41e552f6487585c2b58bc2c9bb4492bc1f17132cd0\","
+ " \"owner_address\": \"A099357684BC659F5166046B56C95A0E99F1265CD1\","
+ " \"name\": \"0x6173736574497373756531353330383934333132313538\","
+ " \"abbr\": \"0x6162627231353330383934333132313538\","
+ " \"total_supply\": 4321,"
Expand All @@ -55,6 +76,14 @@ public void testCreate() {
MockHttpServletResponse response = new MockHttpServletResponse();
createAssetIssueServlet.doPost(request, response);
Assert.assertEquals(200, response.getStatus());
try {
String contentAsString = response.getContentAsString();
JSONObject result = JSONObject.parseObject(contentAsString);
Assert.assertTrue(result.containsKey("raw_data"));
Assert.assertTrue(result.containsKey("txID"));
} catch (UnsupportedEncodingException e) {
fail(e.getMessage());
}

}

Expand Down
Loading

0 comments on commit 1225ace

Please sign in to comment.