-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 <[email protected]>
- Loading branch information
Showing
2 changed files
with
97 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |