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: update mock181's set to check for non-writable params before updating params and for non-existing parameters #192

Merged
merged 4 commits into from
Jul 18, 2024
Merged
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
74 changes: 56 additions & 18 deletions internal/wrphandlers/mocktr181/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,32 +205,70 @@
Names: tr181.Names,
StatusCode: http.StatusAccepted,
}

var (
writableParams []*MockParameter
failedParams []Parameter
)
// Check for any parameters that are not writable.
for _, parameter := range tr181.Parameters {
var found bool
for i := range h.parameters {
mockParameter := &h.parameters[i]
if mockParameter.Name != parameter.Name {
continue
}

switch mockParameter.Access {
case "w", "wr", "rw":
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",
})
result.StatusCode = 520
// Check whether mockParameter is writable.
if strings.Contains(mockParameter.Access, "w") {
found = true
// Add mockParameter to the list of parameters to be updated.
writableParams = append(writableParams, mockParameter)
continue
denopink marked this conversation as resolved.
Show resolved Hide resolved
}

// mockParameter is not writable.
failedParams = append(failedParams, Parameter{
Name: mockParameter.Name,
Message: "Parameter is not writable",
})
}

if !found {
// Requested parameter was not found.
failedParams = append(failedParams, Parameter{
Name: parameter.Name,
Message: "Invalid parameter name",
})
}
}

// Check if any parameters failed.
if len(failedParams) != 0 {
// If any parameter failed, then do not apply any changes to the parameters in writableParams.
writableParams = nil
result.Parameters = failedParams
result.StatusCode = 520
}

// If all the selected parameters are writable, then update the parameters. Otherwise, do nothing.
for _, parameter := range tr181.Parameters {
// writableParams will be nil if any parameters failed (i.e.: were not writable).
for _, mockParameter := range writableParams {
if mockParameter.Name != parameter.Name {
continue

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

View check run for this annotation

Codecov / codecov/patch

internal/wrphandlers/mocktr181/handler.go#L259

Added line #L259 was not covered by tests
}

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",
})
}
}

Expand Down
Loading