Skip to content

Commit

Permalink
test(HMS-4622): Add builders for the tests
Browse files Browse the repository at this point in the history
Add NewErrorResponse and NewErrorInfo builders to
make easier the data creation for the tests.

Signed-off-by: Alejandro Visiedo <[email protected]>
  • Loading branch information
avisiedo committed Sep 19, 2024
1 parent 89f7c98 commit c9d568b
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 0 deletions.
68 changes: 68 additions & 0 deletions internal/test/builder/api/builder_error_info.go
Original file line number Diff line number Diff line change
@@ -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
}
29 changes: 29 additions & 0 deletions internal/test/builder/api/builder_error_response.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit c9d568b

Please sign in to comment.