From c9d568b86db10654ff61f797ce8967f79d0ec543 Mon Sep 17 00:00:00 2001 From: Alejandro Visiedo Date: Fri, 13 Sep 2024 14:04:21 +0200 Subject: [PATCH] test(HMS-4622): Add builders for the tests Add NewErrorResponse and NewErrorInfo builders to make easier the data creation for the tests. Signed-off-by: Alejandro Visiedo --- .../test/builder/api/builder_error_info.go | 68 +++++++++++++++++++ .../builder/api/builder_error_response.go | 29 ++++++++ 2 files changed, 97 insertions(+) create mode 100644 internal/test/builder/api/builder_error_info.go create mode 100644 internal/test/builder/api/builder_error_response.go diff --git a/internal/test/builder/api/builder_error_info.go b/internal/test/builder/api/builder_error_info.go new file mode 100644 index 00000000..65801b54 --- /dev/null +++ b/internal/test/builder/api/builder_error_info.go @@ -0,0 +1,68 @@ +package api + +import ( + "net/http" + "strconv" + + "github.com/openlyinc/pointy" + "github.com/podengo-project/idmsvc-backend/internal/api/public" +) + +type ErrorInfo interface { + Build() *public.ErrorInfo + WithCode(value string) ErrorInfo + WithDetail(value string) ErrorInfo + WithId(value string) ErrorInfo + WithStatus(value int) ErrorInfo + WithTitle(value string) ErrorInfo +} + +type errorInfo public.ErrorInfo + +// NewErrorInfo create new ErrorInfo builder +func NewErrorInfo(statusCode int) ErrorInfo { + return &errorInfo{ + Code: nil, + Detail: nil, + Id: "", + Status: strconv.Itoa(statusCode), + Title: http.StatusText(statusCode), + } +} + +func (b *errorInfo) Build() *public.ErrorInfo { + return (*public.ErrorInfo)(b) +} + +func (b *errorInfo) WithCode(value string) ErrorInfo { + if value == "" { + b.Code = nil + } else { + b.Code = pointy.String(value) + } + return b +} + +func (b *errorInfo) WithDetail(value string) ErrorInfo { + if value == "" { + b.Detail = nil + } else { + b.Detail = pointy.String(value) + } + return b +} + +func (b *errorInfo) WithId(value string) ErrorInfo { + b.Id = value + return b +} + +func (b *errorInfo) WithStatus(value int) ErrorInfo { + b.Status = strconv.Itoa(value) + return b +} + +func (b *errorInfo) WithTitle(value string) ErrorInfo { + b.Title = value + return b +} diff --git a/internal/test/builder/api/builder_error_response.go b/internal/test/builder/api/builder_error_response.go new file mode 100644 index 00000000..493db13a --- /dev/null +++ b/internal/test/builder/api/builder_error_response.go @@ -0,0 +1,29 @@ +package api + +import ( + "github.com/podengo-project/idmsvc-backend/internal/api/public" +) + +type ErrorResponse interface { + Build() *public.ErrorResponse + Add(err public.ErrorInfo) ErrorResponse +} + +type errorResponse public.ErrorResponse + +func NewErrorResponse() ErrorResponse { + return &errorResponse{} +} + +func (b *errorResponse) Build() *public.ErrorResponse { + return (*public.ErrorResponse)(b) +} + +func (b *errorResponse) Add(err public.ErrorInfo) ErrorResponse { + if b.Errors == nil { + errors := make([]public.ErrorInfo, 0, 8) + b.Errors = &errors + } + *b.Errors = append(*b.Errors, err) + return b +}