go.temporal.io/server@v1.23.0/common/payload/payload_test.go (about)

     1  // The MIT License
     2  //
     3  // Copyright (c) 2020 Temporal Technologies Inc.  All rights reserved.
     4  //
     5  // Copyright (c) 2020 Uber Technologies, Inc.
     6  //
     7  // Permission is hereby granted, free of charge, to any person obtaining a copy
     8  // of this software and associated documentation files (the "Software"), to deal
     9  // in the Software without restriction, including without limitation the rights
    10  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    11  // copies of the Software, and to permit persons to whom the Software is
    12  // furnished to do so, subject to the following conditions:
    13  //
    14  // The above copyright notice and this permission notice shall be included in
    15  // all copies or substantial portions of the Software.
    16  //
    17  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    18  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    19  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    20  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    21  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    22  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    23  // THE SOFTWARE.
    24  
    25  package payload
    26  
    27  import (
    28  	"testing"
    29  
    30  	"github.com/stretchr/testify/assert"
    31  	commonpb "go.temporal.io/api/common/v1"
    32  )
    33  
    34  type testStruct struct {
    35  	Int    int
    36  	String string
    37  	Bytes  []byte
    38  }
    39  
    40  func TestToString(t *testing.T) {
    41  	s := assert.New(t)
    42  	var result string
    43  
    44  	p := EncodeString("str")
    45  	result = ToString(p)
    46  	s.Equal(`"str"`, result)
    47  
    48  	p, err := Encode(10)
    49  	s.NoError(err)
    50  	result = ToString(p)
    51  	s.Equal("10", result)
    52  
    53  	p, err = Encode([]byte{41, 42, 43})
    54  	s.NoError(err)
    55  	result = ToString(p)
    56  	s.Equal("KSor", result)
    57  
    58  	p, err = Encode(&testStruct{
    59  		Int:    10,
    60  		String: "str",
    61  		Bytes:  []byte{51, 52, 53},
    62  	})
    63  	s.NoError(err)
    64  	result = ToString(p)
    65  	s.Equal(`{"Int":10,"String":"str","Bytes":"MzQ1"}`, result)
    66  
    67  	p, err = Encode(nil)
    68  	s.NoError(err)
    69  	result = ToString(p)
    70  	s.Equal("nil", result)
    71  
    72  	result = ToString(nil)
    73  	s.Equal("", result)
    74  }
    75  
    76  func TestMergeMapOfPayload(t *testing.T) {
    77  	s := assert.New(t)
    78  
    79  	var currentMap map[string]*commonpb.Payload
    80  	var newMap map[string]*commonpb.Payload
    81  	resultMap := MergeMapOfPayload(currentMap, newMap)
    82  	s.Equal(newMap, resultMap)
    83  
    84  	newMap = make(map[string]*commonpb.Payload)
    85  	resultMap = MergeMapOfPayload(currentMap, newMap)
    86  	s.Equal(newMap, resultMap)
    87  
    88  	newMap = map[string]*commonpb.Payload{"key": EncodeString("val")}
    89  	resultMap = MergeMapOfPayload(currentMap, newMap)
    90  	s.Equal(newMap, resultMap)
    91  
    92  	newMap = map[string]*commonpb.Payload{
    93  		"key":        EncodeString("val"),
    94  		"nil":        nilPayload,
    95  		"emptyArray": emptySlicePayload,
    96  	}
    97  	resultMap = MergeMapOfPayload(currentMap, newMap)
    98  	s.Equal(map[string]*commonpb.Payload{"key": EncodeString("val")}, resultMap)
    99  
   100  	currentMap = map[string]*commonpb.Payload{"number": EncodeString("1")}
   101  	resultMap = MergeMapOfPayload(currentMap, newMap)
   102  	s.Equal(
   103  		map[string]*commonpb.Payload{"number": EncodeString("1"), "key": EncodeString("val")},
   104  		resultMap,
   105  	)
   106  
   107  	newValue, _ := Encode(nil)
   108  	newMap = map[string]*commonpb.Payload{"number": newValue}
   109  	resultMap = MergeMapOfPayload(currentMap, newMap)
   110  	s.Equal(0, len(resultMap))
   111  
   112  	newValue, _ = Encode([]int{})
   113  	newValue.Metadata["key"] = []byte("foo")
   114  	newMap = map[string]*commonpb.Payload{"number": newValue}
   115  	resultMap = MergeMapOfPayload(currentMap, newMap)
   116  	s.Equal(0, len(resultMap))
   117  }
   118  
   119  func TestIsEqual(t *testing.T) {
   120  	s := assert.New(t)
   121  
   122  	a, _ := Encode(nil)
   123  	b, _ := Encode(nil)
   124  	s.True(isEqual(a, b))
   125  
   126  	a, _ = Encode([]string{})
   127  	b, _ = Encode([]string{})
   128  	s.True(isEqual(a, b))
   129  
   130  	a.Metadata["key"] = []byte("foo")
   131  	b.Metadata["key"] = []byte("bar")
   132  	s.True(isEqual(a, b))
   133  
   134  	a, _ = Encode(nil)
   135  	b, _ = Encode([]string{})
   136  	s.False(isEqual(a, b))
   137  
   138  	a, _ = Encode([]string{})
   139  	b, _ = Encode("foo")
   140  	s.False(isEqual(a, b))
   141  }