github.com/xmidt-org/webpa-common@v1.11.9/service/gateaccessor_test.go (about)

     1  package service
     2  
     3  import (
     4  	"errors"
     5  	"github.com/stretchr/testify/assert"
     6  	"github.com/stretchr/testify/mock"
     7  	"testing"
     8  	"time"
     9  )
    10  
    11  /****************** BEGIN MOCK DECLARATIONS ***********************/
    12  
    13  type mockGate struct {
    14  	mock.Mock
    15  }
    16  
    17  func (r mockGate) Raise() bool {
    18  	args := r.Called()
    19  	return args.Bool(0)
    20  }
    21  
    22  func (r mockGate) Lower() bool {
    23  	args := r.Called()
    24  	return args.Bool(0)
    25  }
    26  
    27  func (r mockGate) Open() bool {
    28  	args := r.Called()
    29  	return args.Bool(0)
    30  }
    31  
    32  func (r mockGate) State() (bool, time.Time) {
    33  	args := r.Called()
    34  	return args.Bool(0), args.Get(1).(time.Time)
    35  }
    36  
    37  func (r mockGate) String() string {
    38  	args := r.Called()
    39  	return args.String(0)
    40  }
    41  
    42  /******************* END MOCK DECLARATIONS ************************/
    43  
    44  func TestGateAccessor(t *testing.T) {
    45  	assert := assert.New(t)
    46  
    47  	accessor := new(MockAccessor)
    48  	gate := new(mockGate)
    49  
    50  	gateAcessor := GateAccessor(gate, accessor)
    51  
    52  	instance := "a valid instance"
    53  
    54  	gate.On("Open").Return(false).Once()
    55  	accessor.On("Get", []byte("testA")).Return(instance, nil)
    56  	i, err := gateAcessor.Get([]byte("testA"))
    57  	assert.Equal(instance, i)
    58  	assert.Equal(errGateClosed, err)
    59  
    60  	gate.On("Open").Return(true).Once()
    61  	accessor.On("Get", []byte("testB")).Return(instance, nil)
    62  	i, err = gateAcessor.Get([]byte("testB"))
    63  	assert.Equal(instance, i)
    64  	assert.NoError(err)
    65  
    66  	expectedErr := errors.New("no instances")
    67  	accessor.On("Get", []byte("testC")).Return(instance, expectedErr)
    68  	i, err = gateAcessor.Get([]byte("testC"))
    69  	assert.Equal(instance, i)
    70  	assert.Equal(expectedErr, err)
    71  
    72  	defaultGateAccessor := GateAccessor(nil, nil)
    73  	i, err = defaultGateAccessor.Get([]byte("testC"))
    74  	assert.Empty(i)
    75  	assert.Error(err)
    76  
    77  	gate.AssertExpectations(t)
    78  	accessor.AssertExpectations(t)
    79  }