Skip to content

Commit

Permalink
patch: add statusCode to mocktr181's response payload
Browse files Browse the repository at this point in the history
- add the missing `statusCode` field to mocktr181's response payload
- add the expected values for the response fields `Command` and `Names`
- add missing `Success` message for successful `gets` and `patches`
  • Loading branch information
denopink committed Jul 9, 2024
1 parent af06700 commit d556a1d
Showing 1 changed file with 27 additions and 19 deletions.
46 changes: 27 additions & 19 deletions internal/wrphandlers/mocktr181/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ type Tr181Payload struct {
Command string `json:"command"`
Names []string `json:"names"`
Parameters []Parameter `json:"parameters"`
StatusCode int `json:"statusCode"`
}

type Parameters struct {
Expand Down Expand Up @@ -127,13 +128,13 @@ func (h Handler) HandleWrp(msg wrp.Message) error {

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

case "SET":
statusCode, payloadResponse, err = h.set(payload.Parameters)
statusCode, payloadResponse, err = h.set(payload)
if err != nil {
return err
}
Expand All @@ -156,11 +157,14 @@ func (h Handler) HandleWrp(msg wrp.Message) error {
return err
}

func (h Handler) get(names []string) (int64, []byte, error) {
result := Tr181Payload{}
statusCode := int64(http.StatusOK)
func (h Handler) get(tr181 *Tr181Payload) (int64, []byte, error) {
result := Tr181Payload{
Command: tr181.Command,
Names: tr181.Names,
StatusCode: http.StatusOK,
}

for _, name := range names {
for _, name := range tr181.Names {
for _, mockParameter := range h.parameters {
if !strings.HasPrefix(mockParameter.Name, name) {
continue
Expand All @@ -173,32 +177,36 @@ func (h Handler) get(names []string) (int64, []byte, error) {
Value: mockParameter.Value,
DataType: mockParameter.DataType,
Attributes: mockParameter.Attributes,
Message: "Success",
})
default:
result.Parameters = append(result.Parameters, Parameter{
Message: fmt.Sprintf("Invalid parameter name: %s", mockParameter.Name),
})
statusCode = 520
result.StatusCode = 520
}
}
}

if len(result.Parameters) == 0 {
result.StatusCode = 520
}

payload, err := json.Marshal(result)
if err != nil {
return http.StatusInternalServerError, payload, errors.Join(ErrInvalidResponsePayload, err)
}

if len(result.Parameters) == 0 {
statusCode = int64(520)
}

return statusCode, payload, nil
return int64(result.StatusCode), payload, nil
}

func (h Handler) set(parameters []Parameter) (int64, []byte, error) {
statusCode := http.StatusAccepted
result := Tr181Payload{}
for _, parameter := range parameters {
func (h Handler) set(tr181 *Tr181Payload) (int64, []byte, error) {
result := Tr181Payload{
Command: tr181.Command,
Names: tr181.Names,
StatusCode: http.StatusOK,
}
for _, parameter := range tr181.Parameters {
for i := range h.parameters {
mockParameter := &h.parameters[i]
if mockParameter.Name != parameter.Name {
Expand All @@ -210,19 +218,19 @@ func (h Handler) set(parameters []Parameter) (int64, []byte, error) {
mockParameter.Value = parameter.Value
mockParameter.DataType = parameter.DataType
mockParameter.Attributes = parameter.Attributes

result.Parameters = append(result.Parameters, Parameter{
Name: mockParameter.Name,
Value: mockParameter.Value,
DataType: mockParameter.DataType,
Attributes: mockParameter.Attributes,
Message: "Success",
})
default:
result.Parameters = append(result.Parameters, Parameter{
Name: mockParameter.Name,
Message: "Parameter is not writable",
})
statusCode = 520
result.StatusCode = 520
}
}
}
Expand All @@ -232,7 +240,7 @@ func (h Handler) set(parameters []Parameter) (int64, []byte, error) {
return http.StatusInternalServerError, payload, errors.Join(ErrInvalidResponsePayload, err)
}

return int64(statusCode), payload, nil
return int64(result.StatusCode), payload, nil
}

func (h Handler) loadFile() ([]MockParameter, error) {
Expand Down

0 comments on commit d556a1d

Please sign in to comment.