Skip to content

Commit

Permalink
Merge pull request #215 from xmidt-org/denopink/patch/simplify-mocktr…
Browse files Browse the repository at this point in the history
…181-HandleWrp-structure

patch: simplify mocktr181 HandleWrp and handle empty payload case
  • Loading branch information
denopink authored Aug 6, 2024
2 parents 22e2200 + 9a7c36d commit e3a592f
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 33 deletions.
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 @@ func (h Handler) Enabled() bool {

// 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 {
return statusCode, []byte(fmt.Sprintf(`{"message": ""Invalid Input Command"", "statusCode": %d}`, statusCode)), nil
}

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

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

0 comments on commit e3a592f

Please sign in to comment.