Skip to content

Commit

Permalink
fix(dp): add max_ibw_mhz validation (#13293)
Browse files Browse the repository at this point in the history
* fix(dp): add max_ibw_mhz validation

Signed-off-by: Jarosław Jaszczuk <jaroslaw.jaszczuk@freedomfi.com>

* validate that max_ibw_mhz is not less than bandwidth_mhz

Signed-off-by: Jarosław Jaszczuk <jaroslaw.jaszczuk@freedomfi.com>

* add MutableCbsd.ValidateModel tests

Signed-off-by: Jarosław Jaszczuk <jaroslaw.jaszczuk@freedomfi.com>

* reorganize validation_test.go imports

Signed-off-by: Jarosław Jaszczuk <jaroslaw.jaszczuk@freedomfi.com>

Co-authored-by: Jarosław Jaszczuk <jaroslaw.jaszczuk@freedomfi.com>
  • Loading branch information
hejjestemjarek and Jarosław Jaszczuk committed Jul 18, 2022
1 parent f9641e2 commit e0cf136
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 19 deletions.
8 changes: 8 additions & 0 deletions dp/cloud/go/services/dp/obsidian/cbsd/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,14 @@ func (s *HandlersTestSuite) TestCreateCbsd() {
Payload,
expectedStatus: http.StatusBadRequest,
expectedError: "grant_redundancy cannot be set to false when carrier_aggregation_enabled is enabled",
}, {
name: "test create with max_ibw_mhz lesser than bandwidth_mhz",
inputPayload: b.NewMutableCbsdModelPayloadBuilder().
WithBandwidth(10).
WithMaxIbwMhz(5).
Payload,
expectedStatus: http.StatusBadRequest,
expectedError: "max_ibw_mhz cannot be less than bandwidth_mhz",
}, {
name: "test failed model validation raises 400",
inputPayload: b.NewMutableCbsdModelPayloadBuilder().
Expand Down
10 changes: 10 additions & 0 deletions dp/cloud/go/services/dp/obsidian/models/capabilities_swaggergen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 0 additions & 19 deletions dp/cloud/go/services/dp/obsidian/models/conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@ limitations under the License.
package models

import (
"context"
"fmt"

oerrors "github.com/go-openapi/errors"
"github.com/go-openapi/strfmt"

"magma/dp/cloud/go/protos"
Expand Down Expand Up @@ -79,21 +75,6 @@ func CbsdFromBackend(details *protos.CbsdDetails) *Cbsd {
}
}

func (m *MutableCbsd) ValidateModel(ctx context.Context) error {
if err := m.Validate(strfmt.Default); err != nil {
return err
}
var res []error
if !*m.GrantRedundancy && *m.CarrierAggregationEnabled {
err := fmt.Errorf("grant_redundancy cannot be set to false when carrier_aggregation_enabled is enabled")
res = append(res, err)
}
if len(res) > 0 {
return oerrors.CompositeValidationError(res...)
}
return nil
}

func makeSliceNotNil(s []int64) []int64 {
if len(s) == 0 {
return []int64{}
Expand Down
2 changes: 2 additions & 0 deletions dp/cloud/go/services/dp/obsidian/models/swagger.v1.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ definitions:
description: this is the maximum allowed difference in MHz between leftmost end of leftmost channel and rightmost end of rightmost channel used by a Base Station (eNB)
type: integer
x-nullable: false
maximum: 150
multipleOf: 5
min_power:
description: min tx power available on cbsd
type: number
Expand Down
28 changes: 28 additions & 0 deletions dp/cloud/go/services/dp/obsidian/models/validation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package models

import (
"context"
"fmt"

"github.com/go-openapi/errors"
"github.com/go-openapi/strfmt"
)

func (m *MutableCbsd) ValidateModel(_ context.Context) error {
if err := m.Validate(strfmt.Default); err != nil {
return err
}
var res []error
if !*m.GrantRedundancy && *m.CarrierAggregationEnabled {
err := fmt.Errorf("grant_redundancy cannot be set to false when carrier_aggregation_enabled is enabled")
res = append(res, err)
}
if m.Capabilities.MaxIbwMhz < m.FrequencyPreferences.BandwidthMhz {
err := fmt.Errorf("max_ibw_mhz cannot be less than bandwidth_mhz")
res = append(res, err)
}
if len(res) > 0 {
return errors.CompositeValidationError(res...)
}
return nil
}
37 changes: 37 additions & 0 deletions dp/cloud/go/services/dp/obsidian/models/validation_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package models_test

import (
"context"
"testing"

"github.com/go-openapi/strfmt"
"github.com/stretchr/testify/assert"

b "magma/dp/cloud/go/services/dp/builders"
"magma/dp/cloud/go/services/dp/obsidian/models"
"magma/dp/cloud/go/services/dp/obsidian/to_pointer"
)

func TestMutableCbsd_Validate(t *testing.T) {
Expand Down Expand Up @@ -83,6 +85,14 @@ func TestMutableCbsd_Validate(t *testing.T) {
name: "Should validate max ibw mhz",
data: b.NewMutableCbsdModelPayloadBuilder().WithMaxIbwMhz(0).Payload,
expectedError: "max_ibw_mhz in body is required",
}, {
name: "Should validate max ibw mhz too high",
data: b.NewMutableCbsdModelPayloadBuilder().WithMaxIbwMhz(151).Payload,
expectedError: "max_ibw_mhz in body should be less than or equal to 150",
}, {
name: "Should validate max ibw mhz not multiple of 5",
data: b.NewMutableCbsdModelPayloadBuilder().WithMaxIbwMhz(7).Payload,
expectedError: "max_ibw_mhz in body should be a multiple of 5",
}}
for _, tt := range testData {
t.Run(tt.name, func(t *testing.T) {
Expand All @@ -91,3 +101,30 @@ func TestMutableCbsd_Validate(t *testing.T) {
})
}
}

func TestMutableCbsd_ValidateModel(t *testing.T) {
testData := []struct {
name string
data *models.MutableCbsd
expectedError string
}{{
name: "Should validate grant_redundancy false with carrier aggregation enabled",
data: b.NewMutableCbsdModelPayloadBuilder().
WithGrantRedundancy(to_pointer.Bool(false)).
WithCarrierAggregationEnabled(to_pointer.Bool(true)).
Payload,
expectedError: "grant_redundancy cannot be set to false when carrier_aggregation_enabled is enabled",
}, {
name: "Should validate max ibw mhz lesser than bandwidth mhz",
data: b.NewMutableCbsdModelPayloadBuilder().
WithMaxIbwMhz(5).WithBandwidth(10).Payload,
expectedError: "max_ibw_mhz cannot be less than bandwidth_mhz",
}}
c := context.TODO()
for _, tt := range testData {
t.Run(tt.name, func(t *testing.T) {
err := tt.data.ValidateModel(c)
assert.Contains(t, err.Error(), tt.expectedError)
})
}
}
2 changes: 2 additions & 0 deletions orc8r/cloud/go/services/obsidian/swagger/v1/swagger.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit e0cf136

Please sign in to comment.