github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/core/service/registry_test.go (about) 1 /* 2 * Copyright (C) 2018 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 service 19 20 import ( 21 "errors" 22 "testing" 23 24 "github.com/mysteriumnetwork/node/market" 25 "github.com/stretchr/testify/assert" 26 ) 27 28 var ( 29 proposalMock = market.ServiceProposal{} 30 serviceMock = &serviceFake{} 31 ) 32 33 func TestRegistry_Factory(t *testing.T) { 34 registry := NewRegistry() 35 assert.Len(t, registry.factories, 0) 36 } 37 38 func TestRegistry_Register(t *testing.T) { 39 registry := mockRegistryEmpty() 40 41 registry.Register( 42 "any", 43 func(options Options) (Service, error) { 44 return serviceMock, nil 45 }, 46 ) 47 assert.Len(t, registry.factories, 1) 48 } 49 50 func TestRegistry_Create_NonExisting(t *testing.T) { 51 registry := mockRegistryEmpty() 52 53 service, err := registry.Create("missing-service", nil) 54 assert.Nil(t, service) 55 assert.Equal(t, ErrUnsupportedServiceType, err) 56 } 57 58 func TestRegistry_Create_Existing(t *testing.T) { 59 registry := mockRegistryWith( 60 "fake-service", 61 func(options Options) (Service, error) { 62 return serviceMock, nil 63 }, 64 ) 65 66 service, err := registry.Create("fake-service", nil) 67 assert.Equal(t, serviceMock, service) 68 assert.NoError(t, err) 69 } 70 71 func TestRegistry_Create_BubblesErrors(t *testing.T) { 72 fakeErr := errors.New("I am broken") 73 registry := mockRegistryWith( 74 "broken-service", 75 func(options Options) (Service, error) { 76 return nil, fakeErr 77 }, 78 ) 79 80 service, err := registry.Create("broken-service", nil) 81 assert.Nil(t, service) 82 assert.Exactly(t, fakeErr, err) 83 } 84 85 func mockRegistryEmpty() *Registry { 86 return &Registry{ 87 factories: map[string]RegistryFactory{}, 88 } 89 } 90 91 func mockRegistryWith(serviceType string, serviceFactory RegistryFactory) *Registry { 92 return &Registry{ 93 factories: map[string]RegistryFactory{ 94 serviceType: serviceFactory, 95 }, 96 } 97 }