github.com/yacovm/fabric@v2.0.0-alpha.0.20191128145320-c5d4087dc723+incompatible/common/policies/inquire/merge_test.go (about) 1 /* 2 Copyright IBM Corp. All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 package inquire 8 9 import ( 10 "testing" 11 12 "github.com/stretchr/testify/assert" 13 ) 14 15 var ( 16 member1 = NewComparablePrincipal(member("Org1MSP")) 17 member2 = NewComparablePrincipal(member("Org2MSP")) 18 member3 = NewComparablePrincipal(member("Org3MSP")) 19 member4 = NewComparablePrincipal(member("Org4MSP")) 20 member5 = NewComparablePrincipal(member("Org5MSP")) 21 peer1 = NewComparablePrincipal(peer("Org1MSP")) 22 peer2 = NewComparablePrincipal(peer("Org2MSP")) 23 peer3 = NewComparablePrincipal(peer("Org3MSP")) 24 peer4 = NewComparablePrincipal(peer("Org4MSP")) 25 peer5 = NewComparablePrincipal(peer("Org5MSP")) 26 ) 27 28 func TestString(t *testing.T) { 29 cps := ComparablePrincipalSet{member1, member2, NewComparablePrincipal(ou("Org3MSP"))} 30 assert.Equal(t, "[Org1MSP.MEMBER, Org2MSP.MEMBER, Org3MSP.ou]", cps.String()) 31 } 32 33 func TestClone(t *testing.T) { 34 cps := ComparablePrincipalSet{member1, member2} 35 clone := cps.Clone() 36 assert.Equal(t, cps, clone) 37 // Nil out the first entry and ensure that it isn't reflected in the clone 38 cps[0] = nil 39 assert.False(t, cps[0] == clone[0]) 40 } 41 42 func TestMergeInclusiveWithPlurality(t *testing.T) { 43 // Scenario: 44 // S1 = {Org1.member, Org2.member, Org2.member}, {Org3.peer, Org4.peer} 45 // S2 = {Org1.peer, Org2.peer}, {Org3.member, Org4.member} 46 // Expected merge result: 47 // {Org1.peer, Org2.peer, Org2.member}, {Org3.peer, Org4.peer} 48 49 members12 := ComparablePrincipalSet{member1, member2, member2} 50 members34 := ComparablePrincipalSet{member3, member4} 51 peers12 := ComparablePrincipalSet{peer1, peer2} 52 peers34 := ComparablePrincipalSet{peer3, peer4} 53 54 peers12member2 := ComparablePrincipalSet{peer1, peer2, member2} 55 56 s1 := ComparablePrincipalSets{members12, peers34} 57 s2 := ComparablePrincipalSets{peers12, members34} 58 59 merged := Merge(s1, s2) 60 expected := ComparablePrincipalSets{peers12member2, peers34} 61 assert.Equal(t, expected, merged) 62 63 // Shuffle the order of principal sets and ensure the result is the same 64 s1 = ComparablePrincipalSets{peers34, members12} 65 s2 = ComparablePrincipalSets{members34, peers12} 66 merged = Merge(s1, s2) 67 assert.Equal(t, expected, merged) 68 69 // Shuffle the order to the call 70 merged = Merge(s2, s1) 71 expected = ComparablePrincipalSets{peers34, peers12member2} 72 assert.Equal(t, expected, merged) 73 } 74 75 func TestMergeExclusiveWithPlurality(t *testing.T) { 76 // Scenario: 77 // S1 = {Org1.member, Org2.member, Org2.member}, {Org3.peer, Org4.peer} 78 // S2 = {Org2.member, Org3.member}, {Org4.peer, Org5.peer} 79 // Expected merge result: 80 // {Org1.member, Org2.member, Org3.member}, {Org3.peer, Org4.peer, Org5.peer}, 81 // {Org1.member, Org2.member, Org4.peer, Org5.peer}, {Org3.peer, Org4.peer, Org2.member, Org3.member} 82 83 members122 := ComparablePrincipalSet{member1, member2, member2} 84 members23 := ComparablePrincipalSet{member2, member3} 85 peers34 := ComparablePrincipalSet{peer3, peer4} 86 peers45 := ComparablePrincipalSet{peer4, peer5} 87 members1223 := ComparablePrincipalSet{member1, member2, member2, member3} 88 peers345 := ComparablePrincipalSet{peer3, peer4, peer5} 89 members122peers45 := ComparablePrincipalSet{member1, member2, member2, peer4, peer5} 90 peers34members23 := ComparablePrincipalSet{peer3, peer4, member2, member3} 91 92 s1 := ComparablePrincipalSets{members122, peers34} 93 s2 := ComparablePrincipalSets{members23, peers45} 94 merged := Merge(s1, s2) 95 expected := ComparablePrincipalSets{members1223, peers345, members122peers45, peers34members23} 96 assert.True(t, expected.IsEqual(merged)) 97 } 98 99 func TestMergePartialExclusiveWithPlurality(t *testing.T) { 100 // Scenario: 101 // S1 = {Org1.member, Org2.member}, {Org3.peer, Org4.peer, Org5.member} 102 // S2 = {Org2.member, Org3.member}, {Org3.peer, Org4.member, Org4.member}, {Org4.peer, Org5.member} 103 // The naive solution would be: 104 // {Org1.member, Org2.member, Org3.member}, {Org3.peer, Org4.peer, Org4.member, Org5.member}, {Org3.peer, Org4.peer, Org5.member} 105 // However - {Org3.peer, Org4.peer, Org4.member, Org5.member} contains {Org3.peer, Org4.peer, Org5.member} 106 // So the result should be optimized, hence {Org3.peer, Org4.peer, Org4.member, Org5.member} will be omitted. 107 // Expected merge result: 108 // {Org1.member, Org2.member, Org3.member}, {Org3.peer, Org4.peer, Org5.member} 109 110 members12 := ComparablePrincipalSet{member1, member2} 111 members23 := ComparablePrincipalSet{member2, member3} 112 peers34member5 := ComparablePrincipalSet{peer3, peer4, member5} 113 peer3members44 := ComparablePrincipalSet{peer3, member4, member4} 114 peer4member5 := ComparablePrincipalSet{peer4, member5} 115 members123 := ComparablePrincipalSet{member1, member2, member3} 116 117 s1 := ComparablePrincipalSets{members12, peers34member5} 118 s2 := ComparablePrincipalSets{members23, peer3members44, peer4member5} 119 merged := Merge(s1, s2) 120 expected := ComparablePrincipalSets{members123, peers34member5} 121 assert.True(t, expected.IsEqual(merged)) 122 } 123 124 func TestMergeWithPlurality(t *testing.T) { 125 pair := comparablePrincipalSetPair{ 126 contained: ComparablePrincipalSet{peer3, member4, member4}, 127 containing: ComparablePrincipalSet{peer3, peer4, member5}, 128 } 129 merged := pair.MergeWithPlurality() 130 expected := ComparablePrincipalSet{peer3, peer4, member5, member4} 131 assert.Equal(t, expected, merged) 132 } 133 134 func TestMergeSubsetPrincipalSets(t *testing.T) { 135 member1And2 := ComparablePrincipalSets{ComparablePrincipalSet{member1, member2}} 136 member1Or2 := ComparablePrincipalSets{ComparablePrincipalSet{member1}, ComparablePrincipalSet{member2}} 137 merged := Merge(member1And2, member1Or2) 138 expected := member1And2 139 assert.True(t, expected.IsEqual(merged)) 140 } 141 142 func TestIsSubset(t *testing.T) { 143 members12 := ComparablePrincipalSet{member1, member2, member2} 144 members321 := ComparablePrincipalSet{member3, member2, member1} 145 members13 := ComparablePrincipalSet{member1, member3} 146 assert.True(t, members12.IsSubset(members12)) 147 assert.True(t, members12.IsSubset(members321)) 148 assert.False(t, members12.IsSubset(members13)) 149 } 150 151 func TestReduce(t *testing.T) { 152 members12 := ComparablePrincipalSet{member1, member2} 153 members123 := ComparablePrincipalSet{member1, member2, member3} 154 members12peers45 := ComparablePrincipalSet{member1, member2, peer4, peer5} 155 peers45 := ComparablePrincipalSet{peer4, peer5} 156 peers34 := ComparablePrincipalSet{peer3, peer4} 157 s := ComparablePrincipalSets{members12, peers34, members123, members123, members12peers45, peers45} 158 expected := ComparablePrincipalSets{members12, peers34, peers45} 159 assert.Equal(t, expected, s.Reduce()) 160 } 161 162 // IsEqual returns whether this ComparablePrincipalSets contains the elements of the given ComparablePrincipalSets 163 // in some order 164 func (cps ComparablePrincipalSets) IsEqual(sets ComparablePrincipalSets) bool { 165 return cps.IsSubset(sets) && sets.IsSubset(cps) 166 } 167 168 // IsSubset returns whether this ComparablePrincipalSets is a subset of the given ComparablePrincipalSets 169 func (cps ComparablePrincipalSets) IsSubset(sets ComparablePrincipalSets) bool { 170 for _, sets1 := range cps { 171 var found bool 172 for _, sets2 := range sets { 173 if sets1.IsEqual(sets2) { 174 found = true 175 } 176 } 177 if !found { 178 return false 179 } 180 } 181 return true 182 } 183 184 // IsEqual returns whether this ComparablePrincipalSet contains the elements of the given ComparablePrincipalSet 185 // in some order 186 func (cps ComparablePrincipalSet) IsEqual(otherSet ComparablePrincipalSet) bool { 187 return cps.IsSubset(otherSet) && otherSet.IsSubset(cps) 188 }