github.com/hyperledger-labs/bdls@v2.1.1+incompatible/core/chaincode/mock/mockpeerccsupport.go (about)

     1  /*
     2  Copyright IBM Corp. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package mock
     8  
     9  import (
    10  	"fmt"
    11  
    12  	pb "github.com/hyperledger/fabric-protos-go/peer"
    13  )
    14  
    15  //MockPeerCCSupport provides CC support for peer interfaces.
    16  type MockPeerCCSupport struct {
    17  	ccStream map[string]*MockCCComm
    18  }
    19  
    20  //NewMockPeerSupport getsa mock peer support
    21  func NewMockPeerSupport() *MockPeerCCSupport {
    22  	return &MockPeerCCSupport{ccStream: make(map[string]*MockCCComm)}
    23  }
    24  
    25  //AddCC adds a cc to the MockPeerCCSupport
    26  func (mp *MockPeerCCSupport) AddCC(name string, recv chan *pb.ChaincodeMessage, send chan *pb.ChaincodeMessage) (*MockCCComm, error) {
    27  	if mp.ccStream[name] != nil {
    28  		return nil, fmt.Errorf("CC %s already added", name)
    29  	}
    30  	mcc := &MockCCComm{name: name, recvStream: recv, sendStream: send}
    31  	mp.ccStream[name] = mcc
    32  	return mcc, nil
    33  }
    34  
    35  //GetCC gets a cc from the MockPeerCCSupport
    36  func (mp *MockPeerCCSupport) GetCC(name string) (*MockCCComm, error) {
    37  	s := mp.ccStream[name]
    38  	if s == nil {
    39  		return nil, fmt.Errorf("CC %s not added", name)
    40  	}
    41  	return s, nil
    42  }
    43  
    44  //GetCCMirror creates a MockCCStream with streans switched
    45  func (mp *MockPeerCCSupport) GetCCMirror(name string) *MockCCComm {
    46  	s := mp.ccStream[name]
    47  	if s == nil {
    48  		return nil
    49  	}
    50  
    51  	return &MockCCComm{name: name, recvStream: s.sendStream, sendStream: s.recvStream, skipClose: true}
    52  }
    53  
    54  //RemoveCC removes a cc
    55  func (mp *MockPeerCCSupport) RemoveCC(name string) error {
    56  	if mp.ccStream[name] == nil {
    57  		return fmt.Errorf("CC %s not added", name)
    58  	}
    59  	delete(mp.ccStream, name)
    60  	return nil
    61  }
    62  
    63  //RemoveAll removes all ccs
    64  func (mp *MockPeerCCSupport) RemoveAll() error {
    65  	mp.ccStream = make(map[string]*MockCCComm)
    66  	return nil
    67  }