1 package bsl
2
3 import (
4 "errors"
5 "testing"
6
7 "github.com/stretchr/testify/assert"
8 )
9
10 func TestSetMessage(t *testing.T) {
11 err := New("Invalid Credentials")
12 assert.Equal(t, err.Message, "Invalid Credentials")
13 }
14
15 func TestSetStatusCode(t *testing.T) {
16 err := New("").SetStatusCode(500)
17 assert.Equal(t, err.StatusCode, 500)
18 }
19
20 func TestSetURL(t *testing.T) {
21 err := New("").SetURL("https://ncr.com")
22 assert.Equal(t, err.URL, "https://ncr.com")
23 }
24
25 func TestSetMethod(t *testing.T) {
26 err := New("").SetMethod("POST")
27 assert.Equal(t, err.Method, "POST")
28 }
29
30 func TestSetErrorType(t *testing.T) {
31 err := New("").SetErrorType("com.ncr.nep.common.exception.InvalidCredentialsException")
32 assert.Equal(t, err.ErrorType, "com.ncr.nep.common.exception.InvalidCredentialsException")
33 }
34
35 func TestSetVerbose(t *testing.T) {
36 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}")
37 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}")
38 }
39
40 func TestError(t *testing.T) {
41 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}")
42 assert.Equal(t, err.Error(), "Invalid Credentials")
43 }
44
45 func TestUnmarshalErrorResponse(t *testing.T) {
46 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}"))
47 assert.Equal(t, err.Message, "Incorrect username, password, or organization.")
48 assert.Equal(t, err.StatusCode, 401)
49 assert.Equal(t, err.ErrorType, "com.ncr.nep.common.exception.InvalidCredentialsException")
50 }
51
52 func TestExtensions(t *testing.T) {
53 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}")
54 assert.Len(t, err.Extensions(), 5)
55 }
56
57 func TestStripSymbols(t *testing.T) {
58 before := "[The value '' is invalid for the path 'resetUrl': size must be between 5 and 1024]"
59 expected := "The value '' is invalid for the path 'resetUrl': size must be between 5 and 1024"
60 actual := stripSymbols(before)
61 assert.Equal(t, expected, actual)
62 }
63
64 func TestIsErrorEqual(t *testing.T) {
65 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}")
66 err2 := New("").SetMessage("Invalid Credentials").SetStatusCode(401).SetURL("https://ncr.com").SetMethod("POST").SetErrorType("com.ncr.nep.common.exception.InvalidCredentialsException")
67 assert.True(t, err1.IsError(err2))
68 }
69
70 func TestIsErrorNotEqual(t *testing.T) {
71 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}")
72 err2 := New("Invalid Credentials").SetStatusCode(401).SetURL("https://google.com").SetMethod("GET").SetErrorType("com.ncr.nep.common.exception.InvalidCredentialsException")
73 assert.False(t, err1.IsError(err2))
74 }
75
76 func TestIsEqual(t *testing.T) {
77 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}")
78 err2 := errors.New("Invalid Credentials")
79 assert.True(t, err1.Is(err2))
80 }
81
82 func TestIsNotEqual(t *testing.T) {
83 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}")
84 err2 := errors.New("Internal Server Error")
85 assert.False(t, err1.Is(err2))
86 }
87
View as plain text