Class NodeFake

  • All Implemented Interfaces:
    Node

    public final class NodeFake
    extends 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 (MemoryDb db = MemoryDb.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 Detail

      • NodeFake

        public NodeFake​(MemoryDb database)
        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​(MemoryDb database,
                        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 Detail

      • withSnapshot

        public <ResultT> ResultT withSnapshot​(Function<Snapshot,​ResultT> snapshotFunction)
        Description copied from interface: Node
        Performs a given function with a snapshot of the current database state.
        Specified by:
        withSnapshot 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
      • 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 MemoryDb getDatabase()
        Returns the underlying database.