|
| 1 | +// Copyright © 2022 Kaleido, Inc. |
| 2 | +// |
| 3 | +// SPDX-License-Identifier: Apache-2.0 |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +// you may not use this file except in compliance with the License. |
| 7 | +// You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, software |
| 12 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +// See the License for the specific language governing permissions and |
| 15 | +// limitations under the License. |
| 16 | + |
| 17 | +package fftm |
| 18 | + |
| 19 | +import ( |
| 20 | + "encoding/json" |
| 21 | + "fmt" |
| 22 | + "testing" |
| 23 | + |
| 24 | + "github.com/go-resty/resty/v2" |
| 25 | + "github.com/hyperledger/firefly-common/pkg/fftypes" |
| 26 | + "github.com/hyperledger/firefly-transaction-manager/mocks/ffcapimocks" |
| 27 | + "github.com/hyperledger/firefly-transaction-manager/pkg/apitypes" |
| 28 | + "github.com/hyperledger/firefly-transaction-manager/pkg/ffcapi" |
| 29 | + "github.com/stretchr/testify/assert" |
| 30 | + "github.com/stretchr/testify/mock" |
| 31 | +) |
| 32 | + |
| 33 | +func TestGetAddressBalanceOK(t *testing.T) { |
| 34 | + url, m, done := newTestManager(t) |
| 35 | + defer done() |
| 36 | + |
| 37 | + mfc := m.connector.(*ffcapimocks.API) |
| 38 | + |
| 39 | + mfc.On("AddressBalance", mock.Anything, mock.Anything).Return(&ffcapi.AddressBalanceResponse{Balance: fftypes.NewFFBigInt(999)}, ffcapi.ErrorReason(""), nil) |
| 40 | + |
| 41 | + err := m.Start() |
| 42 | + assert.NoError(t, err) |
| 43 | + |
| 44 | + var liv apitypes.LiveAddressBalance |
| 45 | + res, err := resty.New().R(). |
| 46 | + SetResult(&liv). |
| 47 | + Get(url + "/gastoken/balances/0x4a8c8f1717570f9774652075e249ded38124d708") |
| 48 | + assert.NoError(t, err) |
| 49 | + assert.Equal(t, 200, res.StatusCode()) |
| 50 | + var responseObj fftypes.JSONObject |
| 51 | + err = json.Unmarshal(res.Body(), &responseObj) |
| 52 | + assert.NoError(t, err) |
| 53 | + assert.Equal(t, responseObj.GetString("balance"), "999") |
| 54 | +} |
| 55 | + |
| 56 | +func TestGetAddressBalanceBadAddress(t *testing.T) { |
| 57 | + url, m, done := newTestManager(t) |
| 58 | + defer done() |
| 59 | + |
| 60 | + mfc := m.connector.(*ffcapimocks.API) |
| 61 | + mfc.On("AddressBalance", mock.Anything, mock.Anything).Return(nil, ffcapi.ErrorReason(""), fmt.Errorf("pop")) |
| 62 | + |
| 63 | + err := m.Start() |
| 64 | + assert.NoError(t, err) |
| 65 | + |
| 66 | + var liv apitypes.LiveAddressBalance |
| 67 | + res, err := resty.New().R(). |
| 68 | + SetResult(&liv). |
| 69 | + Get(url + "/gastoken/balances/0x4a8c8f1717570f9774652075e249ded38124d708") |
| 70 | + assert.NoError(t, err) |
| 71 | + assert.Equal(t, 500, res.StatusCode()) |
| 72 | +} |
0 commit comments