fix: error coverage

This commit is contained in:
mayur.popli
2025-03-25 02:52:44 +05:30
parent 7db185fdfb
commit ce70a467b9
4 changed files with 126 additions and 111 deletions

27
pkg/model/coverage.out Normal file
View File

@@ -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

View File

@@ -6,24 +6,20 @@ import (
"strings" "strings"
) )
// Error represents an error response.
type Error struct { type Error struct {
Code string `json:"code"` Code string `json:"code"`
Paths string `json:"paths,omitempty"` Paths string `json:"paths,omitempty"`
Message string `json:"message"` Message string `json:"message"`
} }
// Error implements the error interface for the Error struct.
func (e *Error) Error() string { func (e *Error) Error() string {
return fmt.Sprintf("Error: Code=%s, Path=%s, Message=%s", e.Code, e.Paths, e.Message) 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 { type SchemaValidationErr struct {
Errors []Error Errors []Error
} }
// Error implements the error interface for SchemaValidationErr.
func (e *SchemaValidationErr) Error() string { func (e *SchemaValidationErr) Error() string {
var errorMessages []string var errorMessages []string
for _, err := range e.Errors { for _, err := range e.Errors {
@@ -39,8 +35,6 @@ func (e *SchemaValidationErr) BecknError() *Error {
Message: "Schema validation error.", Message: "Schema validation error.",
} }
} }
// Collect all error paths and messages
var paths []string var paths []string
var messages []string var messages []string
for _, err := range e.Errors { 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 { type SignValidationErr struct {
error error
} }
@@ -77,7 +70,6 @@ func (e *SignValidationErr) BecknError() *Error {
} }
} }
// SignalidationErr represents a collection of schema validation failures.
type BadReqErr struct { type BadReqErr struct {
error error
} }
@@ -97,7 +89,6 @@ func (e *BadReqErr) BecknError() *Error {
} }
} }
// SignalidationErr represents a collection of schema validation failures.
type NotFoundErr struct { type NotFoundErr struct {
error error
} }

View File

