From ffa1afc485ec11914d3b06f0f9eac0a698c4fa58 Mon Sep 17 00:00:00 2001 From: Kavindu Dodanduwa Date: Tue, 25 Jun 2024 13:50:04 -0700 Subject: [PATCH] add mandatory flags property in bulk response Signed-off-by: Kavindu Dodanduwa --- core/pkg/service/ofrep/models.go | 2 +- core/pkg/service/ofrep/models_test.go | 60 +++++++++++++++++++-------- 2 files changed, 43 insertions(+), 19 deletions(-) diff --git a/core/pkg/service/ofrep/models.go b/core/pkg/service/ofrep/models.go index 4aaa82413..2cc0c736c 100644 --- a/core/pkg/service/ofrep/models.go +++ b/core/pkg/service/ofrep/models.go @@ -20,7 +20,7 @@ type EvaluationSuccess struct { } type BulkEvaluationResponse struct { - Flags []interface{} `json:"flags,omitempty"` + Flags []interface{} `json:"flags"` } type EvaluationError struct { diff --git a/core/pkg/service/ofrep/models_test.go b/core/pkg/service/ofrep/models_test.go index 0fa4d82a0..a9b62f117 100644 --- a/core/pkg/service/ofrep/models_test.go +++ b/core/pkg/service/ofrep/models_test.go @@ -1,6 +1,7 @@ package ofrep import ( + "encoding/json" "errors" "reflect" "testing" @@ -45,31 +46,54 @@ func TestSuccessResult(t *testing.T) { } } -func TestBulkEvaluationResult(t *testing.T) { - // given - values := []evaluator.AnyValue{ +func TestBulkEvaluationResponse(t *testing.T) { + tests := []struct { + name string + input []evaluator.AnyValue + marshalledOutput string + }{ { - Value: false, - Variant: "false", - Reason: model.StaticReason, - FlagKey: "key", - Metadata: map[string]interface{}{ - "key": "value", - }, + name: "empty input", + input: nil, + marshalledOutput: "{\"flags\":[]}", }, { - Value: false, - Variant: "false", - Reason: model.ErrorReason, - FlagKey: "errorFlag", - Error: errors.New(model.FlagNotFoundErrorCode), + name: "valid values", + input: []evaluator.AnyValue{ + { + Value: false, + Variant: "false", + Reason: model.StaticReason, + FlagKey: "key", + Metadata: map[string]interface{}{ + "key": "value", + }, + }, + { + Value: false, + Variant: "false", + Reason: model.ErrorReason, + FlagKey: "errorFlag", + Error: errors.New(model.FlagNotFoundErrorCode), + }, + }, + marshalledOutput: "{\"flags\":[{\"value\":false,\"key\":\"key\",\"reason\":\"STATIC\",\"variant\":\"false\",\"metadata\":{\"key\":\"value\"}},{\"key\":\"errorFlag\",\"errorCode\":\"FLAG_NOT_FOUND\",\"errorDetails\":\"flag `errorFlag` does not exist\"}]}", }, } - bulkResponse := BulkEvaluationResponseFrom(values) + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + response := BulkEvaluationResponseFrom(test.input) + + marshal, err := json.Marshal(response) + if err != nil { + t.Errorf("error marshalling the response: %v", err) + } - if len(bulkResponse.Flags) != 2 { - t.Errorf("expected bulk response to contain 2 results, but got %d", len(bulkResponse.Flags)) + if test.marshalledOutput != string(marshal) { + t.Errorf("expected %s, got %s", test.marshalledOutput, string(marshal)) + } + }) } }