github.com/yacovm/fabric@v2.0.0-alpha.0.20191128145320-c5d4087dc723+incompatible/common/mocks/peer/mockpeerccsupport.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 peer 18 19 import ( 20 "fmt" 21 22 pb "github.com/hyperledger/fabric-protos-go/peer" 23 ) 24 25 //MockPeerCCSupport provides CC support for peer interfaces. 26 type MockPeerCCSupport struct { 27 ccStream map[string]*MockCCComm 28 } 29 30 //NewMockPeerSupport getsa mock peer support 31 func NewMockPeerSupport() *MockPeerCCSupport { 32 return &MockPeerCCSupport{ccStream: make(map[string]*MockCCComm)} 33 } 34 35 //AddCC adds a cc to the MockPeerCCSupport 36 func (mp *MockPeerCCSupport) AddCC(name string, recv chan *pb.ChaincodeMessage, send chan *pb.ChaincodeMessage) (*MockCCComm, error) { 37 if mp.ccStream[name] != nil { 38 return nil, fmt.Errorf("CC %s already added", name) 39 } 40 mcc := &MockCCComm{name: name, recvStream: recv, sendStream: send} 41 mp.ccStream[name] = mcc 42 return mcc, nil 43 } 44 45 //GetCC gets a cc from the MockPeerCCSupport 46 func (mp *MockPeerCCSupport) GetCC(name string) (*MockCCComm, error) { 47 s := mp.ccStream[name] 48 if s == nil { 49 return nil, fmt.Errorf("CC %s not added", name) 50 } 51 return s, nil 52 } 53 54 //GetCCMirror creates a MockCCStream with streans switched 55 func (mp *MockPeerCCSupport) GetCCMirror(name string) *MockCCComm { 56 s := mp.ccStream[name] 57 if s == nil { 58 return nil 59 } 60 61 return &MockCCComm{name: name, recvStream: s.sendStream, sendStream: s.recvStream, skipClose: true} 62 } 63 64 //RemoveCC removes a cc 65 func (mp *MockPeerCCSupport) RemoveCC(name string) error { 66 if mp.ccStream[name] == nil { 67 return fmt.Errorf("CC %s not added", name) 68 } 69 delete(mp.ccStream, name) 70 return nil 71 } 72 73 //RemoveAll removes all ccs 74 func (mp *MockPeerCCSupport) RemoveAll() error { 75 mp.ccStream = make(map[string]*MockCCComm) 76 return nil 77 }