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

patch: simplify mocktr181 HandleWrp and handle empty payload case #215

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
61 changes: 30 additions & 31 deletions internal/wrphandlers/mocktr181/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,53 +115,52 @@

// HandleWrp is called to process a tr181 command
func (h Handler) HandleWrp(msg wrp.Message) error {
payload := new(Tr181Payload)

err := json.Unmarshal(msg.Payload, &payload)
statusCode, payloadResponse, err := h.proccessCommand(msg.Payload)
if err != nil {
return errors.Join(err, wrpkit.ErrNotHandled)
}

var payloadResponse []byte
var statusCode int64

command := payload.Command

switch command {
case "GET":
statusCode, payloadResponse, err = h.get(payload)
if err != nil {
return errors.Join(err, wrpkit.ErrNotHandled)
}

case "SET":
statusCode, payloadResponse, err = h.set(payload)
if err != nil {
return errors.Join(err, wrpkit.ErrNotHandled)
}

default:
// currently only get and set are implemented for existing mocktr181
statusCode = 520
payloadResponse = []byte(fmt.Sprintf(`{"message": "command '%s' is not supported", "statusCode": %d}`, command, statusCode))
}

response := msg
response.Destination = msg.Source
response.Source = h.source
response.ContentType = "application/json"
response.Payload = payloadResponse

response.Status = &statusCode

err = h.egress.HandleWrp(response)
if err != nil {
if err = h.egress.HandleWrp(response); err != nil {
return errors.Join(err, wrpkit.ErrNotHandled)
}

return nil
}

func (h Handler) proccessCommand(wrpPayload []byte) (int64, []byte, error) {
var (
err error
payloadResponse []byte
statusCode = int64(520)
)

if len(wrpPayload) == 0 {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

support empty payload case (520 status code)

return statusCode, []byte(fmt.Sprintf(`{"message": ""Invalid Input Command"", "statusCode": %d}`, statusCode)), nil
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we say empty so we know the payload is empty?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we would but that's the expected behavior of the old c simulator.

we're trying to match the old c simulator as close as possible

}

payload := new(Tr181Payload)
err = json.Unmarshal(wrpPayload, &payload)
if err != nil {
return statusCode, payloadResponse, err

Check warning on line 150 in internal/wrphandlers/mocktr181/handler.go

View check run for this annotation

Codecov / codecov/patch

internal/wrphandlers/mocktr181/handler.go#L150

Added line #L150 was not covered by tests
}

switch payload.Command {
case "GET":
return h.get(payload)
case "SET":
return h.set(payload)
default:
// currently only get and set are implemented for existing mocktr181
return statusCode, []byte(fmt.Sprintf(`{"message": "command '%s' is not supported", "statusCode": %d}`, payload.Command, statusCode)), nil
}
}

func (h Handler) get(tr181 *Tr181Payload) (int64, []byte, error) {
result := Tr181Payload{
Command: tr181.Command,
Expand Down
10 changes: 8 additions & 2 deletions internal/wrphandlers/mocktr181/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,19 @@ func TestHandler_HandleWrp(t *testing.T) {
return nil
},
}, {
description: "no payload",
expectedErr: wrpkit.ErrNotHandled,
description: "no payload",
egressCallCount: 1,
msg: wrp.Message{
Type: wrp.SimpleEventMessageType,
Source: "dns:tr1d1um.example.com/service/ignored",
Destination: "event:event_1/ignored",
},
validate: func(a *assert.Assertions, msg wrp.Message, h *Handler) error {
a.Equal(int64(520), *msg.Status)
a.True(h.Enabled())

return nil
},
},
}
for _, tc := range tests {
Expand Down
Loading