### Question:
Execute extends AbstractCommand { @Override public void run() { JsonObject json = convertJsonParameter(argument); if (json != null) { executeContract(toKey(id), json); } JsonArray functions = json.getJsonArray("_functions_"); if (functions != null) { functions.forEach( func -> { String functionId = ((JsonString) func).getString(); Function f = functionManager.getInstance(functionId); f.invoke( databaseEmulator, Optional.ofNullable(convertJsonParameter(functionArgument)), json, contractManager.get(toKey(id)).getProperties()); }); } } @Inject  Execute(
      TerminalWrapper terminal,
      ContractManagerEmulator contractManager,
      TamperEvidentAssetbase assetbase,
      Ledger ledger,
      FunctionManager functionManager,
      MutableDatabaseEmulator databaseEmulator); @Override void run();  }### Answer:
@Test public void run_ExecuteContract_ShouldCallInvokeOnTheContract() { JsonObject argument = Json.createObjectBuilder().add("x", "y").build(); ContractEntry.Key key = new ContractEntry.Key(CONTRACT_ID, new CertificateEntry.Key("emulator_user", 0)); ContractEntry entry = new ContractEntry( "id", "binaryName", "cert_holder_id", 1, "contract".getBytes(), null, 1, "signature".getBytes()); when(contractManager.get(key)).thenReturn(entry); when(contractManager.getInstance(key.getId())).thenReturn(contract); when(contract.invoke(ledger, argument, Optional.empty())).thenReturn(null); CommandLine.run(execute, CONTRACT_ID, argument.toString()); verify(contract).invoke(ledger, argument, Optional.empty()); }
@Test public void run_ExecuteContractWithFunction_ShouldSucceed() { JsonObject contractArgument = Json.createObjectBuilder() .add("_functions_", Json.createArrayBuilder().add("function_foo")) .build(); JsonObject functionArgument = Json.createObjectBuilder().add("foo", "bar").build(); ContractEntry.Key key = new ContractEntry.Key(CONTRACT_ID, new CertificateEntry.Key("emulator_user", 0)); ContractEntry entry = new ContractEntry( "id", "binaryName", "cert_holder_id", 1, "contract".getBytes(), null, 1, "signature".getBytes()); when(contractManager.get(key)).thenReturn(entry); when(contractManager.getInstance(key.getId())).thenReturn(contract); when(functionManager.getInstance("function_foo")).thenReturn(function); CommandLine.run( execute, CONTRACT_ID, contractArgument.toString(), "-fa=" + functionArgument.toString()); verify(contract).invoke(ledger, contractArgument, Optional.empty()); verify(function) .invoke(database, Optional.of(functionArgument), contractArgument, Optional.empty()); }
@Test public void run_ExecuteContractWithFunctionNoArgument_ShouldSucceed() { JsonObject contractArgument = Json.createObjectBuilder() .add("_functions_", Json.createArrayBuilder().add("function_foo")) .build(); ContractEntry.Key key = new ContractEntry.Key(CONTRACT_ID, new CertificateEntry.Key("emulator_user", 0)); ContractEntry entry = new ContractEntry( "id", "binaryName", "cert_holder_id", 1, "contract".getBytes(), null, 1, "signature".getBytes()); when(contractManager.get(key)).thenReturn(entry); when(contractManager.getInstance(key.getId())).thenReturn(contract); when(functionManager.getInstance("function_foo")).thenReturn(function); CommandLine.run(execute, CONTRACT_ID, contractArgument.toString()); verify(contract).invoke(ledger, contractArgument, Optional.empty()); verify(function).invoke(database, Optional.empty(), contractArgument, Optional.empty()); }