1 package msgdata
2
3 import (
4 "testing"
5
6 "github.com/stretchr/testify/assert"
7 )
8
9 var (
10 defaultReqMsgID = "abcd"
11 defaultResponseData = ResponseData{
12 Type: "Output",
13 ExitCode: 0,
14 Output: "hello\n",
15 TimeStamp: "01-01-2023 00:00:00",
16 Duration: 0.1,
17 }
18 defaultAttr = GeneralAttributes{
19 BannerID: "banner",
20 StoreID: "store",
21 TerminalID: "terminal",
22 SessionID: "orderingKey",
23 Identity: "identity",
24 Version: "1.0",
25 Signature: "signature",
26 }
27 defaultAttrMap = map[string]string{
28 "bannerId": "banner",
29 "storeId": "store",
30 "terminalId": "terminal",
31 "sessionId": "orderingKey",
32 "identity": "identity",
33 "version": "1.0",
34 "signature": "signature",
35 "request-message-uuid": defaultReqMsgID,
36 "commandId": defaultComID,
37 }
38 defaultResponseAttr = ResponseAttributes{
39 GeneralAttributes: defaultAttr,
40 ReqMsgID: defaultReqMsgID,
41 }
42 defaultResponseDataJSON = []byte(`
43 {
44 "type": "Output",
45 "exitCode": 0,
46 "output": "hello\n",
47 "timestamp": "01-01-2023 00:00:00",
48 "duration": 0.1
49 }`)
50 defaultJSON = `
51 {
52 "data": {
53 "type": "Output",
54 "exitCode": 0,
55 "output": "hello\n",
56 "timestamp": "01-01-2023 00:00:00",
57 "duration": 0.1
58 },
59 "attributes": {
60 "bannerId": "banner",
61 "storeId": "store",
62 "terminalId": "terminal",
63 "sessionId": "orderingKey",
64 "identity": "identity",
65 "version": "1.0",
66 "signature": "signature",
67 "request-message-uuid": "abcd"
68 }
69 }`
70 )
71
72 func TestSuccessNewCommandResponse(t *testing.T) {
73 expected := &commandResponse{
74 data: defaultResponseData,
75 attributes: defaultResponseAttr,
76 }
77 resp, err := NewCommandResponse(defaultResponseDataJSON, defaultAttrMap)
78 assert.NoError(t, err)
79 assert.Equal(t, expected, resp)
80 }
81
82 func TestFailNewCommandResponse(t *testing.T) {
83 resp, err := NewCommandResponse(nil, defaultAttrMap)
84 assert.Error(t, err)
85 assert.Nil(t, resp)
86
87 resp, err = NewCommandResponse(defaultResponseDataJSON, nil)
88 assert.Error(t, err)
89 assert.Nil(t, resp)
90 }
91
92 func TestAttrValidate(t *testing.T) {
93 attr, _ := newGeneralAttributes(defaultAttrMap)
94 assert.Len(t, attr.validate(make([]error, 0)), 0)
95
96 badAttr := GeneralAttributes{}
97 assert.Len(t, badAttr.validate(make([]error, 0)), 5)
98 }
99
100 func TestCommandResponseValidate(t *testing.T) {
101 resp := commandResponse{
102 data: defaultResponseData,
103 attributes: defaultResponseAttr,
104 }
105 assert.NoError(t, resp.validate())
106
107 badResp := commandResponse{}
108 assert.Len(t, badResp.validate(), 7)
109 }
110
111 func TestCommandResponseMarshalJSON(t *testing.T) {
112 resp := commandResponse{
113 data: defaultResponseData,
114 attributes: defaultResponseAttr,
115 }
116
117 expected := defaultJSON
118 data, err := resp.MarshalJSON()
119 assert.NoError(t, err)
120 assert.JSONEq(t, expected, string(data))
121 }
122
123 func TestGettersReturn(t *testing.T) {
124 req := commandRequest{
125 data: defaultRequestData,
126 attributes: defaultRequestAttr,
127 }
128 resp := commandResponse{
129 data: defaultResponseData,
130 attributes: defaultResponseAttr,
131 }
132
133 reqAttributes := req.Attributes()
134 reqData := req.Data()
135 respAttributes := resp.Attributes()
136 respData := resp.Data()
137
138 assert.Equal(t, req.attributes, reqAttributes)
139 assert.Equal(t, req.data, reqData)
140 assert.Equal(t, resp.attributes, respAttributes)
141 assert.Equal(t, resp.data, respData)
142 }
143
144 func TestGettersAreCopies(t *testing.T) {
145 req := commandRequest{
146 data: defaultRequestData,
147 attributes: defaultRequestAttr,
148 }
149 resp := commandResponse{
150 data: defaultResponseData,
151 attributes: defaultResponseAttr,
152 }
153
154 replacement := "something else"
155
156 reqAttributes := req.Attributes()
157 reqAttributes.BannerID = replacement
158
159 reqData := req.Data()
160 reqData.Command = replacement
161
162 respAttributes := resp.Attributes()
163 respAttributes.BannerID = replacement
164
165 respData := resp.Data()
166 respData.Output = replacement
167
168 assert.NotEqual(t, req.attributes, reqAttributes)
169 assert.NotEqual(t, req.data, reqData)
170 assert.NotEqual(t, resp.attributes, respAttributes)
171 assert.NotEqual(t, resp.data, respData)
172 }
173
View as plain text