This repository has been archived by the owner on Oct 25, 2024. It is now read-only.
forked from schwartzmx/gremtune
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathresponse_test.go
264 lines (208 loc) · 8.83 KB
/
response_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
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
package gremcos
import (
"fmt"
"reflect"
"testing"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/supplyon/gremcos/interfaces"
mock_interfaces "github.com/supplyon/gremcos/test/mocks/interfaces"
)
// Dummy responses for mocking
var dummySuccessfulResponse = []byte(`{"result":{"data":[{"id": 2,"label": "person","type": "vertex","properties": [
{"id": 2, "value": "vadas", "label": "name"},
{"id": 3, "value": 27, "label": "age"}]}
], "meta":{}},
"requestId":"1d6d02bd-8e56-421d-9438-3bd6d0079ff1",
"status":{"code":200,"attributes":{},"message":""}}`)
var dummyNeedAuthenticationResponse = []byte(`{"result":{},
"requestId":"1d6d02bd-8e56-421d-9438-3bd6d0079ff1",
"status":{"code":407,"attributes":{},"message":""}}`)
var dummySuccessfulResponseMarshalled = interfaces.Response{
RequestID: "1d6d02bd-8e56-421d-9438-3bd6d0079ff1",
Status: interfaces.Status{Code: 200, Attributes: map[string]interface{}{}},
Result: interfaces.Result{Data: []byte(`[{"id": 2,"label": "person","type": "vertex","properties": [
{"id": 2, "value": "vadas", "label": "name"},
{"id": 3, "value": 27, "label": "age"}]}
]`), Meta: map[string]interface{}{}},
}
var dummyNeedAuthenticationResponseMarshalled = interfaces.Response{
RequestID: "1d6d02bd-8e56-421d-9438-3bd6d0079ff1",
Status: interfaces.Status{Code: 407},
Result: interfaces.Result{Data: []byte("")},
}
var dummyPartialResponse1Marshalled = interfaces.Response{
RequestID: "1d6d02bd-8e56-421d-9438-3bd6d0079ff1",
Status: interfaces.Status{Code: 206}, // Code 206 indicates that the response is not the terminating response in a sequence of responses
Result: interfaces.Result{Data: []byte("testPartialData1")},
}
var dummyPartialResponse2Marshalled = interfaces.Response{
RequestID: "1d6d02bd-8e56-421d-9438-3bd6d0079ff1",
Status: interfaces.Status{Code: 200},
Result: interfaces.Result{Data: []byte("testPartialData2")},
}
// TestResponseHandling tests the overall response handling mechanism of gremcos
func TestResponseHandling(t *testing.T) {
// GIVEN
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()
mockedDialer := mock_interfaces.NewMockDialer(mockCtrl)
c := newClient(mockedDialer)
err := c.handleResponse(dummySuccessfulResponse)
require.NoError(t, err)
var expected []interfaces.Response
expected = append(expected, dummySuccessfulResponseMarshalled)
r, err := c.retrieveResponse(dummySuccessfulResponseMarshalled.RequestID)
require.NoError(t, err)
assert.Equal(t, reflect.TypeOf(r), reflect.TypeOf(expected))
}
func TestAuthRequested(t *testing.T) {
// GIVEN
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()
mockedDialer := mock_interfaces.NewMockDialer(mockCtrl)
c := newClient(mockedDialer, SetAuth(StaticCredentialProvider{
UsernameStatic: "username",
PasswordStatic: "password",
}))
// WHEN
err := c.handleResponse(dummyNeedAuthenticationResponse)
// THEN
require.NoError(t, err)
}
func TestPrepareAuthenRequest(t *testing.T) {
// GIVEN
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()
mockedDialer := mock_interfaces.NewMockDialer(mockCtrl)
c := newClient(mockedDialer)
req := prepareAuthRequest(dummyNeedAuthenticationResponseMarshalled.RequestID, "test", "test")
sampleAuthRequest, err := packageRequest(req)
require.NoError(t, err)
c.dispatchRequest(sampleAuthRequest)
authRequest := <-c.requests //Simulate that client send auth challenge to server
assert.Equal(t, authRequest, sampleAuthRequest, "Expected data type does not match actual.")
}
func TestAuthCompleted(t *testing.T) {
// GIVEN
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()
mockedDialer := mock_interfaces.NewMockDialer(mockCtrl)
c := newClient(mockedDialer)
err := c.handleResponse(dummySuccessfulResponse) //If authentication is successful the server returns the origin petition
require.NoError(t, err)
var expectedSuccessful []interfaces.Response
expectedSuccessful = append(expectedSuccessful, dummySuccessfulResponseMarshalled)
response, err := c.retrieveResponse(dummySuccessfulResponseMarshalled.RequestID)
require.NoError(t, err)
assert.Equal(t, reflect.TypeOf(expectedSuccessful), reflect.TypeOf(response), "Expected data type does not match actual.")
}
// TestResponseMarshalling tests the ability to marshal a response into a designated response struct for further manipulation
func TestResponseMarshalling(t *testing.T) {
resp, err := marshalResponse(dummySuccessfulResponse)
require.NoError(t, err)
assert.Equal(t, resp.RequestID, dummySuccessfulResponseMarshalled.RequestID)
assert.Equal(t, dummySuccessfulResponseMarshalled.Status.Code, resp.Status.Code)
assert.Equal(t, reflect.TypeOf(resp.Result.Data).String(), "json.RawMessage")
}
// TestResponseSortingSingleResponse tests the ability for sortResponse to save a response received from Gremlin Server
func TestResponseSortingSingleResponse(t *testing.T) {
// GIVEN
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()
mockedDialer := mock_interfaces.NewMockDialer(mockCtrl)
c := newClient(mockedDialer)
c.saveResponse(dummySuccessfulResponseMarshalled, nil)
var expected []interface{}
expected = append(expected, dummySuccessfulResponseMarshalled)
// WHEN
result, ok := c.results.Load(dummySuccessfulResponseMarshalled.RequestID)
// THEN
assert.True(t, ok)
assert.Equal(t, expected, result.(interface{}))
}
// TestResponseSortingMultipleResponse tests the ability for the sortResponse function to categorize and group responses that are sent in a stream
func TestResponseSortingMultipleResponse(t *testing.T) {
// GIVEN
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()
mockedDialer := mock_interfaces.NewMockDialer(mockCtrl)
c := newClient(mockedDialer)
// WHEN
c.saveResponse(dummyPartialResponse1Marshalled, nil)
c.saveResponse(dummyPartialResponse2Marshalled, nil)
var expected []interface{}
expected = append(expected, dummyPartialResponse1Marshalled)
expected = append(expected, dummyPartialResponse2Marshalled)
result, ok := c.results.Load(dummyPartialResponse1Marshalled.RequestID)
// THEN
assert.True(t, ok)
assert.Equal(t, expected, result.([]interface{}))
}
// TestResponseRetrieval tests the ability for a requester to retrieve the response for a specified requestId generated when sending the request
func TestResponseRetrieval(t *testing.T) {
// GIVEN
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()
mockedDialer := mock_interfaces.NewMockDialer(mockCtrl)
c := newClient(mockedDialer)
c.saveResponse(dummyPartialResponse1Marshalled, nil)
c.saveResponse(dummyPartialResponse2Marshalled, nil)
resp, err := c.retrieveResponse(dummyPartialResponse1Marshalled.RequestID)
require.NoError(t, err)
var expected []interfaces.Response
expected = append(expected, dummyPartialResponse1Marshalled)
expected = append(expected, dummyPartialResponse2Marshalled)
assert.Equal(t, resp, expected)
}
func TestResponseRetrievalFail(t *testing.T) {
// GIVEN
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()
mockedDialer := mock_interfaces.NewMockDialer(mockCtrl)
c := newClient(mockedDialer)
resp, err := c.retrieveResponse("nonexistent response")
assert.Error(t, err)
assert.Nil(t, resp)
}
// TestResponseDeletion tests the ability for a requester to clean up after retrieving a response after delivery to a client
func TestResponseDeletion(t *testing.T) {
// GIVEN
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()
mockedDialer := mock_interfaces.NewMockDialer(mockCtrl)
c := newClient(mockedDialer)
c.saveResponse(dummyPartialResponse1Marshalled, nil)
_, ok := c.results.Load(dummyPartialResponse1Marshalled.RequestID)
assert.True(t, ok)
c.saveResponse(dummyPartialResponse2Marshalled, nil)
_, ok = c.results.Load(dummyPartialResponse1Marshalled.RequestID)
assert.True(t, ok)
// WHEN
c.deleteResponse(dummyPartialResponse1Marshalled.RequestID)
// THEN
_, ok = c.results.Load(dummyPartialResponse1Marshalled.RequestID)
assert.False(t, ok)
}
func TestAsyncResponseRetrieval(t *testing.T) {
// GIVEN
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()
mockedDialer := mock_interfaces.NewMockDialer(mockCtrl)
c := newClient(mockedDialer)
c.saveResponse(dummyPartialResponse1Marshalled, nil)
c.saveResponse(dummyPartialResponse2Marshalled, nil)
responseChannel := make(chan interfaces.AsyncResponse, 10)
c.retrieveResponseAsync(dummyPartialResponse1Marshalled.RequestID, responseChannel)
resp := <-responseChannel
expectedAsync := interfaces.AsyncResponse{Response: dummyPartialResponse1Marshalled}
assert.Equal(t, expectedAsync, resp)
resp = <-responseChannel
expectedAsync = interfaces.AsyncResponse{Response: dummyPartialResponse2Marshalled}
assert.Equal(t, expectedAsync, resp)
}
func TestEmptyIfNilOrError(t *testing.T) {
assert.Empty(t, emptyIfNilOrError(nil))
assert.Equal(t, "failure", emptyIfNilOrError(fmt.Errorf("failure")))
}