@@ -2,143 +2,152 @@ package model
import ( import (
"errors" "errors"
"net/http"
"testing" "testing"
"github.com/stretchr/testify/assert"
"gopkg.in/yaml.v2"
) )
func TestError_Error(t *testing.T) { func TestError_Error(t *testing.T) {
err := &Error{ err := &Error{
Code: "400", Code: "404",
Paths: "/path/to/field", Paths: "/api/v1/user",
Message: "Invalid value", Message: "User not found",
} }
expected := "Error: Code=400, Path=/path/to/field, Message=Invalid value" expected := "Error: Code=404, Path=/api/v1/user, Message=User not found"
if err.Error() != expected { actual := err.Error()
t.Errorf("Expected %s, got %s", expected, err.Error())
if actual != expected {
t.Errorf("expected %s, got %s", expected, actual)
} }
} }
func TestSchemaValidationErr_Error(t *testing.T) { func TestSchemaValidationErr_Error(t *testing.T) {
errs := SchemaValidationErr{ schemaErr := &SchemaValidationErr{
Errors: []Error{ Errors: []Error{
{Paths: "/field1", Message: "Field is required"}, {Paths: "/user", Message: "Field required"},
{Paths: "/field2", Message: "Invalid format"}, {Paths: "/email", Message: "Invalid format"},
}, },
} }
expected := "/field1: Field is required; /field2: Invalid format" expected := "/user: Field required; /email: Invalid format"
if errs.Error() != expected { actual := schemaErr.Error()
t.Errorf("Expected %s, got %s", expected, errs.Error())
if actual != expected {
t.Errorf("expected %s, got %s", expected, actual)
} }
} }
func TestSchemaValidationErr_BecknError(t *testing.T) { func TestSchemaValidationErr_BecknError(t *testing.T) {
errs := SchemaValidationErr{ schemaErr := &SchemaValidationErr{
Errors: []Error{ Errors: []Error{
{Paths: "/field1", Message: "Field is required"}, {Paths: "/user", Message: "Field required"},
{Paths: "/field2", Message: "Invalid format"},
}, },
} }
result := errs.BecknError() beErr := schemaErr.BecknError()
if result.Code != http.StatusText(http.StatusBadRequest) { expected := "Bad Request"
t.Errorf("Expected %s, got %s", http.StatusText(http.StatusBadRequest), result.Code) if beErr.Code != expected {
} t.Errorf("expected %s, got %s", expected, beErr.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())
} }
} }
func TestSignValidationErr_BecknError(t *testing.T) { func TestSignValidationErr_BecknError(t *testing.T) {
err := NewSignValidationErr(errors.New("invalid signature")) signErr := NewSignValidationErr(errors.New("signature failed"))
result := err.BecknError() beErr := signErr.BecknError()
expected := "Signature Validation Error: invalid signature" expectedMsg := "Signature Validation Error: signature failed"
if result.Code != http.StatusText(http.StatusUnauthorized) { if beErr.Message != expectedMsg {
t.Errorf("Expected %s, got %s", http.StatusText(http.StatusUnauthorized), result.Code) t.Errorf("expected %s, got %s", expectedMsg, beErr.Message)
}
if result.Message != expected {
t.Errorf("Expected %s, got %s", expected, result.Message)
} }
} }
func TestNewBadReqErr(t *testing.T) { func TestNewSignValidationErrf(t *testing.T) {
baseErr := errors.New("bad request error") signErr := NewSignValidationErrf("error %s", "signature failed")
err := NewBadReqErr(baseErr) expected := "error signature failed"
if err.Error() != "bad request error" { if signErr.Error() != expected {
t.Errorf("Expected %s, got %s", "bad request error", err.Error()) t.Errorf("expected %s, got %s", expected, signErr.Error())
} }
} }
func TestNewBadReqErrf(t *testing.T) { func TestNewSignValidationErr(t *testing.T) {
err := NewBadReqErrf("missing %s", "field") err := errors.New("signature error")
expected := "missing field" signErr := NewSignValidationErr(err)
if err.Error() != expected {
t.Errorf("Expected %s, got %s", expected, err.Error()) if signErr.Error() != err.Error() {
t.Errorf("expected %s, got %s", err.Error(), signErr.Error())
} }
} }
func TestBadReqErr_BecknError(t *testing.T) { func TestBadReqErr_BecknError(t *testing.T) {
err := NewBadReqErr(errors.New("invalid payload")) badReqErr := NewBadReqErr(errors.New("invalid input"))
result := err.BecknError() beErr := badReqErr.BecknError()
expected := "BAD Request: invalid payload" expectedMsg := "BAD Request: invalid input"
if result.Code != http.StatusText(http.StatusBadRequest) { if beErr.Message != expectedMsg {
t.Errorf("Expected %s, got %s", http.StatusText(http.StatusBadRequest), result.Code) t.Errorf("expected %s, got %s", expectedMsg, beErr.Message)
}
if result.Message != expected {
t.Errorf("Expected %s, got %s", expected, result.Message)
} }
} }
func TestNewNotFoundErr(t *testing.T) { func TestNewBadReqErrf(t *testing.T) {
baseErr := errors.New("resource not found") badReqErr := NewBadReqErrf("invalid field %s", "name")
err := NewNotFoundErr(baseErr) expected := "invalid field name"
if err.Error() != "resource not found" { if badReqErr.Error() != expected {
t.Errorf("Expected %s, got %s", "resource not found", err.Error()) t.Errorf("expected %s, got %s", expected, badReqErr.Error())
} }
} }
func TestNewNotFoundErrf(t *testing.T) { func TestNewBadReqErr(t *testing.T) {
err := NewNotFoundErrf("route %s not found", "/api/data") err := errors.New("bad request")
expected := "route /api/data not found" badReqErr := NewBadReqErr(err)
if err.Error() != expected {
t.Errorf("Expected %s, got %s", expected, err.Error()) if badReqErr.Error() != err.Error() {
t.Errorf("expected %s, got %s", err.Error(), badReqErr.Error())
} }
} }
func TestNotFoundErr_BecknError(t *testing.T) { func TestNotFoundErr_BecknError(t *testing.T) {
err := NewNotFoundErr(errors.New("endpoint not available")) notFoundErr := NewNotFoundErr(errors.New("resource not found"))
result := err.BecknError() beErr := notFoundErr.BecknError()
expected := "Endpoint not found: endpoint not available" expectedMsg := "Endpoint not found: resource not found"
if result.Code != http.StatusText(http.StatusNotFound) { if beErr.Message != expectedMsg {
t.Errorf("Expected %s, got %s", http.StatusText(http.StatusNotFound), result.Code) t.Errorf("expected %s, got %s", expectedMsg, beErr.Message)
}
if result.Message != expected {
t.Errorf("Expected %s, got %s", expected, result.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")
}

View File

@@ -8,15 +8,12 @@ import (
"time" "time"
) )
// Subscriber represents a unique operational configuration of a trusted platform on a network.
type Subscriber struct { type Subscriber struct {
SubscriberID string `json:"subscriber_id"` SubscriberID string `json:"subscriber_id"`
URL string `json:"url" format:"uri"` URL string `json:"url" format:"uri"`
Type string `json:"type" enum:"BAP,BPP,BG"` Type string `json:"type" enum:"BAP,BPP,BG"`
Domain string `json:"domain"` Domain string `json:"domain"`
} }
// SubscriptionDetails represents subscription details of a Network Participant.
type Subscription struct { type Subscription struct {
Subscriber `json:",inline"` Subscriber `json:",inline"`
KeyID string `json:"key_id" format:"uuid"` KeyID string `json:"key_id" format:"uuid"`
@@ -39,7 +36,6 @@ const (
type contextKey string type contextKey string
// Correctly define MsgIDKey with a variable, not a const
var MsgIDKey = contextKey("message_id") var MsgIDKey = contextKey("message_id")
type Role string type Role string
@@ -51,7 +47,6 @@ const (
RoleRegistery Role = "registery" RoleRegistery Role = "registery"
) )
// validRoles ensures only allowed values are accepted
var validRoles = map[Role]bool{ var validRoles = map[Role]bool{
RoleBAP: true, RoleBAP: true,
RoleBPP: true, RoleBPP: true,
@@ -59,7 +54,6 @@ var validRoles = map[Role]bool{
RoleRegistery: true, RoleRegistery: true,
} }
// Custom YAML unmarshalling to validate Role names
func (r *Role) UnmarshalYAML(unmarshal func(interface{}) error) error { func (r *Role) UnmarshalYAML(unmarshal func(interface{}) error) error {
var roleName string var roleName string
if err := unmarshal(&roleName); err != nil { if err := unmarshal(&roleName); err != nil {
@@ -91,10 +85,9 @@ type StepContext struct {
} }
func (ctx *StepContext) WithContext(newCtx context.Context) { 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 type Status string
const ( const (
@@ -102,18 +95,13 @@ const (
StatusNACK Status = "NACK" StatusNACK Status = "NACK"
) )
// Ack represents an acknowledgment response.
type Ack struct { 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 { type Message struct {
Ack Ack `json:"ack"` Ack Ack `json:"ack"`
Error *Error `json:"error,omitempty"` Error *Error `json:"error,omitempty"`
} }
// Response represents the main response structure.
type Response struct { type Response struct {
Message Message `json:"message"` Message Message `json:"message"`
} }