Skip to content

Commit

Permalink
save
Browse files Browse the repository at this point in the history
  • Loading branch information
marcia-piccione committed Apr 2, 2024
1 parent 556190f commit a3f5bc3
Showing 1 changed file with 51 additions and 18 deletions.
69 changes: 51 additions & 18 deletions internal/wrphandlers/mocktr181/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ import (
"io"
"net/http"
"os"
"strings"

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

var (
Expand All @@ -20,14 +22,6 @@ var (
ErrUnableToReadFile = fmt.Errorf("unable to read file")
)

const (
// statusCode is the status code to return when a message is not authorized.
statusCode = 403

// wildcard is the wildcard partner id that matches all partner ids.
wildcard = "*"
)

// Option is a functional option type for WS.
type Option interface {
apply(*Handler) error
Expand All @@ -47,14 +41,16 @@ type Handler struct {
source string
filePath string
parameters *MockParameters
logger *zap.Logger
}

type MockParameter struct {
Name string
Value string
Access string
DataType int // add json labels here
Delay int
Name string
Value string
Access string
DataType int // add json labels here
Attributes map[string]interface{}
Delay int
}

type MockParameters struct {
Expand All @@ -67,6 +63,10 @@ type Payload struct {
Parameters []Parameter `json:"parameters"`
}

type Parameters struct {
Parameters []Parameter
}

type Parameter struct {
Name string `json:"name"`
Value string `json:"value"`
Expand All @@ -79,7 +79,7 @@ type Parameter struct {
// the handler that will be called to send the response if/when the next handler
// fails to handle the message. The parameter source is the source to use in
// the response message.
func New(next, egress wrpkit.Handler, source string, opts ...Option) (*Handler, error) {
func New(next, egress wrpkit.Handler, source string, logger *zap.Logger, opts ...Option) (*Handler, error) {
// TODO - load config from file system

h := Handler{
Expand All @@ -98,6 +98,7 @@ func New(next, egress wrpkit.Handler, source string, opts ...Option) (*Handler,

parameters, err := h.loadFile()
if err != nil {
h.logger.Error("unable to load mock parameters", zap.Error(err))
return nil, ErrUnableToReadFile
}

Expand All @@ -115,14 +116,15 @@ func (h Handler) HandleWrp(msg wrp.Message) error {
payload := make(map[string]interface{})
err := json.Unmarshal(msg.Payload, &payload)
if err != nil {
// TODO - need logger
h.logger.Error("unable to unmarshal msg payload", zap.Error(err))
return err
}

command := payload["command"].(string)
var payloadResponse []byte
var statusCode int64

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

switch command {
case "GET":
statusCode, payloadResponse = h.get(payload["names"].([]string))
Expand All @@ -131,7 +133,8 @@ func (h Handler) HandleWrp(msg wrp.Message) error {
statusCode = h.set(payload["parameters"].([]Parameter))

default:

// currently only get and set are implemented for existing mocktr181
statusCode = http.StatusOK
}

response := msg
Expand All @@ -148,19 +151,48 @@ func (h Handler) HandleWrp(msg wrp.Message) error {
}

func (h Handler) get(names []string) (int64, []byte) {
var payload []byte
result := []Parameter{}

for _, name := range names {
for _, mockParameter := range h.parameters.Parameters {
if strings.HasPrefix(mockParameter.Name, name) {
result = append(result, Parameter{
Name: mockParameter.Name,
Value: mockParameter.Value,
DataType: mockParameter.DataType,
Attributes: mockParameter.Attributes,
})
}
}
}

payload, err := json.Marshal(result)
if err != nil {
h.logger.Error("unable to marshal get result", zap.Error(err))
return http.StatusInternalServerError, payload
}

return http.StatusAccepted, payload
}

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

return http.StatusOK
}

func (h Handler) loadFile() (*MockParameters, 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()
Expand All @@ -169,6 +201,7 @@ func (h Handler) loadFile() (*MockParameters, error) {
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
}

Expand Down

0 comments on commit a3f5bc3

Please sign in to comment.