Merge branch 'beckn-onix-v1.0-develop' of https://github.com/beckn/beckn-onix into feature/core

This commit is contained in:
MohitKatare-protean
2025-03-28 22:18:39 +05:30
29 changed files with 3473 additions and 323 deletions

View File

@@ -6,24 +6,24 @@ import (
"strings"
)
// Error represents an error response.
// Error represents a standard 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.
// This 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.
// SchemaValidationErr occurs when schema validation errors are encountered.
type SchemaValidationErr struct {
Errors []Error
}
// Error implements the error interface for SchemaValidationErr.
// This implements the error interface for SchemaValidationErr.
func (e *SchemaValidationErr) Error() string {
var errorMessages []string
for _, err := range e.Errors {
@@ -32,7 +32,7 @@ func (e *SchemaValidationErr) Error() string {
return strings.Join(errorMessages, "; ")
}
// BecknError converts SchemaValidationErr into a Beckn-compliant Error response.
// BecknError converts the SchemaValidationErr to an instance of Error.
func (e *SchemaValidationErr) BecknError() *Error {
if len(e.Errors) == 0 {
return &Error{
@@ -58,22 +58,17 @@ func (e *SchemaValidationErr) BecknError() *Error {
}
}
// SignValidationErr represents an error that occurs during signature validation.
// SignValidationErr occurs when signature validation fails.
type SignValidationErr struct {
error
}
// NewSignValidationErrf creates a new SignValidationErr with a formatted message.
func NewSignValidationErrf(format string, a ...any) *SignValidationErr {
return &SignValidationErr{fmt.Errorf(format, a...)}
}
// NewSignValidationErr creates a new SignValidationErr from an existing error.
// NewSignValidationErr creates a new instance of SignValidationErr from an error.
func NewSignValidationErr(e error) *SignValidationErr {
return &SignValidationErr{e}
}
// BecknError converts SignValidationErr into a Beckn-compliant Error response.
// BecknError converts the SignValidationErr to an instance of Error.
func (e *SignValidationErr) BecknError() *Error {
return &Error{
Code: http.StatusText(http.StatusUnauthorized),
@@ -81,22 +76,17 @@ func (e *SignValidationErr) BecknError() *Error {
}
}
// BadReqErr represents an error related to a bad request.
// BadReqErr occurs when a bad request is encountered.
type BadReqErr struct {
error
}
// NewBadReqErr creates a new BadReqErr from an existing error.
// NewBadReqErr creates a new instance of BadReqErr from an error.
func NewBadReqErr(err error) *BadReqErr {
return &BadReqErr{err}
}
// NewBadReqErrf creates a new BadReqErr with a formatted message.
func NewBadReqErrf(format string, a ...any) *BadReqErr {
return &BadReqErr{fmt.Errorf(format, a...)}
}
// BecknError converts BadReqErr into a Beckn-compliant Error response.
// BecknError converts the BadReqErr to an instance of Error.
func (e *BadReqErr) BecknError() *Error {
return &Error{
Code: http.StatusText(http.StatusBadRequest),
@@ -104,22 +94,17 @@ func (e *BadReqErr) BecknError() *Error {
}
}
// NotFoundErr represents an error for a missing resource or endpoint.
// NotFoundErr occurs when a requested endpoint is not found.
type NotFoundErr struct {
error
}
// NewNotFoundErr creates a new NotFoundErr from an existing error.
// NewNotFoundErr creates a new instance of NotFoundErr from an error.
func NewNotFoundErr(err error) *NotFoundErr {
return &NotFoundErr{err}
}
// NewNotFoundErrf creates a new NotFoundErr with a formatted message.
func NewNotFoundErrf(format string, a ...any) *NotFoundErr {
return &NotFoundErr{fmt.Errorf(format, a...)}
}
// BecknError converts NotFoundErr into a Beckn-compliant Error response.
// BecknError converts the NotFoundErr to an instance of Error.
func (e *NotFoundErr) BecknError() *Error {
return &Error{
Code: http.StatusText(http.StatusNotFound),

200
pkg/model/error_test.go Normal file
View File

@@ -0,0 +1,200 @@
package model
import (
"errors"
"fmt"
"testing"
"net/http"
"github.com/stretchr/testify/assert"
"gopkg.in/yaml.v2"
)
// NewSignValidationErrf creates a new SignValidationErr with a formatted error message.
func NewSignValidationErrf(format string, a ...any) *SignValidationErr {
return &SignValidationErr{fmt.Errorf(format, a...)}
}
// NewNotFoundErrf creates a new NotFoundErr with a formatted error message.
func NewNotFoundErrf(format string, a ...any) *NotFoundErr {
return &NotFoundErr{fmt.Errorf(format, a...)}
}
// NewBadReqErrf creates a new BadReqErr with a formatted error message.
func NewBadReqErrf(format string, a ...any) *BadReqErr {
return &BadReqErr{fmt.Errorf(format, a...)}
}
func TestError_Error(t *testing.T) {
err := &Error{
Code: "404",
Paths: "/api/v1/user",
Message: "User not found",
}
expected := "Error: Code=404, Path=/api/v1/user, Message=User not found"
actual := err.Error()
if actual != expected {
t.Errorf("err.Error() = %s, want %s",
actual, expected)
}
}
func TestSchemaValidationErr_Error(t *testing.T) {
schemaErr := &SchemaValidationErr{
Errors: []Error{
{Paths: "/user", Message: "Field required"},
{Paths: "/email", Message: "Invalid format"},
},
}
expected := "/user: Field required; /email: Invalid format"
actual := schemaErr.Error()
if actual != expected {
t.Errorf("err.Error() = %s, want %s",
actual, expected)
}
}
func TestSchemaValidationErr_BecknError(t *testing.T) {
schemaErr := &SchemaValidationErr{
Errors: []Error{
{Paths: "/user", Message: "Field required"},
},
}
beErr := schemaErr.BecknError()
expected := "Bad Request"
if beErr.Code != expected {
t.Errorf("err.Error() = %s, want %s",
beErr.Code, expected)
}
}
func TestSignValidationErr_BecknError(t *testing.T) {
signErr := NewSignValidationErr(errors.New("signature failed"))
beErr := signErr.BecknError()
expectedMsg := "Signature Validation Error: signature failed"
if beErr.Message != expectedMsg {
t.Errorf("err.Error() = %s, want %s",
beErr.Message, expectedMsg)
}
}
func TestNewSignValidationErrf(t *testing.T) {
signErr := NewSignValidationErrf("error %s", "signature failed")
expected := "error signature failed"
if signErr.Error() != expected {
t.Errorf("err.Error() = %s, want %s",
signErr.Error(), expected)
}
}
func TestNewSignValidationErr(t *testing.T) {
err := errors.New("signature error")
signErr := NewSignValidationErr(err)
if signErr.Error() != err.Error() {
t.Errorf("err.Error() = %s, want %s", err.Error(),
signErr.Error())
}
}
func TestBadReqErr_BecknError(t *testing.T) {
badReqErr := NewBadReqErr(errors.New("invalid input"))
beErr := badReqErr.BecknError()
expectedMsg := "BAD Request: invalid input"
if beErr.Message != expectedMsg {
t.Errorf("err.Error() = %s, want %s",
beErr.Message, expectedMsg)
}
}
func TestNewBadReqErrf(t *testing.T) {
badReqErr := NewBadReqErrf("invalid field %s", "name")
expected := "invalid field name"
if badReqErr.Error() != expected {
t.Errorf("err.Error() = %s, want %s",
badReqErr, expected)
}
}
func TestNewBadReqErr(t *testing.T) {
err := errors.New("bad request")
badReqErr := NewBadReqErr(err)
if badReqErr.Error() != err.Error() {
t.Errorf("err.Error() = %s, want %s",
badReqErr.Error(), err.Error())
}
}
func TestNotFoundErr_BecknError(t *testing.T) {
notFoundErr := NewNotFoundErr(errors.New("resource not found"))
beErr := notFoundErr.BecknError()
expectedMsg := "Endpoint not found: resource not found"
if beErr.Message != expectedMsg {
t.Errorf("err.Error() = %s, want %s",
beErr.Message, expectedMsg)
}
}
func TestNewNotFoundErrf(t *testing.T) {
notFoundErr := NewNotFoundErrf("resource %s not found", "user")
expected := "resource user not found"
if notFoundErr.Error() != expected {
t.Errorf("err.Error() = %s, want %s",
notFoundErr.Error(), expected)
}
}
func TestNewNotFoundErr(t *testing.T) {
err := errors.New("not found")
notFoundErr := NewNotFoundErr(err)
if notFoundErr.Error() != err.Error() {
t.Errorf("err.Error() = %s, want %s",
notFoundErr.Error(), err.Error())
}
}
func TestRole_UnmarshalYAML_ValidRole(t *testing.T) {
var role Role
yamlData := []byte("bap")
err := yaml.Unmarshal(yamlData, &role)
assert.NoError(t, err) //TODO: should replace assert here
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")
}
func TestSchemaValidationErr_BecknError_NoErrors(t *testing.T) {
schemaValidationErr := &SchemaValidationErr{Errors: nil}
beErr := schemaValidationErr.BecknError()
expectedMsg := "Schema validation error."
expectedCode := http.StatusText(http.StatusBadRequest)
if beErr.Message != expectedMsg {
t.Errorf("beErr.Message = %s, want %s", beErr.Message, expectedMsg)
}
if beErr.Code != expectedCode {
t.Errorf("beErr.Code = %s, want %s", beErr.Code, expectedCode)
}
}

View File

@@ -38,27 +38,19 @@ const (
UnaAuthorizedHeaderGateway string = "Proxy-Authenticate"
)
// MsgIDKey represents the key for the message ID.
const MsgIDKey = "message_id"
type contextKey string
const MsgIDKey = contextKey("message_id")
// Role defines different roles in the network.
type Role string
const (
// RoleBAP represents a Buyer App Participant.
RoleBAP Role = "bap"
// RoleBPP represents a Buyer Platform Participant.
RoleBPP Role = "bpp"
// RoleGateway represents a Network Gateway.
RoleGateway Role = "gateway"
// RoleRegistery represents a Registry Service.
RoleBAP Role = "bap"
RoleBPP Role = "bpp"
RoleGateway Role = "gateway"
RoleRegistery Role = "registery"
)
// validRoles ensures only allowed values are accepted
var validRoles = map[Role]bool{
RoleBAP: true,
RoleBPP: true,
@@ -99,28 +91,20 @@ type StepContext struct {
RespHeader http.Header
}
// WithContext updates the context in StepContext while keeping other fields unchanged.
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 (
// StatusACK indicates a successful acknowledgment.
StatusACK Status = "ACK"
// StatusNACK indicates a negative acknowledgment.
StatusACK Status = "ACK"
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"`