Skip to content

Instantly share code, notes, and snippets.

@TheVishnuKumar
Created October 15, 2018 18:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TheVishnuKumar/5cff22f2b3924e658a869da42ad77f90 to your computer and use it in GitHub Desktop.
Save TheVishnuKumar/5cff22f2b3924e658a869da42ad77f90 to your computer and use it in GitHub Desktop.
@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