github.com/hellobchain/third_party@v0.0.0-20230331131523-deb0478a2e52/hyperledger/fabric-config/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  	"github.com/hellobchain/third_party/hyperledger/fabric-config/protolator/testprotos"
    22  	"testing"
    23  
    24  	. "github.com/onsi/gomega"
    25  )
    26  
    27  func TestPlainNestedMsg(t *testing.T) {
    28  	gt := NewGomegaWithT(t)
    29  
    30  	fromPrefix := "from"
    31  	toPrefix := "to"
    32  	tppff := &testProtoPlainFieldFactory{
    33  		fromPrefix: fromPrefix,
    34  		toPrefix:   toPrefix,
    35  	}
    36  
    37  	fieldFactories = []protoFieldFactory{tppff}
    38  
    39  	pfValue := "foo"
    40  	startMsg := &testprotos.NestedMsg{
    41  		PlainNestedField: &testprotos.SimpleMsg{
    42  			PlainField: pfValue,
    43  		},
    44  	}
    45  
    46  	var buffer bytes.Buffer
    47  	err := DeepMarshalJSON(&buffer, startMsg)
    48  	gt.Expect(err).NotTo(HaveOccurred())
    49  	newMsg := &testprotos.NestedMsg{}
    50  	err = DeepUnmarshalJSON(bytes.NewReader(buffer.Bytes()), newMsg)
    51  	gt.Expect(err).NotTo(HaveOccurred())
    52  	gt.Expect(newMsg.PlainNestedField.PlainField).NotTo(Equal(fromPrefix + toPrefix + startMsg.PlainNestedField.PlainField))
    53  
    54  	fieldFactories = []protoFieldFactory{tppff, nestedFieldFactory{}}
    55  
    56  	buffer.Reset()
    57  	err = DeepMarshalJSON(&buffer, startMsg)
    58  	gt.Expect(err).NotTo(HaveOccurred())
    59  	err = DeepUnmarshalJSON(bytes.NewReader(buffer.Bytes()), newMsg)
    60  	gt.Expect(err).NotTo(HaveOccurred())
    61  	gt.Expect(newMsg.PlainNestedField.PlainField).To(Equal(fromPrefix + toPrefix + startMsg.PlainNestedField.PlainField))
    62  }
    63  
    64  func TestMapNestedMsg(t *testing.T) {
    65  	gt := NewGomegaWithT(t)
    66  
    67  	fromPrefix := "from"
    68  	toPrefix := "to"
    69  	tppff := &testProtoPlainFieldFactory{
    70  		fromPrefix: fromPrefix,
    71  		toPrefix:   toPrefix,
    72  	}
    73  
    74  	fieldFactories = []protoFieldFactory{tppff}
    75  
    76  	pfValue := "foo"
    77  	mapKey := "bar"
    78  	startMsg := &testprotos.NestedMsg{
    79  		MapNestedField: map[string]*testprotos.SimpleMsg{
    80  			mapKey: {
    81  				PlainField: pfValue,
    82  			},
    83  		},
    84  	}
    85  
    86  	var buffer bytes.Buffer
    87  	err := DeepMarshalJSON(&buffer, startMsg)
    88  	gt.Expect(err).NotTo(HaveOccurred())
    89  	newMsg := &testprotos.NestedMsg{}
    90  	err = DeepUnmarshalJSON(bytes.NewReader(buffer.Bytes()), newMsg)
    91  	gt.Expect(err).NotTo(HaveOccurred())
    92  	gt.Expect(newMsg.MapNestedField[mapKey].PlainField).NotTo(Equal(fromPrefix + toPrefix + startMsg.MapNestedField[mapKey].PlainField))
    93  
    94  	fieldFactories = []protoFieldFactory{tppff, nestedMapFieldFactory{}}
    95  
    96  	buffer.Reset()
    97  	err = DeepMarshalJSON(&buffer, startMsg)
    98  	gt.Expect(err).NotTo(HaveOccurred())
    99  	err = DeepUnmarshalJSON(bytes.NewReader(buffer.Bytes()), newMsg)
   100  	gt.Expect(err).NotTo(HaveOccurred())
   101  	gt.Expect(newMsg.MapNestedField[mapKey].PlainField).To(Equal(fromPrefix + toPrefix + startMsg.MapNestedField[mapKey].PlainField))
   102  }
   103  
   104  func TestSliceNestedMsg(t *testing.T) {
   105  	gt := NewGomegaWithT(t)
   106  
   107  	fromPrefix := "from"
   108  	toPrefix := "to"
   109  	tppff := &testProtoPlainFieldFactory{
   110  		fromPrefix: fromPrefix,
   111  		toPrefix:   toPrefix,
   112  	}
   113  
   114  	fieldFactories = []protoFieldFactory{tppff}
   115  
   116  	pfValue := "foo"
   117  	startMsg := &testprotos.NestedMsg{
   118  		SliceNestedField: []*testprotos.SimpleMsg{
   119  			{
   120  				PlainField: pfValue,
   121  			},
   122  		},
   123  	}
   124  
   125  	var buffer bytes.Buffer
   126  	err := DeepMarshalJSON(&buffer, startMsg)
   127  	gt.Expect(err).NotTo(HaveOccurred())
   128  	newMsg := &testprotos.NestedMsg{}
   129  	err = DeepUnmarshalJSON(bytes.NewReader(buffer.Bytes()), newMsg)
   130  	gt.Expect(err).NotTo(HaveOccurred())
   131  	gt.Expect(newMsg.SliceNestedField[0].PlainField).NotTo(Equal(fromPrefix + toPrefix + startMsg.SliceNestedField[0].PlainField))
   132  
   133  	fieldFactories = []protoFieldFactory{tppff, nestedSliceFieldFactory{}}
   134  
   135  	buffer.Reset()
   136  	err = DeepMarshalJSON(&buffer, startMsg)
   137  	gt.Expect(err).NotTo(HaveOccurred())
   138  	err = DeepUnmarshalJSON(bytes.NewReader(buffer.Bytes()), newMsg)
   139  	gt.Expect(err).NotTo(HaveOccurred())
   140  	gt.Expect(newMsg.SliceNestedField[0].PlainField).To(Equal(fromPrefix + toPrefix + startMsg.SliceNestedField[0].PlainField))
   141  }