github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/market/service_proposal_test.go (about) 1 /* 2 * Copyright (C) 2017 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 market 19 20 import ( 21 "encoding/json" 22 "testing" 23 24 "github.com/mysteriumnetwork/node/config" 25 "github.com/stretchr/testify/assert" 26 ) 27 28 type mockContact struct{} 29 30 func init() { 31 RegisterContactUnserializer("mock_contact", 32 func(rawMessage *json.RawMessage) (ContactDefinition, error) { 33 return mockContact{}, nil 34 }, 35 ) 36 } 37 38 func Test_ServiceProposal_Serialize(t *testing.T) { 39 config.Current.SetDefault(config.FlagDefaultCurrency.Name, "MYSTT") 40 sp := NewProposal("node", "mock_service", NewProposalOpts{ 41 Quality: &Quality{ 42 Quality: 2.0, 43 Latency: 5, 44 Bandwidth: 100, 45 Uptime: 20, 46 }, 47 Contacts: ContactList{}, 48 }) 49 50 jsonBytes, err := json.Marshal(sp) 51 assert.Nil(t, err) 52 53 expectedJSON := `{ 54 "compatibility": 2, 55 "format": "service-proposal/v3", 56 "service_type": "mock_service", 57 "provider_id": "node", 58 "location": {}, 59 "id": 0, 60 "quality": { 61 "quality": 2.0, 62 "latency": 5, 63 "bandwidth": 100, 64 "uptime": 20 65 }, 66 "contacts": [] 67 }` 68 assert.JSONEq(t, expectedJSON, string(jsonBytes)) 69 } 70 71 func Test_ServiceProposal_Unserialize(t *testing.T) { 72 RegisterServiceType("mock_service") 73 jsonData := []byte(`{ 74 "id": 1, 75 "format": "service-proposal/v3", 76 "provider_id": "node", 77 "service_type": "mock_service", 78 "contacts": [ 79 { "type" : "mock_contact" , "definition" : {}} 80 ] 81 }`) 82 83 var actual ServiceProposal 84 err := json.Unmarshal(jsonData, &actual) 85 assert.NoError(t, err) 86 87 expected := ServiceProposal{ 88 ID: 1, 89 Format: proposalFormat, 90 ServiceType: "mock_service", 91 ProviderID: "node", 92 Contacts: ContactList{ 93 Contact{ 94 Type: "mock_contact", 95 Definition: mockContact{}, 96 }, 97 }, 98 } 99 assert.Equal(t, expected, actual) 100 assert.True(t, actual.IsSupported()) 101 } 102 103 func Test_ServiceProposal_UnserializeAccessPolicy(t *testing.T) { 104 RegisterServiceType("mock_service") 105 jsonData := []byte(`{ 106 "id": 1, 107 "format": "service-proposal/v3", 108 "service_type": "mock_service", 109 "provider_id": "node", 110 "contacts": [ 111 { "type" : "mock_contact" , "definition" : {}} 112 ], 113 "access_policies": [{ 114 "id": "verified-traffic", 115 "source": "https://mysterium-oracle.mysterium.network/v1/lists/verified-traffic" 116 }, 117 { 118 "id": "dvpn-traffic", 119 "source": "https://mysterium-oracle.mysterium.network/v1/lists/dvpn-traffic" 120 }] 121 }`) 122 123 var actual ServiceProposal 124 err := json.Unmarshal(jsonData, &actual) 125 assert.NoError(t, err) 126 127 accessPolicies := []AccessPolicy{ 128 { 129 ID: "verified-traffic", 130 Source: "https://mysterium-oracle.mysterium.network/v1/lists/verified-traffic", 131 }, 132 { 133 ID: "dvpn-traffic", 134 Source: "https://mysterium-oracle.mysterium.network/v1/lists/dvpn-traffic", 135 }, 136 } 137 expected := ServiceProposal{ 138 ID: 1, 139 Format: proposalFormat, 140 ServiceType: "mock_service", 141 ProviderID: "node", 142 Contacts: ContactList{ 143 Contact{ 144 Type: "mock_contact", 145 Definition: mockContact{}, 146 }, 147 }, 148 AccessPolicies: &accessPolicies, 149 } 150 assert.Equal(t, expected, actual) 151 assert.True(t, actual.IsSupported()) 152 }