From ce70a467b98ea88079ad2526cfc5a9aba83f685f Mon Sep 17 00:00:00 2001 From: "mayur.popli" Date: Tue, 25 Mar 2025 02:52:44 +0530 Subject: [PATCH] fix: error coverage --- pkg/model/coverage.out | 27 ++++++ pkg/model/error.go | 9 -- pkg/model/error_test.go | 185 +++++++++++++++++++++------------------- pkg/model/model.go | 16 +--- 4 files changed, 126 insertions(+), 111 deletions(-) create mode 100644 pkg/model/coverage.out diff --git a/pkg/model/coverage.out b/pkg/model/coverage.out new file mode 100644 index 0000000..dd8a37a --- /dev/null +++ b/pkg/model/coverage.out @@ -0,0 +1,27 @@ +mode: set +github.com/beckn/beckn-onix/pkg/model/error.go:15.32,17.2 1 1 +github.com/beckn/beckn-onix/pkg/model/error.go:23.46,25.31 2 1 +github.com/beckn/beckn-onix/pkg/model/error.go:25.31,27.3 1 1 +github.com/beckn/beckn-onix/pkg/model/error.go:28.2,28.42 1 1 +github.com/beckn/beckn-onix/pkg/model/error.go:31.51,32.24 1 1 +github.com/beckn/beckn-onix/pkg/model/error.go:32.24,37.3 1 0 +github.com/beckn/beckn-onix/pkg/model/error.go:38.2,40.31 3 1 +github.com/beckn/beckn-onix/pkg/model/error.go:40.31,41.22 1 1 +github.com/beckn/beckn-onix/pkg/model/error.go:41.22,43.4 1 1 +github.com/beckn/beckn-onix/pkg/model/error.go:44.3,44.43 1 1 +github.com/beckn/beckn-onix/pkg/model/error.go:47.2,51.3 1 1 +github.com/beckn/beckn-onix/pkg/model/error.go:58.72,60.2 1 1 +github.com/beckn/beckn-onix/pkg/model/error.go:62.55,64.2 1 1 +github.com/beckn/beckn-onix/pkg/model/error.go:66.49,71.2 1 1 +github.com/beckn/beckn-onix/pkg/model/error.go:77.41,79.2 1 1 +github.com/beckn/beckn-onix/pkg/model/error.go:81.56,83.2 1 1 +github.com/beckn/beckn-onix/pkg/model/error.go:85.41,90.2 1 1 +github.com/beckn/beckn-onix/pkg/model/error.go:96.45,98.2 1 1 +github.com/beckn/beckn-onix/pkg/model/error.go:100.60,102.2 1 1 +github.com/beckn/beckn-onix/pkg/model/error.go:104.43,109.2 1 1 +github.com/beckn/beckn-onix/pkg/model/model.go:63.71,65.45 2 0 +github.com/beckn/beckn-onix/pkg/model/model.go:65.45,67.3 1 0 +github.com/beckn/beckn-onix/pkg/model/model.go:69.2,70.23 2 0 +github.com/beckn/beckn-onix/pkg/model/model.go:70.23,72.3 1 0 +github.com/beckn/beckn-onix/pkg/model/model.go:73.2,74.12 2 0 +github.com/beckn/beckn-onix/pkg/model/model.go:93.61,95.2 1 0 diff --git a/pkg/model/error.go b/pkg/model/error.go index 5c5f450..5dad0c9 100644 --- a/pkg/model/error.go +++ b/pkg/model/error.go @@ -6,24 +6,20 @@ import ( "strings" ) -// Error represents an error response. type Error struct { Code string `json:"code"` Paths string `json:"paths,omitempty"` Message string `json:"message"` } -// Error implements the error interface for the Error struct. func (e *Error) Error() string { return fmt.Sprintf("Error: Code=%s, Path=%s, Message=%s", e.Code, e.Paths, e.Message) } -// SchemaValidationErr represents a collection of schema validation failures. type SchemaValidationErr struct { Errors []Error } -// Error implements the error interface for SchemaValidationErr. func (e *SchemaValidationErr) Error() string { var errorMessages []string for _, err := range e.Errors { @@ -39,8 +35,6 @@ func (e *SchemaValidationErr) BecknError() *Error { Message: "Schema validation error.", } } - - // Collect all error paths and messages var paths []string var messages []string for _, err := range e.Errors { @@ -57,7 +51,6 @@ func (e *SchemaValidationErr) BecknError() *Error { } } -// SignalidationErr represents a collection of schema validation failures. type SignValidationErr struct { error } @@ -77,7 +70,6 @@ func (e *SignValidationErr) BecknError() *Error { } } -// SignalidationErr represents a collection of schema validation failures. type BadReqErr struct { error } @@ -97,7 +89,6 @@ func (e *BadReqErr) BecknError() *Error { } } -// SignalidationErr represents a collection of schema validation failures. type NotFoundErr struct { error } diff --git a/pkg/model/error_test.go b/pkg/model/error_test.go index 4213211..29d4536 100644 --- a/pkg/model/error_test.go +++ b/pkg/model/error_test.go @@ -2,143 +2,152 @@ package model import ( "errors" - "net/http" "testing" + + "github.com/stretchr/testify/assert" + "gopkg.in/yaml.v2" ) func TestError_Error(t *testing.T) { err := &Error{ - Code: "400", - Paths: "/path/to/field", - Message: "Invalid value", + Code: "404", + Paths: "/api/v1/user", + Message: "User not found", } - expected := "Error: Code=400, Path=/path/to/field, Message=Invalid value" - if err.Error() != expected { - t.Errorf("Expected %s, got %s", expected, err.Error()) + expected := "Error: Code=404, Path=/api/v1/user, Message=User not found" + actual := err.Error() + + if actual != expected { + t.Errorf("expected %s, got %s", expected, actual) } } func TestSchemaValidationErr_Error(t *testing.T) { - errs := SchemaValidationErr{ + schemaErr := &SchemaValidationErr{ Errors: []Error{ - {Paths: "/field1", Message: "Field is required"}, - {Paths: "/field2", Message: "Invalid format"}, + {Paths: "/user", Message: "Field required"}, + {Paths: "/email", Message: "Invalid format"}, }, } - expected := "/field1: Field is required; /field2: Invalid format" - if errs.Error() != expected { - t.Errorf("Expected %s, got %s", expected, errs.Error()) + expected := "/user: Field required; /email: Invalid format" + actual := schemaErr.Error() + + if actual != expected { + t.Errorf("expected %s, got %s", expected, actual) } } func TestSchemaValidationErr_BecknError(t *testing.T) { - errs := SchemaValidationErr{ + schemaErr := &SchemaValidationErr{ Errors: []Error{ - {Paths: "/field1", Message: "Field is required"}, - {Paths: "/field2", Message: "Invalid format"}, + {Paths: "/user", Message: "Field required"}, }, } - result := errs.BecknError() - if result.Code != http.StatusText(http.StatusBadRequest) { - t.Errorf("Expected %s, got %s", http.StatusText(http.StatusBadRequest), result.Code) - } - - expectedPaths := "/field1;/field2" - expectedMessage := "Field is required; Invalid format" - if result.Paths != expectedPaths { - t.Errorf("Expected paths %s, got %s", expectedPaths, result.Paths) - } - if result.Message != expectedMessage { - t.Errorf("Expected message %s, got %s", expectedMessage, result.Message) - } -} - -func TestNewSignValidationErrf(t *testing.T) { - err := NewSignValidationErrf("signature %s", "invalid") - expected := "signature invalid" - if err.Error() != expected { - t.Errorf("Expected %s, got %s", expected, err.Error()) - } -} - -func TestNewSignValidationErr(t *testing.T) { - baseErr := errors.New("invalid signature") - err := NewSignValidationErr(baseErr) - if err.Error() != "invalid signature" { - t.Errorf("Expected %s, got %s", "invalid signature", err.Error()) + beErr := schemaErr.BecknError() + expected := "Bad Request" + if beErr.Code != expected { + t.Errorf("expected %s, got %s", expected, beErr.Code) } } func TestSignValidationErr_BecknError(t *testing.T) { - err := NewSignValidationErr(errors.New("invalid signature")) - result := err.BecknError() + signErr := NewSignValidationErr(errors.New("signature failed")) + beErr := signErr.BecknError() - expected := "Signature Validation Error: invalid signature" - if result.Code != http.StatusText(http.StatusUnauthorized) { - t.Errorf("Expected %s, got %s", http.StatusText(http.StatusUnauthorized), result.Code) - } - if result.Message != expected { - t.Errorf("Expected %s, got %s", expected, result.Message) + expectedMsg := "Signature Validation Error: signature failed" + if beErr.Message != expectedMsg { + t.Errorf("expected %s, got %s", expectedMsg, beErr.Message) } } -func TestNewBadReqErr(t *testing.T) { - baseErr := errors.New("bad request error") - err := NewBadReqErr(baseErr) - if err.Error() != "bad request error" { - t.Errorf("Expected %s, got %s", "bad request error", err.Error()) +func TestNewSignValidationErrf(t *testing.T) { + signErr := NewSignValidationErrf("error %s", "signature failed") + expected := "error signature failed" + if signErr.Error() != expected { + t.Errorf("expected %s, got %s", expected, signErr.Error()) } } -func TestNewBadReqErrf(t *testing.T) { - err := NewBadReqErrf("missing %s", "field") - expected := "missing field" - if err.Error() != expected { - t.Errorf("Expected %s, got %s", expected, err.Error()) +func TestNewSignValidationErr(t *testing.T) { + err := errors.New("signature error") + signErr := NewSignValidationErr(err) + + if signErr.Error() != err.Error() { + t.Errorf("expected %s, got %s", err.Error(), signErr.Error()) } } func TestBadReqErr_BecknError(t *testing.T) { - err := NewBadReqErr(errors.New("invalid payload")) - result := err.BecknError() + badReqErr := NewBadReqErr(errors.New("invalid input")) + beErr := badReqErr.BecknError() - expected := "BAD Request: invalid payload" - if result.Code != http.StatusText(http.StatusBadRequest) { - t.Errorf("Expected %s, got %s", http.StatusText(http.StatusBadRequest), result.Code) - } - if result.Message != expected { - t.Errorf("Expected %s, got %s", expected, result.Message) + expectedMsg := "BAD Request: invalid input" + if beErr.Message != expectedMsg { + t.Errorf("expected %s, got %s", expectedMsg, beErr.Message) } } -func TestNewNotFoundErr(t *testing.T) { - baseErr := errors.New("resource not found") - err := NewNotFoundErr(baseErr) - if err.Error() != "resource not found" { - t.Errorf("Expected %s, got %s", "resource not found", err.Error()) +func TestNewBadReqErrf(t *testing.T) { + badReqErr := NewBadReqErrf("invalid field %s", "name") + expected := "invalid field name" + if badReqErr.Error() != expected { + t.Errorf("expected %s, got %s", expected, badReqErr.Error()) } } -func TestNewNotFoundErrf(t *testing.T) { - err := NewNotFoundErrf("route %s not found", "/api/data") - expected := "route /api/data not found" - if err.Error() != expected { - t.Errorf("Expected %s, got %s", expected, err.Error()) +func TestNewBadReqErr(t *testing.T) { + err := errors.New("bad request") + badReqErr := NewBadReqErr(err) + + if badReqErr.Error() != err.Error() { + t.Errorf("expected %s, got %s", err.Error(), badReqErr.Error()) } } func TestNotFoundErr_BecknError(t *testing.T) { - err := NewNotFoundErr(errors.New("endpoint not available")) - result := err.BecknError() + notFoundErr := NewNotFoundErr(errors.New("resource not found")) + beErr := notFoundErr.BecknError() - expected := "Endpoint not found: endpoint not available" - if result.Code != http.StatusText(http.StatusNotFound) { - t.Errorf("Expected %s, got %s", http.StatusText(http.StatusNotFound), result.Code) - } - if result.Message != expected { - t.Errorf("Expected %s, got %s", expected, result.Message) + expectedMsg := "Endpoint not found: resource not found" + if beErr.Message != expectedMsg { + t.Errorf("expected %s, got %s", expectedMsg, beErr.Message) } } + +func TestNewNotFoundErrf(t *testing.T) { + notFoundErr := NewNotFoundErrf("resource %s not found", "user") + expected := "resource user not found" + if notFoundErr.Error() != expected { + t.Errorf("expected %s, got %s", expected, notFoundErr.Error()) + } +} + +func TestNewNotFoundErr(t *testing.T) { + err := errors.New("not found") + notFoundErr := NewNotFoundErr(err) + + if notFoundErr.Error() != err.Error() { + t.Errorf("expected %s, got %s", err.Error(), notFoundErr.Error()) + } +} + +func TestRole_UnmarshalYAML_ValidRole(t *testing.T) { + var role Role + yamlData := []byte("bap") + + err := yaml.Unmarshal(yamlData, &role) + assert.NoError(t, err) + assert.Equal(t, RoleBAP, role) +} + +func TestRole_UnmarshalYAML_InvalidRole(t *testing.T) { + var role Role + yamlData := []byte("invalid") + + err := yaml.Unmarshal(yamlData, &role) + assert.Error(t, err) + assert.Contains(t, err.Error(), "invalid Role") +} diff --git a/pkg/model/model.go b/pkg/model/model.go index f5ffe22..e13c7ba 100644 --- a/pkg/model/model.go +++ b/pkg/model/model.go @@ -8,15 +8,12 @@ import ( "time" ) -// Subscriber represents a unique operational configuration of a trusted platform on a network. type Subscriber struct { SubscriberID string `json:"subscriber_id"` URL string `json:"url" format:"uri"` Type string `json:"type" enum:"BAP,BPP,BG"` Domain string `json:"domain"` } - -// SubscriptionDetails represents subscription details of a Network Participant. type Subscription struct { Subscriber `json:",inline"` KeyID string `json:"key_id" format:"uuid"` @@ -39,7 +36,6 @@ const ( type contextKey string -// Correctly define MsgIDKey with a variable, not a const var MsgIDKey = contextKey("message_id") type Role string @@ -51,7 +47,6 @@ const ( RoleRegistery Role = "registery" ) -// validRoles ensures only allowed values are accepted var validRoles = map[Role]bool{ RoleBAP: true, RoleBPP: true, @@ -59,7 +54,6 @@ var validRoles = map[Role]bool{ RoleRegistery: true, } -// Custom YAML unmarshalling to validate Role names func (r *Role) UnmarshalYAML(unmarshal func(interface{}) error) error { var roleName string if err := unmarshal(&roleName); err != nil { @@ -91,10 +85,9 @@ type StepContext struct { } func (ctx *StepContext) WithContext(newCtx context.Context) { - ctx.Context = newCtx // Update the existing context, keeping all other fields unchanged. + ctx.Context = newCtx } -// Status represents the status of an acknowledgment. type Status string const ( @@ -102,18 +95,13 @@ const ( StatusNACK Status = "NACK" ) -// Ack represents an acknowledgment response. type Ack struct { - Status Status `json:"status"` // ACK or NACK + Status Status `json:"status"` } - -// Message represents the message object in the response. type Message struct { Ack Ack `json:"ack"` Error *Error `json:"error,omitempty"` } - -// Response represents the main response structure. type Response struct { Message Message `json:"message"` }