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