github.com/kaituanwang/hyperledger@v2.0.1+incompatible/core/handlers/decoration/decoration_test.go (about) 1 /* 2 Copyright SecureKey Technologies Inc. All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 package decoration 8 9 import ( 10 "encoding/binary" 11 "testing" 12 13 "github.com/hyperledger/fabric-protos-go/peer" 14 "github.com/stretchr/testify/assert" 15 ) 16 17 const ( 18 decorationKey = "sequence" 19 ) 20 21 func TestApplyDecorations(t *testing.T) { 22 iterations := 15 23 decorators := createNDecorators(iterations) 24 initialInput := &peer.ChaincodeInput{Decorations: make(map[string][]byte)} 25 seq := make([]byte, 4) 26 binary.BigEndian.PutUint32(seq, 0) 27 initialInput.Decorations[decorationKey] = seq 28 29 finalInput := Apply(nil, initialInput, decorators...) 30 for i := 0; i < iterations; i++ { 31 assert.Equal(t, uint32(i), decorators[i].(*mockDecorator).sequence, 32 "Expected decorators to be applied in the provided sequence") 33 } 34 35 assert.Equal(t, uint32(iterations), binary.BigEndian.Uint32(finalInput.Decorations[decorationKey]), 36 "Expected decorators to be applied in the provided sequence") 37 } 38 39 func createNDecorators(n int) []Decorator { 40 decorators := make([]Decorator, n) 41 for i := 0; i < n; i++ { 42 decorators[i] = &mockDecorator{} 43 } 44 return decorators 45 } 46 47 type mockDecorator struct { 48 sequence uint32 49 } 50 51 func (d *mockDecorator) Decorate(proposal *peer.Proposal, 52 input *peer.ChaincodeInput) *peer.ChaincodeInput { 53 54 d.sequence = binary.BigEndian.Uint32(input.Decorations[decorationKey]) 55 binary.BigEndian.PutUint32(input.Decorations[decorationKey], d.sequence+1) 56 57 return input 58 }