github.com/xiaqingdoc/fabric@v2.1.1+incompatible/gossip/api/api_test.go (about)

     1  /*
     2  Copyright IBM Corp. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package api
     8  
     9  import (
    10  	"bytes"
    11  	"testing"
    12  
    13  	"github.com/hyperledger/fabric/gossip/common"
    14  	"github.com/stretchr/testify/assert"
    15  )
    16  
    17  func TestPeerIdentitySetByOrg(t *testing.T) {
    18  	p1 := PeerIdentityInfo{
    19  		Organization: OrgIdentityType("ORG1"),
    20  		Identity:     PeerIdentityType("Peer1"),
    21  	}
    22  	p2 := PeerIdentityInfo{
    23  		Organization: OrgIdentityType("ORG2"),
    24  		Identity:     PeerIdentityType("Peer2"),
    25  	}
    26  	is := PeerIdentitySet{
    27  		p1, p2,
    28  	}
    29  	m := is.ByOrg()
    30  	assert.Len(t, m, 2)
    31  	assert.Equal(t, PeerIdentitySet{p1}, m["ORG1"])
    32  	assert.Equal(t, PeerIdentitySet{p2}, m["ORG2"])
    33  }
    34  
    35  func TestPeerIdentitySetByID(t *testing.T) {
    36  	p1 := PeerIdentityInfo{
    37  		Organization: OrgIdentityType("ORG1"),
    38  		PKIId:        common.PKIidType("p1"),
    39  	}
    40  	p2 := PeerIdentityInfo{
    41  		Organization: OrgIdentityType("ORG2"),
    42  		PKIId:        common.PKIidType("p2"),
    43  	}
    44  	is := PeerIdentitySet{
    45  		p1, p2,
    46  	}
    47  	assert.Equal(t, map[string]PeerIdentityInfo{
    48  		"p1": p1,
    49  		"p2": p2,
    50  	}, is.ByID())
    51  }
    52  
    53  func TestPeerIdentitySetFilter(t *testing.T) {
    54  	p1 := PeerIdentityInfo{
    55  		Organization: OrgIdentityType("ORG1"),
    56  		PKIId:        common.PKIidType("p1"),
    57  	}
    58  	p2 := PeerIdentityInfo{
    59  		Organization: OrgIdentityType("ORG2"),
    60  		PKIId:        common.PKIidType("p2"),
    61  	}
    62  	p3 := PeerIdentityInfo{
    63  		Organization: OrgIdentityType("ORG2"),
    64  		PKIId:        common.PKIidType("p3"),
    65  	}
    66  	is := PeerIdentitySet{
    67  		p1, p2, p3,
    68  	}
    69  	assert.Equal(t, PeerIdentitySet{p1}, is.Filter(func(info PeerIdentityInfo) bool {
    70  		return bytes.Equal(info.Organization, OrgIdentityType("ORG1"))
    71  	}))
    72  	var emptySet PeerIdentitySet
    73  	assert.Equal(t, emptySet, is.Filter(func(_ PeerIdentityInfo) bool {
    74  		return false
    75  	}))
    76  	assert.Equal(t, PeerIdentitySet{p3}, is.Filter(func(info PeerIdentityInfo) bool {
    77  		return bytes.Equal(info.Organization, OrgIdentityType("ORG2"))
    78  	}).Filter(func(info PeerIdentityInfo) bool {
    79  		return bytes.Equal(info.PKIId, common.PKIidType("p3"))
    80  	}))
    81  }