github.com/hyperledger/aries-framework-go@v0.3.2/pkg/didcomm/messaging/msghandler/msghandler_test.go (about)

     1  /*
     2   *
     3   * Copyright SecureKey Technologies Inc. All Rights Reserved.
     4   *
     5   * SPDX-License-Identifier: Apache-2.0
     6   * /
     7   *
     8   */
     9  
    10  package msghandler
    11  
    12  import (
    13  	"fmt"
    14  	"testing"
    15  
    16  	"github.com/stretchr/testify/require"
    17  
    18  	"github.com/hyperledger/aries-framework-go/pkg/didcomm/dispatcher"
    19  	"github.com/hyperledger/aries-framework-go/pkg/mock/didcomm/protocol/generic"
    20  )
    21  
    22  func TestRegistrar_Register(t *testing.T) {
    23  	tests := []struct {
    24  		testName string
    25  		svcs     []dispatcher.MessageService
    26  		err      string
    27  	}{
    28  		{
    29  			testName: "empty msg services",
    30  		},
    31  		{
    32  			testName: "add multiple msg services on empty list- test success",
    33  			svcs: []dispatcher.MessageService{
    34  				generic.NewCustomMockMessageSvc("test1", "sample-name-01"),
    35  				generic.NewCustomMockMessageSvc("test2", "sample-name-02"),
    36  				generic.NewCustomMockMessageSvc("test3", "sample-name-03"),
    37  			},
    38  		},
    39  		{
    40  			testName: "add multiple msg services on non empty list- test success",
    41  			svcs: []dispatcher.MessageService{
    42  				generic.NewCustomMockMessageSvc("test1", "sample-name-11"),
    43  				generic.NewCustomMockMessageSvc("test2", "sample-name-12"),
    44  				generic.NewCustomMockMessageSvc("test3", "sample-name-13"),
    45  			},
    46  		},
    47  		{
    48  			testName: "add multiple msg services - test duplicate scenario-1",
    49  			svcs: []dispatcher.MessageService{
    50  				generic.NewCustomMockMessageSvc("test1", "sample-name-21"),
    51  				generic.NewCustomMockMessageSvc("test2", "sample-name-11"),
    52  				generic.NewCustomMockMessageSvc("test3", "sample-name-23"),
    53  			},
    54  			err: fmt.Sprintf(errAlreadyRegistered, "sample-name-11"),
    55  		},
    56  		{
    57  			testName: "add multiple msg services - test duplicate scenario-2",
    58  			svcs: []dispatcher.MessageService{
    59  				generic.NewCustomMockMessageSvc("test1", "sample-name-21"),
    60  				generic.NewCustomMockMessageSvc("test2", "sample-name-22"),
    61  				generic.NewCustomMockMessageSvc("test3", "sample-name-23"),
    62  			},
    63  		},
    64  		{
    65  			testName: "add multiple msg services of same type - test success",
    66  			svcs: []dispatcher.MessageService{
    67  				generic.NewCustomMockMessageSvc("test", "sample-name-31"),
    68  				generic.NewCustomMockMessageSvc("test", "sample-name-32"),
    69  				generic.NewCustomMockMessageSvc("test", "sample-name-33"),
    70  			},
    71  		},
    72  	}
    73  
    74  	handler := NewRegistrar()
    75  	require.NotNil(t, handler)
    76  
    77  	for _, test := range tests {
    78  		tc := test
    79  
    80  		t.Run(tc.testName, func(t *testing.T) {
    81  			b4Count := len(handler.services)
    82  			err := handler.Register(tc.svcs...)
    83  
    84  			if tc.err != "" {
    85  				require.Error(t, err, "expected error: %s", tc.err)
    86  				require.Contains(t, err.Error(), tc.err)
    87  				require.Len(t, handler.Services(), b4Count)
    88  				return
    89  			}
    90  
    91  			require.NoError(t, err)
    92  			require.Len(t, handler.Services(), b4Count+len(tc.svcs))
    93  		})
    94  	}
    95  }
    96  
    97  func TestRegistrar_Unregister(t *testing.T) {
    98  	tests := []struct {
    99  		testName      string
   100  		register      []dispatcher.MessageService
   101  		unregister    []string
   102  		err           string
   103  		expectedCount int
   104  	}{
   105  		{
   106  			testName:   "unregister on empty msg services",
   107  			unregister: []string{"sample-name-01"},
   108  			err:        fmt.Sprintf(errNeverRegistered, "sample-name-01"),
   109  		},
   110  		{
   111  			testName: "add multiple msg services on empty list- test success",
   112  			register: []dispatcher.MessageService{
   113  				generic.NewCustomMockMessageSvc("test1", "sample-name-01"),
   114  				generic.NewCustomMockMessageSvc("test2", "sample-name-02"),
   115  				generic.NewCustomMockMessageSvc("test3", "sample-name-03"),
   116  			},
   117  			unregister:    []string{"sample-name-01", "sample-name-02"},
   118  			expectedCount: 1,
   119  		},
   120  		{
   121  			testName:   "unregister on empty msg services",
   122  			unregister: []string{"sample-name-01"},
   123  			err:        fmt.Sprintf(errNeverRegistered, "sample-name-01"),
   124  		},
   125  	}
   126  
   127  	handler := NewRegistrar()
   128  	require.NotNil(t, handler)
   129  
   130  	for _, test := range tests {
   131  		tc := test
   132  
   133  		t.Run(tc.testName, func(t *testing.T) {
   134  			err := handler.Register(tc.register...)
   135  			require.NoError(t, err)
   136  
   137  			for _, name := range tc.unregister {
   138  				b4Count := len(handler.services)
   139  				err = handler.Unregister(name)
   140  
   141  				if tc.err != "" {
   142  					require.Error(t, err, "expected error: %s", tc.err)
   143  					require.Contains(t, err.Error(), tc.err)
   144  					require.Len(t, handler.Services(), b4Count)
   145  					return
   146  				}
   147  
   148  				require.NoError(t, err)
   149  			}
   150  
   151  			require.Len(t, handler.Services(), tc.expectedCount)
   152  		})
   153  	}
   154  }