-
Notifications
You must be signed in to change notification settings - Fork 172
/
handler-mock.go
170 lines (145 loc) · 5.41 KB
/
handler-mock.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*
* Copyright (c) 2018. Abstrium SAS <team (at) pydio.com>
* This file is part of Pydio Cells.
*
* Pydio Cells is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Pydio Cells is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Pydio Cells. If not, see <http://www.gnu.org/licenses/>.
*
* The latest code can be found at <https://pydio.com>.
*/
package views
import (
"context"
"errors"
"io"
"strings"
"github.com/micro/go-micro/client"
"github.com/pydio/minio-go"
"github.com/pydio/cells/common/log"
"github.com/pydio/cells/common/proto/tree"
)
func NewHandlerMock() *HandlerMock {
return &HandlerMock{
Nodes: make(map[string]*tree.Node),
}
}
type HandlerMock struct {
Nodes map[string]*tree.Node
Context context.Context
}
type MockReadCloser struct {
io.Reader
}
func (r MockReadCloser) Close() error {
return nil
}
func (h *HandlerMock) SetNextHandler(handler Handler) {}
func (h *HandlerMock) SetClientsPool(p *ClientsPool) {}
func (h *HandlerMock) ExecuteWrapped(inputFilter NodeFilter, outputFilter NodeFilter, provider NodesCallback) error {
return provider(inputFilter, outputFilter)
}
func (h *HandlerMock) ReadNode(ctx context.Context, in *tree.ReadNodeRequest, opts ...client.CallOption) (*tree.ReadNodeResponse, error) {
h.Nodes["in"] = in.Node
h.Context = ctx
if n, ok := h.Nodes[in.Node.Path]; ok {
return &tree.ReadNodeResponse{Node: n}, nil
}
return nil, errors.New("Not Found")
}
func (h *HandlerMock) ListNodes(ctx context.Context, in *tree.ListNodesRequest, opts ...client.CallOption) (tree.NodeProvider_ListNodesClient, error) {
h.Nodes["in"] = in.Node
h.Context = ctx
streamer := NewWrappingStreamer()
go func() {
defer streamer.Close()
for _, n := range h.Nodes {
if strings.HasPrefix(n.Path, in.Node.Path+"/") {
streamer.Send(&tree.ListNodesResponse{Node: n})
}
}
}()
return streamer, nil
}
func (h *HandlerMock) CreateNode(ctx context.Context, in *tree.CreateNodeRequest, opts ...client.CallOption) (*tree.CreateNodeResponse, error) {
log.Logger(ctx).Info("[MOCK] Create Node " + in.Node.Path)
h.Nodes["in"] = in.Node
h.Context = ctx
return nil, nil
}
func (h *HandlerMock) UpdateNode(ctx context.Context, in *tree.UpdateNodeRequest, opts ...client.CallOption) (*tree.UpdateNodeResponse, error) {
log.Logger(ctx).Info("[MOCK] Update Node " + in.From.Path + " to " + in.To.Path)
h.Nodes["from"] = in.From
h.Nodes["to"] = in.To
h.Context = ctx
return nil, nil
}
func (h *HandlerMock) DeleteNode(ctx context.Context, in *tree.DeleteNodeRequest, opts ...client.CallOption) (*tree.DeleteNodeResponse, error) {
log.Logger(ctx).Info("[MOCK] Delete Node" + in.Node.Path)
h.Nodes["in"] = in.Node
if _, ok := h.Nodes[in.Node.Path]; ok {
delete(h.Nodes, in.Node.Path)
}
h.Context = ctx
return nil, nil
}
func (h *HandlerMock) GetObject(ctx context.Context, node *tree.Node, requestData *GetRequestData) (io.ReadCloser, error) {
h.Nodes["in"] = node
h.Context = ctx
if n, ok := h.Nodes[node.Path]; ok {
// Fake node content : node path + hello world
closer := MockReadCloser{}
closer.Reader = strings.NewReader(n.Path + "hello world")
return closer, nil
}
return nil, errors.New("Not Found")
}
func (h *HandlerMock) PutObject(ctx context.Context, node *tree.Node, reader io.Reader, requestData *PutRequestData) (int64, error) {
log.Logger(ctx).Info("[MOCK] PutObject" + node.Path)
h.Nodes["in"] = node
h.Context = ctx
return 0, nil
}
func (h *HandlerMock) CopyObject(ctx context.Context, from *tree.Node, to *tree.Node, requestData *CopyRequestData) (int64, error) {
log.Logger(ctx).Info("[MOCK] CopyObject " + from.Path + " to " + to.Path)
h.Nodes["from"] = from
h.Nodes["to"] = to
h.Context = ctx
return 0, nil
}
func (h *HandlerMock) MultipartCreate(ctx context.Context, target *tree.Node, requestData *MultipartRequestData) (string, error) {
h.Nodes["in"] = target
h.Context = ctx
return "", nil
}
func (h *HandlerMock) MultipartPutObjectPart(ctx context.Context, target *tree.Node, uploadID string, partNumberMarker int, reader io.Reader, requestData *PutRequestData) (op minio.ObjectPart, e error) {
return
}
func (h *HandlerMock) MultipartList(ctx context.Context, prefix string, requestData *MultipartRequestData) (minio.ListMultipartUploadsResult, error) {
h.Context = ctx
return minio.ListMultipartUploadsResult{}, nil
}
func (h *HandlerMock) MultipartAbort(ctx context.Context, target *tree.Node, uploadID string, requestData *MultipartRequestData) error {
h.Context = ctx
h.Nodes["in"] = target
return nil
}
func (h *HandlerMock) MultipartComplete(ctx context.Context, target *tree.Node, uploadID string, uploadedParts []minio.CompletePart) (minio.ObjectInfo, error) {
h.Nodes["in"] = target
h.Context = ctx
return minio.ObjectInfo{}, nil
}
func (h *HandlerMock) MultipartListObjectParts(ctx context.Context, target *tree.Node, uploadID string, partNumberMarker int, maxParts int) (minio.ListObjectPartsResult, error) {
h.Nodes["in"] = target
h.Context = ctx
return minio.ListObjectPartsResult{}, nil
}