package bsl import ( "errors" "testing" "github.com/stretchr/testify/assert" ) func TestSetMessage(t *testing.T) { err := New("Invalid Credentials") assert.Equal(t, err.Message, "Invalid Credentials") } func TestSetStatusCode(t *testing.T) { err := New("").SetStatusCode(500) assert.Equal(t, err.StatusCode, 500) } func TestSetURL(t *testing.T) { err := New("").SetURL("https://ncr.com") assert.Equal(t, err.URL, "https://ncr.com") } func TestSetMethod(t *testing.T) { err := New("").SetMethod("POST") assert.Equal(t, err.Method, "POST") } func TestSetErrorType(t *testing.T) { err := New("").SetErrorType("com.ncr.nep.common.exception.InvalidCredentialsException") assert.Equal(t, err.ErrorType, "com.ncr.nep.common.exception.InvalidCredentialsException") } func TestSetVerbose(t *testing.T) { err := New("").SetVerbose("{\"details\":[\"USER_CREDENTIALS\",\"The user credentials are invalid\"],\"errorType\":\"com.ncr.nep.common.exception.InvalidCredentialsException\",\"message\":\"USER_CREDENTIALS - The user credentials are invalid\",\"statusCode\":401}") assert.Equal(t, err.Verbose, "{\"details\":[\"USER_CREDENTIALS\",\"The user credentials are invalid\"],\"errorType\":\"com.ncr.nep.common.exception.InvalidCredentialsException\",\"message\":\"USER_CREDENTIALS - The user credentials are invalid\",\"statusCode\":401}") } func TestError(t *testing.T) { err := New("Invalid Credentials").SetStatusCode(401).SetURL("https://ncr.com").SetMethod("POST").SetErrorType("com.ncr.nep.common.exception.InvalidCredentialsException").SetVerbose("{\"details\":[\"USER_CREDENTIALS\",\"The user credentials are invalid\"],\"errorType\":\"com.ncr.nep.common.exception.InvalidCredentialsException\",\"message\":\"USER_CREDENTIALS - The user credentials are invalid\",\"statusCode\":401}") assert.Equal(t, err.Error(), "Invalid Credentials") } func TestUnmarshalErrorResponse(t *testing.T) { err := New("").UnmarshalErrorResponse([]byte("{\"details\":[\"USER_CREDENTIALS\",\"The user credentials are invalid\"],\"errorType\":\"com.ncr.nep.common.exception.InvalidCredentialsException\",\"message\":\"USER_CREDENTIALS - The user credentials are invalid\",\"statusCode\":401}")) assert.Equal(t, err.Message, "Incorrect username, password, or organization.") assert.Equal(t, err.StatusCode, 401) assert.Equal(t, err.ErrorType, "com.ncr.nep.common.exception.InvalidCredentialsException") } func TestExtensions(t *testing.T) { err := New("Invalid Credentials").SetStatusCode(401).SetURL("https://ncr.com").SetMethod("POST").SetErrorType("com.ncr.nep.common.exception.InvalidCredentialsException").SetVerbose("{\"details\":[\"USER_CREDENTIALS\",\"The user credentials are invalid\"],\"errorType\":\"com.ncr.nep.common.exception.InvalidCredentialsException\",\"message\":\"USER_CREDENTIALS - The user credentials are invalid\",\"statusCode\":401}") assert.Len(t, err.Extensions(), 5) } func TestStripSymbols(t *testing.T) { before := "[The value '' is invalid for the path 'resetUrl': size must be between 5 and 1024]" expected := "The value '' is invalid for the path 'resetUrl': size must be between 5 and 1024" actual := stripSymbols(before) assert.Equal(t, expected, actual) } func TestIsErrorEqual(t *testing.T) { err1 := New("Invalid Credentials").SetStatusCode(401).SetURL("https://ncr.com").SetMethod("POST").SetErrorType("com.ncr.nep.common.exception.InvalidCredentialsException").SetVerbose("{\"details\":[\"USER_CREDENTIALS\",\"The user credentials are invalid\"],\"errorType\":\"com.ncr.nep.common.exception.InvalidCredentialsException\",\"message\":\"USER_CREDENTIALS - The user credentials are invalid\",\"statusCode\":401}") err2 := New("").SetMessage("Invalid Credentials").SetStatusCode(401).SetURL("https://ncr.com").SetMethod("POST").SetErrorType("com.ncr.nep.common.exception.InvalidCredentialsException") assert.True(t, err1.IsError(err2)) } func TestIsErrorNotEqual(t *testing.T) { err1 := New("Invalid Credentials").SetStatusCode(409).SetURL("https://ncr.com").SetMethod("POST").SetErrorType("com.ncr.nep.common.exception.InvalidCredentialsException").SetVerbose("{\"details\":[\"USER_CREDENTIALS\",\"The user credentials are invalid\"],\"errorType\":\"com.ncr.nep.common.exception.InvalidCredentialsException\",\"message\":\"USER_CREDENTIALS - The user credentials are invalid\",\"statusCode\":401}") err2 := New("Invalid Credentials").SetStatusCode(401).SetURL("https://google.com").SetMethod("GET").SetErrorType("com.ncr.nep.common.exception.InvalidCredentialsException") assert.False(t, err1.IsError(err2)) } func TestIsEqual(t *testing.T) { err1 := New("").SetMessage("Invalid Credentials").SetStatusCode(402).SetURL("https://ncr.com").SetMethod("POST").SetErrorType("com.ncr.nep.common.exception.InvalidCredentialsException").SetVerbose("{\"details\":[\"USER_CREDENTIALS\",\"The user credentials are invalid\"],\"errorType\":\"com.ncr.nep.common.exception.InvalidCredentialsException\",\"message\":\"USER_CREDENTIALS - The user credentials are invalid\",\"statusCode\":401}") err2 := errors.New("Invalid Credentials") assert.True(t, err1.Is(err2)) } func TestIsNotEqual(t *testing.T) { err1 := New("").SetMessage("Invalid Credentials").SetStatusCode(409).SetURL("https://ncr.com").SetMethod("POST").SetErrorType("com.ncr.nep.common.exception.InvalidCredentialsException").SetVerbose("{\"details\":[\"USER_CREDENTIALS\",\"The user credentials are invalid\"],\"errorType\":\"com.ncr.nep.common.exception.InvalidCredentialsException\",\"message\":\"USER_CREDENTIALS - The user credentials are invalid\",\"statusCode\":401}") err2 := errors.New("Internal Server Error") assert.False(t, err1.Is(err2)) }