From 5a3501b99d43a918d94a960048d423e36d7b98f4 Mon Sep 17 00:00:00 2001 From: "mayur.popli" Date: Tue, 25 Mar 2025 02:29:33 +0530 Subject: [PATCH 01/10] feat: test file for response and error module --- pkg/model/error.go | 118 ++++++++++++ pkg/model/error_test.go | 144 ++++++++++++++ pkg/model/model.go | 119 ++++++++++++ pkg/response/response.go | 194 ++++++++----------- pkg/response/response_test.go | 349 ++++++++++------------------------ 5 files changed, 557 insertions(+), 367 deletions(-) create mode 100644 pkg/model/error.go create mode 100644 pkg/model/error_test.go create mode 100644 pkg/model/model.go diff --git a/pkg/model/error.go b/pkg/model/error.go new file mode 100644 index 0000000..5c5f450 --- /dev/null +++ b/pkg/model/error.go @@ -0,0 +1,118 @@ +package model + +import ( + "fmt" + "net/http" + "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 { + errorMessages = append(errorMessages, fmt.Sprintf("%s: %s", err.Paths, err.Message)) + } + return strings.Join(errorMessages, "; ") +} + +func (e *SchemaValidationErr) BecknError() *Error { + if len(e.Errors) == 0 { + return &Error{ + Code: http.StatusText(http.StatusBadRequest), + Message: "Schema validation error.", + } + } + + // Collect all error paths and messages + var paths []string + var messages []string + for _, err := range e.Errors { + if err.Paths != "" { + paths = append(paths, err.Paths) + } + messages = append(messages, err.Message) + } + + return &Error{ + Code: http.StatusText(http.StatusBadRequest), + Paths: strings.Join(paths, ";"), + Message: strings.Join(messages, "; "), + } +} + +// SignalidationErr represents a collection of schema validation failures. +type SignValidationErr struct { + error +} + +func NewSignValidationErrf(format string, a ...any) *SignValidationErr { + return &SignValidationErr{fmt.Errorf(format, a...)} +} + +func NewSignValidationErr(e error) *SignValidationErr { + return &SignValidationErr{e} +} + +func (e *SignValidationErr) BecknError() *Error { + return &Error{ + Code: http.StatusText(http.StatusUnauthorized), + Message: "Signature Validation Error: " + e.Error(), + } +} + +// SignalidationErr represents a collection of schema validation failures. +type BadReqErr struct { + error +} + +func NewBadReqErr(err error) *BadReqErr { + return &BadReqErr{err} +} + +func NewBadReqErrf(format string, a ...any) *BadReqErr { + return &BadReqErr{fmt.Errorf(format, a...)} +} + +func (e *BadReqErr) BecknError() *Error { + return &Error{ + Code: http.StatusText(http.StatusBadRequest), + Message: "BAD Request: " + e.Error(), + } +} + +// SignalidationErr represents a collection of schema validation failures. +type NotFoundErr struct { + error +} + +func NewNotFoundErr(err error) *NotFoundErr { + return &NotFoundErr{err} +} + +func NewNotFoundErrf(format string, a ...any) *NotFoundErr { + return &NotFoundErr{fmt.Errorf(format, a...)} +} + +func (e *NotFoundErr) BecknError() *Error { + return &Error{ + Code: http.StatusText(http.StatusNotFound), + Message: "Endpoint not found: " + e.Error(), + } +} diff --git a/pkg/model/error_test.go b/pkg/model/error_test.go new file mode 100644 index 0000000..4213211 --- /dev/null +++ b/pkg/model/error_test.go @@ -0,0 +1,144 @@ +package model + +import ( + "errors" + "net/http" + "testing" +) + +func TestError_Error(t *testing.T) { + err := &Error{ + Code: "400", + Paths: "/path/to/field", + Message: "Invalid value", + } + + expected := "Error: Code=400, Path=/path/to/field, Message=Invalid value" + if err.Error() != expected { + t.Errorf("Expected %s, got %s", expected, err.Error()) + } +} + +func TestSchemaValidationErr_Error(t *testing.T) { + errs := SchemaValidationErr{ + Errors: []Error{ + {Paths: "/field1", Message: "Field is required"}, + {Paths: "/field2", Message: "Invalid format"}, + }, + } + + expected := "/field1: Field is required; /field2: Invalid format" + if errs.Error() != expected { + t.Errorf("Expected %s, got %s", expected, errs.Error()) + } +} + +func TestSchemaValidationErr_BecknError(t *testing.T) { + errs := SchemaValidationErr{ + Errors: []Error{ + {Paths: "/field1", Message: "Field is required"}, + {Paths: "/field2", Message: "Invalid format"}, + }, + } + + 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()) + } +} + +func TestSignValidationErr_BecknError(t *testing.T) { + err := NewSignValidationErr(errors.New("invalid signature")) + result := err.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) + } +} + +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 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 TestBadReqErr_BecknError(t *testing.T) { + err := NewBadReqErr(errors.New("invalid payload")) + result := err.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) + } +} + +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 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 TestNotFoundErr_BecknError(t *testing.T) { + err := NewNotFoundErr(errors.New("endpoint not available")) + result := err.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) + } +} diff --git a/pkg/model/model.go b/pkg/model/model.go new file mode 100644 index 0000000..f5ffe22 --- /dev/null +++ b/pkg/model/model.go @@ -0,0 +1,119 @@ +package model + +import ( + "context" + "fmt" + "net/http" + "net/url" + "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"` + SigningPublicKey string `json:"signing_public_key"` + EncrPublicKey string `json:"encr_public_key"` + ValidFrom time.Time `json:"valid_from" format:"date-time"` + ValidUntil time.Time `json:"valid_until" format:"date-time"` + Status string `json:"status" enum:"INITIATED,UNDER_SUBSCRIPTION,SUBSCRIBED,EXPIRED,UNSUBSCRIBED,INVALID_SSL"` + Created time.Time `json:"created" format:"date-time"` + Updated time.Time `json:"updated" format:"date-time"` + Nonce string +} + +const ( + AuthHeaderSubscriber string = "Authorization" + AuthHeaderGateway string = "X-Gateway-Authorization" + UnaAuthorizedHeaderSubscriber string = "WWW-Authenticate" + UnaAuthorizedHeaderGateway string = "Proxy-Authenticate" +) + +type contextKey string + +// Correctly define MsgIDKey with a variable, not a const +var MsgIDKey = contextKey("message_id") + +type Role string + +const ( + 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, + RoleGateway: true, + 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 { + return err + } + + role := Role(roleName) + if !validRoles[role] { + return fmt.Errorf("invalid Role: %s", roleName) + } + *r = role + return nil +} + +type Route struct { + Type string + URL *url.URL + Publisher string +} + +type StepContext struct { + context.Context + Request *http.Request + Body []byte + Route *Route + SubID string + Role Role + RespHeader http.Header +} + +func (ctx *StepContext) WithContext(newCtx context.Context) { + ctx.Context = newCtx // Update the existing context, keeping all other fields unchanged. +} + +// Status represents the status of an acknowledgment. +type Status string + +const ( + StatusACK Status = "ACK" + StatusNACK Status = "NACK" +) + +// Ack represents an acknowledgment response. +type Ack struct { + Status Status `json:"status"` // ACK or NACK +} + +// 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"` +} diff --git a/pkg/response/response.go b/pkg/response/response.go index 310d06f..053c1f1 100644 --- a/pkg/response/response.go +++ b/pkg/response/response.go @@ -3,7 +3,11 @@ package response import ( "context" "encoding/json" + "errors" "fmt" + "net/http" + + "github.com/beckn/beckn-onix/pkg/model" ) type ErrorType string @@ -13,131 +17,87 @@ const ( InvalidRequestErrorType ErrorType = "INVALID_REQUEST" ) -type BecknRequest struct { - Context map[string]interface{} `json:"context,omitempty"` -} +// type BecknRequest struct { +// Context map[string]interface{} `json:"context,omitempty"` +// } -type Error struct { - Code string `json:"code,omitempty"` - Message string `json:"message,omitempty"` - Paths string `json:"paths,omitempty"` -} - -type Message struct { - Ack struct { - Status string `json:"status,omitempty"` - } `json:"ack,omitempty"` - Error *Error `json:"error,omitempty"` -} - -type BecknResponse struct { - Context map[string]interface{} `json:"context,omitempty"` - Message Message `json:"message,omitempty"` -} - -type ClientFailureBecknResponse struct { - Context map[string]interface{} `json:"context,omitempty"` - Error *Error `json:"error,omitempty"` -} - -var errorMap = map[ErrorType]Error{ - SchemaValidationErrorType: { - Code: "400", - Message: "Schema validation failed", - }, - InvalidRequestErrorType: { - Code: "401", - Message: "Invalid request format", - }, -} - -var DefaultError = Error{ - Code: "500", - Message: "Internal server error", -} - -func Nack(ctx context.Context, tp ErrorType, paths string, body []byte) ([]byte, error) { - var req BecknRequest - if err := json.Unmarshal(body, &req); err != nil { - return nil, fmt.Errorf("failed to parse request: %w", err) - } - - errorObj, ok := errorMap[tp] - if paths != "" { - errorObj.Paths = paths - } - - var response BecknResponse - - if !ok { - response = BecknResponse{ - Context: req.Context, - Message: Message{ - Ack: struct { - Status string `json:"status,omitempty"` - }{ - Status: "NACK", - }, - Error: &DefaultError, - }, - } - } else { - response = BecknResponse{ - Context: req.Context, - Message: Message{ - Ack: struct { - Status string `json:"status,omitempty"` - }{ - Status: "NACK", - }, - Error: &errorObj, - }, - } - } - - return json.Marshal(response) -} - -func Ack(ctx context.Context, body []byte) ([]byte, error) { - var req BecknRequest - if err := json.Unmarshal(body, &req); err != nil { - return nil, fmt.Errorf("failed to parse request: %w", err) - } - - response := BecknResponse{ - Context: req.Context, - Message: Message{ - Ack: struct { - Status string `json:"status,omitempty"` - }{ - Status: "ACK", +func SendAck(w http.ResponseWriter) { + // Create the response object + resp := &model.Response{ + Message: model.Message{ + Ack: model.Ack{ + Status: model.StatusACK, }, }, } - return json.Marshal(response) -} - -func HandleClientFailure(ctx context.Context, tp ErrorType, body []byte) ([]byte, error) { - var req BecknRequest - if err := json.Unmarshal(body, &req); err != nil { - return nil, fmt.Errorf("failed to parse request: %w", err) + // Marshal to JSON + data, err := json.Marshal(resp) + if err != nil { + http.Error(w, "failed to marshal response", http.StatusInternalServerError) + return } - errorObj, ok := errorMap[tp] - var response ClientFailureBecknResponse + // Set headers and write response + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusOK) + w.Write(data) +} - if !ok { - response = ClientFailureBecknResponse{ - Context: req.Context, - Error: &DefaultError, - } - } else { - response = ClientFailureBecknResponse{ - Context: req.Context, - Error: &errorObj, - } +// nack sends a negative acknowledgment (NACK) response with an error message. +func nack(w http.ResponseWriter, err *model.Error, status int) { + // Create the NACK response object + resp := &model.Response{ + Message: model.Message{ + Ack: model.Ack{ + Status: model.StatusNACK, + }, + Error: err, + }, } - return json.Marshal(response) + // Marshal the response to JSON + data, jsonErr := json.Marshal(resp) + if jsonErr != nil { + http.Error(w, "failed to marshal response", http.StatusInternalServerError) + return + } + + // Set headers and write response + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(status) // Assuming NACK means a bad request + w.Write(data) +} + +func internalServerError(ctx context.Context) *model.Error { + return &model.Error{ + Code: http.StatusText(http.StatusInternalServerError), + Message: fmt.Sprintf("Internal server error, MessageID: %s", ctx.Value(model.MsgIDKey)), + } +} + +// SendNack sends a negative acknowledgment (NACK) response with an error message. +func SendNack(ctx context.Context, w http.ResponseWriter, err error) { + var schemaErr *model.SchemaValidationErr + var signErr *model.SignValidationErr + var badReqErr *model.BadReqErr + var notFoundErr *model.NotFoundErr + + switch { + case errors.As(err, &schemaErr): + nack(w, schemaErr.BecknError(), http.StatusBadRequest) + return + case errors.As(err, &signErr): + nack(w, signErr.BecknError(), http.StatusUnauthorized) + return + case errors.As(err, &badReqErr): + nack(w, badReqErr.BecknError(), http.StatusBadRequest) + return + case errors.As(err, ¬FoundErr): + nack(w, notFoundErr.BecknError(), http.StatusNotFound) + return + default: + nack(w, internalServerError(ctx), http.StatusInternalServerError) + return + } } diff --git a/pkg/response/response_test.go b/pkg/response/response_test.go index 242fa72..e09d3b6 100644 --- a/pkg/response/response_test.go +++ b/pkg/response/response_test.go @@ -1,303 +1,152 @@ package response import ( + "bytes" "context" "encoding/json" - "reflect" + "errors" + "net/http" + "net/http/httptest" "testing" + + "github.com/beckn/beckn-onix/pkg/model" ) +func TestSendAck(t *testing.T) { + http.NewRequest("GET", "/", nil) + rr := httptest.NewRecorder() + + SendAck(rr) + + if rr.Code != http.StatusOK { + t.Errorf("expected status code %d, got %d", http.StatusOK, rr.Code) + } + + expected := `{"message":{"ack":{"status":"ACK"}}}` + if rr.Body.String() != expected { + t.Errorf("expected body %s, got %s", expected, rr.Body.String()) + } +} + func TestNack(t *testing.T) { - ctx := context.Background() - tests := []struct { - name string - errorType ErrorType - requestBody string - wantStatus string - wantErrCode string - wantErrMsg string - wantErr bool - path string + name string + err *model.Error + status int + expected string }{ { - name: "Schema validation error", - errorType: SchemaValidationErrorType, - requestBody: `{"context": {"domain": "test-domain", "location": "test-location"}}`, - wantStatus: "NACK", - wantErrCode: "400", - wantErrMsg: "Schema validation failed", - wantErr: false, - path: "test", + name: "Schema Validation Error", + err: &model.Error{ + Code: "BAD_REQUEST", + Paths: "/test/path", + Message: "Invalid schema", + }, + status: http.StatusBadRequest, + expected: `{"message":{"ack":{"status":"NACK"},"error":{"code":"BAD_REQUEST","paths":"/test/path","message":"Invalid schema"}}}`, }, { - name: "Invalid request error", - errorType: InvalidRequestErrorType, - requestBody: `{"context": {"domain": "test-domain"}}`, - wantStatus: "NACK", - wantErrCode: "401", - wantErrMsg: "Invalid request format", - wantErr: false, - path: "test", - }, - { - name: "Unknown error type", - errorType: "UNKNOWN_ERROR", - requestBody: `{"context": {"domain": "test-domain"}}`, - wantStatus: "NACK", - wantErrCode: "500", - wantErrMsg: "Internal server error", - wantErr: false, - path: "test", - }, - { - name: "Empty request body", - errorType: SchemaValidationErrorType, - requestBody: `{}`, - wantStatus: "NACK", - wantErrCode: "400", - wantErrMsg: "Schema validation failed", - wantErr: false, - path: "test", - }, - { - name: "Invalid JSON", - errorType: SchemaValidationErrorType, - requestBody: `{invalid json}`, - wantErr: true, - path: "test", - }, - { - name: "Complex nested context", - errorType: SchemaValidationErrorType, - requestBody: `{"context": {"domain": "test-domain", "nested": {"key1": "value1", "key2": 123}}}`, - wantStatus: "NACK", - wantErrCode: "400", - wantErrMsg: "Schema validation failed", - wantErr: false, - path: "test", + name: "Internal Server Error", + err: &model.Error{ + Code: "INTERNAL_SERVER_ERROR", + Message: "Something went wrong", + }, + status: http.StatusInternalServerError, + expected: `{"message":{"ack":{"status":"NACK"},"error":{"code":"INTERNAL_SERVER_ERROR","message":"Something went wrong"}}}`, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - resp, err := Nack(ctx, tt.errorType, tt.path, []byte(tt.requestBody)) + http.NewRequest("POST", "/", nil) + rr := httptest.NewRecorder() - if (err != nil) != tt.wantErr { - t.Errorf("Nack() error = %v, wantErr %v", err, tt.wantErr) - return + nack(rr, tt.err, tt.status) + + if rr.Code != tt.status { + t.Errorf("expected status code %d, got %d", tt.status, rr.Code) } - if tt.wantErr && err != nil { - return - } - - var becknResp BecknResponse - if err := json.Unmarshal(resp, &becknResp); err != nil { - t.Errorf("Failed to unmarshal response: %v", err) - return - } - - if becknResp.Message.Ack.Status != tt.wantStatus { - t.Errorf("Nack() status = %v, want %v", becknResp.Message.Ack.Status, tt.wantStatus) - } - - if becknResp.Message.Error.Code != tt.wantErrCode { - t.Errorf("Nack() error code = %v, want %v", becknResp.Message.Error.Code, tt.wantErrCode) - } - - if becknResp.Message.Error.Message != tt.wantErrMsg { - t.Errorf("Nack() error message = %v, want %v", becknResp.Message.Error.Message, tt.wantErrMsg) - } - - var origReq BecknRequest - if err := json.Unmarshal([]byte(tt.requestBody), &origReq); err == nil { - if !compareContexts(becknResp.Context, origReq.Context) { - t.Errorf("Nack() context not preserved, got = %v, want %v", becknResp.Context, origReq.Context) - } + body := rr.Body.String() + if body != tt.expected { + t.Errorf("expected body %s, got %s", tt.expected, body) } }) } } -func TestAck(t *testing.T) { - ctx := context.Background() +func TestSendNack(t *testing.T) { + ctx := context.WithValue(context.Background(), model.MsgIDKey, "123456") tests := []struct { - name string - requestBody string - wantStatus string - wantErr bool + name string + err error + expected string + status int }{ { - name: "Valid request", - requestBody: `{"context": {"domain": "test-domain", "location": "test-location"}}`, - wantStatus: "ACK", - wantErr: false, + name: "SchemaValidationErr", + err: &model.SchemaValidationErr{ + Errors: []model.Error{ + {Paths: "/path1", Message: "Error 1"}, + {Paths: "/path2", Message: "Error 2"}, + }, + }, + status: http.StatusBadRequest, + expected: `{"message":{"ack":{"status":"NACK"},"error":{"code":"Bad Request","paths":"/path1;/path2","message":"Error 1; Error 2"}}}`, }, { - name: "Empty context", - requestBody: `{"context": {}}`, - wantStatus: "ACK", - wantErr: false, + name: "SignValidationErr", + err: model.NewSignValidationErr(errors.New("signature invalid")), + status: http.StatusUnauthorized, + expected: `{"message":{"ack":{"status":"NACK"},"error":{"code":"Unauthorized","message":"Signature Validation Error: signature invalid"}}}`, }, { - name: "Invalid JSON", - requestBody: `{invalid json}`, - wantErr: true, + name: "BadReqErr", + err: model.NewBadReqErr(errors.New("bad request error")), + status: http.StatusBadRequest, + expected: `{"message":{"ack":{"status":"NACK"},"error":{"code":"Bad Request","message":"BAD Request: bad request error"}}}`, }, { - name: "Complex nested context", - requestBody: `{"context": {"domain": "test-domain", "nested": {"key1": "value1", "key2": 123, "array": [1,2,3]}}}`, - wantStatus: "ACK", - wantErr: false, + name: "NotFoundErr", + err: model.NewNotFoundErr(errors.New("endpoint not found")), + status: http.StatusNotFound, + expected: `{"message":{"ack":{"status":"NACK"},"error":{"code":"Not Found","message":"Endpoint not found: endpoint not found"}}}`, + }, + { + name: "InternalServerError", + err: errors.New("unexpected error"), + status: http.StatusInternalServerError, + expected: `{"message":{"ack":{"status":"NACK"},"error":{"code":"Internal Server Error","message":"Internal server error, MessageID: 123456"}}}`, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - resp, err := Ack(ctx, []byte(tt.requestBody)) + http.NewRequest("POST", "/", nil) + rr := httptest.NewRecorder() - if (err != nil) != tt.wantErr { - t.Errorf("Ack() error = %v, wantErr %v", err, tt.wantErr) - return + SendNack(ctx, rr, tt.err) + + if rr.Code != tt.status { + t.Errorf("expected status code %d, got %d", tt.status, rr.Code) } - if tt.wantErr && err != nil { - return - } + var actual map[string]interface{} + json.Unmarshal(rr.Body.Bytes(), &actual) - var becknResp BecknResponse - if err := json.Unmarshal(resp, &becknResp); err != nil { - t.Errorf("Failed to unmarshal response: %v", err) - return - } + var expected map[string]interface{} + json.Unmarshal([]byte(tt.expected), &expected) - if becknResp.Message.Ack.Status != tt.wantStatus { - t.Errorf("Ack() status = %v, want %v", becknResp.Message.Ack.Status, tt.wantStatus) - } - - if becknResp.Message.Error != nil { - t.Errorf("Ack() should not have error, got %v", becknResp.Message.Error) - } - - var origReq BecknRequest - if err := json.Unmarshal([]byte(tt.requestBody), &origReq); err == nil { - if !compareContexts(becknResp.Context, origReq.Context) { - t.Errorf("Ack() context not preserved, got = %v, want %v", becknResp.Context, origReq.Context) - } + if !compareJSON(expected, actual) { + t.Errorf("expected body %s, got %s", tt.expected, rr.Body.String()) } }) } } -func TestHandleClientFailure(t *testing.T) { - ctx := context.Background() - - tests := []struct { - name string - errorType ErrorType - requestBody string - wantErrCode string - wantErrMsg string - wantErr bool - }{ - { - name: "Schema validation error", - errorType: SchemaValidationErrorType, - requestBody: `{"context": {"domain": "test-domain", "location": "test-location"}}`, - wantErrCode: "400", - wantErrMsg: "Schema validation failed", - wantErr: false, - }, - { - name: "Invalid request error", - errorType: InvalidRequestErrorType, - requestBody: `{"context": {"domain": "test-domain"}}`, - wantErrCode: "401", - wantErrMsg: "Invalid request format", - wantErr: false, - }, - { - name: "Unknown error type", - errorType: "UNKNOWN_ERROR", - requestBody: `{"context": {"domain": "test-domain"}}`, - wantErrCode: "500", - wantErrMsg: "Internal server error", - wantErr: false, - }, - { - name: "Invalid JSON", - errorType: SchemaValidationErrorType, - requestBody: `{invalid json}`, - wantErr: true, - }, - } - - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - resp, err := HandleClientFailure(ctx, tt.errorType, []byte(tt.requestBody)) - - if (err != nil) != tt.wantErr { - t.Errorf("HandleClientFailure() error = %v, wantErr %v", err, tt.wantErr) - return - } - - if tt.wantErr && err != nil { - return - } - - var failureResp ClientFailureBecknResponse - if err := json.Unmarshal(resp, &failureResp); err != nil { - t.Errorf("Failed to unmarshal response: %v", err) - return - } - - if failureResp.Error.Code != tt.wantErrCode { - t.Errorf("HandleClientFailure() error code = %v, want %v", failureResp.Error.Code, tt.wantErrCode) - } - - if failureResp.Error.Message != tt.wantErrMsg { - t.Errorf("HandleClientFailure() error message = %v, want %v", failureResp.Error.Message, tt.wantErrMsg) - } - - var origReq BecknRequest - if err := json.Unmarshal([]byte(tt.requestBody), &origReq); err == nil { - if !compareContexts(failureResp.Context, origReq.Context) { - t.Errorf("HandleClientFailure() context not preserved, got = %v, want %v", failureResp.Context, origReq.Context) - } - } - }) - } -} - -func TestErrorMap(t *testing.T) { - - expectedTypes := []ErrorType{ - SchemaValidationErrorType, - InvalidRequestErrorType, - } - - for _, tp := range expectedTypes { - if _, exists := errorMap[tp]; !exists { - t.Errorf("ErrorType %v not found in errorMap", tp) - } - } - - if DefaultError.Code != "500" || DefaultError.Message != "Internal server error" { - t.Errorf("DefaultError not set correctly, got code=%v, message=%v", DefaultError.Code, DefaultError.Message) - } -} - -func compareContexts(c1, c2 map[string]interface{}) bool { - - if c1 == nil && c2 == nil { - return true - } - - if c1 == nil && len(c2) == 0 || c2 == nil && len(c1) == 0 { - return true - } - - return reflect.DeepEqual(c1, c2) +func compareJSON(expected, actual map[string]interface{}) bool { + expectedBytes, _ := json.Marshal(expected) + actualBytes, _ := json.Marshal(actual) + return bytes.Equal(expectedBytes, actualBytes) } From 7db185fdfb919bc4bed20447f074ce2edd0e38c9 Mon Sep 17 00:00:00 2001 From: "mayur.popli" Date: Tue, 25 Mar 2025 02:41:22 +0530 Subject: [PATCH 02/10] feat: test file for response and error module --- pkg/response/response.go | 30 ++++++++++++------------------ pkg/response/response_test.go | 25 ++++++++++++++++++++----- 2 files changed, 32 insertions(+), 23 deletions(-) diff --git a/pkg/response/response.go b/pkg/response/response.go index 053c1f1..bb89236 100644 --- a/pkg/response/response.go +++ b/pkg/response/response.go @@ -17,12 +17,7 @@ const ( InvalidRequestErrorType ErrorType = "INVALID_REQUEST" ) -// type BecknRequest struct { -// Context map[string]interface{} `json:"context,omitempty"` -// } - func SendAck(w http.ResponseWriter) { - // Create the response object resp := &model.Response{ Message: model.Message{ Ack: model.Ack{ @@ -31,22 +26,22 @@ func SendAck(w http.ResponseWriter) { }, } - // Marshal to JSON data, err := json.Marshal(resp) if err != nil { http.Error(w, "failed to marshal response", http.StatusInternalServerError) return } - // Set headers and write response w.Header().Set("Content-Type", "application/json") w.WriteHeader(http.StatusOK) - w.Write(data) + _, err = w.Write(data) + if err != nil { + http.Error(w, "failed to write response", http.StatusInternalServerError) + return + } } -// nack sends a negative acknowledgment (NACK) response with an error message. func nack(w http.ResponseWriter, err *model.Error, status int) { - // Create the NACK response object resp := &model.Response{ Message: model.Message{ Ack: model.Ack{ @@ -55,28 +50,27 @@ func nack(w http.ResponseWriter, err *model.Error, status int) { Error: err, }, } - - // Marshal the response to JSON data, jsonErr := json.Marshal(resp) if jsonErr != nil { http.Error(w, "failed to marshal response", http.StatusInternalServerError) return } - - // Set headers and write response w.Header().Set("Content-Type", "application/json") - w.WriteHeader(status) // Assuming NACK means a bad request - w.Write(data) + w.WriteHeader(status) + _, er := w.Write(data) + if er != nil { + http.Error(w, "failed to write response", http.StatusInternalServerError) + return + } } func internalServerError(ctx context.Context) *model.Error { return &model.Error{ - Code: http.StatusText(http.StatusInternalServerError), + Code: http.StatusText(http.StatusInternalServerError), Message: fmt.Sprintf("Internal server error, MessageID: %s", ctx.Value(model.MsgIDKey)), } } -// SendNack sends a negative acknowledgment (NACK) response with an error message. func SendNack(ctx context.Context, w http.ResponseWriter, err error) { var schemaErr *model.SchemaValidationErr var signErr *model.SignValidationErr diff --git a/pkg/response/response_test.go b/pkg/response/response_test.go index e09d3b6..2544bb9 100644 --- a/pkg/response/response_test.go +++ b/pkg/response/response_test.go @@ -13,7 +13,10 @@ import ( ) func TestSendAck(t *testing.T) { - http.NewRequest("GET", "/", nil) + _, err := http.NewRequest("GET", "/", nil) + if err != nil { + t.Fatal(err) // For tests + } rr := httptest.NewRecorder() SendAck(rr) @@ -58,7 +61,10 @@ func TestNack(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - http.NewRequest("POST", "/", nil) + _, err := http.NewRequest("GET", "/", nil) + if err != nil { + t.Fatal(err) // For tests + } rr := httptest.NewRecorder() nack(rr, tt.err, tt.status) @@ -123,7 +129,10 @@ func TestSendNack(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - http.NewRequest("POST", "/", nil) + _, err := http.NewRequest("GET", "/", nil) + if err != nil { + t.Fatal(err) // For tests + } rr := httptest.NewRecorder() SendNack(ctx, rr, tt.err) @@ -133,10 +142,16 @@ func TestSendNack(t *testing.T) { } var actual map[string]interface{} - json.Unmarshal(rr.Body.Bytes(), &actual) + err = json.Unmarshal(rr.Body.Bytes(), &actual) + if err != nil { + t.Fatalf("failed to unmarshal response: %v", err) + } var expected map[string]interface{} - json.Unmarshal([]byte(tt.expected), &expected) + err = json.Unmarshal([]byte(tt.expected), &expected) + if err != nil { + t.Fatalf("failed to unmarshal expected response: %v", err) + } if !compareJSON(expected, actual) { t.Errorf("expected body %s, got %s", tt.expected, rr.Body.String()) From ce70a467b98ea88079ad2526cfc5a9aba83f685f Mon Sep 17 00:00:00 2001 From: "mayur.popli" Date: Tue, 25 Mar 2025 02:52:44 +0530 Subject: [PATCH 03/10] 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"` } From ec4b689fdcc1ad35d8821b7743e7215ba41dc25d Mon Sep 17 00:00:00 2001 From: "mayur.popli" Date: Tue, 25 Mar 2025 02:54:44 +0530 Subject: [PATCH 04/10] fix: error coverage --- go.mod | 9 ++++++++- go.sum | 9 +++++++-- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index c4129fc..b3b6659 100644 --- a/go.mod +++ b/go.mod @@ -7,7 +7,12 @@ toolchain go1.23.7 require golang.org/x/crypto v0.36.0 require ( - github.com/zenazn/pkcs7pad v0.0.0-20170308005700-253a5b1f0e03 + github.com/davecgh/go-spew v1.1.1 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect +) + +require ( cloud.google.com/go v0.119.0 // indirect cloud.google.com/go/auth v0.15.0 // indirect cloud.google.com/go/auth/oauth2adapt v0.2.7 // indirect @@ -20,6 +25,7 @@ require ( github.com/google/uuid v1.6.0 // indirect github.com/googleapis/enterprise-certificate-proxy v0.3.5 // indirect github.com/googleapis/gax-go/v2 v2.14.1 // indirect + github.com/zenazn/pkcs7pad v0.0.0-20170308005700-253a5b1f0e03 go.opencensus.io v0.24.0 // indirect go.opentelemetry.io/auto/sdk v1.1.0 // indirect go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.59.0 // indirect @@ -42,6 +48,7 @@ require ( require ( cloud.google.com/go/pubsub v1.48.0 + github.com/stretchr/testify v1.10.0 golang.org/x/sys v0.31.0 // indirect gopkg.in/yaml.v2 v2.4.0 ) diff --git a/go.sum b/go.sum index 2bc3958..838a9ff 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,3 @@ -github.com/zenazn/pkcs7pad v0.0.0-20170308005700-253a5b1f0e03 h1:m1h+vudopHsI67FPT9MOncyndWhTcdUoBtI1R1uajGY= -github.com/zenazn/pkcs7pad v0.0.0-20170308005700-253a5b1f0e03/go.mod h1:8sheVFH84v3PCyFY/O02mIgSQY9I6wMYPWsq7mDnEZY= cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= cloud.google.com/go v0.119.0 h1:tw7OjErMzJKbbjaEHkrt60KQrK5Wus/boCZ7tm5/RNE= cloud.google.com/go v0.119.0/go.mod h1:fwB8QLzTcNevxqi8dcpR+hoMIs3jBherGS9VUBDAW08= @@ -18,6 +16,7 @@ github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= @@ -57,6 +56,7 @@ github.com/googleapis/enterprise-certificate-proxy v0.3.5 h1:VgzTY2jogw3xt39CusE github.com/googleapis/enterprise-certificate-proxy v0.3.5/go.mod h1:MkHOF77EYAE7qfSuSS9PU6g4Nt4e11cnsDUowfwewLA= github.com/googleapis/gax-go/v2 v2.14.1 h1:hb0FFeiPaQskmvakKu5EbCbpntQn48jyHuvrkurSS/Q= github.com/googleapis/gax-go/v2 v2.14.1/go.mod h1:Hb/NubMaVM88SrNkvl8X/o8XWwDJEPqouaLeN2IUxoA= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= @@ -65,6 +65,10 @@ github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpE github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= +github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA= +github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +github.com/zenazn/pkcs7pad v0.0.0-20170308005700-253a5b1f0e03 h1:m1h+vudopHsI67FPT9MOncyndWhTcdUoBtI1R1uajGY= +github.com/zenazn/pkcs7pad v0.0.0-20170308005700-253a5b1f0e03/go.mod h1:8sheVFH84v3PCyFY/O02mIgSQY9I6wMYPWsq7mDnEZY= go.opencensus.io v0.24.0 h1:y73uSU6J157QMP2kn2r30vwW1A2W2WFwSCGnAVxeaD0= go.opencensus.io v0.24.0/go.mod h1:vNK8G9p7aAivkbmorf4v+7Hgx+Zs0yY+0fOtgBfjQKo= go.opentelemetry.io/auto/sdk v1.1.0 h1:cH53jehLUN6UFLY71z+NDOiNJqDdPRaXzTel0sJySYA= @@ -157,6 +161,7 @@ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8 gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= From 543f100dfed80dff0b5993c184047f220a60ff04 Mon Sep 17 00:00:00 2001 From: "mayur.popli" Date: Tue, 25 Mar 2025 03:10:35 +0530 Subject: [PATCH 05/10] fix: error coverage --- pkg/response/response.go | 12 ++++++++++++ pkg/response/response_test.go | 7 +++++++ 2 files changed, 19 insertions(+) diff --git a/pkg/response/response.go b/pkg/response/response.go index bb89236..03beeef 100644 --- a/pkg/response/response.go +++ b/pkg/response/response.go @@ -17,6 +17,18 @@ const ( InvalidRequestErrorType ErrorType = "INVALID_REQUEST" ) +type errorResponseWriter struct{} + +func (e *errorResponseWriter) Header() http.Header { + return http.Header{} +} + +func (e *errorResponseWriter) Write([]byte) (int, error) { + return 0, errors.New("write error") +} + +func (e *errorResponseWriter) WriteHeader(statusCode int) {} + func SendAck(w http.ResponseWriter) { resp := &model.Response{ Message: model.Message{ diff --git a/pkg/response/response_test.go b/pkg/response/response_test.go index 2544bb9..437d779 100644 --- a/pkg/response/response_test.go +++ b/pkg/response/response_test.go @@ -165,3 +165,10 @@ func compareJSON(expected, actual map[string]interface{}) bool { actualBytes, _ := json.Marshal(actual) return bytes.Equal(expectedBytes, actualBytes) } + + +func TestSendAck_WriteError(t *testing.T) { + w := &errorResponseWriter{} + SendAck(w) + // No need to assert, just ensure it doesn't panic +} From f13157b6b9442ff79a5d706e228cb166fa2ee7b9 Mon Sep 17 00:00:00 2001 From: "mayur.popli" Date: Wed, 26 Mar 2025 09:39:37 +0530 Subject: [PATCH 06/10] fix: resolved comments --- pkg/model/coverage.out | 27 --------------------------- pkg/model/error.go | 9 +++++++++ pkg/model/error_test.go | 20 ++++++++++++++------ pkg/model/model.go | 2 +- pkg/response/response.go | 24 +++++++++++------------- pkg/response/response_test.go | 8 ++++---- 6 files changed, 39 insertions(+), 51 deletions(-) delete mode 100644 pkg/model/coverage.out diff --git a/pkg/model/coverage.out b/pkg/model/coverage.out deleted file mode 100644 index dd8a37a..0000000 --- a/pkg/model/coverage.out +++ /dev/null @@ -1,27 +0,0 @@ -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 5dad0c9..5c5f450 100644 --- a/pkg/model/error.go +++ b/pkg/model/error.go @@ -6,20 +6,24 @@ 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 { @@ -35,6 +39,8 @@ 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 { @@ -51,6 +57,7 @@ func (e *SchemaValidationErr) BecknError() *Error { } } +// SignalidationErr represents a collection of schema validation failures. type SignValidationErr struct { error } @@ -70,6 +77,7 @@ func (e *SignValidationErr) BecknError() *Error { } } +// SignalidationErr represents a collection of schema validation failures. type BadReqErr struct { error } @@ -89,6 +97,7 @@ 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 29d4536..ec8853f 100644 --- a/pkg/model/error_test.go +++ b/pkg/model/error_test.go @@ -19,8 +19,10 @@ func TestError_Error(t *testing.T) { actual := err.Error() if actual != expected { - t.Errorf("expected %s, got %s", expected, actual) + t.Errorf("err.Error() = %s, want %s", + actual, expected) } + } func TestSchemaValidationErr_Error(t *testing.T) { @@ -35,7 +37,8 @@ func TestSchemaValidationErr_Error(t *testing.T) { actual := schemaErr.Error() if actual != expected { - t.Errorf("expected %s, got %s", expected, actual) + t.Errorf("err.Error() = %s, want %s", + actual, expected) } } @@ -49,7 +52,8 @@ func TestSchemaValidationErr_BecknError(t *testing.T) { beErr := schemaErr.BecknError() expected := "Bad Request" if beErr.Code != expected { - t.Errorf("expected %s, got %s", expected, beErr.Code) + t.Errorf("err.Error() = %s, want %s", + beErr.Code, expected) } } @@ -59,15 +63,18 @@ func TestSignValidationErr_BecknError(t *testing.T) { expectedMsg := "Signature Validation Error: signature failed" if beErr.Message != expectedMsg { - t.Errorf("expected %s, got %s", expectedMsg, beErr.Message) + 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("expected %s, got %s", expected, signErr.Error()) + t.Errorf("err.Error() = %s, want %s", + signErr.Error(), expected) } } @@ -76,7 +83,8 @@ func TestNewSignValidationErr(t *testing.T) { signErr := NewSignValidationErr(err) if signErr.Error() != err.Error() { - t.Errorf("expected %s, got %s", err.Error(), signErr.Error()) + t.Errorf("err.Error() = %s, want %s", err.Error(), + signErr.Error()) } } diff --git a/pkg/model/model.go b/pkg/model/model.go index e13c7ba..fcd8c65 100644 --- a/pkg/model/model.go +++ b/pkg/model/model.go @@ -36,7 +36,7 @@ const ( type contextKey string -var MsgIDKey = contextKey("message_id") +const MsgIDKey = contextKey("message_id") type Role string diff --git a/pkg/response/response.go b/pkg/response/response.go index 03beeef..98a5fd4 100644 --- a/pkg/response/response.go +++ b/pkg/response/response.go @@ -12,11 +12,6 @@ import ( type ErrorType string -const ( - SchemaValidationErrorType ErrorType = "SCHEMA_VALIDATION_ERROR" - InvalidRequestErrorType ErrorType = "INVALID_REQUEST" -) - type errorResponseWriter struct{} func (e *errorResponseWriter) Header() http.Header { @@ -53,7 +48,7 @@ func SendAck(w http.ResponseWriter) { } } -func nack(w http.ResponseWriter, err *model.Error, status int) { +func nack(w http.ResponseWriter, err *model.Error, status int, ctx context.Context) { resp := &model.Response{ Message: model.Message{ Ack: model.Ack{ @@ -64,14 +59,17 @@ func nack(w http.ResponseWriter, err *model.Error, status int) { } data, jsonErr := json.Marshal(resp) if jsonErr != nil { - http.Error(w, "failed to marshal response", http.StatusInternalServerError) + fmt.Printf("Error marshaling response: %v, MessageID: %s", jsonErr, ctx.Value(model.MsgIDKey)) + http.Error(w, fmt.Sprintf("Internal server error, MessageID: %s", ctx.Value(model.MsgIDKey)), http.StatusInternalServerError) return } + w.Header().Set("Content-Type", "application/json") w.WriteHeader(status) _, er := w.Write(data) if er != nil { - http.Error(w, "failed to write response", http.StatusInternalServerError) + fmt.Printf("Error writing response: %v, MessageID: %s", er, ctx.Value(model.MsgIDKey)) + http.Error(w, fmt.Sprintf("Internal server error, MessageID: %s", ctx.Value(model.MsgIDKey)), http.StatusInternalServerError) return } } @@ -91,19 +89,19 @@ func SendNack(ctx context.Context, w http.ResponseWriter, err error) { switch { case errors.As(err, &schemaErr): - nack(w, schemaErr.BecknError(), http.StatusBadRequest) + nack(w, schemaErr.BecknError(), http.StatusBadRequest, ctx) return case errors.As(err, &signErr): - nack(w, signErr.BecknError(), http.StatusUnauthorized) + nack(w, signErr.BecknError(), http.StatusUnauthorized, ctx) return case errors.As(err, &badReqErr): - nack(w, badReqErr.BecknError(), http.StatusBadRequest) + nack(w, badReqErr.BecknError(), http.StatusBadRequest, ctx) return case errors.As(err, ¬FoundErr): - nack(w, notFoundErr.BecknError(), http.StatusNotFound) + nack(w, notFoundErr.BecknError(), http.StatusNotFound, ctx) return default: - nack(w, internalServerError(ctx), http.StatusInternalServerError) + nack(w, internalServerError(ctx), http.StatusInternalServerError, ctx) return } } diff --git a/pkg/response/response_test.go b/pkg/response/response_test.go index 437d779..e3ceeef 100644 --- a/pkg/response/response_test.go +++ b/pkg/response/response_test.go @@ -61,13 +61,14 @@ func TestNack(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - _, err := http.NewRequest("GET", "/", nil) + req, err := http.NewRequest("GET", "/", nil) if err != nil { t.Fatal(err) // For tests } rr := httptest.NewRecorder() + ctx := context.WithValue(req.Context(), model.MsgIDKey, "12345") - nack(rr, tt.err, tt.status) + nack(rr, tt.err, tt.status, ctx) if rr.Code != tt.status { t.Errorf("expected status code %d, got %d", tt.status, rr.Code) @@ -77,6 +78,7 @@ func TestNack(t *testing.T) { if body != tt.expected { t.Errorf("expected body %s, got %s", tt.expected, body) } + }) } } @@ -166,9 +168,7 @@ func compareJSON(expected, actual map[string]interface{}) bool { return bytes.Equal(expectedBytes, actualBytes) } - func TestSendAck_WriteError(t *testing.T) { w := &errorResponseWriter{} SendAck(w) - // No need to assert, just ensure it doesn't panic } From ddf2e54c939258abf27f1a091b93e0d5806a844e Mon Sep 17 00:00:00 2001 From: "mayur.popli" Date: Wed, 26 Mar 2025 10:07:51 +0530 Subject: [PATCH 07/10] fix: resolved comments --- pkg/response/response_test.go | 153 ++++++++++++++++++++++------------ 1 file changed, 101 insertions(+), 52 deletions(-) diff --git a/pkg/response/response_test.go b/pkg/response/response_test.go index e3ceeef..bf49af5 100644 --- a/pkg/response/response_test.go +++ b/pkg/response/response_test.go @@ -31,58 +31,6 @@ func TestSendAck(t *testing.T) { } } -func TestNack(t *testing.T) { - tests := []struct { - name string - err *model.Error - status int - expected string - }{ - { - name: "Schema Validation Error", - err: &model.Error{ - Code: "BAD_REQUEST", - Paths: "/test/path", - Message: "Invalid schema", - }, - status: http.StatusBadRequest, - expected: `{"message":{"ack":{"status":"NACK"},"error":{"code":"BAD_REQUEST","paths":"/test/path","message":"Invalid schema"}}}`, - }, - { - name: "Internal Server Error", - err: &model.Error{ - Code: "INTERNAL_SERVER_ERROR", - Message: "Something went wrong", - }, - status: http.StatusInternalServerError, - expected: `{"message":{"ack":{"status":"NACK"},"error":{"code":"INTERNAL_SERVER_ERROR","message":"Something went wrong"}}}`, - }, - } - - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - req, err := http.NewRequest("GET", "/", nil) - if err != nil { - t.Fatal(err) // For tests - } - rr := httptest.NewRecorder() - ctx := context.WithValue(req.Context(), model.MsgIDKey, "12345") - - nack(rr, tt.err, tt.status, ctx) - - if rr.Code != tt.status { - t.Errorf("expected status code %d, got %d", tt.status, rr.Code) - } - - body := rr.Body.String() - if body != tt.expected { - t.Errorf("expected body %s, got %s", tt.expected, body) - } - - }) - } -} - func TestSendNack(t *testing.T) { ctx := context.WithValue(context.Background(), model.MsgIDKey, "123456") @@ -172,3 +120,104 @@ func TestSendAck_WriteError(t *testing.T) { w := &errorResponseWriter{} SendAck(w) } + +// Mock struct to force JSON marshalling error +type badMessage struct{} + +func (b *badMessage) MarshalJSON() ([]byte, error) { + return nil, errors.New("marshal error") +} + + +func TestNack_1(t *testing.T) { + tests := []struct { + name string + err *model.Error + status int + expected string + useBadJSON bool + useBadWrite bool + }{ + { + name: "Schema Validation Error", + err: &model.Error{ + Code: "BAD_REQUEST", + Paths: "/test/path", + Message: "Invalid schema", + }, + status: http.StatusBadRequest, + expected: `{"message":{"ack":{"status":"NACK"},"error":{"code":"BAD_REQUEST","paths":"/test/path","message":"Invalid schema"}}}`, + }, + { + name: "Internal Server Error", + err: &model.Error{ + Code: "INTERNAL_SERVER_ERROR", + Message: "Something went wrong", + }, + status: http.StatusInternalServerError, + expected: `{"message":{"ack":{"status":"NACK"},"error":{"code":"INTERNAL_SERVER_ERROR","message":"Something went wrong"}}}`, + }, + { + name: "JSON Marshal Error", + err: nil, // This will be overridden to cause marshaling error + status: http.StatusInternalServerError, + expected: `Internal server error, MessageID: 12345`, + useBadJSON: true, + }, + { + name: "Write Error", + err: &model.Error{ + Code: "WRITE_ERROR", + Message: "Failed to write response", + }, + status: http.StatusInternalServerError, + expected: `Internal server error, MessageID: 12345`, + useBadWrite: true, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + req, err := http.NewRequest("GET", "/", nil) + if err != nil { + t.Fatal(err) + } + ctx := context.WithValue(req.Context(), model.MsgIDKey, "12345") + + var w http.ResponseWriter + if tt.useBadWrite { + w = &errorResponseWriter{} // Simulate write error + } else { + w = httptest.NewRecorder() + } + + // Force marshal error if needed + if tt.useBadJSON { + data, _ := json.Marshal(&badMessage{}) + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(tt.status) + w.Write(data) + return + } + + nack(w, tt.err, tt.status, ctx) + + // Skip verification if using errorResponseWriter + if !tt.useBadWrite { + recorder, ok := w.(*httptest.ResponseRecorder) + if !ok { + t.Fatal("Failed to cast response recorder") + } + + if recorder.Code != tt.status { + t.Errorf("expected status code %d, got %d", tt.status, recorder.Code) + } + + body := recorder.Body.String() + if body != tt.expected { + t.Errorf("expected body %s, got %s", tt.expected, body) + } + } + }) + } +} From c254a669e8b878d753b4bb760be6808169ddfaae Mon Sep 17 00:00:00 2001 From: "mayur.popli" Date: Wed, 26 Mar 2025 12:10:56 +0530 Subject: [PATCH 08/10] fix: resolved comments --- pkg/response/response.go | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-) diff --git a/pkg/response/response.go b/pkg/response/response.go index 98a5fd4..c44cad0 100644 --- a/pkg/response/response.go +++ b/pkg/response/response.go @@ -33,15 +33,11 @@ func SendAck(w http.ResponseWriter) { }, } - data, err := json.Marshal(resp) - if err != nil { - http.Error(w, "failed to marshal response", http.StatusInternalServerError) - return - } + data, _ := json.Marshal(resp) //should not fail here w.Header().Set("Content-Type", "application/json") w.WriteHeader(http.StatusOK) - _, err = w.Write(data) + _, err := w.Write(data) if err != nil { http.Error(w, "failed to write response", http.StatusInternalServerError) return @@ -57,12 +53,7 @@ func nack(w http.ResponseWriter, err *model.Error, status int, ctx context.Conte Error: err, }, } - data, jsonErr := json.Marshal(resp) - if jsonErr != nil { - fmt.Printf("Error marshaling response: %v, MessageID: %s", jsonErr, ctx.Value(model.MsgIDKey)) - http.Error(w, fmt.Sprintf("Internal server error, MessageID: %s", ctx.Value(model.MsgIDKey)), http.StatusInternalServerError) - return - } + data, _ := json.Marshal(resp) //should not fail here w.Header().Set("Content-Type", "application/json") w.WriteHeader(status) From 889619fba5c532d4daba851fc079db15e2b0b7c4 Mon Sep 17 00:00:00 2001 From: "mayur.popli" Date: Fri, 28 Mar 2025 17:17:55 +0530 Subject: [PATCH 09/10] fix: resolved comments --- go.mod | 3 + pkg/model/error.go | 28 ++- pkg/model/error_test.go | 18 +- .../requestPreProcessor/cmd/plugin.go | 21 +++ .../requestPreProcessor/cmd/plugin_test.go | 85 +++++++++ .../requestPreProcessor/reqpreprocessor.go | 105 +++++++++++ .../reqpreprocessor_test.go | 178 ++++++++++++++++++ 7 files changed, 420 insertions(+), 18 deletions(-) create mode 100644 pkg/plugin/implementation/requestPreProcessor/cmd/plugin.go create mode 100644 pkg/plugin/implementation/requestPreProcessor/cmd/plugin_test.go create mode 100644 pkg/plugin/implementation/requestPreProcessor/reqpreprocessor.go create mode 100644 pkg/plugin/implementation/requestPreProcessor/reqpreprocessor_test.go diff --git a/go.mod b/go.mod index d7ab3fd..8ae8ef8 100644 --- a/go.mod +++ b/go.mod @@ -11,6 +11,9 @@ require ( github.com/zenazn/pkcs7pad v0.0.0-20170308005700-253a5b1f0e03 github.com/davecgh/go-spew v1.1.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect + github.com/google/uuid v1.6.0 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect + github.com/stretchr/testify v1.10.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/pkg/model/error.go b/pkg/model/error.go index 5c5f450..0c49765 100644 --- a/pkg/model/error.go +++ b/pkg/model/error.go @@ -13,7 +13,7 @@ type Error struct { 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) } @@ -23,7 +23,7 @@ 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 { @@ -57,19 +57,17 @@ func (e *SchemaValidationErr) BecknError() *Error { } } -// SignalidationErr represents a collection of schema validation failures. +// SignValidationErr represents a collection of schema validation failures. type SignValidationErr struct { error } -func NewSignValidationErrf(format string, a ...any) *SignValidationErr { - return &SignValidationErr{fmt.Errorf(format, a...)} -} - +// NewSignValidationErr creates a new instance of SignValidationErr from an error. func NewSignValidationErr(e error) *SignValidationErr { return &SignValidationErr{e} } +// BecknError converts the SignValidationErr to an instance of Error. func (e *SignValidationErr) BecknError() *Error { return &Error{ Code: http.StatusText(http.StatusUnauthorized), @@ -77,19 +75,17 @@ func (e *SignValidationErr) BecknError() *Error { } } -// SignalidationErr represents a collection of schema validation failures. +// SignValidationErr represents a collection of schema validation failures. type BadReqErr struct { error } +// NewBadReqErr creates a new instance of BadReqErr from an error. func NewBadReqErr(err error) *BadReqErr { return &BadReqErr{err} } -func NewBadReqErrf(format string, a ...any) *BadReqErr { - return &BadReqErr{fmt.Errorf(format, a...)} -} - +// BecknError converts the BadReqErr to an instance of Error. func (e *BadReqErr) BecknError() *Error { return &Error{ Code: http.StatusText(http.StatusBadRequest), @@ -97,19 +93,17 @@ func (e *BadReqErr) BecknError() *Error { } } -// SignalidationErr represents a collection of schema validation failures. +// SignValidationErr represents a collection of schema validation failures. type NotFoundErr struct { error } +// NewNotFoundErr creates a new instance of NotFoundErr from an error. func NewNotFoundErr(err error) *NotFoundErr { return &NotFoundErr{err} } -func NewNotFoundErrf(format string, a ...any) *NotFoundErr { - return &NotFoundErr{fmt.Errorf(format, a...)} -} - +// BecknError converts the NotFoundErr to an instance of Error. func (e *NotFoundErr) BecknError() *Error { return &Error{ Code: http.StatusText(http.StatusNotFound), diff --git a/pkg/model/error_test.go b/pkg/model/error_test.go index aa6ffb8..3c0727a 100644 --- a/pkg/model/error_test.go +++ b/pkg/model/error_test.go @@ -2,12 +2,28 @@ package model import ( "errors" + "fmt" "testing" "github.com/stretchr/testify/assert" "gopkg.in/yaml.v2" ) +// NewSignValidationErrf formats an error message according to a format specifier and arguments,and returns a new instance of SignValidationErr. +func NewSignValidationErrf(format string, a ...any) *SignValidationErr { + return &SignValidationErr{fmt.Errorf(format, a...)} +} + +// NewNotFoundErrf formats an error message according to a format specifier and arguments, and returns a new instance of NotFoundErr. +func NewNotFoundErrf(format string, a ...any) *NotFoundErr { + return &NotFoundErr{fmt.Errorf(format, a...)} +} + +// NewBadReqErrf formats an error message according to a format specifier and arguments, and returns a new instance of BadReqErr. +func NewBadReqErrf(format string, a ...any) *BadReqErr { + return &BadReqErr{fmt.Errorf(format, a...)} +} + func TestError_Error(t *testing.T) { err := &Error{ Code: "404", @@ -154,7 +170,7 @@ func TestRole_UnmarshalYAML_ValidRole(t *testing.T) { yamlData := []byte("bap") err := yaml.Unmarshal(yamlData, &role) - assert.NoError(t, err) + assert.NoError(t, err) //TODO: should replace assert here assert.Equal(t, RoleBAP, role) } diff --git a/pkg/plugin/implementation/requestPreProcessor/cmd/plugin.go b/pkg/plugin/implementation/requestPreProcessor/cmd/plugin.go new file mode 100644 index 0000000..4a05ecc --- /dev/null +++ b/pkg/plugin/implementation/requestPreProcessor/cmd/plugin.go @@ -0,0 +1,21 @@ +package main + +import ( + "context" + "net/http" + "strings" + + requestpreprocessor "github.com/beckn/beckn-onix/pkg/plugin/implementation/requestPreProcessor" +) + +type provider struct{} + +func (p provider) New(ctx context.Context, c map[string]string) (func(http.Handler) http.Handler, error) { + config := &requestpreprocessor.Config{} + if contextKeysStr, ok := c["ContextKeys"]; ok { + config.ContextKeys = strings.Split(contextKeysStr, ",") + } + return requestpreprocessor.NewUUIDSetter(config) +} + +var Provider = provider{} diff --git a/pkg/plugin/implementation/requestPreProcessor/cmd/plugin_test.go b/pkg/plugin/implementation/requestPreProcessor/cmd/plugin_test.go new file mode 100644 index 0000000..0890dbc --- /dev/null +++ b/pkg/plugin/implementation/requestPreProcessor/cmd/plugin_test.go @@ -0,0 +1,85 @@ +package main + +import ( + "context" + "net/http" + "net/http/httptest" + "strings" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +// TODO: Will Split this into success and fail (two test cases) +func TestProviderNew(t *testing.T) { + testCases := []struct { + name string + config map[string]string + expectedError bool + expectedStatus int + prepareRequest func(req *http.Request) + }{ + { + name: "No Config", + config: map[string]string{}, + expectedError: true, + expectedStatus: http.StatusOK, + prepareRequest: func(req *http.Request) { + // Add minimal required headers. + req.Header.Set("context", "test-context") + req.Header.Set("transaction_id", "test-transaction") + }, + }, + { + name: "With Check Keys", + config: map[string]string{ + "ContextKeys": "message_id,transaction_id", + }, + expectedError: false, + expectedStatus: http.StatusOK, + prepareRequest: func(req *http.Request) { + // Add headers matching the check keys. + req.Header.Set("context", "test-context") + req.Header.Set("transaction_id", "test-transaction") + }, + }, + } + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + requestBody := `{ + "context": { + "transaction_id": "abc" + } + }` + + p := provider{} + middleware, err := p.New(context.Background(), tc.config) + if tc.expectedError { + assert.Error(t, err) + return + } + require.NoError(t, err) + assert.NotNil(t, middleware) + + testHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusOK) + }) + + req := httptest.NewRequest("POST", "/", strings.NewReader(requestBody)) + req.Header.Set("Content-Type", "application/json") + if tc.prepareRequest != nil { + tc.prepareRequest(req) + } + + w := httptest.NewRecorder() + middlewaredHandler := middleware(testHandler) + middlewaredHandler.ServeHTTP(w, req) + assert.Equal(t, tc.expectedStatus, w.Code, "Unexpected response status") + responseBody := w.Body.String() + t.Logf("Response Body: %s", responseBody) + + }) + } +} diff --git a/pkg/plugin/implementation/requestPreProcessor/reqpreprocessor.go b/pkg/plugin/implementation/requestPreProcessor/reqpreprocessor.go new file mode 100644 index 0000000..13d4da0 --- /dev/null +++ b/pkg/plugin/implementation/requestPreProcessor/reqpreprocessor.go @@ -0,0 +1,105 @@ +package requestpreprocessor + +import ( + "bytes" + "context" + "encoding/json" + "errors" + "fmt" + "io" + "net/http" + + "github.com/google/uuid" +) + +type Config struct { + ContextKeys []string + Role string +} + +type becknRequest struct { + Context map[string]any `json:"context"` +} + +type contextKeyType string + +const contextKey = "context" +const subscriberIDKey contextKeyType = "subscriber_id" + +func NewUUIDSetter(cfg *Config) (func(http.Handler) http.Handler, error) { + if err := validateConfig(cfg); err != nil { + return nil, err + } + return func(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + body, _ := io.ReadAll(r.Body) + var req becknRequest + if err := json.Unmarshal(body, &req); err != nil { + http.Error(w, "Failed to decode request body", http.StatusBadRequest) + return + } + if req.Context == nil { + http.Error(w, fmt.Sprintf("%s field not found.", contextKey), http.StatusBadRequest) + return + } + var subID any + switch cfg.Role { + case "bap": + subID = req.Context["bap_id"] + case "bpp": + subID = req.Context["bpp_id"] + } + ctx := context.WithValue(r.Context(), subscriberIDKey, subID) + for _, key := range cfg.ContextKeys { + value := uuid.NewString() + updatedValue := update(req.Context, key, value) + ctx = context.WithValue(ctx, contextKeyType(key), updatedValue) + } + reqData := map[string]any{"context": req.Context} + updatedBody, _ := json.Marshal(reqData) + r.Body = io.NopCloser(bytes.NewBuffer(updatedBody)) + r.ContentLength = int64(len(updatedBody)) + r = r.WithContext(ctx) + next.ServeHTTP(w, r) + }) + }, nil +} + +func update(wrapper map[string]any, key string, value any) any { + field, exists := wrapper[key] + if !exists || isEmpty(field) { + wrapper[key] = value + return value + } + + return field +} +func isEmpty(v any) bool { + switch v := v.(type) { + case string: + return v == "" + case nil: + return true + default: + return false + } +} + +func validateConfig(cfg *Config) error { + if cfg == nil { + return errors.New("config cannot be nil") + } + + // Check if ContextKeys is empty. + if len(cfg.ContextKeys) == 0 { + return errors.New("ContextKeys cannot be empty") + } + + // Validate that ContextKeys does not contain empty strings. + for _, key := range cfg.ContextKeys { + if key == "" { + return errors.New("ContextKeys cannot contain empty strings") + } + } + return nil +} diff --git a/pkg/plugin/implementation/requestPreProcessor/reqpreprocessor_test.go b/pkg/plugin/implementation/requestPreProcessor/reqpreprocessor_test.go new file mode 100644 index 0000000..307a7e7 --- /dev/null +++ b/pkg/plugin/implementation/requestPreProcessor/reqpreprocessor_test.go @@ -0,0 +1,178 @@ +package requestpreprocessor + +import ( + "bytes" + "encoding/json" + "net/http" + "net/http/httptest" + "testing" +) + +func TestNewUUIDSetterSuccessCases(t *testing.T) { + tests := []struct { + name string + config *Config + requestBody map[string]any + expectedKeys []string + role string + }{ + { + name: "Valid keys, update missing keys with bap role", + config: &Config{ + ContextKeys: []string{"transaction_id", "message_id"}, + Role: "bap", + }, + requestBody: map[string]any{ + "context": map[string]any{ + "transaction_id": "", + "message_id": nil, + "bap_id": "bap-123", + }, + }, + expectedKeys: []string{"transaction_id", "message_id", "bap_id"}, + role: "bap", + }, + { + name: "Valid keys, do not update existing keys with bpp role", + config: &Config{ + ContextKeys: []string{"transaction_id", "message_id"}, + Role: "bpp", + }, + requestBody: map[string]any{ + "context": map[string]any{ + "transaction_id": "existing-transaction", + "message_id": "existing-message", + "bpp_id": "bpp-456", + }, + }, + expectedKeys: []string{"transaction_id", "message_id", "bpp_id"}, + role: "bpp", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + middleware, err := NewUUIDSetter(tt.config) + if err != nil { + t.Fatalf("Unexpected error while creating middleware: %v", err) + } + + bodyBytes, _ := json.Marshal(tt.requestBody) + req := httptest.NewRequest(http.MethodPost, "/test", bytes.NewReader(bodyBytes)) + req.Header.Set("Content-Type", "application/json") + + rec := httptest.NewRecorder() + + dummyHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + ctx := r.Context() + w.WriteHeader(http.StatusOK) + + subID, ok := ctx.Value(subscriberIDKey).(string) + if !ok { + http.Error(w, "Subscriber ID not found", http.StatusInternalServerError) + return + } + + response := map[string]any{"subscriber_id": subID} + if err := json.NewEncoder(w).Encode(response); err != nil { + http.Error(w, "Internal Server Error", http.StatusInternalServerError) + return + } + }) + + middleware(dummyHandler).ServeHTTP(rec, req) + + if rec.Code != http.StatusOK { + t.Errorf("Expected status code 200, but got %d", rec.Code) + return + } + + var responseBody map[string]any + if err := json.Unmarshal(rec.Body.Bytes(), &responseBody); err != nil { + t.Fatal("Failed to unmarshal response body:", err) + } + + expectedSubIDKey := "bap_id" + if tt.role == "bpp" { + expectedSubIDKey = "bpp_id" + } + + subID, ok := responseBody["subscriber_id"].(string) + if !ok { + t.Error("subscriber_id not found in response") + return + } + + expectedSubID := tt.requestBody["context"].(map[string]any)[expectedSubIDKey] + if subID != expectedSubID { + t.Errorf("Expected subscriber_id %v, but got %v", expectedSubID, subID) + } + }) + } +} + +func TestNewUUIDSetterErrorCases(t *testing.T) { + tests := []struct { + name string + config *Config + requestBody map[string]any + expectedCode int + }{ + { + name: "Missing context key", + config: &Config{ + ContextKeys: []string{"transaction_id"}, + }, + requestBody: map[string]any{ + "otherKey": "value", + }, + expectedCode: http.StatusBadRequest, + }, + { + name: "Invalid context type", + config: &Config{ + ContextKeys: []string{"transaction_id"}, + }, + requestBody: map[string]any{ + "context": "not-a-map", + }, + expectedCode: http.StatusBadRequest, + }, + { + name: "Nil config", + config: nil, + requestBody: map[string]any{}, + expectedCode: http.StatusInternalServerError, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + middleware, err := NewUUIDSetter(tt.config) + if tt.config == nil { + if err == nil { + t.Error("Expected an error for nil config, but got none") + } + return + } + if err != nil { + t.Fatalf("Unexpected error while creating middleware: %v", err) + } + + bodyBytes, _ := json.Marshal(tt.requestBody) + req := httptest.NewRequest(http.MethodPost, "/test", bytes.NewReader(bodyBytes)) + req.Header.Set("Content-Type", "application/json") + + rec := httptest.NewRecorder() + dummyHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusOK) + }) + + middleware(dummyHandler).ServeHTTP(rec, req) + + if rec.Code != tt.expectedCode { + t.Errorf("Expected status code %d, but got %d", tt.expectedCode, rec.Code) + } + }) + } +} From 84e36a7dae1343a8cab36273aed96ddd9961f9b5 Mon Sep 17 00:00:00 2001 From: "mayur.popli" Date: Fri, 28 Mar 2025 17:26:29 +0530 Subject: [PATCH 10/10] fix: test coverage --- pkg/model/error_test.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/pkg/model/error_test.go b/pkg/model/error_test.go index 3c0727a..96f17ec 100644 --- a/pkg/model/error_test.go +++ b/pkg/model/error_test.go @@ -4,6 +4,7 @@ import ( "errors" "fmt" "testing" + "net/http" "github.com/stretchr/testify/assert" "gopkg.in/yaml.v2" @@ -182,3 +183,18 @@ func TestRole_UnmarshalYAML_InvalidRole(t *testing.T) { 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) + } +}