Skip to content

Commit

Permalink
feat(test): improve unit test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
lxcmyf committed Aug 29, 2024
1 parent 773b374 commit 59fe2b7
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,52 +37,58 @@ public synchronized void testParseFilterQueryBlockNumber() {

@Test
public synchronized void testMatchFilter() {
String[] addrList = {"address1", "address2"};
String[] adrList = {"address1", "address2"};
String[] topList = {"top1", "top2"};
Map topMap = new HashMap<String, String>();
Map<String, String> topMap = new HashMap<>();
List<byte[]> addressList = new ArrayList<>();
addressList.add(addrList[0].getBytes());
addressList.add(addrList[1].getBytes());
addressList.add(adrList[0].getBytes());
addressList.add(adrList[1].getBytes());
topMap.put("1", topList[0]);
topMap.put("2", topList[1]);
LogEventWrapper event = new LogEventWrapper();
((LogEventWrapper) event).setTopicList(addressList);
((LogEventWrapper) event).setData(new byte[]{});
((LogEventWrapper) event).setEventSignature("");
((LogEventWrapper) event).setAbiEntry(Entry.newBuilder().setName("testABI").build());
event.setBlockNumber(new Long(123));
event.setTopicList(addressList);
event.setData(new byte[]{});
event.setEventSignature("");
event.setAbiEntry(Entry.newBuilder().setName("testABI").build());
event.setBlockNumber(123L);
ContractEventTriggerCapsule capsule = new ContractEventTriggerCapsule(event);
capsule.setContractEventTrigger(capsule.getContractEventTrigger());
capsule.getContractEventTrigger().setContractAddress("address1");
capsule.setLatestSolidifiedBlockNumber(0);
capsule.setData(capsule.getData());
capsule.setTopicList(capsule.getTopicList());
capsule.setAbiEntry(capsule.getAbiEntry());
capsule.getContractEventTrigger().setTopicMap(topMap);

{
Assert.assertEquals(true, matchFilter(capsule.getContractEventTrigger()));
Assert.assertTrue(matchFilter(capsule.getContractEventTrigger()));
}

{
FilterQuery filterQuery = new FilterQuery();
filterQuery.setFromBlock(1);
filterQuery.setToBlock(100);
EventPluginLoader.getInstance().setFilterQuery(filterQuery);
Assert.assertEquals(false, matchFilter(capsule.getContractEventTrigger()));
Assert.assertFalse(matchFilter(capsule.getContractEventTrigger()));
}

{
FilterQuery filterQuery = new FilterQuery();
filterQuery.setFromBlock(133);
filterQuery.setToBlock(190);
EventPluginLoader.getInstance().setFilterQuery(filterQuery);
Assert.assertEquals(false, matchFilter(capsule.getContractEventTrigger()));
Assert.assertFalse(matchFilter(capsule.getContractEventTrigger()));
}

{
FilterQuery filterQuery = new FilterQuery();
filterQuery.setFromBlock(100);
filterQuery.setToBlock(190);
filterQuery.setContractAddressList(Arrays.asList(addrList));
filterQuery.setContractAddressList(Arrays.asList(adrList));
filterQuery.setContractTopicList(Arrays.asList(topList));
EventPluginLoader.getInstance().setFilterQuery(filterQuery);
Assert.assertEquals(true, matchFilter(capsule.getContractEventTrigger()));
Assert.assertTrue(matchFilter(capsule.getContractEventTrigger()));
capsule.processTrigger();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package org.tron.common.logsfilter.capsule;

import com.google.protobuf.ByteString;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.tron.common.utils.Sha256Hash;
import org.tron.core.capsule.BlockCapsule;

public class BlockFilterCapsuleTest {

private BlockFilterCapsule blockFilterCapsule;

@Before
public void setUp() {
BlockCapsule blockCapsule = new BlockCapsule(1, Sha256Hash.ZERO_HASH,
System.currentTimeMillis(), ByteString.EMPTY);
blockFilterCapsule = new BlockFilterCapsule(blockCapsule, false);
}

@Test
public void testSetAndGetBlockHash() {
blockFilterCapsule
.setBlockHash("e58f33f9baf9305dc6f82b9f1934ea8f0ade2defb951258d50167028c780351f");
System.out.println(blockFilterCapsule);
Assert.assertEquals("e58f33f9baf9305dc6f82b9f1934ea8f0ade2defb951258d50167028c780351f",
blockFilterCapsule.getBlockHash());
}

@Test
public void testSetAndIsSolidified() {
blockFilterCapsule = new BlockFilterCapsule(
"e58f33f9baf9305dc6f82b9f1934ea8f0ade2defb951258d50167028c780351f", false);
blockFilterCapsule.setSolidified(true);
blockFilterCapsule.processFilterTrigger();
Assert.assertTrue(blockFilterCapsule.isSolidified());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package org.tron.common.logsfilter.capsule;

import com.google.protobuf.ByteString;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.tron.common.utils.Sha256Hash;
import org.tron.core.capsule.BlockCapsule;

public class BlockLogTriggerCapsuleTest {

private BlockLogTriggerCapsule blockLogTriggerCapsule;

@Before
public void setUp() {
BlockCapsule blockCapsule = new BlockCapsule(1, Sha256Hash.ZERO_HASH,
System.currentTimeMillis(), ByteString.EMPTY);
blockLogTriggerCapsule = new BlockLogTriggerCapsule(blockCapsule);
}

@Test
public void testSetAndGetBlockLogTrigger() {
blockLogTriggerCapsule
.setBlockLogTrigger(blockLogTriggerCapsule.getBlockLogTrigger());
Assert.assertEquals(1,
blockLogTriggerCapsule.getBlockLogTrigger().getBlockNumber());
}

@Test
public void testSetLatestSolidifiedBlockNumber() {
blockLogTriggerCapsule.setLatestSolidifiedBlockNumber(0);
Assert.assertEquals(0,
blockLogTriggerCapsule.getBlockLogTrigger().getLatestSolidifiedBlockNumber());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package org.tron.common.logsfilter.capsule;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThrows;
import static org.tron.common.logsfilter.trigger.Trigger.CONTRACTLOG_TRIGGER_NAME;

import org.junit.Before;
import org.junit.Test;
import org.tron.common.logsfilter.trigger.ContractLogTrigger;

public class ContractLogTriggerCapsuleTest {

private ContractLogTriggerCapsule capsule;

@Before
public void setUp() {
ContractLogTrigger contractLogTrigger = new ContractLogTrigger();
contractLogTrigger.setBlockNumber(0L);
capsule = new ContractLogTriggerCapsule(contractLogTrigger);
capsule.setLatestSolidifiedBlockNumber(0);
}

@Test
public void testSetAndGetContractLogTrigger() {
capsule.setContractLogTrigger(capsule.getContractLogTrigger());
assertEquals(CONTRACTLOG_TRIGGER_NAME, capsule.getContractLogTrigger().getTriggerName());
assertThrows(NullPointerException.class, capsule::processTrigger);
}

}

0 comments on commit 59fe2b7

Please sign in to comment.