[RICPLT-1783] Add ValidateAndBuildRanLoadInformationKey | Fix dependencies | Reformat...
[ric-plt/nodeb-rnib.git] / common / rNibErrors_test.go
1 //
2 // Copyright 2019 AT&T Intellectual Property
3 // Copyright 2019 Nokia
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //      http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 package common
19
20 import (
21         "fmt"
22         "github.com/pkg/errors"
23         "github.com/stretchr/testify/assert"
24         "testing"
25 )
26
27 func TestNewResourceNotFoundError(t *testing.T){
28         e := errors.New("Expected error")
29         expectedErr := NewResourceNotFoundError(e)
30         assert.NotNil(t, expectedErr)
31         assert.Equal(t, RESOURCE_NOT_FOUND, expectedErr.GetCode())
32         assert.Contains(t, expectedErr.Error(), fmt.Sprintf("%d",expectedErr.GetCode()))
33         assert.Contains(t, expectedErr.Error(), rNibError_names[expectedErr.GetCode()])
34         assert.Contains(t, expectedErr.Error(), e.Error())
35         assert.Equal(t, RESOURCE_NOT_FOUND, expectedErr.GetCode())
36 }
37
38 func TestNewInternalError(t *testing.T){
39         e := errors.New("Expected error")
40         expectedErr := NewInternalError(e)
41         assert.NotNil(t, expectedErr)
42         assert.Equal(t, INTERNAL_ERROR, expectedErr.GetCode())
43         assert.Contains(t, expectedErr.Error(), fmt.Sprintf("%d",expectedErr.GetCode()))
44         assert.Contains(t, expectedErr.Error(), rNibError_names[expectedErr.GetCode()])
45         assert.Contains(t, expectedErr.Error(), e.Error())
46         assert.Equal(t, INTERNAL_ERROR, expectedErr.GetCode())
47 }
48
49 func TestNewValidationError(t *testing.T){
50         e := errors.New("Expected error")
51         expectedErr := NewValidationError(e)
52         assert.NotNil(t, expectedErr)
53         assert.Equal(t, VALIDATION_ERROR, expectedErr.GetCode())
54         assert.Contains(t, expectedErr.Error(), fmt.Sprintf("%d",expectedErr.GetCode()))
55         assert.Contains(t, expectedErr.Error(), rNibError_names[expectedErr.GetCode()])
56         assert.Contains(t, expectedErr.Error(), e.Error())
57         assert.Equal(t, VALIDATION_ERROR, expectedErr.GetCode())
58 }
59
60 func TestNewRNibErrorWithEmptyError(t *testing.T){
61         var e error
62         expectedErr := NewResourceNotFoundError(e)
63         assert.NotNil(t, expectedErr)
64         assert.Equal(t, RESOURCE_NOT_FOUND, expectedErr.GetCode())
65         assert.Contains(t, expectedErr.Error(), fmt.Sprintf("%d",expectedErr.GetCode()))
66         assert.Contains(t, expectedErr.Error(), rNibError_names[expectedErr.GetCode()])
67         assert.Equal(t, 1, expectedErr.GetCode())
68 }
69
70 func TestGetError(t *testing.T){
71         e := errors.New("Expected error")
72         expectedErr := NewInternalError(e)
73         assert.NotNil(t, expectedErr)
74         assert.NotNil(t, expectedErr.GetError())
75         assert.Equal(t, expectedErr.GetError(), e)
76 }
77
78 func TestGetCodeInternalError(t *testing.T){
79         e := errors.New("Expected error")
80         expectedErr := NewInternalError(e)
81         assert.NotNil(t, expectedErr)
82         assert.NotNil(t, expectedErr.GetError())
83         assert.Equal(t, INTERNAL_ERROR, expectedErr.GetCode())
84 }
85
86 func TestGetCodeNotFoundError(t *testing.T){
87         e := errors.New("Expected error")
88         expectedErr := NewResourceNotFoundError(e)
89         assert.NotNil(t, expectedErr)
90         assert.NotNil(t, expectedErr.GetError())
91         assert.Equal(t, RESOURCE_NOT_FOUND, expectedErr.GetCode())
92 }
93
94 func TestGetCodeValidationError(t *testing.T){
95         e := errors.New("Expected error")
96         expectedErr := NewValidationError(e)
97         assert.NotNil(t, expectedErr)
98         assert.NotNil(t, expectedErr.GetError())
99         assert.Equal(t, VALIDATION_ERROR, expectedErr.GetCode())
100 }