github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/node_delete_transaction_unit_test.go (about)

     1  //go:build all || unit
     2  // +build all unit
     3  
     4  package hedera
     5  
     6  /*-
     7   *
     8   * Hedera Go SDK
     9   *
    10   * Copyright (C) 2020 - 2024 Hedera Hashgraph, LLC
    11   *
    12   * Licensed under the Apache License, Version 2.0 (the "License");
    13   * you may not use this file except in compliance with the License.
    14   * You may obtain a copy of the License at
    15   *
    16   *      http://www.apache.org/licenses/LICENSE-2.0
    17   *
    18   * Unless required by applicable law or agreed to in writing, software
    19   * distributed under the License is distributed on an "AS IS" BASIS,
    20   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    21   * See the License for the specific language governing permissions and
    22   * limitations under the License.
    23   *
    24   */
    25  
    26  import (
    27  	"testing"
    28  	"time"
    29  
    30  	"github.com/hashgraph/hedera-protobufs-go/services"
    31  	"google.golang.org/grpc/codes"
    32  	"google.golang.org/grpc/status"
    33  
    34  	"github.com/stretchr/testify/require"
    35  )
    36  
    37  func TestUnitNodeDeleteTransactionMock(t *testing.T) {
    38  	t.Parallel()
    39  
    40  	responses := [][]interface{}{{
    41  		status.New(codes.Unavailable, "node is UNAVAILABLE").Err(),
    42  		status.New(codes.Internal, "Received RST_STREAM with code 0").Err(),
    43  		&services.TransactionResponse{
    44  			NodeTransactionPrecheckCode: services.ResponseCodeEnum_BUSY,
    45  		},
    46  		&services.TransactionResponse{
    47  			NodeTransactionPrecheckCode: services.ResponseCodeEnum_OK,
    48  		},
    49  		&services.Response{
    50  			Response: &services.Response_TransactionGetReceipt{
    51  				TransactionGetReceipt: &services.TransactionGetReceiptResponse{
    52  					Header: &services.ResponseHeader{
    53  						Cost:         0,
    54  						ResponseType: services.ResponseType_COST_ANSWER,
    55  					},
    56  				},
    57  			},
    58  		},
    59  		&services.Response{
    60  			Response: &services.Response_TransactionGetReceipt{
    61  				TransactionGetReceipt: &services.TransactionGetReceiptResponse{
    62  					Header: &services.ResponseHeader{
    63  						Cost:         0,
    64  						ResponseType: services.ResponseType_ANSWER_ONLY,
    65  					},
    66  					Receipt: &services.TransactionReceipt{
    67  						Status: services.ResponseCodeEnum_RECEIPT_NOT_FOUND,
    68  					},
    69  				},
    70  			},
    71  		},
    72  		&services.Response{
    73  			Response: &services.Response_TransactionGetReceipt{
    74  				TransactionGetReceipt: &services.TransactionGetReceiptResponse{
    75  					Header: &services.ResponseHeader{
    76  						Cost:         0,
    77  						ResponseType: services.ResponseType_ANSWER_ONLY,
    78  					},
    79  					Receipt: &services.TransactionReceipt{
    80  						Status: services.ResponseCodeEnum_SUCCESS,
    81  						AccountID: &services.AccountID{Account: &services.AccountID_AccountNum{
    82  							AccountNum: 234,
    83  						}},
    84  					},
    85  				},
    86  			},
    87  		},
    88  	}}
    89  
    90  	client, server := NewMockClientAndServer(responses)
    91  	defer server.Close()
    92  
    93  	tran := TransactionIDGenerate(AccountID{Account: 3})
    94  
    95  	resp, err := NewNodeDeleteTransaction().
    96  		SetNodeID(1).
    97  		SetTransactionID(tran).
    98  		Execute(client)
    99  	require.NoError(t, err)
   100  
   101  	receipt, err := resp.GetReceipt(client)
   102  	require.NoError(t, err)
   103  	require.Equal(t, receipt.AccountID, &AccountID{Account: 234})
   104  }
   105  
   106  func TestUnitNodeDeleteTransactionGet(t *testing.T) {
   107  	t.Parallel()
   108  
   109  	nodeAccountID := []AccountID{{Account: 10}, {Account: 11}, {Account: 12}}
   110  
   111  	transactionID := TransactionIDGenerate(AccountID{Account: 324})
   112  
   113  	transaction, err := NewNodeDeleteTransaction().
   114  		SetNodeID(1).
   115  		SetTransactionID(transactionID).
   116  		SetNodeAccountIDs(nodeAccountID).
   117  		SetTransactionMemo("").
   118  		SetTransactionValidDuration(60 * time.Second).
   119  		Freeze()
   120  	require.NoError(t, err)
   121  
   122  	transaction.GetTransactionID()
   123  	transaction.GetNodeAccountIDs()
   124  
   125  	_, err = transaction.GetTransactionHash()
   126  	require.NoError(t, err)
   127  
   128  	transaction.GetMaxTransactionFee()
   129  	transaction.GetTransactionMemo()
   130  	transaction.GetRegenerateTransactionID()
   131  	_, err = transaction.GetSignatures()
   132  	require.NoError(t, err)
   133  	transaction.GetRegenerateTransactionID()
   134  	transaction.GetMaxTransactionFee()
   135  	transaction.GetRegenerateTransactionID()
   136  	transaction.GetNodeID()
   137  }
   138  
   139  func TestUnitNodeDeleteTransactionSetNothing(t *testing.T) {
   140  	t.Parallel()
   141  
   142  	nodeAccountID := []AccountID{{Account: 10}, {Account: 11}, {Account: 12}}
   143  	transactionID := TransactionIDGenerate(AccountID{Account: 324})
   144  
   145  	transaction, err := NewNodeDeleteTransaction().
   146  		SetTransactionID(transactionID).
   147  		SetNodeAccountIDs(nodeAccountID).
   148  		Freeze()
   149  	require.NoError(t, err)
   150  
   151  	transaction.GetTransactionID()
   152  	transaction.GetNodeAccountIDs()
   153  
   154  	_, err = transaction.GetTransactionHash()
   155  	require.NoError(t, err)
   156  
   157  	transaction.GetMaxTransactionFee()
   158  	transaction.GetTransactionMemo()
   159  	transaction.GetRegenerateTransactionID()
   160  	_, err = transaction.GetSignatures()
   161  	require.NoError(t, err)
   162  	transaction.GetRegenerateTransactionID()
   163  	transaction.GetMaxTransactionFee()
   164  	transaction.GetRegenerateTransactionID()
   165  	transaction.GetRegenerateTransactionID()
   166  	transaction.GetMaxTransactionFee()
   167  	transaction.GetRegenerateTransactionID()
   168  	transaction.GetNodeID()
   169  }
   170  
   171  func TestUnitNodeDeleteTransactionProtoCheck(t *testing.T) {
   172  	t.Parallel()
   173  
   174  	nodeAccountID := []AccountID{{Account: 10}, {Account: 11}, {Account: 12}}
   175  
   176  	transactionID := TransactionIDGenerate(AccountID{Account: 324})
   177  
   178  	transaction, err := NewNodeDeleteTransaction().
   179  		SetTransactionID(transactionID).
   180  		SetNodeAccountIDs(nodeAccountID).
   181  		SetNodeID(1).
   182  		SetTransactionValidDuration(60 * time.Second).
   183  		Freeze()
   184  	require.NoError(t, err)
   185  
   186  	transaction.GetTransactionID()
   187  	transaction.GetNodeAccountIDs()
   188  
   189  	proto := transaction.build().GetNodeDelete()
   190  	require.Equal(t, proto.NodeId, uint64(1))
   191  }
   192  
   193  func TestUnitNodeDeleteTransactionCoverage(t *testing.T) {
   194  	t.Parallel()
   195  
   196  	nodeAccountID := []AccountID{{Account: 10}}
   197  	transactionID := TransactionIDGenerate(AccountID{Account: 324})
   198  
   199  	key, err := PrivateKeyGenerateEd25519()
   200  	require.NoError(t, err)
   201  
   202  	client, err := _NewMockClient()
   203  	client.SetLedgerID(*NewLedgerIDTestnet())
   204  	require.NoError(t, err)
   205  	client.SetAutoValidateChecksums(true)
   206  
   207  	trx, err := NewNodeDeleteTransaction().
   208  		SetTransactionID(transactionID).
   209  		SetNodeAccountIDs(nodeAccountID).
   210  		SetNodeID(1).
   211  		SetTransactionMemo("").
   212  		SetTransactionValidDuration(60 * time.Second).
   213  		SetMaxTransactionFee(NewHbar(3)).
   214  		SetMaxRetry(3).
   215  		SetMaxBackoff(time.Second * 30).
   216  		SetMinBackoff(time.Second * 10).
   217  		SetTransactionMemo("no").
   218  		SetTransactionValidDuration(time.Second * 30).
   219  		SetRegenerateTransactionID(false).
   220  		Freeze()
   221  	require.NoError(t, err)
   222  
   223  	trx.validateNetworkOnIDs(client)
   224  	_, err = trx.Schedule()
   225  	require.NoError(t, err)
   226  	trx.GetTransactionID()
   227  	trx.GetNodeAccountIDs()
   228  	trx.GetMaxRetry()
   229  	trx.GetMaxTransactionFee()
   230  	trx.GetMaxBackoff()
   231  	trx.GetMinBackoff()
   232  	trx.GetRegenerateTransactionID()
   233  	byt, err := trx.ToBytes()
   234  	require.NoError(t, err)
   235  	_, err = TransactionFromBytes(byt)
   236  	require.NoError(t, err)
   237  	_, err = key.SignTransaction(&trx.Transaction)
   238  	require.NoError(t, err)
   239  
   240  	_, err = trx.GetTransactionHash()
   241  	require.NoError(t, err)
   242  	trx.GetMaxTransactionFee()
   243  	trx.GetTransactionMemo()
   244  	trx.GetRegenerateTransactionID()
   245  	trx.GetNodeID()
   246  	_, err = trx.GetSignatures()
   247  	require.NoError(t, err)
   248  	trx.getName()
   249  }