From d9aea885bf120bcc72c523a3d5a9a86aaa56c660 Mon Sep 17 00:00:00 2001 From: Martin Redolatti Date: Tue, 19 Sep 2023 16:15:06 -0300 Subject: [PATCH] fix get-treatments-with-config on cli client --- splitio/commitsha.go | 2 +- splitio/link/client/v1/impl.go | 7 +- splitio/link/client/v1/impl_test.go | 85 +++++++++++++++++++++++-- splitio/link/protocol/v1/mocks/mocks.go | 17 +++-- 4 files changed, 101 insertions(+), 10 deletions(-) diff --git a/splitio/commitsha.go b/splitio/commitsha.go index c621c31..f065945 100644 --- a/splitio/commitsha.go +++ b/splitio/commitsha.go @@ -1,3 +1,3 @@ package splitio -const CommitSHA = "7117453" +const CommitSHA = "29ff22d" diff --git a/splitio/link/client/v1/impl.go b/splitio/link/client/v1/impl.go index 377fef2..c361b4b 100644 --- a/splitio/link/client/v1/impl.go +++ b/splitio/link/client/v1/impl.go @@ -219,7 +219,12 @@ func (c *Impl) treatments(key string, bucketingKey string, features []string, at BucketingKey: bucketingKey, } } - results[features[idx]] = types.Result{Treatment: resp.Payload.Results[idx].Treatment, Impression: imp} + + res := types.Result{Treatment: resp.Payload.Results[idx].Treatment, Impression: imp} + if withConfig { + res.Config = resp.Payload.Results[idx].Config + } + results[features[idx]] = res } return results, nil diff --git a/splitio/link/client/v1/impl_test.go b/splitio/link/client/v1/impl_test.go index c06f8af..8e4a637 100644 --- a/splitio/link/client/v1/impl_test.go +++ b/splitio/link/client/v1/impl_test.go @@ -31,7 +31,7 @@ func TestClientGetTreatmentNoImpression(t *testing.T) { *args.Get(1).(*v1.ResponseWrapper[v1.RegisterPayload]) = v1.ResponseWrapper[v1.RegisterPayload]{Status: v1.ResultOk} }).Once() - serializerMock.On("Serialize", proto1Mocks.NewTreatmentRPC("key1", "buck1", "feat1", map[string]interface{}{"a": 1})). + serializerMock.On("Serialize", proto1Mocks.NewTreatmentRPC("key1", "buck1", "feat1", map[string]interface{}{"a": 1}, false)). Return([]byte("treatmentMessage"), nil).Once() serializerMock.On("Parse", []byte("treatmentResult"), mock.Anything).Return(nil).Run(func(args mock.Arguments) { *args.Get(1).(*v1.ResponseWrapper[v1.TreatmentPayload]) = v1.ResponseWrapper[v1.TreatmentPayload]{ @@ -49,6 +49,41 @@ func TestClientGetTreatmentNoImpression(t *testing.T) { assert.Nil(t, res.Impression) } +func TestClientGetTreatmentWithConfig(t *testing.T) { + + logger := logging.NewLogger(nil) + + rawConnMock := &transferMocks.RawConnMock{} + rawConnMock.On("SendMessage", []byte("registrationMessage")).Return(nil).Once() + rawConnMock.On("ReceiveMessage").Return([]byte("registrationSuccess"), nil).Once() + rawConnMock.On("SendMessage", []byte("treatmentWithConfigMessage")).Return(nil).Once() + rawConnMock.On("ReceiveMessage").Return([]byte("treatmentWithConfigResult"), nil).Once() + + serializerMock := &serializerMocks.SerializerMock{} + serializerMock.On("Serialize", proto1Mocks.NewRegisterRPC("some", false)).Return([]byte("registrationMessage"), nil).Once() + serializerMock.On("Parse", []byte("registrationSuccess"), mock.Anything).Return(nil).Run(func(args mock.Arguments) { + *args.Get(1).(*v1.ResponseWrapper[v1.RegisterPayload]) = v1.ResponseWrapper[v1.RegisterPayload]{Status: v1.ResultOk} + }).Once() + + serializerMock.On("Serialize", proto1Mocks.NewTreatmentRPC("key1", "buck1", "feat1", map[string]interface{}{"a": 1}, true)). + Return([]byte("treatmentWithConfigMessage"), nil).Once() + serializerMock.On("Parse", []byte("treatmentWithConfigResult"), mock.Anything).Return(nil).Run(func(args mock.Arguments) { + *args.Get(1).(*v1.ResponseWrapper[v1.TreatmentPayload]) = v1.ResponseWrapper[v1.TreatmentPayload]{ + Status: v1.ResultOk, + Payload: v1.TreatmentPayload{Treatment: "on", Config: lang.Ref(`{"some": 1}`)}, + } + }).Once() + client, err := New("some", logger, rawConnMock, serializerMock, false) + assert.NotNil(t, client) + assert.Nil(t, err) + + res, err := client.TreatmentWithConfig("key1", "buck1", "feat1", map[string]interface{}{"a": 1}) + assert.Nil(t, err) + assert.Equal(t, lang.Ref(`{"some": 1}`), res.Config) + assert.Equal(t, "on", res.Treatment) + assert.Nil(t, res.Impression) +} + func TestTrack(t *testing.T) { logger := logging.NewLogger(nil) @@ -94,7 +129,7 @@ func TestClientGetTreatmentWithImpression(t *testing.T) { *args.Get(1).(*v1.ResponseWrapper[v1.RegisterPayload]) = v1.ResponseWrapper[v1.RegisterPayload]{Status: v1.ResultOk} }).Once() - serializerMock.On("Serialize", proto1Mocks.NewTreatmentRPC("key1", "buck1", "feat1", map[string]interface{}{"a": 1})). + serializerMock.On("Serialize", proto1Mocks.NewTreatmentRPC("key1", "buck1", "feat1", map[string]interface{}{"a": 1}, false)). Return([]byte("treatmentMessage"), nil).Once() serializerMock.On("Parse", []byte("treatmentResult"), mock.Anything).Return(nil).Run(func(args mock.Arguments) { *args.Get(1).(*v1.ResponseWrapper[v1.TreatmentPayload]) = v1.ResponseWrapper[v1.TreatmentPayload]{ @@ -140,7 +175,7 @@ func TestClientGetTreatmentsNoImpression(t *testing.T) { *args.Get(1).(*v1.ResponseWrapper[v1.RegisterPayload]) = v1.ResponseWrapper[v1.RegisterPayload]{Status: v1.ResultOk} }).Once() - serializerMock.On("Serialize", proto1Mocks.NewTreatmentsRPC("key1", "buck1", []string{"a", "b", "c"}, map[string]interface{}{"a": 1})). + serializerMock.On("Serialize", proto1Mocks.NewTreatmentsRPC("key1", "buck1", []string{"a", "b", "c"}, map[string]interface{}{"a": 1}, false)). Return([]byte("treatmentsMessage"), nil).Once() serializerMock.On("Parse", []byte("treatmentsResult"), mock.Anything).Return(nil).Run(func(args mock.Arguments) { *args.Get(1).(*v1.ResponseWrapper[v1.TreatmentsPayload]) = v1.ResponseWrapper[v1.TreatmentsPayload]{ @@ -162,6 +197,48 @@ func TestClientGetTreatmentsNoImpression(t *testing.T) { } +func TestClientGetTreatmentsWithConfig(t *testing.T) { + + logger := logging.NewLogger(nil) + + rawConnMock := &transferMocks.RawConnMock{} + rawConnMock.On("SendMessage", []byte("registrationMessage")).Return(nil).Once() + rawConnMock.On("ReceiveMessage").Return([]byte("registrationSuccess"), nil).Once() + rawConnMock.On("SendMessage", []byte("treatmentsWithConfigMessage")).Return(nil).Once() + rawConnMock.On("ReceiveMessage").Return([]byte("treatmentsWithConfigResult"), nil).Once() + + serializerMock := &serializerMocks.SerializerMock{} + serializerMock.On("Serialize", proto1Mocks.NewRegisterRPC("some", false)).Return([]byte("registrationMessage"), nil).Once() + serializerMock.On("Parse", []byte("registrationSuccess"), mock.Anything).Return(nil).Run(func(args mock.Arguments) { + *args.Get(1).(*v1.ResponseWrapper[v1.RegisterPayload]) = v1.ResponseWrapper[v1.RegisterPayload]{Status: v1.ResultOk} + }).Once() + + serializerMock.On("Serialize", proto1Mocks.NewTreatmentsRPC("key1", "buck1", []string{"a", "b", "c"}, map[string]interface{}{"a": 1}, true)). + Return([]byte("treatmentsWithConfigMessage"), nil).Once() + serializerMock.On("Parse", []byte("treatmentsWithConfigResult"), mock.Anything).Return(nil).Run(func(args mock.Arguments) { + *args.Get(1).(*v1.ResponseWrapper[v1.TreatmentsPayload]) = v1.ResponseWrapper[v1.TreatmentsPayload]{ + Status: v1.ResultOk, + Payload: v1.TreatmentsPayload{Results: []v1.TreatmentPayload{ + {Treatment: "on", Config: lang.Ref(`{"some": 2}`)}, {Treatment: "off"}, {Treatment: "na"}}}} + }).Once() + client, err := New("some", logger, rawConnMock, serializerMock, false) + assert.NotNil(t, client) + assert.Nil(t, err) + + res, err := client.TreatmentsWithConfig("key1", "buck1", []string{"a", "b", "c"}, map[string]interface{}{"a": 1}) + assert.Nil(t, err) + assert.Equal(t, "on", res["a"].Treatment) + assert.Nil(t, res["a"].Impression) + assert.Equal(t, lang.Ref(`{"some": 2}`), res["a"].Config) + assert.Equal(t, "off", res["b"].Treatment) + assert.Nil(t, res["b"].Impression) + assert.Nil(t, res["b"].Config) + assert.Equal(t, "na", res["c"].Treatment) + assert.Nil(t, res["c"].Config) + assert.Nil(t, res["c"].Impression) + +} + func TestClientGetTreatmentsWithImpression(t *testing.T) { logger := logging.NewLogger(nil) @@ -178,7 +255,7 @@ func TestClientGetTreatmentsWithImpression(t *testing.T) { *args.Get(1).(*v1.ResponseWrapper[v1.RegisterPayload]) = v1.ResponseWrapper[v1.RegisterPayload]{Status: v1.ResultOk} }).Once() - serializerMock.On("Serialize", proto1Mocks.NewTreatmentsRPC("key1", "buck1", []string{"a", "b", "c"}, map[string]interface{}{"a": 1})). + serializerMock.On("Serialize", proto1Mocks.NewTreatmentsRPC("key1", "buck1", []string{"a", "b", "c"}, map[string]interface{}{"a": 1}, false)). Return([]byte("treatmentsMessage"), nil).Once() serializerMock.On("Parse", []byte("treatmentsResult"), mock.Anything).Return(nil).Run(func(args mock.Arguments) { *args.Get(1).(*v1.ResponseWrapper[v1.TreatmentsPayload]) = v1.ResponseWrapper[v1.TreatmentsPayload]{ diff --git a/splitio/link/protocol/v1/mocks/mocks.go b/splitio/link/protocol/v1/mocks/mocks.go index 0e87440..bb63661 100644 --- a/splitio/link/protocol/v1/mocks/mocks.go +++ b/splitio/link/protocol/v1/mocks/mocks.go @@ -22,20 +22,29 @@ func NewRegisterRPC(id string, listener bool) *v1.RPC { } } -func NewTreatmentRPC(key string, bucketing string, feature string, attrs map[string]interface{}) *v1.RPC { - return &v1.RPC{ +func NewTreatmentRPC(key string, bucketing string, feature string, attrs map[string]interface{}, withConfig bool) *v1.RPC { + rpc := &v1.RPC{ RPCBase: protocol.RPCBase{Version: protocol.V1}, OpCode: v1.OCTreatment, Args: []interface{}{key, bucketing, feature, attrs}, } + if withConfig { + rpc.OpCode = v1.OCTreatmentWithConfig + } + return rpc } -func NewTreatmentsRPC(key string, bucketing string, features []string, attrs map[string]interface{}) *v1.RPC { - return &v1.RPC{ +func NewTreatmentsRPC(key string, bucketing string, features []string, attrs map[string]interface{}, withConfig bool) *v1.RPC { + rpc := &v1.RPC{ RPCBase: protocol.RPCBase{Version: protocol.V1}, OpCode: v1.OCTreatments, Args: []interface{}{key, bucketing, features, attrs}, } + + if withConfig { + rpc.OpCode = v1.OCTreatmentsWithConfig + } + return rpc } func NewTrackRPC(key string, trafficType string, eventType string, eventVal *float64, props map[string]interface{}) *v1.RPC {