Class NodeFake

java.lang.Object
com.exonum.binding.core.service.NodeFake
All Implemented Interfaces:
Node, java.lang.AutoCloseable

public final class NodeFake
extends java.lang.Object
implements Node
An implementation of a Node interface for testing purposes. Use it in tests of your handlers that need some data in the database:

 try (TemporaryDb db = TemporaryDb.newInstance();
      Cleaner cleaner = new Cleaner()) {

   // Setup database to include some test data
   Fork fork = db.createFork(cleaner);
   MapIndex balance = MapIndexProxy.newInstance("balance", fork, stringSerializer,
       stringSerializer);
   balance.put("John Doe", "$1000.00");
   db.merge(fork);

   // Create a node fake from the database
   NodeFake node = new NodeFake(db);

   WalletController controller = new WalletController(node);

   assertThat(controller.getBalance("John Doe"), equalTo("$1000.00"));
 }
 
  • Constructor Summary

    Constructors 
    Constructor Description
    NodeFake​(TemporaryDb database, java.lang.String serviceName)
    Creates a new node fake with the given database and an empty public key.
    NodeFake​(TemporaryDb database, java.lang.String serviceName, PublicKey publicKey)
    Creates a new node fake with the given database.
  • Method Summary

    Modifier and Type Method Description
    void close()
    Closes this node.
    TemporaryDb getDatabase()
    Returns the underlying database.
    PublicKey getPublicKey()
    Returns the service public key of this node.
    HashCode submitTransaction​(RawTransaction transaction)
    Returns a zero hash always, ignoring the transaction.
    <ResultT> ResultT withBlockchainData​(java.util.function.Function<BlockchainData,​ResultT> snapshotFunction)
    Performs the given function with a snapshot of the current database state.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface com.exonum.binding.core.service.Node

    withServiceData
  • Constructor Details

    • NodeFake

      public NodeFake​(TemporaryDb database, java.lang.String serviceName)
      Creates a new node fake with the given database and an empty public key.
      Parameters:
      database - a database to provide snapshots of
    • NodeFake

      public NodeFake​(TemporaryDb database, java.lang.String serviceName, PublicKey publicKey)
      Creates a new node fake with the given database.
      Parameters:
      database - a database to provide snapshots of
      publicKey - a public key of the node
  • Method Details

    • submitTransaction

      public HashCode submitTransaction​(RawTransaction transaction)
      Returns a zero hash always, ignoring the transaction.
      Specified by:
      submitTransaction in interface Node
      Parameters:
      transaction - a transaction to send
      Returns:
      hash of the transaction message created by the framework
      Throws:
      java.lang.NullPointerException - if the transaction is null
      See Also:
      Blockchain.getTxMessages()
    • withBlockchainData

      public <ResultT> ResultT withBlockchainData​(java.util.function.Function<BlockchainData,​ResultT> snapshotFunction)
      Description copied from interface: Node
      Performs the given function with a snapshot of the current database state.
      Specified by:
      withBlockchainData in interface Node
      Type Parameters:
      ResultT - a type the function returns
      Parameters:
      snapshotFunction - a function to execute
      Returns:
      the result of applying the given function to the database state
      See Also:
      Node.withServiceData(Function)
    • getPublicKey

      public PublicKey getPublicKey()
      Description copied from interface: Node
      Returns the service public key of this node. The corresponding private key is used for signing transactions in Node.submitTransaction(RawTransaction).

      This key is stored under "service_public_key" key in the node configuration file.

      Specified by:
      getPublicKey in interface Node
    • getDatabase

      public TemporaryDb getDatabase()
      Returns the underlying database.
    • close

      public void close()
      Description copied from interface: Node
      Closes this node. A closed node must no longer be used.
      Specified by:
      close in interface java.lang.AutoCloseable
      Specified by:
      close in interface Node