Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 28 additions & 41 deletions api/groups/networkGroup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"math/big"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -96,8 +95,8 @@ func TestNetworkConfigMetrics_ShouldWork(t *testing.T) {

statusMetricsProvider := statusHandler.NewStatusMetrics()
key := common.MetricMinGasLimit
value := uint64(37)
statusMetricsProvider.SetUInt64Value(key, value)
val := uint64(37)
statusMetricsProvider.SetUInt64Value(key, val)

facade := mock.FacadeStub{}
facade.StatusMetricsHandler = func() external.StatusMetricsHandler {
Expand All @@ -113,17 +112,15 @@ func TestNetworkConfigMetrics_ShouldWork(t *testing.T) {
resp := httptest.NewRecorder()
ws.ServeHTTP(resp, req)

respBytes, _ := ioutil.ReadAll(resp.Body)
respBytes, _ := io.ReadAll(resp.Body)
respStr := string(respBytes)
assert.Equal(t, resp.Code, http.StatusOK)

keyAndValueFoundInResponse := strings.Contains(respStr, key) && strings.Contains(respStr, fmt.Sprintf("%d", value))
keyAndValueFoundInResponse := strings.Contains(respStr, key) && strings.Contains(respStr, fmt.Sprintf("%d", val))
assert.True(t, keyAndValueFoundInResponse)
}

func TestGetNetworkConfig_ShouldReturnErrorIfFacadeReturnsError(t *testing.T) {
expectedErr := errors.New("i am an error")

facade := mock.FacadeStub{
StatusMetricsHandler: func() external.StatusMetricsHandler {
return &testscommon.StatusMetricsStub{
Expand Down Expand Up @@ -152,8 +149,6 @@ func TestGetNetworkConfig_ShouldReturnErrorIfFacadeReturnsError(t *testing.T) {
}

func TestGetNetworkStatus_ShouldReturnErrorIfFacadeReturnsError(t *testing.T) {
expectedErr := errors.New("i am an error")

facade := mock.FacadeStub{
StatusMetricsHandler: func() external.StatusMetricsHandler {
return &testscommon.StatusMetricsStub{
Expand Down Expand Up @@ -186,8 +181,8 @@ func TestNetworkConfigMetrics_GasLimitGuardedTxShouldWork(t *testing.T) {

statusMetricsProvider := statusHandler.NewStatusMetrics()
key := common.MetricExtraGasLimitGuardedTx
value := uint64(37)
statusMetricsProvider.SetUInt64Value(key, value)
val := uint64(37)
statusMetricsProvider.SetUInt64Value(key, val)

facade := mock.FacadeStub{}
facade.StatusMetricsHandler = func() external.StatusMetricsHandler {
Expand All @@ -203,11 +198,11 @@ func TestNetworkConfigMetrics_GasLimitGuardedTxShouldWork(t *testing.T) {
resp := httptest.NewRecorder()
ws.ServeHTTP(resp, req)

respBytes, _ := ioutil.ReadAll(resp.Body)
respBytes, _ := io.ReadAll(resp.Body)
respStr := string(respBytes)
assert.Equal(t, resp.Code, http.StatusOK)

keyAndValueFoundInResponse := strings.Contains(respStr, key) && strings.Contains(respStr, fmt.Sprintf("%d", value))
keyAndValueFoundInResponse := strings.Contains(respStr, key) && strings.Contains(respStr, fmt.Sprintf("%d", val))
assert.True(t, keyAndValueFoundInResponse)
}

Expand All @@ -216,8 +211,8 @@ func TestNetworkStatusMetrics_ShouldWork(t *testing.T) {

statusMetricsProvider := statusHandler.NewStatusMetrics()
key := common.MetricEpochNumber
value := uint64(37)
statusMetricsProvider.SetUInt64Value(key, value)
val := uint64(37)
statusMetricsProvider.SetUInt64Value(key, val)

facade := mock.FacadeStub{}
facade.StatusMetricsHandler = func() external.StatusMetricsHandler {
Expand All @@ -233,19 +228,19 @@ func TestNetworkStatusMetrics_ShouldWork(t *testing.T) {
resp := httptest.NewRecorder()
ws.ServeHTTP(resp, req)

respBytes, _ := ioutil.ReadAll(resp.Body)
respBytes, _ := io.ReadAll(resp.Body)
respStr := string(respBytes)
assert.Equal(t, resp.Code, http.StatusOK)

keyAndValueFoundInResponse := strings.Contains(respStr, key) && strings.Contains(respStr, fmt.Sprintf("%d", value))
keyAndValueFoundInResponse := strings.Contains(respStr, key) && strings.Contains(respStr, fmt.Sprintf("%d", val))
assert.True(t, keyAndValueFoundInResponse)
}

func TestEconomicsMetrics_ShouldWork(t *testing.T) {
statusMetricsProvider := statusHandler.NewStatusMetrics()
key := common.MetricTotalSupply
value := "12345"
statusMetricsProvider.SetStringValue(key, value)
val := "12345"
statusMetricsProvider.SetStringValue(key, val)

facade := mock.FacadeStub{
GetTotalStakedValueHandler: func() (*api.StakeValues, error) {
Expand All @@ -268,17 +263,15 @@ func TestEconomicsMetrics_ShouldWork(t *testing.T) {
resp := httptest.NewRecorder()
ws.ServeHTTP(resp, req)

respBytes, _ := ioutil.ReadAll(resp.Body)
respBytes, _ := io.ReadAll(resp.Body)
respStr := string(respBytes)
assert.Equal(t, resp.Code, http.StatusOK)

keyAndValueFoundInResponse := strings.Contains(respStr, key) && strings.Contains(respStr, value)
keyAndValueFoundInResponse := strings.Contains(respStr, key) && strings.Contains(respStr, val)
assert.True(t, keyAndValueFoundInResponse)
}

func TestGetEconomicValues_ShouldReturnErrorIfFacadeReturnsError(t *testing.T) {
expectedErr := errors.New("i am an error")

facade := mock.FacadeStub{
StatusMetricsHandler: func() external.StatusMetricsHandler {
return &testscommon.StatusMetricsStub{
Expand Down Expand Up @@ -312,8 +305,8 @@ func TestGetEconomicValues_ShouldReturnErrorIfFacadeReturnsError(t *testing.T) {
func TestEconomicsMetrics_CannotGetStakeValues(t *testing.T) {
statusMetricsProvider := statusHandler.NewStatusMetrics()
key := common.MetricTotalSupply
value := "12345"
statusMetricsProvider.SetStringValue(key, value)
val := "12345"
statusMetricsProvider.SetStringValue(key, val)

localErr := fmt.Errorf("%s", "local error")
facade := mock.FacadeStub{
Expand Down Expand Up @@ -410,7 +403,7 @@ func TestDirectStakedInfo_ShouldWork(t *testing.T) {
resp := httptest.NewRecorder()
ws.ServeHTTP(resp, req)

respBytes, _ := ioutil.ReadAll(resp.Body)
respBytes, _ := io.ReadAll(resp.Body)
respStr := string(respBytes)
assert.Equal(t, resp.Code, http.StatusOK)

Expand Down Expand Up @@ -440,7 +433,7 @@ func TestDirectStakedInfo_CannotGetDirectStakedList(t *testing.T) {
resp := httptest.NewRecorder()
ws.ServeHTTP(resp, req)

respBytes, _ := ioutil.ReadAll(resp.Body)
respBytes, _ := io.ReadAll(resp.Body)
respStr := string(respBytes)

assert.Equal(t, resp.Code, http.StatusInternalServerError)
Expand Down Expand Up @@ -495,7 +488,7 @@ func TestDelegatedInfo_ShouldWork(t *testing.T) {
resp := httptest.NewRecorder()
ws.ServeHTTP(resp, req)

respBytes, _ := ioutil.ReadAll(resp.Body)
respBytes, _ := io.ReadAll(resp.Body)
respStr := string(respBytes)
assert.Equal(t, resp.Code, http.StatusOK)

Expand Down Expand Up @@ -534,16 +527,14 @@ func TestDelegatedInfo_CannotGetDelegatedList(t *testing.T) {
resp := httptest.NewRecorder()
ws.ServeHTTP(resp, req)

respBytes, _ := ioutil.ReadAll(resp.Body)
respBytes, _ := io.ReadAll(resp.Body)
respStr := string(respBytes)

assert.Equal(t, resp.Code, http.StatusInternalServerError)
assert.True(t, strings.Contains(respStr, expectedError.Error()))
}

func TestGetEnableEpochs_ShouldReturnErrorIfFacadeReturnsError(t *testing.T) {
expectedErr := errors.New("i am an error")

facade := mock.FacadeStub{
StatusMetricsHandler: func() external.StatusMetricsHandler {
return &testscommon.StatusMetricsStub{
Expand Down Expand Up @@ -576,8 +567,8 @@ func TestGetEnableEpochs_ShouldWork(t *testing.T) {

statusMetrics := statusHandler.NewStatusMetrics()
key := common.MetricScDeployEnableEpoch
value := uint64(4)
statusMetrics.SetUInt64Value(key, value)
val := uint64(4)
statusMetrics.SetUInt64Value(key, val)

facade := mock.FacadeStub{}
facade.StatusMetricsHandler = func() external.StatusMetricsHandler {
Expand All @@ -593,18 +584,17 @@ func TestGetEnableEpochs_ShouldWork(t *testing.T) {
resp := httptest.NewRecorder()
ws.ServeHTTP(resp, req)

respBytes, _ := ioutil.ReadAll(resp.Body)
respBytes, _ := io.ReadAll(resp.Body)
respStr := string(respBytes)
assert.Equal(t, resp.Code, http.StatusOK)

keyAndValueFoundInResponse := strings.Contains(respStr, key) && strings.Contains(respStr, strconv.FormatUint(value, 10))
keyAndValueFoundInResponse := strings.Contains(respStr, key) && strings.Contains(respStr, strconv.FormatUint(val, 10))
assert.True(t, keyAndValueFoundInResponse)
}

func TestGetESDTTotalSupply_InternalError(t *testing.T) {
t.Parallel()

expectedErr := errors.New("expected error")
facade := mock.FacadeStub{
GetTokenSupplyCalled: func(token string) (*api.ESDTSupply, error) {
return nil, expectedErr
Expand All @@ -620,7 +610,7 @@ func TestGetESDTTotalSupply_InternalError(t *testing.T) {
resp := httptest.NewRecorder()
ws.ServeHTTP(resp, req)

respBytes, _ := ioutil.ReadAll(resp.Body)
respBytes, _ := io.ReadAll(resp.Body)
respStr := string(respBytes)
assert.Equal(t, resp.Code, http.StatusInternalServerError)

Expand All @@ -629,8 +619,6 @@ func TestGetESDTTotalSupply_InternalError(t *testing.T) {
}

func TestGetNetworkRatings_ShouldReturnErrorIfFacadeReturnsError(t *testing.T) {
expectedErr := errors.New("i am an error")

facade := mock.FacadeStub{
StatusMetricsHandler: func() external.StatusMetricsHandler {
return &testscommon.StatusMetricsStub{
Expand Down Expand Up @@ -716,7 +704,7 @@ func TestGetESDTTotalSupply(t *testing.T) {
resp := httptest.NewRecorder()
ws.ServeHTTP(resp, req)

respBytes, _ := ioutil.ReadAll(resp.Body)
respBytes, _ := io.ReadAll(resp.Body)
assert.Equal(t, resp.Code, http.StatusOK)

respSupply := &supplyResponse{}
Expand Down Expand Up @@ -964,7 +952,6 @@ func TestNetworkGroup_UpdateFacade(t *testing.T) {
loadResponse(resp.Body, &response)
assert.Equal(t, builtInCost, response.Data.Configs.BuiltInCost)

expectedErr := errors.New("expected error")
newFacade := mock.FacadeStub{
GetGasConfigsCalled: func() (map[string]map[string]uint64, error) {
return nil, expectedErr
Expand Down
Loading