github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/tequilapi/endpoints/node_test.go (about) 1 /* 2 * Copyright (C) 2021 The "MysteriumNetwork/node" Authors. 3 * 4 * This program is free software: you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License as published by 6 * the Free Software Foundation, either version 3 of the License, or 7 * (at your option) any later version. 8 * 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with this program. If not, see <http://www.gnu.org/licenses/>. 16 */ 17 18 package endpoints 19 20 import ( 21 "encoding/json" 22 "fmt" 23 "net/http" 24 "net/http/httptest" 25 "testing" 26 27 "github.com/mysteriumnetwork/node/core/monitoring" 28 29 "github.com/gin-gonic/gin" 30 "github.com/stretchr/testify/assert" 31 32 "github.com/mysteriumnetwork/node/core/node" 33 "github.com/mysteriumnetwork/node/tequilapi/contract" 34 ) 35 36 type mockNodeStatusProvider struct { 37 status monitoring.Status 38 } 39 40 type mockMonitoringAgent struct { 41 status node.MonitoringAgentStatuses 42 sessions []node.SessionItem 43 data node.TransferredData 44 sessionsCount node.SessionsCount 45 consumersCount node.ConsumersCount 46 earningsSeries node.EarningsSeries 47 sessionsSeries node.SessionsSeries 48 transferredDataSeries node.TransferredDataSeries 49 providerQuality node.QualityInfo 50 providerActivityStats node.ActivityStats 51 serviceEarnings node.EarningsPerService 52 } 53 54 func (nodeStatusTracker *mockNodeStatusProvider) Status() monitoring.Status { 55 return nodeStatusTracker.status 56 } 57 58 func (nodeMonitoringAgentTracker *mockMonitoringAgent) Statuses() (node.MonitoringAgentStatuses, error) { 59 return nodeMonitoringAgentTracker.status, nil 60 } 61 62 func (nodeMonitoringAgentTracker *mockMonitoringAgent) Sessions(_ string) ([]node.SessionItem, error) { 63 return nodeMonitoringAgentTracker.sessions, nil 64 } 65 66 func (nodeMonitoringAgentTracker *mockMonitoringAgent) TransferredData(_ string) (node.TransferredData, error) { 67 return nodeMonitoringAgentTracker.data, nil 68 } 69 70 func (nodeMonitoringAgentTracker *mockMonitoringAgent) SessionsCount(_ string) (node.SessionsCount, error) { 71 return nodeMonitoringAgentTracker.sessionsCount, nil 72 } 73 74 func (nodeMonitoringAgentTracker *mockMonitoringAgent) ConsumersCount(_ string) (node.ConsumersCount, error) { 75 return nodeMonitoringAgentTracker.consumersCount, nil 76 } 77 78 func (nodeMonitoringAgentTracker *mockMonitoringAgent) EarningsSeries(_ string) (node.EarningsSeries, error) { 79 return nodeMonitoringAgentTracker.earningsSeries, nil 80 } 81 82 func (nodeMonitoringAgentTracker *mockMonitoringAgent) SessionsSeries(_ string) (node.SessionsSeries, error) { 83 return nodeMonitoringAgentTracker.sessionsSeries, nil 84 } 85 86 func (nodeMonitoringAgentTracker *mockMonitoringAgent) TransferredDataSeries(_ string) (node.TransferredDataSeries, error) { 87 return nodeMonitoringAgentTracker.transferredDataSeries, nil 88 } 89 90 func (nodeMonitoringAgentTracker *mockMonitoringAgent) ProviderQuality() (node.QualityInfo, error) { 91 return nodeMonitoringAgentTracker.providerQuality, nil 92 } 93 94 func (nodeMonitoringAgentTracker *mockMonitoringAgent) ProviderActivityStats() (node.ActivityStats, error) { 95 return nodeMonitoringAgentTracker.providerActivityStats, nil 96 } 97 98 func (nodeMonitoringAgentTracker *mockMonitoringAgent) EarningsPerService() (node.EarningsPerService, error) { 99 return nodeMonitoringAgentTracker.serviceEarnings, nil 100 } 101 102 func Test_NodeStatus(t *testing.T) { 103 // given: 104 mockStatusTracker := &mockNodeStatusProvider{} 105 mockMonitoringAgentTracker := &mockMonitoringAgent{} 106 107 router := gin.Default() 108 err := AddRoutesForNode(mockStatusTracker, mockMonitoringAgentTracker)(router) 109 assert.NoError(t, err) 110 111 req, err := http.NewRequest(http.MethodGet, "/node/monitoring-status", nil) 112 assert.Nil(t, err) 113 114 // expect: 115 for _, status := range []monitoring.Status{ 116 "success", 117 "failed", 118 "pending", 119 "unknown", 120 } { 121 t.Run(fmt.Sprintf("Consumer receives node status: %s", status), func(t *testing.T) { 122 resp := httptest.NewRecorder() 123 mockStatusTracker.status = status 124 router.ServeHTTP(resp, req) 125 126 result, err := json.Marshal(contract.NodeStatusResponse{Status: status}) 127 assert.NoError(t, err) 128 assert.JSONEq(t, string(result), resp.Body.String()) 129 }) 130 } 131 }