Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add injectEdgeXAuth boolean attribute to RESTAddress types #880

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions dtos/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,9 @@ func (a *Address) Validate() error {
}

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

func NewRESTAddress(host string, port int, httpMethod string) Address {
Expand Down Expand Up @@ -109,8 +110,9 @@ func ToAddressModel(a Address) models.Address {
BaseAddress: models.BaseAddress{
Type: a.Type, Host: a.Host, Port: a.Port,
},
Path: a.RESTAddress.Path,
HTTPMethod: a.RESTAddress.HTTPMethod,
Path: a.RESTAddress.Path,
HTTPMethod: a.RESTAddress.HTTPMethod,
InjectEdgeXAuth: a.RESTAddress.InjectEdgeXAuth,
}
case common.MQTT:
address = models.MQTTPubAddress{
Expand Down Expand Up @@ -146,8 +148,9 @@ func FromAddressModelToDTO(address models.Address) Address {
switch a := address.(type) {
case models.RESTAddress:
dto.RESTAddress = RESTAddress{
Path: a.Path,
HTTPMethod: a.HTTPMethod,
Path: a.Path,
HTTPMethod: a.HTTPMethod,
InjectEdgeXAuth: a.InjectEdgeXAuth,
}
case models.MQTTPubAddress:
dto.MQTTPubAddress = MQTTPubAddress{
Expand Down
28 changes: 28 additions & 0 deletions dtos/address_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,17 @@ var testRESTAddress = Address{
},
}

var testRESTAddressWithAuthInject = Address{
Type: common.REST,
Host: testHost,
Port: testPort,
RESTAddress: RESTAddress{
Path: testPath,
HTTPMethod: testHTTPMethod,
InjectEdgeXAuth: true,
},
}

var testMQTTPubAddress = Address{
Type: common.MQTT,
Host: testHost,
Expand All @@ -61,6 +72,11 @@ func TestAddress_UnmarshalJSON(t *testing.T) {
testRESTAddress.Type, testRESTAddress.Host, testRESTAddress.Port,
testRESTAddress.Path, testRESTAddress.HTTPMethod,
)
restWithInjectJsonStr := fmt.Sprintf(
`{"type":"%s","host":"%s","port":%d,"path":"%s","httpMethod":"%s","injectEdgeXAuth":%v}`,
testRESTAddressWithAuthInject.Type, testRESTAddressWithAuthInject.Host, testRESTAddressWithAuthInject.Port,
testRESTAddressWithAuthInject.Path, testRESTAddressWithAuthInject.HTTPMethod, testRESTAddressWithAuthInject.InjectEdgeXAuth,
)
mqttJsonStr := fmt.Sprintf(
`{"type":"%s","host":"%s","port":%d,"Publisher":"%s","Topic":"%s"}`,
testMQTTPubAddress.Type, testMQTTPubAddress.Host, testMQTTPubAddress.Port,
Expand All @@ -75,6 +91,7 @@ func TestAddress_UnmarshalJSON(t *testing.T) {
wantErr bool
}{
{"unmarshal RESTAddress with success", testRESTAddress, []byte(restJsonStr), false},
{"unmarshal RESTAddressWithAuthInject with success", testRESTAddressWithAuthInject, []byte(restWithInjectJsonStr), false},
{"unmarshal MQTTPubAddress with success", testMQTTPubAddress, []byte(mqttJsonStr), false},
{"unmarshal EmailAddress with success", testEmailAddress, []byte(emailJsonStr), false},
{"unmarshal invalid Address, empty data", Address{}, []byte{}, true},
Expand Down Expand Up @@ -163,6 +180,16 @@ func TestAddress_marshalJSON(t *testing.T) {
`{"type":"%s","host":"%s","port":%d,"httpMethod":"%s"}`,
restAddress.Type, restAddress.Host, restAddress.Port, restAddress.HTTPMethod,
)
restAddressWithAuthInject := Address{
Type: common.REST,
Host: testHost, Port: testPort,
RESTAddress: RESTAddress{HTTPMethod: testHTTPMethod, InjectEdgeXAuth: true},
}
expectedRESTWithAuthInjectJsonStr := fmt.Sprintf(
`{"type":"%s","host":"%s","port":%d,"httpMethod":"%s","injectEdgeXAuth":%v}`,
restAddressWithAuthInject.Type, restAddressWithAuthInject.Host, restAddressWithAuthInject.Port,
restAddressWithAuthInject.HTTPMethod, restAddressWithAuthInject.InjectEdgeXAuth,
)
mattAddress := Address{
Type: common.MQTT,
Host: testHost, Port: testPort,
Expand Down Expand Up @@ -192,6 +219,7 @@ func TestAddress_marshalJSON(t *testing.T) {
expectedJSONStr string
}{
{"marshal REST address", restAddress, expectedRESTJsonStr},
{"marshal REST address with auth inject", restAddressWithAuthInject, expectedRESTWithAuthInjectJsonStr},
{"marshal MQTT address", mattAddress, expectedMQTTJsonStr},
{"marshal Email address", emailAddress, expectedEmailJsonStr},
}
Expand Down
5 changes: 3 additions & 2 deletions models/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,9 @@ type BaseAddress struct {
// RESTAddress is a REST specific struct
type RESTAddress struct {
BaseAddress
Path string
HTTPMethod string
Path string
HTTPMethod string
InjectEdgeXAuth bool
}

func (a RESTAddress) GetBaseAddress() BaseAddress { return a.BaseAddress }
Expand Down