Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
@isTest
public class BlockChainTest{
//Testing Blockchain
@isTest
public static void testBlockChain(){
/**Data Setup**/
//Creating Instance of Blockchain
BlockChain bChain = new BlockChain();
//addBlock method take data as the string
//Changing data to the string with the JSON format
//Adding the first block to the chain
bChain.addBlock( json.serialize( new BCTestDataWrapper('Iron Man', '2334343434') ) );
//Adding the second block to the chain
bChain.addBlock( json.serialize( new BCTestDataWrapper('Thor', '34343434') ) );
/**Positive Testing**/
//isChainValid will return true, as no data has been modified from block
system.assertEquals(true, bChain.isChainValid() );
//Print Blockchain
system.debug('-Blockchain Data Before Modifying--'+Json.serialize( bChain.chain) );
/**Negative Testing**/
//Now updating the 0 index's block
BCTestDataWrapper tData = (BCTestDataWrapper)JSON.deserialize(bChain.chain[0].data, BCTestDataWrapper.class);
tData.name = 'Thanos';
bChain.chain[0].data = json.serialize(tData);
//isChainValid will return false, as the data has been modified from block
system.assertEquals(false, bChain.isChainValid() );
//Print Blockchain
system.debug('-Blockchain Data After Modifying--'+Json.serialize( bChain.chain) );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.