github.com/adnan-c/fabric_e2e_couchdb@v0.6.1-preview.0.20170228180935-21ce6b23cf91/common/mocks/configtx/configtx.go (about)

     1  /*
     2  Copyright IBM Corp. 2017 All Rights Reserved.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8                   http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package configtx
    18  
    19  import (
    20  	configvaluesapi "github.com/hyperledger/fabric/common/configvalues"
    21  	configvalueschannel "github.com/hyperledger/fabric/common/configvalues/channel"
    22  	mockpolicies "github.com/hyperledger/fabric/common/mocks/policies"
    23  	"github.com/hyperledger/fabric/common/policies"
    24  	"github.com/hyperledger/fabric/msp"
    25  	cb "github.com/hyperledger/fabric/protos/common"
    26  )
    27  
    28  type Resources struct {
    29  	// PolicyManagerVal is returned as the result of PolicyManager()
    30  	PolicyManagerVal *mockpolicies.Manager
    31  
    32  	// ChannelConfigVal is returned as the result of ChannelConfig()
    33  	ChannelConfigVal configvalueschannel.ConfigReader
    34  
    35  	// OrdererConfigVal is returned as the result of OrdererConfig()
    36  	OrdererConfigVal configvaluesapi.Orderer
    37  
    38  	// ApplicationConfigVal is returned as the result of ApplicationConfig()
    39  	ApplicationConfigVal configvaluesapi.Application
    40  
    41  	// MSPManagerVal is returned as the result of MSPManager()
    42  	MSPManagerVal msp.MSPManager
    43  }
    44  
    45  // Returns the PolicyManagerVal
    46  func (r *Resources) PolicyManager() policies.Manager {
    47  	return r.PolicyManagerVal
    48  }
    49  
    50  // Returns the ChannelConfigVal
    51  func (r *Resources) ChannelConfig() configvalueschannel.ConfigReader {
    52  	return r.ChannelConfigVal
    53  }
    54  
    55  // Returns the OrdererConfigVal
    56  func (r *Resources) OrdererConfig() configvaluesapi.Orderer {
    57  	return r.OrdererConfigVal
    58  }
    59  
    60  // Returns the ApplicationConfigVal
    61  func (r *Resources) ApplicationConfig() configvaluesapi.Application {
    62  	return r.ApplicationConfigVal
    63  }
    64  
    65  // Returns the MSPManagerVal
    66  func (r *Resources) MSPManager() msp.MSPManager {
    67  	return r.MSPManagerVal
    68  }
    69  
    70  // Transactional implements the configtxapi.Transactional
    71  type Transactional struct{}
    72  
    73  // PreCommit returns nil
    74  func (t *Transactional) PreCommit() error { return nil }
    75  
    76  // CommitConfig does nothing
    77  func (t *Transactional) CommitProposals() {}
    78  
    79  // RollbackConfig does nothing
    80  func (t *Transactional) RollbackProposals() {}
    81  
    82  // Initializer mocks the configtxapi.Initializer interface
    83  type Initializer struct {
    84  	Resources
    85  
    86  	// PolicyProposerVal is returned by PolicyProposers
    87  	PolicyProposerVal *PolicyProposer
    88  
    89  	// ValueProposerVal is returned by ValueProposers
    90  	ValueProposerVal *ValueProposer
    91  }
    92  
    93  // PolicyProposers returns PolicyProposerVal
    94  func (i *Initializer) PolicyProposer() policies.Proposer {
    95  	return i.PolicyProposerVal
    96  }
    97  
    98  // ValueProposers returns ValueProposerVal
    99  func (i *Initializer) ValueProposer() configvaluesapi.ValueProposer {
   100  	return i.ValueProposerVal
   101  }
   102  
   103  // PolicyProposer mocks the policies.Proposer interface
   104  type PolicyProposer struct {
   105  	Transactional
   106  	LastKey               string
   107  	LastPolicy            *cb.ConfigPolicy
   108  	ErrorForProposePolicy error
   109  }
   110  
   111  // ProposeConfig sets LastKey to key, LastPath to path, and LastPolicy to configPolicy, returning ErrorForProposedConfig
   112  func (pp *PolicyProposer) ProposePolicy(key string, configPolicy *cb.ConfigPolicy) error {
   113  	pp.LastKey = key
   114  	pp.LastPolicy = configPolicy
   115  	return pp.ErrorForProposePolicy
   116  }
   117  
   118  // BeginConfig will be removed in the future
   119  func (pp *PolicyProposer) BeginPolicyProposals(groups []string) ([]policies.Proposer, error) {
   120  	handlers := make([]policies.Proposer, len(groups))
   121  	for i := range handlers {
   122  		handlers[i] = pp
   123  	}
   124  	return handlers, nil
   125  }
   126  
   127  // Handler mocks the configtxapi.Handler interface
   128  type ValueProposer struct {
   129  	Transactional
   130  	LastKey               string
   131  	LastValue             *cb.ConfigValue
   132  	ErrorForProposeConfig error
   133  }
   134  
   135  // ProposeConfig sets LastKey to key, and LastValue to configValue, returning ErrorForProposedConfig
   136  func (vp *ValueProposer) ProposeValue(key string, configValue *cb.ConfigValue) error {
   137  	vp.LastKey = key
   138  	vp.LastValue = configValue
   139  	return vp.ErrorForProposeConfig
   140  }
   141  
   142  // BeginConfig returns slices populated by self
   143  func (vp *ValueProposer) BeginValueProposals(groups []string) ([]configvaluesapi.ValueProposer, error) {
   144  	handlers := make([]configvaluesapi.ValueProposer, len(groups))
   145  	for i := range handlers {
   146  		handlers[i] = vp
   147  	}
   148  	return handlers, nil
   149  }
   150  
   151  // Manager is a mock implementation of configtxapi.Manager
   152  type Manager struct {
   153  	Initializer
   154  
   155  	// ChainIDVal is returned as the result of ChainID()
   156  	ChainIDVal string
   157  
   158  	// SequenceVal is returned as the result of Sequence()
   159  	SequenceVal uint64
   160  
   161  	// ApplyVal is returned by Apply
   162  	ApplyVal error
   163  
   164  	// AppliedConfigUpdateEnvelope is set by Apply
   165  	AppliedConfigUpdateEnvelope *cb.ConfigEnvelope
   166  
   167  	// ValidateVal is returned by Validate
   168  	ValidateVal error
   169  
   170  	// ProposeConfigUpdateError is returned as the error value for ProposeConfigUpdate
   171  	ProposeConfigUpdateError error
   172  
   173  	// ProposeConfigUpdateVal is returns as the value for ProposeConfigUpdate
   174  	ProposeConfigUpdateVal *cb.ConfigEnvelope
   175  }
   176  
   177  // ConfigEnvelope is currently unimplemented
   178  func (cm *Manager) ConfigEnvelope() *cb.ConfigEnvelope {
   179  	panic("Unimplemented")
   180  }
   181  
   182  // ConsensusType returns the ConsensusTypeVal
   183  func (cm *Manager) ChainID() string {
   184  	return cm.ChainIDVal
   185  }
   186  
   187  // BatchSize returns the BatchSizeVal
   188  func (cm *Manager) Sequence() uint64 {
   189  	return cm.SequenceVal
   190  }
   191  
   192  // ProposeConfigUpdate
   193  func (cm *Manager) ProposeConfigUpdate(update *cb.Envelope) (*cb.ConfigEnvelope, error) {
   194  	return cm.ProposeConfigUpdateVal, cm.ProposeConfigUpdateError
   195  }
   196  
   197  // Apply returns ApplyVal
   198  func (cm *Manager) Apply(configEnv *cb.ConfigEnvelope) error {
   199  	cm.AppliedConfigUpdateEnvelope = configEnv
   200  	return cm.ApplyVal
   201  }
   202  
   203  // Validate returns ValidateVal
   204  func (cm *Manager) Validate(configEnv *cb.ConfigEnvelope) error {
   205  	return cm.ValidateVal
   206  }