github.com/aergoio/aergo@v1.3.1/rpc/grpcserver_test.go (about)

     1  /**
     2   *  @file
     3   *  @copyright defined in aergo/LICENSE.txt
     4   */
     5  package rpc
     6  
     7  import (
     8  	"context"
     9  	"encoding/hex"
    10  	"fmt"
    11  	"math/big"
    12  	"reflect"
    13  	"testing"
    14  
    15  	"github.com/aergoio/aergo/message/messagemock"
    16  	"github.com/aergoio/aergo/p2p/p2pmock"
    17  	"github.com/golang/mock/gomock"
    18  
    19  	"github.com/aergoio/aergo-actor/actor"
    20  	"github.com/aergoio/aergo/internal/enc"
    21  	"github.com/aergoio/aergo/message"
    22  	"github.com/aergoio/aergo/p2p/p2pcommon"
    23  	"github.com/aergoio/aergo/pkg/component"
    24  	"github.com/aergoio/aergo/types"
    25  	"github.com/mr-tron/base58/base58"
    26  )
    27  
    28  func TestAergoRPCService_dummys(t *testing.T) {
    29  	fmt.Println("dummyBlockHash")
    30  	fmt.Printf("HEX : %s \n", hex.EncodeToString(dummyBlockHash))
    31  	fmt.Printf("B64 : %s \n", enc.ToString(dummyBlockHash))
    32  	fmt.Printf("B58 : %s \n", base58.Encode(dummyBlockHash))
    33  	fmt.Println()
    34  	fmt.Println("dummyTx")
    35  	fmt.Printf("HEX : %s \n", hex.EncodeToString(dummyTxHash))
    36  	fmt.Printf("B64 : %s \n", enc.ToString(dummyTxHash))
    37  	fmt.Printf("B58 : %s \n", base58.Encode(dummyTxHash))
    38  	fmt.Println()
    39  
    40  	fmt.Println("Address1")
    41  	fmt.Printf("HEX : %s \n", hex.EncodeToString(dummyWalletAddress))
    42  	fmt.Printf("B64 : %s \n", enc.ToString(dummyWalletAddress))
    43  	fmt.Printf("B58 : %s \n", base58.Encode(dummyWalletAddress))
    44  	fmt.Println()
    45  
    46  	fmt.Println("Address2")
    47  	fmt.Printf("HEX : %s \n", hex.EncodeToString(dummyWalletAddress2))
    48  	fmt.Printf("B64 : %s \n", enc.ToString(dummyWalletAddress2))
    49  	fmt.Printf("B58 : %s \n", base58.Encode(dummyWalletAddress2))
    50  	fmt.Println()
    51  
    52  }
    53  
    54  var dummyBlockHash, _ = hex.DecodeString("4f461d85e869ade8a0544f8313987c33a9c06534e50c4ad941498299579bd7ac")
    55  var dummyBlockHeight uint32 = 100215
    56  var dummyTxHash, _ = hex.DecodeString("218bdab4e87fb332b55eb89854ef553f9e3d440c81fff4161b672adede1261ee")
    57  
    58  // base64 encoding of dummyTxHash is ""
    59  var dummyWalletAddress, _ = base58.Decode("1Ee8uhLFXzkSRRU1orBpgXFAPpVi64aSYo")
    60  var dummyWalletAddress2, _ = base58.Decode("16Uiu2HAkwgfFvViH6j2QpQYKtGKKdveEKZvU2T5mRkqFLTZKU4Vp")
    61  var dummyPayload = []byte("OPreturn I am groooot")
    62  
    63  var hubStub *component.ComponentHub
    64  var mockCtx context.Context
    65  var mockMsgHelper *messagemock.Helper
    66  var mockActorHelper *MockActorService
    67  
    68  func init() {
    69  	hubStub = &component.ComponentHub{}
    70  
    71  	mockCtx = &Context{}
    72  }
    73  func TestAergoRPCService_GetTX(t *testing.T) {
    74  	ctrl := gomock.NewController(t)
    75  	defer ctrl.Finish()
    76  
    77  	mockMsgHelper := messagemock.NewHelper(ctrl)
    78  	mockActorHelper := p2pmock.NewMockActorService(ctrl)
    79  
    80  	dummyTxBody := types.TxBody{Account: dummyWalletAddress, Amount: new(big.Int).SetUint64(4332).Bytes(),
    81  		Recipient: dummyWalletAddress2, Payload: dummyPayload}
    82  	sampleTx := &types.Tx{Hash: dummyTxHash, Body: &dummyTxBody}
    83  	mockActorHelper.EXPECT().CallRequestDefaultTimeout(message.MemPoolSvc, gomock.Any()).Return(message.MemPoolGetRsp{}, nil)
    84  	mockMsgHelper.EXPECT().ExtractTxFromResponse(gomock.AssignableToTypeOf(message.MemPoolGetRsp{})).Return(sampleTx, nil)
    85  	type fields struct {
    86  		hub         *component.ComponentHub
    87  		actorHelper p2pcommon.ActorService
    88  		msgHelper   message.Helper
    89  	}
    90  	type args struct {
    91  		ctx context.Context
    92  		in  *types.SingleBytes
    93  	}
    94  	tests := []struct {
    95  		name    string
    96  		fields  fields
    97  		args    args
    98  		want    *types.Tx
    99  		wantErr bool
   100  	}{
   101  		{name: "T00", args: args{ctx: mockCtx, in: &types.SingleBytes{Value: append(dummyTxHash, 'b', 'd')}}, fields: fields{hubStub, mockActorHelper, mockMsgHelper},
   102  			want: &types.Tx{Hash: dummyTxHash, Body: &dummyTxBody}, wantErr: false},
   103  		// TODO: Add test cases.
   104  	}
   105  	for _, tt := range tests {
   106  		t.Run(tt.name, func(t *testing.T) {
   107  			rpc := &AergoRPCService{
   108  				hub: tt.fields.hub, actorHelper: mockActorHelper, msgHelper: mockMsgHelper,
   109  			}
   110  			got, err := rpc.GetTX(tt.args.ctx, tt.args.in)
   111  			if (err != nil) != tt.wantErr {
   112  				t.Errorf("AergoRPCService.GetTX() error = %v, wantErr %v", err, tt.wantErr)
   113  				return
   114  			}
   115  			if !reflect.DeepEqual(got, tt.want) {
   116  				t.Errorf("AergoRPCService.GetTX() = %v, want %v", got, tt.want)
   117  			}
   118  		})
   119  	}
   120  }
   121  
   122  type FutureStub struct {
   123  	actor.Future
   124  	dumbResult interface{}
   125  }
   126  
   127  func (fs *FutureStub) Result() interface{} {
   128  	return fs.dumbResult
   129  }
   130  
   131  func NewFutureStub(result interface{}) FutureStub {
   132  	return FutureStub{dumbResult: result}
   133  }