Skip to content

Commit 1f276e7

Browse files
authored
Merge pull request #880 from jrtitus/feat/authenticated-notifications
feat: add injectEdgeXAuth boolean attribute to RESTAddress types
2 parents a40ef71 + 66ec24c commit 1f276e7

File tree

3 files changed

+40
-8
lines changed

3 files changed

+40
-8
lines changed

dtos/address.go

+9-6
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,9 @@ func (a *Address) Validate() error {
5050
}
5151

5252
type RESTAddress struct {
53-
Path string `json:"path,omitempty"`
54-
HTTPMethod string `json:"httpMethod,omitempty" validate:"required,oneof='GET' 'HEAD' 'POST' 'PUT' 'PATCH' 'DELETE' 'TRACE' 'CONNECT'"`
53+
Path string `json:"path,omitempty"`
54+
HTTPMethod string `json:"httpMethod,omitempty" validate:"required,oneof='GET' 'HEAD' 'POST' 'PUT' 'PATCH' 'DELETE' 'TRACE' 'CONNECT'"`
55+
InjectEdgeXAuth bool `json:"injectEdgeXAuth,omitempty"`
5556
}
5657

5758
func NewRESTAddress(host string, port int, httpMethod string) Address {
@@ -109,8 +110,9 @@ func ToAddressModel(a Address) models.Address {
109110
BaseAddress: models.BaseAddress{
110111
Type: a.Type, Host: a.Host, Port: a.Port,
111112
},
112-
Path: a.RESTAddress.Path,
113-
HTTPMethod: a.RESTAddress.HTTPMethod,
113+
Path: a.RESTAddress.Path,
114+
HTTPMethod: a.RESTAddress.HTTPMethod,
115+
InjectEdgeXAuth: a.RESTAddress.InjectEdgeXAuth,
114116
}
115117
case common.MQTT:
116118
address = models.MQTTPubAddress{
@@ -146,8 +148,9 @@ func FromAddressModelToDTO(address models.Address) Address {
146148
switch a := address.(type) {
147149
case models.RESTAddress:
148150
dto.RESTAddress = RESTAddress{
149-
Path: a.Path,
150-
HTTPMethod: a.HTTPMethod,
151+
Path: a.Path,
152+
HTTPMethod: a.HTTPMethod,
153+
InjectEdgeXAuth: a.InjectEdgeXAuth,
151154
}
152155
case models.MQTTPubAddress:
153156
dto.MQTTPubAddress = MQTTPubAddress{

dtos/address_test.go

+28
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,17 @@ var testRESTAddress = Address{
3838
},
3939
}
4040

41+
var testRESTAddressWithAuthInject = Address{
42+
Type: common.REST,
43+
Host: testHost,
44+
Port: testPort,
45+
RESTAddress: RESTAddress{
46+
Path: testPath,
47+
HTTPMethod: testHTTPMethod,
48+
InjectEdgeXAuth: true,
49+
},
50+
}
51+
4152
var testMQTTPubAddress = Address{
4253
Type: common.MQTT,
4354
Host: testHost,
@@ -61,6 +72,11 @@ func TestAddress_UnmarshalJSON(t *testing.T) {
6172
testRESTAddress.Type, testRESTAddress.Host, testRESTAddress.Port,
6273
testRESTAddress.Path, testRESTAddress.HTTPMethod,
6374
)
75+
restWithInjectJsonStr := fmt.Sprintf(
76+
`{"type":"%s","host":"%s","port":%d,"path":"%s","httpMethod":"%s","injectEdgeXAuth":%v}`,
77+
testRESTAddressWithAuthInject.Type, testRESTAddressWithAuthInject.Host, testRESTAddressWithAuthInject.Port,
78+
testRESTAddressWithAuthInject.Path, testRESTAddressWithAuthInject.HTTPMethod, testRESTAddressWithAuthInject.InjectEdgeXAuth,
79+
)
6480
mqttJsonStr := fmt.Sprintf(
6581
`{"type":"%s","host":"%s","port":%d,"Publisher":"%s","Topic":"%s"}`,
6682
testMQTTPubAddress.Type, testMQTTPubAddress.Host, testMQTTPubAddress.Port,
@@ -75,6 +91,7 @@ func TestAddress_UnmarshalJSON(t *testing.T) {
7591
wantErr bool
7692
}{
7793
{"unmarshal RESTAddress with success", testRESTAddress, []byte(restJsonStr), false},
94+
{"unmarshal RESTAddressWithAuthInject with success", testRESTAddressWithAuthInject, []byte(restWithInjectJsonStr), false},
7895
{"unmarshal MQTTPubAddress with success", testMQTTPubAddress, []byte(mqttJsonStr), false},
7996
{"unmarshal EmailAddress with success", testEmailAddress, []byte(emailJsonStr), false},
8097
{"unmarshal invalid Address, empty data", Address{}, []byte{}, true},
@@ -163,6 +180,16 @@ func TestAddress_marshalJSON(t *testing.T) {
163180
`{"type":"%s","host":"%s","port":%d,"httpMethod":"%s"}`,
164181
restAddress.Type, restAddress.Host, restAddress.Port, restAddress.HTTPMethod,
165182
)
183+
restAddressWithAuthInject := Address{
184+
Type: common.REST,
185+
Host: testHost, Port: testPort,
186+
RESTAddress: RESTAddress{HTTPMethod: testHTTPMethod, InjectEdgeXAuth: true},
187+
}
188+
expectedRESTWithAuthInjectJsonStr := fmt.Sprintf(
189+
`{"type":"%s","host":"%s","port":%d,"httpMethod":"%s","injectEdgeXAuth":%v}`,
190+
restAddressWithAuthInject.Type, restAddressWithAuthInject.Host, restAddressWithAuthInject.Port,
191+
restAddressWithAuthInject.HTTPMethod, restAddressWithAuthInject.InjectEdgeXAuth,
192+
)
166193
mattAddress := Address{
167194
Type: common.MQTT,
168195
Host: testHost, Port: testPort,
@@ -192,6 +219,7 @@ func TestAddress_marshalJSON(t *testing.T) {
192219
expectedJSONStr string
193220
}{
194221
{"marshal REST address", restAddress, expectedRESTJsonStr},
222+
{"marshal REST address with auth inject", restAddressWithAuthInject, expectedRESTWithAuthInjectJsonStr},
195223
{"marshal MQTT address", mattAddress, expectedMQTTJsonStr},
196224
{"marshal Email address", emailAddress, expectedEmailJsonStr},
197225
}

models/address.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,9 @@ type BaseAddress struct {
7070
// RESTAddress is a REST specific struct
7171
type RESTAddress struct {
7272
BaseAddress
73-
Path string
74-
HTTPMethod string
73+
Path string
74+
HTTPMethod string
75+
InjectEdgeXAuth bool
7576
}
7677

7778
func (a RESTAddress) GetBaseAddress() BaseAddress { return a.BaseAddress }

0 commit comments

Comments
 (0)