/
createMockRequest.go
197 lines (159 loc) · 5.31 KB
/
createMockRequest.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
package main
import (
"encoding/json"
"fmt"
"math/rand"
"net/http"
"strings"
)
// MockColumn defines how a column of data should be constructed, with boundaries
// for String, Int and Float64 types (which are only the types allowed)
type MockColumn struct {
Column
MaxLength int `json:"max_length"`
IntLowerBound int `json:"int_lower_bound"`
IntUpperBound int `json:"int_upper_bound"`
FloatLowerBound float64 `json:"float_lower_bound"`
FloatUpperBound float64 `json:"float_upper_bound"`
}
// MockCreateRequest specifies the generation of a mock set of records, split
// into pages according to the specified number of records per page
type MockCreateRequest struct {
RecordCount int `json:"max_records"`
Columns []MockColumn `json:"columns"`
RecordsPerPage int `json:"records_per_page"`
}
// MockCreateResponse provides the details to be able to recover any of the pages
// created as a result of a MockCreateRequest being sent to the server
type MockCreateResponse struct {
RequestHash string `json:"hash"`
PageTokens []string `json:"tokens"`
}
// NewMockCreatRequestHandlerFactory returns a factory instance that manufactures Handlers
// which can generate mocked data to be added to the cache.
func NewMockCreatRequestHandlerFactory() HandlerFactory {
return &mockCreatRequestHandlerFactory{}
}
type mockCreatRequestHandlerFactory struct {
}
func (f *mockCreatRequestHandlerFactory) New(pattern string, config *cacheConfig, requestID string) Handler {
h := &mockCreatRequestHandler{}
h.method = http.MethodPost
h.config = config
h.handler = h.handleCreatePages
h.pattern = pattern
h.requestID = requestID
return h
}
type mockCreatRequestHandler struct {
writeHandler
}
// handleCreatePages is invoked after the initial authorization and validation checks are completed,
// and creates pages of random data as defined by the request
func (m *mockCreatRequestHandler) handleCreatePages(w http.ResponseWriter, req *http.Request) {
// Get the details of the requested page
var p MockCreateRequest
err := json.NewDecoder(req.Body).Decode(&p)
if err != nil {
returnError(w, err.Error(), http.StatusBadRequest)
return
}
// Generate the data in the cache
resp, err := m.createMockData(&p)
if err != nil {
returnError(w, err.Error(), http.StatusBadRequest)
return
}
// Return details of created pages
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(resp)
}
func (m *mockCreatRequestHandler) createMockData(req *MockCreateRequest) (*MockCreateResponse, error) {
// Hash should be generated from the request; here is it just a UUID
hash := NewUUID()
// Only create a single page of data for now; token is a UUID
curPageToken := NewUUID()
cols := []Column{}
for _, col := range req.Columns {
cols = append(cols, Column{Name: col.Name, Type: col.Type})
}
resp := &MockCreateResponse{
RequestHash: hash,
PageTokens: []string{curPageToken},
}
remainingRecords := req.RecordCount
records := [][]string{}
for remainingRecords > 0 {
for len(records) < req.RecordsPerPage && remainingRecords > 0 {
records = append(records, m.createRecord(req.Columns))
remainingRecords--
}
if remainingRecords > 0 {
nextPageToken := NewUUID()
err := m.createPage(hash, curPageToken, nextPageToken, cols, records)
if err != nil {
return nil, err
}
resp.PageTokens = append(resp.PageTokens, nextPageToken)
curPageToken = nextPageToken
records = [][]string{}
}
}
err := m.createPage(hash, curPageToken, "", cols, records)
if err != nil {
return nil, err
}
m.Debug("Hash: %v, Pages: %v", resp.RequestHash, resp.PageTokens)
return resp, nil
}
func (m *mockCreatRequestHandler) createRandomString(maxLength int) string {
available := "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012346789"
length := rand.Intn(maxLength)
ret := ""
for i := 0; i < length; i++ {
c := rand.Intn(len(available))
ret = ret + available[c:c+1]
}
return ret
}
func (m *mockCreatRequestHandler) createRandomInt(lowerBound, upperBound int) int {
if lowerBound == upperBound {
return lowerBound
}
if lowerBound > upperBound {
return m.createRandomInt(upperBound, lowerBound)
}
if lowerBound < 0 && upperBound < 0 {
return -m.createRandomInt(-upperBound, -lowerBound)
}
return rand.Intn(upperBound-lowerBound) + lowerBound
}
func (m *mockCreatRequestHandler) createRandomFloat64(lowerBound, upperBound float64) float64 {
if lowerBound == upperBound {
return lowerBound
}
if lowerBound > upperBound {
return m.createRandomFloat64(upperBound, lowerBound)
}
if lowerBound < 0 && upperBound < 0 {
return -m.createRandomFloat64(-upperBound, -lowerBound)
}
return rand.Float64()*(upperBound-lowerBound) + lowerBound
}
func (m *mockCreatRequestHandler) createRecord(cols []MockColumn) []string {
var record []string = []string{}
for _, col := range cols {
switch strings.ToLower(col.Type) {
case "string":
record = append(record, m.createRandomString(col.MaxLength))
case "int":
record = append(record, fmt.Sprintf("%v", m.createRandomInt(col.IntLowerBound, col.IntUpperBound)))
case "float":
record = append(record, fmt.Sprintf("%v", m.createRandomFloat64(col.FloatLowerBound, col.FloatUpperBound)))
default:
record = append(record, "Unsupported Type")
}
}
return record
}