github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/file_contents_query_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 "bytes" 28 "testing" 29 "time" 30 31 "github.com/hashgraph/hedera-protobufs-go/services" 32 33 "github.com/stretchr/testify/assert" 34 35 "github.com/stretchr/testify/require" 36 ) 37 38 func TestUnitFileContentsQueryValidate(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 fileID, err := FileIDFromString("0.0.123-esxsf") 46 require.NoError(t, err) 47 48 fileContents := NewFileContentsQuery(). 49 SetFileID(fileID) 50 51 err = fileContents.validateNetworkOnIDs(client) 52 require.NoError(t, err) 53 } 54 55 func TestUnitFileContentsQueryValidateWrong(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 fileID, err := FileIDFromString("0.0.123-rmkykd") 63 require.NoError(t, err) 64 65 fileContents := NewFileContentsQuery(). 66 SetFileID(fileID) 67 68 err = fileContents.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 TestUnitFileContentsQueryMock(t *testing.T) { 76 t.Parallel() 77 78 responses := [][]interface{}{{ 79 &services.Response{ 80 Response: &services.Response_FileGetContents{ 81 FileGetContents: &services.FileGetContentsResponse{ 82 Header: &services.ResponseHeader{NodeTransactionPrecheckCode: services.ResponseCodeEnum_OK, ResponseType: services.ResponseType_COST_ANSWER, Cost: 3}, 83 }, 84 }, 85 }, 86 &services.Response{ 87 Response: &services.Response_FileGetContents{ 88 FileGetContents: &services.FileGetContentsResponse{ 89 Header: &services.ResponseHeader{NodeTransactionPrecheckCode: services.ResponseCodeEnum_OK, ResponseType: services.ResponseType_COST_ANSWER, Cost: 3}, 90 }, 91 }, 92 }, 93 &services.Response{ 94 Response: &services.Response_FileGetContents{ 95 FileGetContents: &services.FileGetContentsResponse{ 96 Header: &services.ResponseHeader{NodeTransactionPrecheckCode: services.ResponseCodeEnum_OK, ResponseType: services.ResponseType_ANSWER_ONLY, Cost: 2}, 97 FileContents: &services.FileGetContentsResponse_FileContents{ 98 FileID: &services.FileID{FileNum: 3}, 99 Contents: []byte{123}, 100 }, 101 }, 102 }, 103 }, 104 }} 105 106 client, server := NewMockClientAndServer(responses) 107 defer server.Close() 108 109 query := NewFileContentsQuery(). 110 SetFileID(FileID{File: 3}). 111 SetMaxQueryPayment(NewHbar(1)). 112 SetNodeAccountIDs([]AccountID{{Account: 3}}) 113 114 _, err := query.GetCost(client) 115 require.NoError(t, err) 116 117 result, err := query.Execute(client) 118 require.NoError(t, err) 119 120 require.Equal(t, bytes.Compare(result, []byte{123}), 0) 121 } 122 123 func TestUnitFileContentsQueryGet(t *testing.T) { 124 t.Parallel() 125 126 fileID := FileID{File: 7} 127 128 balance := NewFileContentsQuery(). 129 SetFileID(fileID). 130 SetQueryPayment(NewHbar(2)). 131 SetMaxQueryPayment(NewHbar(1)). 132 SetQueryPayment(HbarFromTinybar(25)). 133 SetNodeAccountIDs([]AccountID{{Account: 10}, {Account: 11}, {Account: 12}}) 134 135 balance.GetFileID() 136 balance.GetNodeAccountIDs() 137 balance.GetMinBackoff() 138 balance.GetMaxBackoff() 139 balance.GetMaxRetryCount() 140 balance.GetPaymentTransactionID() 141 balance.GetQueryPayment() 142 balance.GetMaxQueryPayment() 143 } 144 145 func TestUnitFileContentsQuerySetNothing(t *testing.T) { 146 t.Parallel() 147 148 balance := NewFileContentsQuery() 149 150 balance.GetFileID() 151 balance.GetNodeAccountIDs() 152 balance.GetMinBackoff() 153 balance.GetMaxBackoff() 154 balance.GetMaxRetryCount() 155 balance.GetPaymentTransactionID() 156 balance.GetQueryPayment() 157 balance.GetMaxQueryPayment() 158 } 159 160 func TestUnitFileContentsQueryCoverage(t *testing.T) { 161 t.Parallel() 162 163 checksum := "dmqui" 164 grpc := time.Second * 3 165 file := FileID{File: 3, checksum: &checksum} 166 nodeAccountID := []AccountID{{Account: 10}} 167 transactionID := TransactionIDGenerate(AccountID{Account: 324}) 168 169 client, err := _NewMockClient() 170 client.SetLedgerID(*NewLedgerIDTestnet()) 171 require.NoError(t, err) 172 client.SetAutoValidateChecksums(true) 173 174 query := NewFileContentsQuery(). 175 SetFileID(file). 176 SetMaxRetry(3). 177 SetMaxBackoff(time.Second * 30). 178 SetMinBackoff(time.Second * 10). 179 SetNodeAccountIDs(nodeAccountID). 180 SetPaymentTransactionID(transactionID). 181 SetMaxQueryPayment(NewHbar(23)). 182 SetQueryPayment(NewHbar(3)). 183 SetGrpcDeadline(&grpc) 184 185 err = query.validateNetworkOnIDs(client) 186 require.NoError(t, err) 187 query.GetNodeAccountIDs() 188 query.GetMaxBackoff() 189 query.GetMinBackoff() 190 query.getName() 191 query.GetFileID() 192 query.GetQueryPayment() 193 query.GetMaxQueryPayment() 194 }