github.com/myafeier/fabric@v1.0.1-0.20170722181825-3a4b1f2bce86/common/config/standardvalues_test.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 config
    18  
    19  import (
    20  	"testing"
    21  
    22  	cb "github.com/hyperledger/fabric/protos/common"
    23  	"github.com/hyperledger/fabric/protos/utils"
    24  
    25  	"github.com/stretchr/testify/assert"
    26  )
    27  
    28  type foo struct {
    29  	Msg1 *cb.Envelope
    30  	Msg2 *cb.Payload
    31  }
    32  
    33  type bar struct {
    34  	Msg3 *cb.Header
    35  }
    36  
    37  type conflict struct {
    38  	Msg1 *cb.Header
    39  }
    40  
    41  type nonProtos struct {
    42  	Msg   *cb.Envelope
    43  	Wrong string
    44  }
    45  
    46  type unexported struct {
    47  	msg *cb.Envelope
    48  }
    49  
    50  func TestSingle(t *testing.T) {
    51  	fooVal := &foo{}
    52  	sv, err := NewStandardValues(fooVal)
    53  	assert.NoError(t, err, "Valid non-nested structure provided")
    54  	assert.NotNil(t, fooVal.Msg1, "Should have initialized Msg1")
    55  	assert.NotNil(t, fooVal.Msg2, "Should have initialized Msg2")
    56  
    57  	msg1, err := sv.Deserialize("Msg1", utils.MarshalOrPanic(&cb.Envelope{}))
    58  	assert.NoError(t, err, "Should have found map entry")
    59  	assert.Equal(t, msg1, fooVal.Msg1, "Should be same entry")
    60  
    61  	msg2, err := sv.Deserialize("Msg2", utils.MarshalOrPanic(&cb.Payload{}))
    62  	assert.NoError(t, err, "Should have found map entry")
    63  	assert.Equal(t, msg2, fooVal.Msg2, "Should be same entry")
    64  }
    65  
    66  func TestPair(t *testing.T) {
    67  	fooVal := &foo{}
    68  	barVal := &bar{}
    69  	sv, err := NewStandardValues(fooVal, barVal)
    70  	assert.NoError(t, err, "Valid non-nested structure provided")
    71  	assert.NotNil(t, fooVal.Msg1, "Should have initialized Msg1")
    72  	assert.NotNil(t, fooVal.Msg2, "Should have initialized Msg2")
    73  	assert.NotNil(t, barVal.Msg3, "Should have initialized Msg3")
    74  
    75  	msg1, err := sv.Deserialize("Msg1", utils.MarshalOrPanic(&cb.Envelope{}))
    76  	assert.NoError(t, err, "Should have found map entry")
    77  	assert.Equal(t, msg1, fooVal.Msg1, "Should be same entry")
    78  
    79  	msg2, err := sv.Deserialize("Msg2", utils.MarshalOrPanic(&cb.Payload{}))
    80  	assert.NoError(t, err, "Should have found map entry")
    81  	assert.Equal(t, msg2, fooVal.Msg2, "Should be same entry")
    82  
    83  	msg3, err := sv.Deserialize("Msg3", utils.MarshalOrPanic(&cb.Header{}))
    84  	assert.NoError(t, err, "Should have found map entry")
    85  	assert.Equal(t, msg3, barVal.Msg3, "Should be same entry")
    86  }
    87  
    88  func TestPairConflict(t *testing.T) {
    89  	_, err := NewStandardValues(&foo{}, &conflict{})
    90  	assert.Error(t, err, "Conflicting keys provided")
    91  }
    92  
    93  func TestNonProtosStruct(t *testing.T) {
    94  	_, err := NewStandardValues(&nonProtos{})
    95  	assert.Error(t, err, "Structure with non-struct non-proto fields provided")
    96  }
    97  
    98  func TestUnexportedField(t *testing.T) {
    99  	_, err := NewStandardValues(&unexported{})
   100  	assert.Error(t, err, "Structure with unexported fields")
   101  }
   102  
   103  func TestNonPointerParam(t *testing.T) {
   104  	_, err := NewStandardValues(foo{})
   105  	assert.Error(t, err, "Parameter must be pointer")
   106  }
   107  
   108  func TestPointerToNonStruct(t *testing.T) {
   109  	nonStruct := "foo"
   110  	_, err := NewStandardValues(&nonStruct)
   111  	assert.Error(t, err, "Pointer must be to a struct")
   112  }