github.com/lzy4123/fabric@v2.1.1+incompatible/common/tools/protolator/nested_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 protolator
    18  
    19  import (
    20  	"bytes"
    21  	"testing"
    22  
    23  	"github.com/hyperledger/fabric/common/tools/protolator/testprotos"
    24  	"github.com/stretchr/testify/assert"
    25  )
    26  
    27  func TestPlainNestedMsg(t *testing.T) {
    28  	fromPrefix := "from"
    29  	toPrefix := "to"
    30  	tppff := &testProtoPlainFieldFactory{
    31  		fromPrefix: fromPrefix,
    32  		toPrefix:   toPrefix,
    33  	}
    34  
    35  	fieldFactories = []protoFieldFactory{tppff}
    36  
    37  	pfValue := "foo"
    38  	startMsg := &testprotos.NestedMsg{
    39  		PlainNestedField: &testprotos.SimpleMsg{
    40  			PlainField: pfValue,
    41  		},
    42  	}
    43  
    44  	var buffer bytes.Buffer
    45  	assert.NoError(t, DeepMarshalJSON(&buffer, startMsg))
    46  	newMsg := &testprotos.NestedMsg{}
    47  	assert.NoError(t, DeepUnmarshalJSON(bytes.NewReader(buffer.Bytes()), newMsg))
    48  	assert.NotEqual(t, fromPrefix+toPrefix+startMsg.PlainNestedField.PlainField, newMsg.PlainNestedField.PlainField)
    49  
    50  	fieldFactories = []protoFieldFactory{tppff, nestedFieldFactory{}}
    51  
    52  	buffer.Reset()
    53  	assert.NoError(t, DeepMarshalJSON(&buffer, startMsg))
    54  	assert.NoError(t, DeepUnmarshalJSON(bytes.NewReader(buffer.Bytes()), newMsg))
    55  	assert.Equal(t, fromPrefix+toPrefix+startMsg.PlainNestedField.PlainField, newMsg.PlainNestedField.PlainField)
    56  }
    57  
    58  func TestMapNestedMsg(t *testing.T) {
    59  	fromPrefix := "from"
    60  	toPrefix := "to"
    61  	tppff := &testProtoPlainFieldFactory{
    62  		fromPrefix: fromPrefix,
    63  		toPrefix:   toPrefix,
    64  	}
    65  
    66  	fieldFactories = []protoFieldFactory{tppff}
    67  
    68  	pfValue := "foo"
    69  	mapKey := "bar"
    70  	startMsg := &testprotos.NestedMsg{
    71  		MapNestedField: map[string]*testprotos.SimpleMsg{
    72  			mapKey: {
    73  				PlainField: pfValue,
    74  			},
    75  		},
    76  	}
    77  
    78  	var buffer bytes.Buffer
    79  	assert.NoError(t, DeepMarshalJSON(&buffer, startMsg))
    80  	newMsg := &testprotos.NestedMsg{}
    81  	assert.NoError(t, DeepUnmarshalJSON(bytes.NewReader(buffer.Bytes()), newMsg))
    82  	assert.NotEqual(t, fromPrefix+toPrefix+startMsg.MapNestedField[mapKey].PlainField, newMsg.MapNestedField[mapKey].PlainField)
    83  
    84  	fieldFactories = []protoFieldFactory{tppff, nestedMapFieldFactory{}}
    85  
    86  	buffer.Reset()
    87  	assert.NoError(t, DeepMarshalJSON(&buffer, startMsg))
    88  	assert.NoError(t, DeepUnmarshalJSON(bytes.NewReader(buffer.Bytes()), newMsg))
    89  	assert.Equal(t, fromPrefix+toPrefix+startMsg.MapNestedField[mapKey].PlainField, newMsg.MapNestedField[mapKey].PlainField)
    90  }
    91  
    92  func TestSliceNestedMsg(t *testing.T) {
    93  	fromPrefix := "from"
    94  	toPrefix := "to"
    95  	tppff := &testProtoPlainFieldFactory{
    96  		fromPrefix: fromPrefix,
    97  		toPrefix:   toPrefix,
    98  	}
    99  
   100  	fieldFactories = []protoFieldFactory{tppff}
   101  
   102  	pfValue := "foo"
   103  	startMsg := &testprotos.NestedMsg{
   104  		SliceNestedField: []*testprotos.SimpleMsg{
   105  			{
   106  				PlainField: pfValue,
   107  			},
   108  		},
   109  	}
   110  
   111  	var buffer bytes.Buffer
   112  	assert.NoError(t, DeepMarshalJSON(&buffer, startMsg))
   113  	newMsg := &testprotos.NestedMsg{}
   114  	assert.NoError(t, DeepUnmarshalJSON(bytes.NewReader(buffer.Bytes()), newMsg))
   115  	assert.NotEqual(t, fromPrefix+toPrefix+startMsg.SliceNestedField[0].PlainField, newMsg.SliceNestedField[0].PlainField)
   116  
   117  	fieldFactories = []protoFieldFactory{tppff, nestedSliceFieldFactory{}}
   118  
   119  	buffer.Reset()
   120  	assert.NoError(t, DeepMarshalJSON(&buffer, startMsg))
   121  	assert.NoError(t, DeepUnmarshalJSON(bytes.NewReader(buffer.Bytes()), newMsg))
   122  	assert.Equal(t, fromPrefix+toPrefix+startMsg.SliceNestedField[0].PlainField, newMsg.SliceNestedField[0].PlainField)
   123  }