github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/file_contents_query_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 TestIntegrationFileContentsQueryCanExecute(t *testing.T) {
    35  	t.Parallel()
    36  	env := NewIntegrationTestEnv(t)
    37  
    38  	var contents = []byte("Hellow world!")
    39  
    40  	resp, err := NewFileCreateTransaction().
    41  		SetKeys(env.Client.GetOperatorPublicKey()).
    42  		SetNodeAccountIDs(env.NodeAccountIDs).
    43  		SetContents(contents).
    44  		SetTransactionMemo("go sdk e2e tests").
    45  		Execute(env.Client)
    46  
    47  	require.NoError(t, err)
    48  
    49  	receipt, err := resp.SetValidateStatus(true).GetReceipt(env.Client)
    50  	require.NoError(t, err)
    51  
    52  	fileID := receipt.FileID
    53  	assert.NotNil(t, fileID)
    54  
    55  	_, err = resp.SetValidateStatus(true).GetReceipt(env.Client)
    56  	require.NoError(t, err)
    57  
    58  	remoteContents, err := NewFileContentsQuery().
    59  		SetFileID(*fileID).
    60  		SetMaxQueryPayment(NewHbar(1)).
    61  		SetQueryPayment(HbarFromTinybar(25)).
    62  		SetNodeAccountIDs([]AccountID{resp.NodeID}).
    63  		Execute(env.Client)
    64  	require.NoError(t, err)
    65  
    66  	assert.Equal(t, contents, remoteContents)
    67  
    68  	resp, err = NewFileDeleteTransaction().
    69  		SetFileID(*fileID).
    70  		SetNodeAccountIDs([]AccountID{resp.NodeID}).
    71  		Execute(env.Client)
    72  	require.NoError(t, err)
    73  
    74  	_, err = resp.SetValidateStatus(true).GetReceipt(env.Client)
    75  	require.NoError(t, err)
    76  
    77  	err = CloseIntegrationTestEnv(env, nil)
    78  	require.NoError(t, err)
    79  }
    80  
    81  func TestIntegrationFileContentsQueryGetCost(t *testing.T) {
    82  	t.Parallel()
    83  	env := NewIntegrationTestEnv(t)
    84  
    85  	var contents = []byte("Hellow world!")
    86  
    87  	resp, err := NewFileCreateTransaction().
    88  		SetKeys(env.Client.GetOperatorPublicKey()).
    89  		SetNodeAccountIDs(env.NodeAccountIDs).
    90  		SetContents(contents).
    91  		SetTransactionMemo("go sdk e2e tests").
    92  		Execute(env.Client)
    93  
    94  	require.NoError(t, err)
    95  
    96  	receipt, err := resp.SetValidateStatus(true).GetReceipt(env.Client)
    97  	require.NoError(t, err)
    98  
    99  	fileID := receipt.FileID
   100  	assert.NotNil(t, fileID)
   101  
   102  	_, err = resp.SetValidateStatus(true).GetReceipt(env.Client)
   103  	require.NoError(t, err)
   104  
   105  	fileContents := NewFileContentsQuery().
   106  		SetFileID(*fileID).
   107  		SetMaxQueryPayment(NewHbar(1)).
   108  		SetNodeAccountIDs([]AccountID{resp.NodeID})
   109  
   110  	cost, err := fileContents.GetCost(env.Client)
   111  	require.NoError(t, err)
   112  
   113  	remoteContents, err := fileContents.SetQueryPayment(cost).Execute(env.Client)
   114  	require.NoError(t, err)
   115  
   116  	assert.Equal(t, contents, remoteContents)
   117  
   118  	resp, err = NewFileDeleteTransaction().
   119  		SetFileID(*fileID).
   120  		SetNodeAccountIDs([]AccountID{resp.NodeID}).
   121  		Execute(env.Client)
   122  	require.NoError(t, err)
   123  
   124  	_, err = resp.SetValidateStatus(true).GetReceipt(env.Client)
   125  	require.NoError(t, err)
   126  
   127  	err = CloseIntegrationTestEnv(env, nil)
   128  	require.NoError(t, err)
   129  }
   130  
   131  func TestIntegrationFileContentsQuerySetBigMaxPayment(t *testing.T) {
   132  	t.Parallel()
   133  	env := NewIntegrationTestEnv(t)
   134  
   135  	var contents = []byte("Hellow world!")
   136  
   137  	resp, err := NewFileCreateTransaction().
   138  		SetKeys(env.Client.GetOperatorPublicKey()).
   139  		SetNodeAccountIDs(env.NodeAccountIDs).
   140  		SetContents(contents).
   141  		SetTransactionMemo("go sdk e2e tests").
   142  		Execute(env.Client)
   143  
   144  	require.NoError(t, err)
   145  
   146  	receipt, err := resp.SetValidateStatus(true).GetReceipt(env.Client)
   147  	require.NoError(t, err)
   148  
   149  	fileID := receipt.FileID
   150  	assert.NotNil(t, fileID)
   151  
   152  	_, err = resp.SetValidateStatus(true).GetReceipt(env.Client)
   153  	require.NoError(t, err)
   154  
   155  	fileContents := NewFileContentsQuery().
   156  		SetFileID(*fileID).
   157  		SetMaxQueryPayment(NewHbar(100000)).
   158  		SetNodeAccountIDs([]AccountID{resp.NodeID})
   159  
   160  	cost, err := fileContents.GetCost(env.Client)
   161  	require.NoError(t, err)
   162  
   163  	remoteContents, err := fileContents.SetQueryPayment(cost).Execute(env.Client)
   164  	require.NoError(t, err)
   165  
   166  	assert.Equal(t, contents, remoteContents)
   167  
   168  	resp, err = NewFileDeleteTransaction().
   169  		SetFileID(*fileID).
   170  		SetNodeAccountIDs([]AccountID{resp.NodeID}).
   171  		Execute(env.Client)
   172  	require.NoError(t, err)
   173  
   174  	_, err = resp.SetValidateStatus(true).GetReceipt(env.Client)
   175  	require.NoError(t, err)
   176  
   177  	err = CloseIntegrationTestEnv(env, nil)
   178  	require.NoError(t, err)
   179  }
   180  
   181  func TestIntegrationFileContentsQuerySetSmallMaxPayment(t *testing.T) {
   182  	t.Parallel()
   183  	env := NewIntegrationTestEnv(t)
   184  
   185  	var contents = []byte("Hellow world!")
   186  
   187  	resp, err := NewFileCreateTransaction().
   188  		SetKeys(env.Client.GetOperatorPublicKey()).
   189  		SetNodeAccountIDs(env.NodeAccountIDs).
   190  		SetContents(contents).
   191  		SetTransactionMemo("go sdk e2e tests").
   192  		Execute(env.Client)
   193  
   194  	require.NoError(t, err)
   195  
   196  	receipt, err := resp.SetValidateStatus(true).GetReceipt(env.Client)
   197  	require.NoError(t, err)
   198  
   199  	fileID := receipt.FileID
   200  	assert.NotNil(t, fileID)
   201  
   202  	_, err = resp.SetValidateStatus(true).GetReceipt(env.Client)
   203  	require.NoError(t, err)
   204  
   205  	fileContents := NewFileContentsQuery().
   206  		SetFileID(*fileID).
   207  		SetMaxQueryPayment(HbarFromTinybar(1)).
   208  		SetNodeAccountIDs([]AccountID{resp.NodeID})
   209  
   210  	cost, err := fileContents.GetCost(env.Client)
   211  	require.NoError(t, err)
   212  
   213  	_, err = fileContents.Execute(env.Client)
   214  	assert.Error(t, err)
   215  	if err != nil {
   216  		assert.Equal(t, "cost of FileContentsQuery ("+cost.String()+") without explicit payment is greater than the max query payment of 1 tℏ", err.Error())
   217  	}
   218  
   219  	resp, err = NewFileDeleteTransaction().
   220  		SetFileID(*fileID).
   221  		SetNodeAccountIDs([]AccountID{resp.NodeID}).
   222  		Execute(env.Client)
   223  	require.NoError(t, err)
   224  
   225  	_, err = resp.SetValidateStatus(true).GetReceipt(env.Client)
   226  	require.NoError(t, err)
   227  
   228  	err = CloseIntegrationTestEnv(env, nil)
   229  	require.NoError(t, err)
   230  }
   231  
   232  func TestIntegrationFileContentsQueryInsufficientFee(t *testing.T) {
   233  	t.Parallel()
   234  	env := NewIntegrationTestEnv(t)
   235  
   236  	var contents = []byte("Hellow world!")
   237  
   238  	resp, err := NewFileCreateTransaction().
   239  		SetKeys(env.Client.GetOperatorPublicKey()).
   240  		SetNodeAccountIDs(env.NodeAccountIDs).
   241  		SetContents(contents).
   242  		SetTransactionMemo("go sdk e2e tests").
   243  		Execute(env.Client)
   244  
   245  	require.NoError(t, err)
   246  
   247  	receipt, err := resp.SetValidateStatus(true).GetReceipt(env.Client)
   248  	require.NoError(t, err)
   249  
   250  	fileID := receipt.FileID
   251  	assert.NotNil(t, fileID)
   252  
   253  	_, err = resp.SetValidateStatus(true).GetReceipt(env.Client)
   254  	require.NoError(t, err)
   255  
   256  	fileContents := NewFileContentsQuery().
   257  		SetFileID(*fileID).
   258  		SetMaxQueryPayment(NewHbar(1)).
   259  		SetNodeAccountIDs([]AccountID{resp.NodeID})
   260  
   261  	_, err = fileContents.GetCost(env.Client)
   262  	require.NoError(t, err)
   263  
   264  	_, err = fileContents.SetQueryPayment(HbarFromTinybar(1)).Execute(env.Client)
   265  	assert.Error(t, err)
   266  	if err != nil {
   267  		assert.Equal(t, "exceptional precheck status INSUFFICIENT_TX_FEE", err.Error())
   268  	}
   269  
   270  	resp, err = NewFileDeleteTransaction().
   271  		SetFileID(*fileID).
   272  		SetNodeAccountIDs([]AccountID{resp.NodeID}).
   273  		Execute(env.Client)
   274  	require.NoError(t, err)
   275  
   276  	_, err = resp.SetValidateStatus(true).GetReceipt(env.Client)
   277  	require.NoError(t, err)
   278  
   279  	err = CloseIntegrationTestEnv(env, nil)
   280  	require.NoError(t, err)
   281  }
   282  
   283  func TestIntegrationFileContentsQueryNoFileID(t *testing.T) {
   284  	t.Parallel()
   285  	env := NewIntegrationTestEnv(t)
   286  
   287  	_, err := NewFileContentsQuery().
   288  		SetNodeAccountIDs(env.NodeAccountIDs).
   289  		Execute(env.Client)
   290  	assert.Error(t, err)
   291  	if err != nil {
   292  		assert.Equal(t, "exceptional precheck status INVALID_FILE_ID", err.Error())
   293  	}
   294  
   295  	err = CloseIntegrationTestEnv(env, nil)
   296  	require.NoError(t, err)
   297  }