mirror of
https://github.com/planetmint/planetmint-go.git
synced 2025-11-26 15:35:46 +00:00
WIP: add AssetClientService and mock
Signed-off-by: Lorenz Herzberger <lorenzherzberger@gmail.com>
This commit is contained in:
parent
a02204315c
commit
ee5a955179
@ -14,6 +14,7 @@ import (
|
||||
simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims"
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
|
||||
"github.com/golang/mock/gomock"
|
||||
"github.com/planetmint/planetmint-go/app"
|
||||
"github.com/planetmint/planetmint-go/config"
|
||||
"github.com/planetmint/planetmint-go/lib"
|
||||
@ -21,6 +22,8 @@ import (
|
||||
"github.com/planetmint/planetmint-go/util"
|
||||
"github.com/planetmint/planetmint-go/util/mocks"
|
||||
daotypes "github.com/planetmint/planetmint-go/x/dao/types"
|
||||
machine "github.com/planetmint/planetmint-go/x/machine/keeper"
|
||||
machinetestutil "github.com/planetmint/planetmint-go/x/machine/testutil"
|
||||
elements "github.com/rddl-network/elements-rpc"
|
||||
elementsmocks "github.com/rddl-network/elements-rpc/utils/mocks"
|
||||
"github.com/stretchr/testify/require"
|
||||
@ -44,6 +47,14 @@ func Load(t *testing.T, configs ...Config) *Network {
|
||||
util.MQTTClient = &mocks.MockMQTTClient{}
|
||||
elements.Client = &elementsmocks.MockClient{}
|
||||
|
||||
// call to set sync.Once
|
||||
_ = machine.GetAssetServiceClient()
|
||||
ctrl := gomock.NewController(t)
|
||||
ascMock := machinetestutil.NewMockIAssetServiceClient(ctrl)
|
||||
ascMock.EXPECT().RegisterAsset(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).AnyTimes()
|
||||
ascMock.EXPECT().IssueMachineNFT(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).AnyTimes()
|
||||
machine.SetAssetServiceClient(ascMock)
|
||||
|
||||
// enable application logger in tests
|
||||
appLogger := util.GetAppLogger()
|
||||
appLogger.SetTestingLogger(t)
|
||||
|
||||
@ -45,7 +45,8 @@ func (k msgServer) AttestMachine(goCtx context.Context, msg *types.MsgAttestMach
|
||||
domain := k.GetParams(ctx).AssetRegistryDomain
|
||||
path := k.GetParams(ctx).AssetRegistryPath
|
||||
//go func() {
|
||||
localErr := issueMachineNFT(goCtx, msg.Machine, scheme, domain, path)
|
||||
asc := GetAssetServiceClient()
|
||||
localErr := asc.IssueMachineNFT(goCtx, msg.Machine, scheme, domain, path)
|
||||
if localErr != nil {
|
||||
util.GetAppLogger().Error(ctx, "Machine NFT issuance failed : "+localErr.Error())
|
||||
} else {
|
||||
|
||||
@ -11,6 +11,7 @@ import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
errorsmod "cosmossdk.io/errors"
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
@ -20,7 +21,31 @@ import (
|
||||
elements "github.com/rddl-network/elements-rpc"
|
||||
)
|
||||
|
||||
func issueNFTAsset(goCtx context.Context, name string, machineAddress string, domain string) (assetID string, contract string, err error) {
|
||||
var (
|
||||
assetClientService IAssetServiceClient
|
||||
initAssetServiceClient sync.Once
|
||||
)
|
||||
|
||||
type IAssetServiceClient interface {
|
||||
IssueNFTAsset(goCtx context.Context, name string, machineAddress string, domain string) (assetID string, contract string, err error)
|
||||
IssueMachineNFT(goCtx context.Context, machine *types.Machine, scheme string, domain string, path string) error
|
||||
RegisterAsset(goCtx context.Context, assetID string, contract string, assetRegistryEndpoint string) error
|
||||
}
|
||||
|
||||
type AssetServiceClient struct{}
|
||||
|
||||
func GetAssetServiceClient() IAssetServiceClient {
|
||||
initAssetServiceClient.Do(func() {
|
||||
assetClientService = &AssetServiceClient{}
|
||||
})
|
||||
return assetClientService
|
||||
}
|
||||
|
||||
func SetAssetServiceClient(asc IAssetServiceClient) {
|
||||
assetClientService = asc
|
||||
}
|
||||
|
||||
func (asc *AssetServiceClient) IssueNFTAsset(goCtx context.Context, name string, machineAddress string, domain string) (assetID string, contract string, err error) {
|
||||
ctx := sdk.UnwrapSDKContext(goCtx)
|
||||
conf := config.GetConfig()
|
||||
|
||||
@ -113,12 +138,12 @@ func issueNFTAsset(goCtx context.Context, name string, machineAddress string, do
|
||||
return assetID, contract, err
|
||||
}
|
||||
|
||||
func issueMachineNFT(goCtx context.Context, machine *types.Machine, scheme string, domain string, path string) error {
|
||||
func (asc *AssetServiceClient) IssueMachineNFT(goCtx context.Context, machine *types.Machine, scheme string, domain string, path string) error {
|
||||
ctx := sdk.UnwrapSDKContext(goCtx)
|
||||
// asset registration is in order to have the contact published
|
||||
var notarizedAsset types.LiquidAsset
|
||||
notarizedAsset.Registered = true
|
||||
assetID, contract, err := issueNFTAsset(goCtx, machine.Name, machine.Address, domain)
|
||||
assetID, contract, err := asc.IssueNFTAsset(goCtx, machine.Name, machine.Address, domain)
|
||||
if err != nil {
|
||||
util.GetAppLogger().Error(ctx, err.Error())
|
||||
return err
|
||||
@ -126,7 +151,7 @@ func issueMachineNFT(goCtx context.Context, machine *types.Machine, scheme strin
|
||||
assetRegistryEndpoint := fmt.Sprintf("%s://%s/%s", scheme, domain, path)
|
||||
fmt.Println(" Register Asset: " + assetRegistryEndpoint)
|
||||
fmt.Println(" CONTRACT: " + contract)
|
||||
err = RegisterAsset(goCtx, assetID, contract, assetRegistryEndpoint)
|
||||
err = asc.RegisterAsset(goCtx, assetID, contract, assetRegistryEndpoint)
|
||||
if err != nil {
|
||||
util.GetAppLogger().Error(ctx, err.Error())
|
||||
notarizedAsset.Registered = false
|
||||
@ -140,7 +165,7 @@ func issueMachineNFT(goCtx context.Context, machine *types.Machine, scheme strin
|
||||
return err
|
||||
}
|
||||
|
||||
func RegisterAsset(goCtx context.Context, assetID string, contract string, assetRegistryEndpoint string) error {
|
||||
func (asc *AssetServiceClient) RegisterAsset(goCtx context.Context, assetID string, contract string, assetRegistryEndpoint string) error {
|
||||
var contractMap map[string]interface{}
|
||||
err := json.Unmarshal([]byte(contract), &contractMap)
|
||||
if err != nil {
|
||||
|
||||
@ -4,8 +4,10 @@ import (
|
||||
"testing"
|
||||
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
"github.com/golang/mock/gomock"
|
||||
keepertest "github.com/planetmint/planetmint-go/testutil/keeper"
|
||||
"github.com/planetmint/planetmint-go/x/machine/keeper"
|
||||
"github.com/planetmint/planetmint-go/x/machine/testutil"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
@ -15,6 +17,17 @@ func TestRegisterNFT(t *testing.T) {
|
||||
contract := `{"entity":{"domain":"testnet-assets.rddl.io"},"issuer_pubkey":"020000000000000000000000000000000000000000000000000000000000000000","machine_addr":"plmnt10mq5nj8jhh27z7ejnz2ql3nh0qhzjnfvy50877","name":"machine","precision":0,"version":0}`
|
||||
asset := "0000000000000000000000000000000000000000000000001000000000000000"
|
||||
goctx := sdk.WrapSDKContext(ctx)
|
||||
err := keeper.RegisterAsset(goctx, asset, contract, url)
|
||||
|
||||
// Call to set sync.Once
|
||||
_ = keeper.GetAssetServiceClient()
|
||||
|
||||
ctrl := gomock.NewController(t)
|
||||
ascMock := testutil.NewMockIAssetServiceClient(ctrl)
|
||||
ascMock.EXPECT().RegisterAsset(goctx, asset, contract, url).Return(nil).AnyTimes()
|
||||
keeper.SetAssetServiceClient(ascMock)
|
||||
|
||||
asc := keeper.GetAssetServiceClient()
|
||||
|
||||
err := asc.RegisterAsset(goctx, asset, contract, url)
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
|
||||
80
x/machine/testutil/register_nft_mock.go
Normal file
80
x/machine/testutil/register_nft_mock.go
Normal file
@ -0,0 +1,80 @@
|
||||
// Code generated by MockGen. DO NOT EDIT.
|
||||
// Source: x/machine/keeper/register_nft.go
|
||||
|
||||
// Package testutil is a generated GoMock package.
|
||||
package testutil
|
||||
|
||||
import (
|
||||
context "context"
|
||||
reflect "reflect"
|
||||
|
||||
gomock "github.com/golang/mock/gomock"
|
||||
types "github.com/planetmint/planetmint-go/x/machine/types"
|
||||
)
|
||||
|
||||
// MockIAssetServiceClient is a mock of IAssetServiceClient interface.
|
||||
type MockIAssetServiceClient struct {
|
||||
ctrl *gomock.Controller
|
||||
recorder *MockIAssetServiceClientMockRecorder
|
||||
}
|
||||
|
||||
// MockIAssetServiceClientMockRecorder is the mock recorder for MockIAssetServiceClient.
|
||||
type MockIAssetServiceClientMockRecorder struct {
|
||||
mock *MockIAssetServiceClient
|
||||
}
|
||||
|
||||
// NewMockIAssetServiceClient creates a new mock instance.
|
||||
func NewMockIAssetServiceClient(ctrl *gomock.Controller) *MockIAssetServiceClient {
|
||||
mock := &MockIAssetServiceClient{ctrl: ctrl}
|
||||
mock.recorder = &MockIAssetServiceClientMockRecorder{mock}
|
||||
return mock
|
||||
}
|
||||
|
||||
// EXPECT returns an object that allows the caller to indicate expected use.
|
||||
func (m *MockIAssetServiceClient) EXPECT() *MockIAssetServiceClientMockRecorder {
|
||||
return m.recorder
|
||||
}
|
||||
|
||||
// IssueMachineNFT mocks base method.
|
||||
func (m *MockIAssetServiceClient) IssueMachineNFT(goCtx context.Context, machine *types.Machine, scheme, domain, path string) error {
|
||||
m.ctrl.T.Helper()
|
||||
ret := m.ctrl.Call(m, "IssueMachineNFT", goCtx, machine, scheme, domain, path)
|
||||
ret0, _ := ret[0].(error)
|
||||
return ret0
|
||||
}
|
||||
|
||||
// IssueMachineNFT indicates an expected call of IssueMachineNFT.
|
||||
func (mr *MockIAssetServiceClientMockRecorder) IssueMachineNFT(goCtx, machine, scheme, domain, path interface{}) *gomock.Call {
|
||||
mr.mock.ctrl.T.Helper()
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "IssueMachineNFT", reflect.TypeOf((*MockIAssetServiceClient)(nil).IssueMachineNFT), goCtx, machine, scheme, domain, path)
|
||||
}
|
||||
|
||||
// IssueNFTAsset mocks base method.
|
||||
func (m *MockIAssetServiceClient) IssueNFTAsset(goCtx context.Context, name, machineAddress, domain string) (string, string, error) {
|
||||
m.ctrl.T.Helper()
|
||||
ret := m.ctrl.Call(m, "IssueNFTAsset", goCtx, name, machineAddress, domain)
|
||||
ret0, _ := ret[0].(string)
|
||||
ret1, _ := ret[1].(string)
|
||||
ret2, _ := ret[2].(error)
|
||||
return ret0, ret1, ret2
|
||||
}
|
||||
|
||||
// IssueNFTAsset indicates an expected call of IssueNFTAsset.
|
||||
func (mr *MockIAssetServiceClientMockRecorder) IssueNFTAsset(goCtx, name, machineAddress, domain interface{}) *gomock.Call {
|
||||
mr.mock.ctrl.T.Helper()
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "IssueNFTAsset", reflect.TypeOf((*MockIAssetServiceClient)(nil).IssueNFTAsset), goCtx, name, machineAddress, domain)
|
||||
}
|
||||
|
||||
// RegisterAsset mocks base method.
|
||||
func (m *MockIAssetServiceClient) RegisterAsset(goCtx context.Context, assetID, contract, assetRegistryEndpoint string) error {
|
||||
m.ctrl.T.Helper()
|
||||
ret := m.ctrl.Call(m, "RegisterAsset", goCtx, assetID, contract, assetRegistryEndpoint)
|
||||
ret0, _ := ret[0].(error)
|
||||
return ret0
|
||||
}
|
||||
|
||||
// RegisterAsset indicates an expected call of RegisterAsset.
|
||||
func (mr *MockIAssetServiceClientMockRecorder) RegisterAsset(goCtx, assetID, contract, assetRegistryEndpoint interface{}) *gomock.Call {
|
||||
mr.mock.ctrl.T.Helper()
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "RegisterAsset", reflect.TypeOf((*MockIAssetServiceClient)(nil).RegisterAsset), goCtx, assetID, contract, assetRegistryEndpoint)
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user