go.temporal.io/server@v1.23.0/common/codec/jsonpb_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 codec
    26  
    27  import (
    28  	"fmt"
    29  	"testing"
    30  	"time"
    31  
    32  	"github.com/stretchr/testify/suite"
    33  	"google.golang.org/protobuf/types/known/timestamppb"
    34  
    35  	commonpb "go.temporal.io/api/common/v1"
    36  	enumspb "go.temporal.io/api/enums/v1"
    37  	historypb "go.temporal.io/api/history/v1"
    38  
    39  	"go.temporal.io/server/common/testing/protoassert"
    40  )
    41  
    42  type (
    43  	jsonpbEncoderSuite struct {
    44  		suite.Suite
    45  		encoder JSONPBEncoder
    46  	}
    47  )
    48  
    49  var (
    50  	history = &historypb.History{
    51  		Events: []*historypb.HistoryEvent{
    52  			{
    53  				Version:   1234,
    54  				EventId:   130,
    55  				EventTime: timestamppb.New(time.Date(1978, 8, 22, 0, 0, 0, 0, time.UTC)),
    56  				EventType: enumspb.EVENT_TYPE_REQUEST_CANCEL_EXTERNAL_WORKFLOW_EXECUTION_INITIATED,
    57  				Attributes: &historypb.HistoryEvent_RequestCancelExternalWorkflowExecutionInitiatedEventAttributes{RequestCancelExternalWorkflowExecutionInitiatedEventAttributes: &historypb.RequestCancelExternalWorkflowExecutionInitiatedEventAttributes{
    58  					Namespace: "some random target namespace",
    59  					WorkflowExecution: &commonpb.WorkflowExecution{
    60  						WorkflowId: "some random target workflow ID",
    61  						RunId:      "some random target run ID",
    62  					},
    63  					ChildWorkflowOnly: true,
    64  					Control:           "some random control",
    65  				}},
    66  			},
    67  		},
    68  	}
    69  	encodedHistory    = `{"events": [{"eventId":"130","eventTime":"1978-08-22T00:00:00Z","eventType":"EVENT_TYPE_REQUEST_CANCEL_EXTERNAL_WORKFLOW_EXECUTION_INITIATED","version":"1234","requestCancelExternalWorkflowExecutionInitiatedEventAttributes":{"namespace":"some random target namespace","workflowExecution":{"workflowId":"some random target workflow ID","runId":"some random target run ID"},"control":"some random control","childWorkflowOnly":true}}]}`
    70  	oldEncodedHistory = `{"events": [{"eventId":"130","eventTime":"1978-08-22T00:00:00Z","eventType":"RequestCancelExternalWorkflowExecutionInitiated","version":"1234","requestCancelExternalWorkflowExecutionInitiatedEventAttributes":{"namespace":"some random target namespace","workflowExecution":{"workflowId":"some random target workflow ID","runId":"some random target run ID"},"control":"some random control","childWorkflowOnly":true}}]}`
    71  )
    72  
    73  func TestJSONPBEncoderSuite(t *testing.T) {
    74  	s := new(jsonpbEncoderSuite)
    75  	suite.Run(t, s)
    76  }
    77  
    78  func (s *jsonpbEncoderSuite) SetupSuite() {
    79  	s.encoder = NewJSONPBEncoder()
    80  }
    81  
    82  func (s *jsonpbEncoderSuite) TestEncode() {
    83  	json, err := s.encoder.Encode(history)
    84  	s.Nil(err)
    85  	s.JSONEq(encodedHistory, string(json))
    86  }
    87  
    88  func (s *jsonpbEncoderSuite) TestDecode() {
    89  	var val historypb.History
    90  	err := s.encoder.Decode([]byte(encodedHistory), &val)
    91  	s.Nil(err)
    92  	protoassert.ProtoEqual(s.T(), &val, history)
    93  }
    94  
    95  func (s *jsonpbEncoderSuite) TestEncodeHistories() {
    96  	var histories []*historypb.History
    97  	histories = append(histories, history)
    98  	histories = append(histories, history)
    99  	histories = append(histories, history)
   100  
   101  	json, err := s.encoder.EncodeHistories(histories)
   102  	s.Nil(err)
   103  	s.JSONEq(fmt.Sprintf("[%[1]s,%[1]s,%[1]s]", encodedHistory), string(json))
   104  }
   105  
   106  func (s *jsonpbEncoderSuite) TestDecodeHistories() {
   107  	historyJSON := fmt.Sprintf("[%[1]s,%[1]s,%[1]s]", encodedHistory)
   108  
   109  	var histories []*historypb.History
   110  	histories = append(histories, history)
   111  	histories = append(histories, history)
   112  	histories = append(histories, history)
   113  
   114  	decodedHistories, err := s.encoder.DecodeHistories([]byte(historyJSON))
   115  
   116  	s.Nil(err)
   117  	protoassert.ProtoSliceEqual(s.T(), histories, decodedHistories)
   118  }
   119  
   120  func (s *jsonpbEncoderSuite) TestDecodeOldHistories() {
   121  	historyJSON := fmt.Sprintf("[%[1]s,%[1]s,%[1]s]", oldEncodedHistory)
   122  
   123  	var historyEvents []*historypb.History
   124  	historyEvents = append(historyEvents, history)
   125  	historyEvents = append(historyEvents, history)
   126  	historyEvents = append(historyEvents, history)
   127  
   128  	decodedHistories, err := s.encoder.DecodeHistories([]byte(historyJSON))
   129  
   130  	s.Nil(err)
   131  	protoassert.ProtoSliceEqual(s.T(), historyEvents, decodedHistories)
   132  }