github.com/tenywen/fabric@v1.0.0-beta.0.20170620030522-a5b1ed380643/common/mocks/policies/policies.go (about)

     1  /*
     2  Copyright IBM Corp. 2016 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 policies
    18  
    19  import (
    20  	"github.com/hyperledger/fabric/common/policies"
    21  	cb "github.com/hyperledger/fabric/protos/common"
    22  )
    23  
    24  // Policy is a mock implementation of the policies.Policy interface
    25  type Policy struct {
    26  	// Err is the error returned by Evaluate
    27  	Err error
    28  }
    29  
    30  // Evaluate returns the Err set in Policy
    31  func (p *Policy) Evaluate(signatureSet []*cb.SignedData) error {
    32  	return p.Err
    33  }
    34  
    35  // Manager is a mock implementation of the policies.Manager interface
    36  type Manager struct {
    37  	// Policy is returned as the output to GetPolicy if a Policy
    38  	// for id is not in PolicyMap
    39  	Policy *Policy
    40  
    41  	// BasePathVal is returned as the result of BasePath
    42  	BasePathVal string
    43  
    44  	// PolicyMap is returned is used to look up Policies in
    45  	PolicyMap map[string]policies.Policy
    46  
    47  	// SubManagers is used for the return value of Manager
    48  	SubManagersMap map[string]*Manager
    49  }
    50  
    51  // PolicyNames panics
    52  func (m *Manager) PolicyNames() []string {
    53  	panic("Unimplimented")
    54  }
    55  
    56  // BasePath returns BasePathVal
    57  func (m *Manager) BasePath() string {
    58  	return m.BasePathVal
    59  }
    60  
    61  // Manager returns the Manager from SubManagers for the last component of the path
    62  func (m *Manager) Manager(path []string) (policies.Manager, bool) {
    63  	if len(path) == 0 {
    64  		return m, true
    65  	}
    66  	manager, ok := m.SubManagersMap[path[len(path)-1]]
    67  	return manager, ok
    68  }
    69  
    70  // GetPolicy returns the value of Manager.Policy and whether it was nil or not
    71  func (m *Manager) GetPolicy(id string) (policies.Policy, bool) {
    72  	if m.PolicyMap != nil {
    73  		policy, ok := m.PolicyMap[id]
    74  		if ok {
    75  			return policy, true
    76  		}
    77  	}
    78  	return m.Policy, m.Policy != nil
    79  }