-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_copier_test.go
215 lines (197 loc) · 6.93 KB
/
data_copier_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
package main
import (
"bytes"
"encoding/json"
"io"
"io/ioutil"
"os"
"path"
"testing"
"time"
"github.com/stretchr/testify/suite"
)
// In order for 'go test' to run this suite, we need to create
// a normal test function and pass our suite to suite.Run
func TestDataCopierSuite(t *testing.T) {
suite.Run(t, new(DataCopierSuite))
}
type DataCopierSuite struct {
suite.Suite
hieClient *MockHieClient
ingestClient *MockIngestClient
txLogMgr *MockTransactionLogManager
}
type MockHieClient struct {
QueryRecordsFnIndex int
QueryRecordsFns []func(string, *time.Time, *time.Time) (*QueryResponse, error)
DownloadRecordFnIndex int
DownloadRecordFns []func(string) (io.ReadCloser, string, error)
}
func (m *MockHieClient) QueryRecords(mrn string, start *time.Time, end *time.Time) (*QueryResponse, error) {
i := m.QueryRecordsFnIndex
m.QueryRecordsFnIndex++
return m.QueryRecordsFns[i](mrn, start, end)
}
func (m *MockHieClient) DownloadRecord(url string) (content io.ReadCloser, contentType string, err error) {
i := m.DownloadRecordFnIndex
m.DownloadRecordFnIndex++
return m.DownloadRecordFns[i](url)
}
type MockIngestClient struct {
IngestFnIndex int
IngestFns []func(string, io.ReadCloser) error
}
func (m *MockIngestClient) Ingest(contentType string, reader io.ReadCloser) error {
i := m.IngestFnIndex
m.IngestFnIndex++
return m.IngestFns[i](contentType, reader)
}
type MockTransactionLogManager struct {
FindEntriesFnIndex int
FindEntriesFns []func(string) ([]*TransactionLogEntry, error)
StoreEntryFnIndex int
StoreEntryFns []func(*TransactionLogEntry) error
}
func (m *MockTransactionLogManager) FindEntriesByEE(ee string) (entries []*TransactionLogEntry, err error) {
i := m.FindEntriesFnIndex
m.FindEntriesFnIndex++
return m.FindEntriesFns[i](ee)
}
func (m *MockTransactionLogManager) StoreEntry(entry *TransactionLogEntry) error {
i := m.StoreEntryFnIndex
m.StoreEntryFnIndex++
return m.StoreEntryFns[i](entry)
}
func (suite *DataCopierSuite) SetupTest() {
suite.hieClient = &MockHieClient{}
suite.ingestClient = &MockIngestClient{}
suite.txLogMgr = &MockTransactionLogManager{}
}
func (suite *DataCopierSuite) TestSuccessfulOperation() {
require := suite.Require()
suite.SetupMocksForSuccess("")
dataCopier, err := NewDataCopier(suite.hieClient, suite.ingestClient, suite.txLogMgr)
require.NoError(err)
err = dataCopier.CopyRecords("123456789", "XML^HL7^231^CCD^C32")
require.NoError(err)
}
func (suite *DataCopierSuite) TestSuccessfulOperationWithLocalCopies() {
require := suite.Require()
assert := suite.Assert()
tempDir, err := ioutil.TempDir("", "dc_test")
require.NoError(err)
suite.SetupMocksForSuccess(tempDir)
dataCopier, err := NewDataCopierWithLocalCopies(suite.hieClient, suite.ingestClient, suite.txLogMgr, tempDir)
require.NoError(err)
err = dataCopier.CopyRecords("123456789", "XML^HL7^231^CCD^C32")
require.NoError(err)
for _, num := range []string{"1", "2", "3"} {
data, err := ioutil.ReadFile(path.Join(tempDir, "123456789", "1.1.1.1.1."+num+".xml"))
require.NoError(err)
assert.Equal("<foo>"+num+"</foo>", string(data))
}
os.RemoveAll(tempDir)
}
func (suite *DataCopierSuite) SetupMocksForSuccess(localCopyPath string) {
assert := suite.Assert()
require := suite.Require()
// Setup all the mocks
qStart := time.Date(2010, time.January, 1, 0, 0, 0, 0, time.Local)
suite.hieClient.QueryRecordsFns = append(suite.hieClient.QueryRecordsFns, func(mrn string, start *time.Time, end *time.Time) (*QueryResponse, error) {
assert.Equal("123456789", mrn)
assert.Equal(&qStart, start)
assert.Nil(end)
b, err := ioutil.ReadFile("./fixtures/response_success.json")
require.NoError(err)
var r QueryResponse
json.Unmarshal(b, &r)
return &r, nil
})
suite.hieClient.DownloadRecordFns = append(suite.hieClient.DownloadRecordFns, func(url string) (io.ReadCloser, string, error) {
assert.Equal("http://test.foo.net/document/1.1.1.1.1.1", url)
rc := nopCloser{bytes.NewBufferString("<foo>1</foo>")}
return rc, "text/xml", nil
}, func(url string) (io.ReadCloser, string, error) {
assert.Equal("http://test.foo.net/document/1.1.1.1.1.2", url)
rc := nopCloser{bytes.NewBufferString("<foo>2</foo>")}
return rc, "text/xml", nil
}, func(url string) (io.ReadCloser, string, error) {
assert.Equal("http://test.foo.net/document/1.1.1.1.1.3", url)
rc := nopCloser{bytes.NewBufferString("<foo>3</foo>")}
return rc, "text/xml", nil
})
suite.ingestClient.IngestFns = append(suite.ingestClient.IngestFns, func(contentType string, reader io.ReadCloser) error {
assert.Equal("text/xml", contentType)
defer reader.Close()
buf := new(bytes.Buffer)
buf.ReadFrom(reader)
assert.Equal("<foo>1</foo>", buf.String())
return nil
}, func(contentType string, reader io.ReadCloser) error {
assert.Equal("text/xml", contentType)
defer reader.Close()
buf := new(bytes.Buffer)
buf.ReadFrom(reader)
assert.Equal("<foo>2</foo>", buf.String())
return nil
}, func(contentType string, reader io.ReadCloser) error {
assert.Equal("text/xml", contentType)
defer reader.Close()
buf := new(bytes.Buffer)
buf.ReadFrom(reader)
assert.Equal("<foo>3</foo>", buf.String())
return nil
})
suite.txLogMgr.FindEntriesFns = append(suite.txLogMgr.FindEntriesFns, func(ee string) ([]*TransactionLogEntry, error) {
assert.Equal("123456789", ee)
return []*TransactionLogEntry{
&TransactionLogEntry{
QueryResponseEntry: QueryResponseEntry{
RetrieveURL: "http://test.foo.net/document/1.1.1.1.1.0",
CreationTime: time.Date(2007, time.March, 12, 9, 0, 0, 0, time.Local),
Title: "Test Continuity of Care",
DocumentType: "XML^HL7^231^CCD^C32",
Hash: "1827364537281930473627184544327894736482",
Size: 92834,
},
EE: "123456789",
Date: time.Date(2009, time.December, 31, 23, 59, 59, 0, time.Local),
},
}, nil
})
qEnd := time.Date(2016, time.June, 8, 23, 59, 59, 0, time.Local)
suite.txLogMgr.StoreEntryFns = append(suite.txLogMgr.StoreEntryFns, func(entry *TransactionLogEntry) error {
b, err := ioutil.ReadFile("./fixtures/response_success.json")
require.NoError(err)
var r QueryResponse
json.Unmarshal(b, &r)
assert.Equal(&TransactionLogEntry{
QueryResponseEntry: r.Result[0],
EE: "123456789",
Date: qEnd,
}, entry)
return nil
}, func(entry *TransactionLogEntry) error {
b, err := ioutil.ReadFile("./fixtures/response_success.json")
require.NoError(err)
var r QueryResponse
json.Unmarshal(b, &r)
assert.Equal(&TransactionLogEntry{
QueryResponseEntry: r.Result[1],
EE: "123456789",
Date: qEnd,
}, entry)
return nil
}, func(entry *TransactionLogEntry) error {
b, err := ioutil.ReadFile("./fixtures/response_success.json")
require.NoError(err)
var r QueryResponse
json.Unmarshal(b, &r)
assert.Equal(&TransactionLogEntry{
QueryResponseEntry: r.Result[2],
EE: "123456789",
Date: qEnd,
}, entry)
return nil
})
}