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
|
// ========================LICENSE_START=================================
// Copyright (C) 2024: Deutsche Telekom
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// SPDX-License-Identifier: Apache-2.0
// ========================LICENSE_END===================================
//
package handler
import (
"errors"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"policy-opa-pdp/pkg/kafkacomm/publisher/mocks"
"testing"
)
/*
PdpUpdateMessageHandler_success
Description: Test by sending a valid input message for pdp update
Input: valid input
Expected Output: PDP Update Message should be sent sucessfully.
*/
func TestPdpUpdateMessageHandler_Success(t *testing.T) {
messageString := `{
"source":"pap-c17b4dbc-3278-483a-ace9-98f3157245c0",
"pdpHeartbeatIntervalMs":120000,
"policiesToBeDeployed":[],
"policiesToBeUndeployed":[],
"messageName":"PDP_UPDATE",
"requestId":"41c117db-49a0-40b0-8586-5580d042d0a1",
"timestampMs":1730722305297,
"name":"opa-21cabb3e-f652-4ca6-b498-a77e62fcd059",
"pdpGroup":"opaGroup",
"pdpSubgroup":"opa"
}`
mockSender := new(mocks.PdpStatusSender)
mockSender.On("SendPdpStatus", mock.Anything).Return(nil)
err := PdpUpdateMessageHandler([]byte(messageString), mockSender)
assert.NoError(t, err)
}
/*
PdpUpdateMessageHandler_Message_Unmarshal_Failure1
Description: Test by sending a invalid input message which should result in a Json unmarhsal error
Input: invalid input Message by renaming params or removing certain params
Expected Output: Message Handler should exit gracefully stating the error.
*/
func TestPdpUpdateMessageHandler_Message_Unmarshal_Failure1(t *testing.T) {
// sending only source parameter in the message string
messageString := `{
"source":"pap-c17b4dbc-3278-483a-ace9-98f3157245c0"}`
mockSender := new(mocks.PdpStatusSender)
mockSender.On("SendPdpStatus", mock.Anything).Return(errors.New("Jsonunmarshal Error"))
err := PdpUpdateMessageHandler([]byte(messageString), mockSender)
assert.Error(t, err)
}
/*
PdpUpdateMessageHandler_Message_Unmarshal_Failure2
Description: Test by sending a invalid input message which should result in a Json unmarhsal error
Input: invalid input Message by renaming params or removing certain params
Expected Output: Message Handler should exit gracefully stating the error.
*/
func TestPdpUpdateMessageHandler_Message_Unmarshal_Failure2(t *testing.T) {
// invlaid params by mispelling a param "source"
messageString := `{
"soce":"pap-c17b4dbc-3278-483a-ace9-98f3157245c0",
"pdpHeartbeatIntervalMs":120000}`
mockSender := new(mocks.PdpStatusSender)
mockSender.On("SendPdpStatus", mock.Anything).Return(errors.New("Jsonunmarshal Error"))
err := PdpUpdateMessageHandler([]byte(messageString), mockSender)
assert.Error(t, err)
}
/*
PdpUpdateMessageHandler_Message_Unmarshal_Failure3
Description: Test by sending a invalid input message which should result in a Json unmarhsal error
Input: {}
Expected Output: Message Handler should exit gracefully stating the error.
*/
func TestPdpUpdateMessageHandler_Message_Unmarshal_Failure3(t *testing.T) {
// invlaid params by mispelling a param "source"
messageString := `{
"soce:"pap-c17b4dbc-3278-483a-ace9-98f3157245c0",
"pdpHeartbeatIntervalMs":120000}`
mockSender := new(mocks.PdpStatusSender)
mockSender.On("SendPdpStatus", mock.Anything).Return(errors.New("Jsonunmarshal Error"))
err := PdpUpdateMessageHandler([]byte(messageString), mockSender)
assert.Error(t, err)
}
/*
PdpUpdateMessageHandler_Message_Unmarshal_Failure4
Description: Test by sending a invalid input message which should result in a Json unmarhsal error
Input: empty
Expected Output: Message Handler should exit gracefully stating the error.
*/
func TestPdpUpdateMessageHandler_Message_Unmarshal_Failure4(t *testing.T) {
// invlaid params by mispelling a param "source"
messageString := `""`
mockSender := new(mocks.PdpStatusSender)
mockSender.On("SendPdpStatus", mock.Anything).Return(errors.New("Jsonunmarshal Error"))
err := PdpUpdateMessageHandler([]byte(messageString), mockSender)
assert.Error(t, err)
}
/*
PdpUpdateMessageHandler_Fails_Sending_PdpUpdateResponse
Description: Test by sending a invalid attribute for pdpstate which should result in a failure in sending pdp update response
Input: invalid input config set for pdpstate
Expected Output: Message Handler should exit gracefully stating the error.
*/
func TestPdpUpdateMessageHandler_Fails_Sending_UpdateResponse(t *testing.T) {
// invalid value set to pdpSubgroup -->empty ""
messageString := `{
"source":"pap-c17b4dbc-3278-483a-ace9-98f3157245c0",
"pdpHeartbeatIntervalMs":120000,
"policiesToBeDeployed":[],
"policiesToBeUndeployed":[],
"messageName":"PDP_UPDATE",
"requestId":"41c117db-49a0-40b0-8586-5580d042d0a1",
"timestampMs":1730722305297,
"name":"opa-21cabb3e-f652-4ca6-b498-a77e62fcd059",
"pdpGroup":"opaGroup"
}`
mockSender := new(mocks.PdpStatusSender)
mockSender.On("SendPdpStatus", mock.Anything).Return(errors.New("Error in Sending PDP Update Response"))
err := PdpUpdateMessageHandler([]byte(messageString), mockSender)
assert.Error(t, err)
}
/*
PdpUpdateMessageHandler_Invalid_Starttimeinterval
Description: Test by sending a invalid time value attribute for pdpstate which should result in a failure in starting heartbeat interval
Input: invalid input message for pdpstate heartbeat interval
Expected Output: Message Handler should exit gracefully stating the error.
*/
func TestPdpUpdateMessageHandler_Invalid_Starttimeinterval(t *testing.T) {
//invalid interval set to negative -1000
messageString := `{
"source":"pap-c17b4dbc-3278-483a-ace9-98f3157245c0",
"pdpHeartbeatIntervalMs":-1000,
"policiesToBeDeployed":[],
"policiesToBeUndeployed":[],
"messageName":"PDP_UPDATE",
"requestId":"41c117db-49a0-40b0-8586-5580d042d0a1",
"timestampMs":1730722305297,
"name":"opa-21cabb3e-f652-4ca6-b498-a77e62fcd059",
"pdpGroup":"opaGroup",
"pdpSubgroup":"opa"
}`
mockSender := new(mocks.PdpStatusSender)
mockSender.On("SendPdpStatus", mock.Anything).Return(errors.New("Invalid Interval Time for Heartbeat"))
err := PdpUpdateMessageHandler([]byte(messageString), mockSender)
assert.Error(t, err)
}
|