github.com/ewagmig/fabric@v2.1.1+incompatible/common/channelconfig/standardvalues_test.go (about)

     1  /*
     2  Copyright IBM Corp. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package channelconfig
     8  
     9  import (
    10  	"testing"
    11  
    12  	cb "github.com/hyperledger/fabric-protos-go/common"
    13  	"github.com/hyperledger/fabric/protoutil"
    14  	"github.com/stretchr/testify/assert"
    15  )
    16  
    17  type foo struct {
    18  	Msg1 *cb.Envelope
    19  	Msg2 *cb.Payload
    20  }
    21  
    22  type bar struct {
    23  	Msg3 *cb.Header
    24  }
    25  
    26  type conflict struct {
    27  	Msg1 *cb.Header
    28  }
    29  
    30  type nonProtos struct {
    31  	Msg   *cb.Envelope
    32  	Wrong string
    33  }
    34  
    35  type unexported struct {
    36  	msg *cb.Envelope
    37  }
    38  
    39  func TestSingle(t *testing.T) {
    40  	fooVal := &foo{}
    41  	sv, err := NewStandardValues(fooVal)
    42  	assert.NoError(t, err, "Valid non-nested structure provided")
    43  	assert.NotNil(t, fooVal.Msg1, "Should have initialized Msg1")
    44  	assert.NotNil(t, fooVal.Msg2, "Should have initialized Msg2")
    45  
    46  	msg1, err := sv.Deserialize("Msg1", protoutil.MarshalOrPanic(&cb.Envelope{}))
    47  	assert.NoError(t, err, "Should have found map entry")
    48  	assert.Equal(t, msg1, fooVal.Msg1, "Should be same entry")
    49  
    50  	msg2, err := sv.Deserialize("Msg2", protoutil.MarshalOrPanic(&cb.Payload{}))
    51  	assert.NoError(t, err, "Should have found map entry")
    52  	assert.Equal(t, msg2, fooVal.Msg2, "Should be same entry")
    53  }
    54  
    55  func TestPair(t *testing.T) {
    56  	fooVal := &foo{}
    57  	barVal := &bar{}
    58  	sv, err := NewStandardValues(fooVal, barVal)
    59  	assert.NoError(t, err, "Valid non-nested structure provided")
    60  	assert.NotNil(t, fooVal.Msg1, "Should have initialized Msg1")
    61  	assert.NotNil(t, fooVal.Msg2, "Should have initialized Msg2")
    62  	assert.NotNil(t, barVal.Msg3, "Should have initialized Msg3")
    63  
    64  	msg1, err := sv.Deserialize("Msg1", protoutil.MarshalOrPanic(&cb.Envelope{}))
    65  	assert.NoError(t, err, "Should have found map entry")
    66  	assert.Equal(t, msg1, fooVal.Msg1, "Should be same entry")
    67  
    68  	msg2, err := sv.Deserialize("Msg2", protoutil.MarshalOrPanic(&cb.Payload{}))
    69  	assert.NoError(t, err, "Should have found map entry")
    70  	assert.Equal(t, msg2, fooVal.Msg2, "Should be same entry")
    71  
    72  	msg3, err := sv.Deserialize("Msg3", protoutil.MarshalOrPanic(&cb.Header{}))
    73  	assert.NoError(t, err, "Should have found map entry")
    74  	assert.Equal(t, msg3, barVal.Msg3, "Should be same entry")
    75  }
    76  
    77  func TestPairConflict(t *testing.T) {
    78  	_, err := NewStandardValues(&foo{}, &conflict{})
    79  	assert.Error(t, err, "Conflicting keys provided")
    80  }
    81  
    82  func TestNonProtosStruct(t *testing.T) {
    83  	_, err := NewStandardValues(&nonProtos{})
    84  	assert.Error(t, err, "Structure with non-struct non-proto fields provided")
    85  }
    86  
    87  func TestUnexportedField(t *testing.T) {
    88  	_, err := NewStandardValues(&unexported{msg: nil})
    89  	assert.Error(t, err, "Structure with unexported fields")
    90  }
    91  
    92  func TestNonPointerParam(t *testing.T) {
    93  	_, err := NewStandardValues(foo{})
    94  	assert.Error(t, err, "Parameter must be pointer")
    95  }
    96  
    97  func TestPointerToNonStruct(t *testing.T) {
    98  	nonStruct := "foo"
    99  	_, err := NewStandardValues(&nonStruct)
   100  	assert.Error(t, err, "Pointer must be to a struct")
   101  }