github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/core/discovery/brokerdiscovery/registry_test.go (about)

     1  /*
     2   * Copyright (C) 2019 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 brokerdiscovery
    19  
    20  import (
    21  	"encoding/json"
    22  	"testing"
    23  
    24  	"github.com/mysteriumnetwork/node/communication"
    25  	"github.com/mysteriumnetwork/node/communication/nats"
    26  	"github.com/mysteriumnetwork/node/identity"
    27  	"github.com/mysteriumnetwork/node/market"
    28  	"github.com/stretchr/testify/assert"
    29  )
    30  
    31  var (
    32  	newProposal           = market.ServiceProposal{ProviderID: "0x1"}
    33  	newProposalPayload, _ = json.Marshal(newProposal)
    34  )
    35  
    36  func Test_NewRegistry(t *testing.T) {
    37  	connection := nats.NewConnectionMock()
    38  
    39  	assert.Equal(
    40  		t,
    41  		&registryBroker{
    42  			sender: nats.NewSender(connection, communication.NewCodecJSON()),
    43  		},
    44  		NewRegistry(connection),
    45  	)
    46  }
    47  
    48  func Test_Registry_RegisterProposal(t *testing.T) {
    49  	connection := nats.StartConnectionMock()
    50  	defer connection.Close()
    51  
    52  	registry := NewRegistry(connection)
    53  	err := registry.RegisterProposal(newProposal, &identity.SignerFake{})
    54  	assert.NoError(t, err)
    55  
    56  	assert.JSONEq(
    57  		t,
    58  		`{
    59  			"proposal": `+string(newProposalPayload)+`
    60  		}`,
    61  		string(connection.GetLastMessage()),
    62  	)
    63  }
    64  
    65  func Test_Registry_UnregisterProposal(t *testing.T) {
    66  	connection := nats.StartConnectionMock()
    67  	defer connection.Close()
    68  
    69  	registry := NewRegistry(connection)
    70  	err := registry.UnregisterProposal(newProposal, &identity.SignerFake{})
    71  	assert.NoError(t, err)
    72  
    73  	assert.JSONEq(
    74  		t,
    75  		`{
    76  			"proposal": `+string(newProposalPayload)+`
    77  		}`,
    78  		string(connection.GetLastMessage()),
    79  	)
    80  }
    81  
    82  func Test_Registry_PingProposal(t *testing.T) {
    83  	connection := nats.StartConnectionMock()
    84  	defer connection.Close()
    85  
    86  	registry := NewRegistry(connection)
    87  	err := registry.PingProposal(newProposal, &identity.SignerFake{})
    88  	assert.NoError(t, err)
    89  
    90  	assert.JSONEq(
    91  		t,
    92  		`{
    93  			"proposal": `+string(newProposalPayload)+`
    94  		}`,
    95  		string(connection.GetLastMessage()),
    96  	)
    97  }