forked from elimity-com/scim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_handler_test.go
143 lines (122 loc) · 3.09 KB
/
resource_handler_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package scim
import (
"fmt"
"math/rand"
"net/http"
"time"
"github.com/elimity-com/scim/errors"
)
func ExampleResourceHandler() {
var r interface{} = testResourceHandler{}
_, ok := r.(ResourceHandler)
fmt.Println(ok)
// Output: true
}
// simple in-memory resource database
type testResourceHandler struct {
data map[string]ResourceAttributes
}
func (h testResourceHandler) Create(r *http.Request, attributes ResourceAttributes) (Resource, errors.PostError) {
// create unique identifier
rand.Seed(time.Now().UnixNano())
id := fmt.Sprintf("%04d", rand.Intn(9999))
// store resource
h.data[id] = attributes
// return stored resource
return Resource{
ID: id,
Attributes: attributes,
}, errors.PostErrorNil
}
func (h testResourceHandler) Get(r *http.Request, id string) (Resource, errors.GetError) {
// check if resource exists
data, ok := h.data[id]
if !ok {
return Resource{}, errors.GetErrorResourceNotFound
}
// return resource with given identifier
return Resource{
ID: id,
Attributes: data,
}, errors.GetErrorNil
}
func (h testResourceHandler) GetAll(r *http.Request, params ListRequestParams) (Page, errors.GetError) {
resources := make([]Resource, 0)
i := 1
for k, v := range h.data {
if i > (params.StartIndex + params.Count - 1) {
break
}
if i >= params.StartIndex {
resources = append(resources, Resource{
ID: k,
Attributes: v,
})
}
i++
}
return Page{
TotalResults: len(h.data),
Resources: resources,
}, errors.GetErrorNil
}
func (h testResourceHandler) Replace(r *http.Request, id string, attributes ResourceAttributes) (Resource, errors.PutError) {
// check if resource exists
_, ok := h.data[id]
if !ok {
return Resource{}, errors.PutErrorResourceNotFound
}
// replace (all) attributes
h.data[id] = attributes
// return resource with replaced attributes
return Resource{
ID: id,
Attributes: attributes,
}, errors.PutErrorNil
}
func (h testResourceHandler) Delete(r *http.Request, id string) errors.DeleteError {
// check if resource exists
_, ok := h.data[id]
if !ok {
return errors.DeleteErrorResourceNotFound
}
// delete resource
delete(h.data, id)
return errors.DeleteErrorNil
}
func (h testResourceHandler) Patch(r *http.Request, id string, req PatchRequest) (Resource, errors.PatchError) {
for _, op := range req.Operations {
switch op.Op {
case PatchOperationAdd:
if op.Path != "" {
h.data[id][op.Path] = op.Value
} else {
valueMap := op.Value.(map[string]interface{})
for k, v := range valueMap {
if arr, ok := h.data[id][k].([]interface{}); ok {
arr = append(arr, v)
h.data[id][k] = arr
} else {
h.data[id][k] = v
}
}
}
case PatchOperationReplace:
if op.Path != "" {
h.data[id][op.Path] = op.Value
} else {
valueMap := op.Value.(map[string]interface{})
for k, v := range valueMap {
h.data[id][k] = v
}
}
case PatchOperationRemove:
h.data[id][op.Path] = nil
}
}
// return resource with replaced attributes
return Resource{
ID: id,
Attributes: h.data[id],
}, errors.PatchErrorNil
}