Skip to content

Commit

Permalink
save
Browse files Browse the repository at this point in the history
  • Loading branch information
marcia-piccione committed Apr 3, 2024
1 parent a3f5bc3 commit 0bf2a07
Show file tree
Hide file tree
Showing 5 changed files with 28,473 additions and 17 deletions.
2 changes: 1 addition & 1 deletion cmd/xmidt-agent/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ type Storage struct {
}

type MockTr181 struct {
FilePath string
FilePath string
}

// Collect and process the configuration files and env vars and
Expand Down
34 changes: 18 additions & 16 deletions internal/wrphandlers/mocktr181/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ type Handler struct {
egress wrpkit.Handler
source string
filePath string
parameters *MockParameters
parameters []MockParameter
logger *zap.Logger
}

Expand All @@ -57,7 +57,7 @@ type MockParameters struct {
Parameters []MockParameter
}

type Payload struct {
type Tr181Payload struct {
Command string `json:"command"`
Names []string `json:"names"`
Parameters []Parameter `json:"parameters"`
Expand Down Expand Up @@ -113,7 +113,7 @@ func New(next, egress wrpkit.Handler, source string, logger *zap.Logger, opts ..

// HandleWrp is called to process a tr181 command
func (h Handler) HandleWrp(msg wrp.Message) error {
payload := make(map[string]interface{})
payload := new(Tr181Payload)
err := json.Unmarshal(msg.Payload, &payload)
if err != nil {
h.logger.Error("unable to unmarshal msg payload", zap.Error(err))
Expand All @@ -123,14 +123,14 @@ func (h Handler) HandleWrp(msg wrp.Message) error {
var payloadResponse []byte
var statusCode int64

command := payload["command"].(string)
command := payload.Command

switch command {
case "GET":
statusCode, payloadResponse = h.get(payload["names"].([]string))
statusCode, payloadResponse = h.get(payload.Names)

case "SET":
statusCode = h.set(payload["parameters"].([]Parameter))
statusCode = h.set(payload.Parameters)

default:
// currently only get and set are implemented for existing mocktr181
Expand All @@ -154,7 +154,7 @@ func (h Handler) get(names []string) (int64, []byte) {
result := []Parameter{}

for _, name := range names {
for _, mockParameter := range h.parameters.Parameters {
for _, mockParameter := range h.parameters {
if strings.HasPrefix(mockParameter.Name, name) {
result = append(result, Parameter{
Name: mockParameter.Name,
Expand All @@ -172,38 +172,40 @@ func (h Handler) get(names []string) (int64, []byte) {
return http.StatusInternalServerError, payload
}

return http.StatusAccepted, payload
return http.StatusOK, payload
}

func (h Handler) set(parameters []Parameter) int64 {
for _, parameter := range parameters {
for _, mockParameter := range h.parameters.Parameters {
for _, mockParameter := range h.parameters {
if strings.HasPrefix(mockParameter.Name, parameter.Name) {
mockParameter.Value = parameter.Value
mockParameter.DataType = parameter.DataType
mockParameter.Attributes = parameter.Attributes
if mockParameter.Access == "rw" {
mockParameter.Value = parameter.Value
mockParameter.DataType = parameter.DataType
mockParameter.Attributes = parameter.Attributes
}
}
}
}

return http.StatusOK
return http.StatusAccepted
}

func (h Handler) loadFile() (*MockParameters, error) {
func (h Handler) loadFile() ([]MockParameter, error) {
jsonFile, err := os.Open(h.filePath)
if err != nil {
h.logger.Error("unable to open mock parameter file", zap.Error(err))
return nil, err
}
defer jsonFile.Close()

var parameters MockParameters
var parameters []MockParameter
byteValue, _ := io.ReadAll(jsonFile)
err = json.Unmarshal(byteValue, &parameters)
if err != nil {
h.logger.Error("unable to unmarshal mock parameter file", zap.Error(err))
return nil, err
}

return &parameters, nil
return parameters, nil
}
111 changes: 111 additions & 0 deletions internal/wrphandlers/mocktr181/handler_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
// SPDX-FileCopyrightText: 2024 Comcast Cable Communications Management, LLC
// SPDX-License-Identifier: Apache-2.0

package mocktr181

import (
"encoding/json"
"net/http"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/xmidt-org/wrp-go/v3"
"github.com/xmidt-org/xmidt-agent/internal/wrpkit"
"go.uber.org/zap"
)

func TestHandler_HandleWrp(t *testing.T) {
tests := []struct {
description string
nextResult error
nextCallCount int
egressResult error
egressCallCount int
msg wrp.Message
expectedErr error
validate func(*assert.Assertions, wrp.Message) error
}{
{
description: "get success with multiple results",
egressCallCount: 1,
expectedErr: nil,
msg: wrp.Message{
Type: wrp.SimpleEventMessageType,
Source: "dns:tr1d1um.example.com/service/ignored",
Destination: "event:event_1/ignored",
Payload: []byte("{\"command\":\"GET\",\"names\":[\"Device.DeviceInfo.\"]}"),
},
validate: func(a *assert.Assertions, msg wrp.Message) error {
a.Equal(int64(http.StatusOK), *msg.Status)
var result []Parameters
err := json.Unmarshal(msg.Payload, &result)
a.NoError(err)
a.Equal(102, len(result))
return nil
},
}, {
description: "set, success",
egressCallCount: 1,
msg: wrp.Message{
Type: wrp.SimpleEventMessageType,
Source: "dns:tr1d1um.example.com/service/ignored",
Destination: "event:event_1/ignored",
Payload: []byte("{\"command\":\"SET\",\"parameters\":[{\"name\":\"Device.WiFi.Radio.10000.Name\",\"dataType\":0,\"value\":\"anothername\",\"attributes\":{\"notify\":0}}]}"),
},
validate: func(a *assert.Assertions, msg wrp.Message) error {
a.Equal(int64(http.StatusAccepted), *msg.Status)

return nil
},
}, {
description: "set, read only",
egressCallCount: 1,
msg: wrp.Message{
Type: wrp.SimpleEventMessageType,
Source: "dns:tr1d1um.example.com/service/ignored",
Destination: "event:event_1/ignored",
Payload: []byte("{\"command\":\"SET\",\"parameters\":[{\"name\":\"Device.Bridging.MaxBridgeEntries\",\"dataType\":0,\"value\":\"anothername\",\"attributes\":{\"notify\":0}}]}"),
},
validate: func(a *assert.Assertions, msg wrp.Message) error {
a.Equal(int64(http.StatusAccepted), *msg.Status)

return nil
},
},
}
for _, tc := range tests {
t.Run(tc.description, func(t *testing.T) {
assert := assert.New(t)
require := require.New(t)

nextCallCount := 0
next := wrpkit.HandlerFunc(func(wrp.Message) error {
nextCallCount++
return tc.nextResult
})

egressCallCount := 0
egress := wrpkit.HandlerFunc(func(msg wrp.Message) error {
egressCallCount++
if tc.validate != nil {
assert.NoError(tc.validate(assert, msg))
}
return tc.egressResult
})

mockDefaults := []Option{
FilePath("mock_tr181_test.json"),
}

h, err := New(next, egress, "some-source", zap.NewExample(), mockDefaults...)
require.NoError(err)

err = h.HandleWrp(tc.msg)
assert.ErrorIs(err, tc.expectedErr)

assert.Equal(tc.nextCallCount, nextCallCount)
assert.Equal(tc.egressCallCount, egressCallCount)
})
}
}
Loading

0 comments on commit 0bf2a07

Please sign in to comment.