github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/file_append_transaction_e2e_test.go (about) 1 //go:build all || e2e 2 // +build all e2e 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 29 "github.com/stretchr/testify/assert" 30 31 "github.com/stretchr/testify/require" 32 ) 33 34 func TestIntegrationFileAppendTransactionCanExecute(t *testing.T) { 35 t.Parallel() 36 env := NewIntegrationTestEnv(t) 37 38 resp, err := NewFileCreateTransaction(). 39 SetKeys(env.Client.GetOperatorPublicKey()). 40 SetNodeAccountIDs(env.NodeAccountIDs). 41 SetContents([]byte("Hello")). 42 SetTransactionMemo("go sdk e2e tests"). 43 Execute(env.Client) 44 45 require.NoError(t, err) 46 47 receipt, err := resp.SetValidateStatus(true).GetReceipt(env.Client) 48 require.NoError(t, err) 49 50 fileID := *receipt.FileID 51 assert.NotNil(t, fileID) 52 53 resp, err = NewFileAppendTransaction(). 54 SetFileID(fileID). 55 SetNodeAccountIDs([]AccountID{resp.NodeID}). 56 SetContents([]byte(" world!")). 57 Execute(env.Client) 58 require.NoError(t, err) 59 60 _, err = resp.SetValidateStatus(true).GetReceipt(env.Client) 61 require.NoError(t, err) 62 63 contents, err := NewFileContentsQuery(). 64 SetFileID(fileID). 65 SetNodeAccountIDs([]AccountID{resp.NodeID}). 66 Execute(env.Client) 67 require.NoError(t, err) 68 69 assert.Equal(t, []byte("Hello world!"), contents) 70 71 resp, err = NewFileDeleteTransaction(). 72 SetFileID(fileID). 73 SetNodeAccountIDs([]AccountID{resp.NodeID}). 74 Execute(env.Client) 75 require.NoError(t, err) 76 77 _, err = resp.SetValidateStatus(true).GetReceipt(env.Client) 78 require.NoError(t, err) 79 80 err = CloseIntegrationTestEnv(env, nil) 81 require.NoError(t, err) 82 } 83 84 func TestIntegrationFileAppendTransactionNoFileID(t *testing.T) { 85 t.Parallel() 86 env := NewIntegrationTestEnv(t) 87 88 resp, err := NewFileCreateTransaction(). 89 SetKeys(env.Client.GetOperatorPublicKey()). 90 SetNodeAccountIDs(env.NodeAccountIDs). 91 SetContents([]byte("Hello")). 92 SetTransactionMemo("go sdk e2e tests"). 93 Execute(env.Client) 94 95 require.NoError(t, err) 96 97 receipt, err := resp.SetValidateStatus(true).GetReceipt(env.Client) 98 require.NoError(t, err) 99 100 fileID := *receipt.FileID 101 assert.NotNil(t, fileID) 102 103 _, err = NewFileAppendTransaction(). 104 SetNodeAccountIDs([]AccountID{resp.NodeID}). 105 SetContents([]byte(" world!")). 106 Execute(env.Client) 107 assert.Error(t, err) 108 if err != nil { 109 assert.Contains(t, err.Error(), "exceptional precheck status INVALID_FILE_ID", err.Error()) 110 } 111 112 resp, err = NewFileDeleteTransaction(). 113 SetFileID(fileID). 114 SetNodeAccountIDs([]AccountID{resp.NodeID}). 115 Execute(env.Client) 116 require.NoError(t, err) 117 118 _, err = resp.SetValidateStatus(true).GetReceipt(env.Client) 119 require.NoError(t, err) 120 121 err = CloseIntegrationTestEnv(env, nil) 122 require.NoError(t, err) 123 } 124 125 func TestIntegrationFileAppendTransactionNothingSet(t *testing.T) { 126 t.Parallel() 127 env := NewIntegrationTestEnv(t) 128 129 _, err := NewFileAppendTransaction(). 130 SetContents([]byte(" world!")). 131 SetNodeAccountIDs(env.NodeAccountIDs). 132 Execute(env.Client) 133 require.Error(t, err) 134 if err != nil { 135 assert.Contains(t, err.Error(), "exceptional precheck status INVALID_FILE_ID", err.Error()) 136 } 137 138 err = CloseIntegrationTestEnv(env, nil) 139 require.NoError(t, err) 140 } 141 func TestIntegrationFileAppendTransactionCanExecuteAfterSerializationDeserialization(t *testing.T) { 142 t.Parallel() 143 env := NewIntegrationTestEnv(t) 144 145 resp, err := NewFileCreateTransaction(). 146 SetKeys(env.Client.GetOperatorPublicKey()). 147 SetNodeAccountIDs(env.NodeAccountIDs). 148 SetContents([]byte("Hello")). 149 SetTransactionMemo("go sdk e2e tests"). 150 Execute(env.Client) 151 152 require.NoError(t, err) 153 154 receipt, err := resp.SetValidateStatus(true).GetReceipt(env.Client) 155 require.NoError(t, err) 156 157 fileID := *receipt.FileID 158 assert.NotNil(t, fileID) 159 160 before := NewFileAppendTransaction(). 161 SetFileID(fileID) 162 163 bytes, err := before.ToBytes() 164 require.NoError(t, err) 165 166 afterI, err := TransactionFromBytes(bytes) 167 require.NoError(t, err) 168 169 tx := afterI.(FileAppendTransaction) 170 171 resp, err = tx.SetNodeAccountIDs([]AccountID{resp.NodeID}). 172 SetContents([]byte(" world!")).Execute(env.Client) 173 require.NoError(t, err) 174 175 _, err = resp.SetValidateStatus(true).GetReceipt(env.Client) 176 require.NoError(t, err) 177 178 contents, err := NewFileContentsQuery(). 179 SetFileID(fileID). 180 SetNodeAccountIDs([]AccountID{resp.NodeID}). 181 Execute(env.Client) 182 require.NoError(t, err) 183 184 assert.Equal(t, []byte("Hello world!"), contents) 185 186 resp, err = NewFileDeleteTransaction(). 187 SetFileID(fileID). 188 SetNodeAccountIDs([]AccountID{resp.NodeID}). 189 Execute(env.Client) 190 require.NoError(t, err) 191 192 _, err = resp.SetValidateStatus(true).GetReceipt(env.Client) 193 require.NoError(t, err) 194 195 err = CloseIntegrationTestEnv(env, nil) 196 require.NoError(t, err) 197 }