github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/topic_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 protobuf "google.golang.org/protobuf/proto" 32 33 "github.com/stretchr/testify/assert" 34 35 "github.com/stretchr/testify/require" 36 ) 37 38 func TestUnitTopicDeleteTransactionValidate(t *testing.T) { 39 t.Parallel() 40 41 client, err := _NewMockClient() 42 client.SetLedgerID(*NewLedgerIDTestnet()) 43 require.NoError(t, err) 44 client.SetAutoValidateChecksums(true) 45 topicID, err := TopicIDFromString("0.0.123-esxsf") 46 require.NoError(t, err) 47 48 topicDelete := NewTopicDeleteTransaction(). 49 SetTopicID(topicID) 50 51 err = topicDelete.validateNetworkOnIDs(client) 52 require.NoError(t, err) 53 } 54 55 func TestUnitTopicDeleteTransactionValidateWrong(t *testing.T) { 56 t.Parallel() 57 58 client, err := _NewMockClient() 59 client.SetLedgerID(*NewLedgerIDTestnet()) 60 require.NoError(t, err) 61 client.SetAutoValidateChecksums(true) 62 topicID, err := TopicIDFromString("0.0.123-rmkykd") 63 require.NoError(t, err) 64 65 topicDelete := NewTopicDeleteTransaction(). 66 SetTopicID(topicID) 67 68 err = topicDelete.validateNetworkOnIDs(client) 69 assert.Error(t, err) 70 if err != nil { 71 assert.Equal(t, "network mismatch or wrong checksum given, given checksum: rmkykd, correct checksum esxsf, network: testnet", err.Error()) 72 } 73 } 74 75 func TestUnitTopicDeleteTransactionGet(t *testing.T) { 76 t.Parallel() 77 78 topicID := TopicID{Topic: 7} 79 80 nodeAccountID := []AccountID{{Account: 10}, {Account: 11}, {Account: 12}} 81 transactionID := TransactionIDGenerate(AccountID{Account: 324}) 82 83 transaction, err := NewTopicDeleteTransaction(). 84 SetTransactionID(transactionID). 85 SetNodeAccountIDs(nodeAccountID). 86 SetTopicID(topicID). 87 SetMaxTransactionFee(NewHbar(10)). 88 SetTransactionMemo(""). 89 SetTransactionValidDuration(60 * time.Second). 90 SetRegenerateTransactionID(false). 91 Freeze() 92 require.NoError(t, err) 93 94 transaction.GetTransactionID() 95 transaction.GetNodeAccountIDs() 96 97 _, err = transaction.GetTransactionHash() 98 require.NoError(t, err) 99 100 transaction.GetTopicID() 101 transaction.GetMaxTransactionFee() 102 transaction.GetTransactionMemo() 103 transaction.GetRegenerateTransactionID() 104 _, err = transaction.GetSignatures() 105 require.NoError(t, err) 106 transaction.GetRegenerateTransactionID() 107 transaction.GetMaxTransactionFee() 108 transaction.GetRegenerateTransactionID() 109 } 110 111 func TestUnitTopicDeleteTransactionNothingSet(t *testing.T) { 112 t.Parallel() 113 114 nodeAccountID := []AccountID{{Account: 10}, {Account: 11}, {Account: 12}} 115 transactionID := TransactionIDGenerate(AccountID{Account: 324}) 116 117 transaction, err := NewTopicDeleteTransaction(). 118 SetTransactionID(transactionID). 119 SetNodeAccountIDs(nodeAccountID). 120 Freeze() 121 require.NoError(t, err) 122 123 transaction.GetTransactionID() 124 transaction.GetNodeAccountIDs() 125 126 _, err = transaction.GetTransactionHash() 127 require.NoError(t, err) 128 129 transaction.GetTopicID() 130 transaction.GetMaxTransactionFee() 131 transaction.GetTransactionMemo() 132 transaction.GetRegenerateTransactionID() 133 _, err = transaction.GetSignatures() 134 require.NoError(t, err) 135 transaction.GetRegenerateTransactionID() 136 transaction.GetMaxTransactionFee() 137 transaction.GetRegenerateTransactionID() 138 } 139 140 func TestUnitTopicDeleteTransactionCoverage(t *testing.T) { 141 t.Parallel() 142 143 checksum := "dmqui" 144 grpc := time.Second * 30 145 topic := TopicID{Topic: 3, checksum: &checksum} 146 nodeAccountID := []AccountID{{Account: 10}} 147 transactionID := TransactionIDGenerate(AccountID{Account: 324}) 148 149 newKey, err := PrivateKeyGenerateEd25519() 150 require.NoError(t, err) 151 152 client, err := _NewMockClient() 153 client.SetLedgerID(*NewLedgerIDTestnet()) 154 require.NoError(t, err) 155 client.SetAutoValidateChecksums(true) 156 157 transaction, err := NewTopicDeleteTransaction(). 158 SetTransactionID(transactionID). 159 SetNodeAccountIDs(nodeAccountID). 160 SetTopicID(topic). 161 SetGrpcDeadline(&grpc). 162 SetMaxTransactionFee(NewHbar(3)). 163 SetMaxRetry(3). 164 SetMaxBackoff(time.Second * 30). 165 SetMinBackoff(time.Second * 10). 166 SetTransactionMemo("no"). 167 SetTransactionValidDuration(time.Second * 30). 168 SetRegenerateTransactionID(false). 169 Freeze() 170 require.NoError(t, err) 171 172 err = transaction.validateNetworkOnIDs(client) 173 require.NoError(t, err) 174 _, err = transaction.Schedule() 175 require.NoError(t, err) 176 transaction.GetTransactionID() 177 transaction.GetNodeAccountIDs() 178 transaction.GetMaxRetry() 179 transaction.GetMaxTransactionFee() 180 transaction.GetMaxBackoff() 181 transaction.GetMinBackoff() 182 transaction.GetRegenerateTransactionID() 183 byt, err := transaction.ToBytes() 184 require.NoError(t, err) 185 txFromBytes, err := TransactionFromBytes(byt) 186 require.NoError(t, err) 187 sig, err := newKey.SignTransaction(&transaction.Transaction) 188 require.NoError(t, err) 189 190 _, err = transaction.GetTransactionHash() 191 require.NoError(t, err) 192 transaction.GetMaxTransactionFee() 193 transaction.GetTransactionMemo() 194 transaction.GetRegenerateTransactionID() 195 transaction.GetTopicID() 196 _, err = transaction.GetSignatures() 197 require.NoError(t, err) 198 transaction.getName() 199 switch b := txFromBytes.(type) { 200 case TopicDeleteTransaction: 201 b.AddSignature(newKey.PublicKey(), sig) 202 } 203 } 204 205 func TestUnitTopicDeleteTransactionMock(t *testing.T) { 206 t.Parallel() 207 208 newKey, err := PrivateKeyFromStringEd25519("302e020100300506032b657004220420a869f4c6191b9c8c99933e7f6b6611711737e4b1a1a5a4cb5370e719a1f6df98") 209 require.NoError(t, err) 210 211 call := func(request *services.Transaction) *services.TransactionResponse { 212 require.NotEmpty(t, request.SignedTransactionBytes) 213 signedTransaction := services.SignedTransaction{} 214 _ = protobuf.Unmarshal(request.SignedTransactionBytes, &signedTransaction) 215 216 require.NotEmpty(t, signedTransaction.BodyBytes) 217 transactionBody := services.TransactionBody{} 218 _ = protobuf.Unmarshal(signedTransaction.BodyBytes, &transactionBody) 219 220 require.NotNil(t, transactionBody.TransactionID) 221 transactionId := transactionBody.TransactionID.String() 222 require.NotEqual(t, "", transactionId) 223 224 sigMap := signedTransaction.GetSigMap() 225 require.NotNil(t, sigMap) 226 227 return &services.TransactionResponse{ 228 NodeTransactionPrecheckCode: services.ResponseCodeEnum_OK, 229 } 230 } 231 responses := [][]interface{}{{ 232 call, 233 }} 234 235 client, server := NewMockClientAndServer(responses) 236 defer server.Close() 237 238 checksum := "dmqui" 239 topic := TopicID{Topic: 3, checksum: &checksum} 240 241 freez, err := NewTopicDeleteTransaction(). 242 SetNodeAccountIDs([]AccountID{{Account: 3}}). 243 SetTopicID(topic). 244 FreezeWith(client) 245 require.NoError(t, err) 246 247 _, err = freez.Sign(newKey).Execute(client) 248 require.NoError(t, err) 249 }