-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathhl7_decode_test.go
166 lines (137 loc) · 3.91 KB
/
hl7_decode_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
// Unit tests for HL7 v2 decoding
package main
import (
"strings"
"testing"
"github.com/google/gopacket"
"github.com/google/gopacket/pcap"
)
var testHl7Decoder HL7Decoder
func init() {
if err := testHl7Decoder.Initialize(); err != nil {
panic("Failed to build queries")
}
}
func TestHL7DecodeFile(t *testing.T) {
handle, err := pcap.OpenOffline("testdata/HL7-ADT-UDI-PRT.pcap")
if err != nil {
panic(err)
}
packetSource := gopacket.NewPacketSource(handle, handle.LinkType())
for packet := range packetSource.Packets() {
app := packet.ApplicationLayer()
if app == nil {
continue // Ignore packets without an application layer
}
_, _, err := testHl7Decoder.DecodePayload(&app)
if err != nil {
panic(err)
}
}
}
func appLayerFromString(s string) *gopacket.ApplicationLayer {
bytes := []byte(s)
appLayer := gopacket.ApplicationLayer(gopacket.Payload(bytes))
return &appLayer
}
func TestHL7DecodeTooShort(t *testing.T) {
appLayer := appLayerFromString(".")
ident, _, err := testHl7Decoder.DecodePayload(appLayer)
if ident != "" {
t.Errorf("Got identifier when none was expected")
}
if err == nil {
t.Errorf("Expected an error from too-short HL7 message")
}
}
func testHL7DecodeEmpty(s string, t *testing.T) {
appLayer := appLayerFromString(s)
ident, _, err := testHl7Decoder.DecodePayload(appLayer)
if ident != "" {
t.Errorf("Got identifier when none was expected")
}
if err != nil {
panic(err)
}
}
func TestHL7DecodeEmpty1(t *testing.T) { testHL7DecodeEmpty("MSH|^~\\&", t) }
func TestHL7DecodeEmpty2(t *testing.T) { testHL7DecodeEmpty("MSH|^~\\&|", t) }
func identFromString(s string) string {
appLayer := appLayerFromString(s)
ident, _, err := testHl7Decoder.DecodePayload(appLayer)
if err != nil {
panic(err)
}
return ident
}
// Well-formed message header segment to be prepended to messages for testing
const okHL7Header = ("" +
// Header and delimiter
"MSH|^~\\&|" +
// Envelope information
"Sender|Sender Facility|" +
"Receiver|Receiver Facility|" +
// Timestamp (YYYYMMDDHHMM) + Security (blank)
"201801131030||" +
// Message type: ORU = observations & results
"ORU^R01|" +
// Control ID
"CNTRL-12345|" +
// Processing ID
"P|" +
// Version ID + segment delimiter (carriage return)
"2.4\r")
func getNRecordString(nrec int) string {
if nrec < 1 || nrec > 26 {
return ""
}
alphas := make([]string, nrec)
for i := 0; i < nrec; i++ {
alphas[i] = string('A' + i)
}
return strings.Join(alphas, "|")
}
func TestNRecordString(t *testing.T) {
if getNRecordString(-1) != "" || getNRecordString(0) != "" || getNRecordString(27) != "" {
panic("Out-of-range n-record string broken")
}
if getNRecordString(1) != "A" {
panic("1-record string broken")
}
if getNRecordString(3) != "A|B|C" {
panic("3-record string broken")
}
}
func TestHL7IdentFromOBX18(t *testing.T) {
str := okHL7Header + "OBX|" + getNRecordString(17) + "|Grospira Peach B+\r"
parsed := identFromString(str)
if parsed != "Grospira Peach B+" {
t.Errorf("Failed to parse identifier from string; got '%s'", parsed)
}
}
func BenchmarkHL7IdentFromOBX18(b *testing.B) {
str := okHL7Header + "OBX|" + getNRecordString(17) + "|Grospira Peach B+\r"
for i := 0; i < b.N; i++ {
identFromString(str)
}
}
func TestHL7IdentFromPRT16(t *testing.T) {
str := okHL7Header + "PRT|" + getNRecordString(15) + "|Grospira Peach B+\r"
parsed := identFromString(str)
if parsed != "Grospira Peach B+" {
t.Errorf("Failed to parse identifier from string; got '%s'", parsed)
}
}
func TestHL7IdentFromPrt16TrailingPipes(t *testing.T) {
str := okHL7Header + "PRT|A|B|C|D|E|F|G|H|I|||||||Grospira Pluot C+||||\r"
parsed := identFromString(str)
if parsed != "Grospira Pluot C+" {
t.Errorf("Failed to parse identifier from string; got '%s'", parsed)
}
}
func BenchmarkHL7IdentFromPRT16(b *testing.B) {
str := okHL7Header + "PRT|" + getNRecordString(15) + "|Grospira Peach B+\r"
for i := 0; i < b.N; i++ {
identFromString(str)
}
}