go.temporal.io/server@v1.23.0/common/archiver/metadata_mock.go (about)

     1  // The MIT License
     2  //
     3  // Copyright (c) 2020 Temporal Technologies Inc.  All rights reserved.
     4  //
     5  // Copyright (c) 2020 Uber Technologies, Inc.
     6  //
     7  // Permission is hereby granted, free of charge, to any person obtaining a copy
     8  // of this software and associated documentation files (the "Software"), to deal
     9  // in the Software without restriction, including without limitation the rights
    10  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    11  // copies of the Software, and to permit persons to whom the Software is
    12  // furnished to do so, subject to the following conditions:
    13  //
    14  // The above copyright notice and this permission notice shall be included in
    15  // all copies or substantial portions of the Software.
    16  //
    17  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    18  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    19  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    20  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    21  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    22  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    23  // THE SOFTWARE.
    24  
    25  package archiver
    26  
    27  import (
    28  	"github.com/golang/mock/gomock"
    29  )
    30  
    31  // MetadataMock is an implementation of ArchivalMetadata that can be used for testing.
    32  // It can be used as a mock, but it also provides default values, which is something that can't be done with
    33  // *MockArchivalMetadata. This cuts down on the amount of boilerplate code needed to write tests.
    34  type MetadataMock interface {
    35  	ArchivalMetadata
    36  	// EXPECT returns a MetadataMockRecorder which can be used to set expectations on the mock.
    37  	EXPECT() MetadataMockRecorder
    38  	// SetHistoryEnabledByDefault sets the default history archival config to be enabled.
    39  	SetHistoryEnabledByDefault()
    40  	// SetVisibilityEnabledByDefault sets the default visibility archival config to be enabled.
    41  	SetVisibilityEnabledByDefault()
    42  }
    43  
    44  // NewMetadataMock returns a new MetadataMock which uses the provided controller to create a MockArchivalMetadata
    45  // instance.
    46  func NewMetadataMock(controller *gomock.Controller) MetadataMock {
    47  	m := &metadataMock{
    48  		MockArchivalMetadata:    NewMockArchivalMetadata(controller),
    49  		defaultHistoryConfig:    NewDisabledArchvialConfig(),
    50  		defaultVisibilityConfig: NewDisabledArchvialConfig(),
    51  	}
    52  	return m
    53  }
    54  
    55  // MetadataMockRecorder is a wrapper around a ArchivalMetadata mock recorder.
    56  // It is used to determine whether any calls to EXPECT().GetHistoryConfig() or EXPECT().GetVisibilityConfig() were made.
    57  // A call to EXPECT().GetSomeConfig() causes that default config to no longer be used.
    58  type MetadataMockRecorder interface {
    59  	GetHistoryConfig() *gomock.Call
    60  	GetVisibilityConfig() *gomock.Call
    61  }
    62  
    63  type metadataMock struct {
    64  	*MockArchivalMetadata
    65  	defaultHistoryConfig    ArchivalConfig
    66  	defaultVisibilityConfig ArchivalConfig
    67  	historyOverwritten      bool
    68  	visibilityOverwritten   bool
    69  }
    70  
    71  func (m *metadataMock) SetHistoryEnabledByDefault() {
    72  	m.defaultHistoryConfig = NewEnabledArchivalConfig()
    73  }
    74  
    75  func (m *metadataMock) SetVisibilityEnabledByDefault() {
    76  	m.defaultVisibilityConfig = NewEnabledArchivalConfig()
    77  }
    78  
    79  func (m *metadataMock) GetHistoryConfig() ArchivalConfig {
    80  	if !m.historyOverwritten {
    81  		return m.defaultHistoryConfig
    82  	}
    83  	return m.MockArchivalMetadata.GetHistoryConfig()
    84  }
    85  
    86  func (m *metadataMock) GetVisibilityConfig() ArchivalConfig {
    87  	if !m.visibilityOverwritten {
    88  		return m.defaultVisibilityConfig
    89  	}
    90  	return m.MockArchivalMetadata.GetVisibilityConfig()
    91  }
    92  
    93  func (m *metadataMock) EXPECT() MetadataMockRecorder {
    94  	return metadataMockRecorder{m}
    95  }
    96  
    97  type metadataMockRecorder struct {
    98  	*metadataMock
    99  }
   100  
   101  func (r metadataMockRecorder) GetHistoryConfig() *gomock.Call {
   102  	r.metadataMock.historyOverwritten = true
   103  	return r.MockArchivalMetadata.EXPECT().GetHistoryConfig()
   104  }
   105  
   106  func (r metadataMockRecorder) GetVisibilityConfig() *gomock.Call {
   107  	r.metadataMock.visibilityOverwritten = true
   108  	return r.MockArchivalMetadata.EXPECT().GetVisibilityConfig()
   109  }