Skip to content

Commit

Permalink
fixed tests
Browse files Browse the repository at this point in the history
Signed-off-by: Wojciech Sadowy <wojciech.sadowy@freedomfi.com>
  • Loading branch information
Wojciech Sadowy committed May 20, 2022
1 parent c7ae5d1 commit 550f354
Show file tree
Hide file tree
Showing 14 changed files with 520 additions and 272 deletions.
92 changes: 84 additions & 8 deletions dp/cloud/go/services/dp/builders_test/builders.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ func NewDBCbsdBuilder() *DBCbsdBuilder {
}
}

func (b *DBCbsdBuilder) Empty() *DBCbsdBuilder {
b.Cbsd = &storage.DBCbsd{}
return b
}

func (b *DBCbsdBuilder) WithId(id int64) *DBCbsdBuilder {
b.Cbsd.Id = db.MakeInt(id)
return b
Expand All @@ -60,6 +65,46 @@ func (b *DBCbsdBuilder) WithNetworkId(id string) *DBCbsdBuilder {
return b
}

func (b *DBCbsdBuilder) WithFccId(id string) *DBCbsdBuilder {
b.Cbsd.FccId = db.MakeString(id)
return b
}

func (b *DBCbsdBuilder) WithUserId(id string) *DBCbsdBuilder {
b.Cbsd.UserId = db.MakeString(id)
return b
}

func (b *DBCbsdBuilder) WithAntennaGain(gain float64) *DBCbsdBuilder {
b.Cbsd.AntennaGain = db.MakeFloat(gain)
return b
}

func (b *DBCbsdBuilder) WithLatitude(lat float64) *DBCbsdBuilder {
b.Cbsd.LatitudeDeg = db.MakeFloat(lat)
return b
}

func (b *DBCbsdBuilder) WithLongitude(lon float64) *DBCbsdBuilder {
b.Cbsd.LongitudeDeg = db.MakeFloat(lon)
return b
}

func (b *DBCbsdBuilder) WithNumberOfPorts(num int64) *DBCbsdBuilder {
b.Cbsd.NumberOfPorts = db.MakeInt(num)
return b
}

func (b *DBCbsdBuilder) WithMaxPower(pow float64) *DBCbsdBuilder {
b.Cbsd.MaxPower = db.MakeFloat(pow)
return b
}

func (b *DBCbsdBuilder) WithMinPower(pow float64) *DBCbsdBuilder {
b.Cbsd.MinPower = db.MakeFloat(pow)
return b
}

func (b *DBCbsdBuilder) WithLastSeen(t int64) *DBCbsdBuilder {
b.Cbsd.LastSeen = db.MakeTime(time.Unix(t, 0).UTC())
return b
Expand All @@ -86,10 +131,6 @@ func (b *DBCbsdBuilder) WithFullInstallationParam() *DBCbsdBuilder {
b.Cbsd.IndoorDeployment = db.MakeBool(true)
b.Cbsd.HeightM = db.MakeFloat(12.5)
b.Cbsd.HeightType = db.MakeString("agl")
b.Cbsd.AntennaAzimuthDeg = db.MakeInt(1)
b.Cbsd.AntennaDowntiltDeg = db.MakeInt(2)
b.Cbsd.AntennaBeamwidthDeg = db.MakeInt(3)
b.Cbsd.AntennaModel = db.MakeString(someModel)
b.Cbsd.AntennaGain = db.MakeFloat(4.5)
return b
}
Expand All @@ -111,6 +152,21 @@ func (b *DBCbsdBuilder) WithSingleStepEnabled(enabled bool) *DBCbsdBuilder {
return b
}

func (b *DBCbsdBuilder) WithShouldDeregister(should bool) *DBCbsdBuilder {
b.Cbsd.ShouldDeregister = db.MakeBool(should)
return b
}

func (b *DBCbsdBuilder) WithPreferredBandwidthMHz(bandwidth int64) *DBCbsdBuilder {
b.Cbsd.PreferredBandwidthMHz = db.MakeInt(bandwidth)
return b
}

func (b *DBCbsdBuilder) WithPreferredFrequenciesMHz(freq string) *DBCbsdBuilder {
b.Cbsd.PreferredFrequenciesMHz = db.MakeString(freq)
return b
}

func (b *DBCbsdBuilder) WithCbsdCategory(cat string) *DBCbsdBuilder {
b.Cbsd.CbsdCategory = db.MakeString(cat)
return b
Expand Down Expand Up @@ -183,6 +239,11 @@ func NewCbsdProtoPayloadBuilder() *CbsdProtoPayloadBuilder {
}
}

func (b *CbsdProtoPayloadBuilder) Empty() *CbsdProtoPayloadBuilder {
b.Payload = &protos.CbsdData{}
return b
}

func (b *CbsdProtoPayloadBuilder) WithSingleStepEnabled() *CbsdProtoPayloadBuilder {
b.Payload.SingleStepEnabled = true
return b
Expand All @@ -198,6 +259,11 @@ func (b *CbsdProtoPayloadBuilder) WithEmptyInstallationParam() *CbsdProtoPayload
return b
}

func (b *CbsdProtoPayloadBuilder) WithAntennaGain(gain float64) *CbsdProtoPayloadBuilder {
b.Payload.InstallationParam.AntennaGain = wrapperspb.Double(gain)
return b
}

func (b *CbsdProtoPayloadBuilder) WithFullInstallationParam() *CbsdProtoPayloadBuilder {
b.Payload.InstallationParam = &protos.InstallationParam{
LatitudeDeg: wrapperspb.Double(10.5),
Expand Down Expand Up @@ -334,11 +400,11 @@ func GetDetailedProtoCbsdList(builder *DetailedProtoCbsdBuilder) *protos.ListCbs
}
}

func GetMutableDBCbsd(cbsd *storage.DBCbsd) *storage.MutableCbsd {
func GetMutableDBCbsd(cbsd *storage.DBCbsd, state string) *storage.MutableCbsd {
return &storage.MutableCbsd{
Cbsd: cbsd,
DesiredState: &storage.DBCbsdState{
Name: db.MakeString(registered),
Name: db.MakeString(state),
},
}
}
Expand Down Expand Up @@ -430,12 +496,22 @@ func NewMutableCbsdModelPayloadBuilder() *MutableCbsdModelBuilder {
}}
}

func (b *MutableCbsdModelBuilder) withSingleStepEnabled() *MutableCbsdModelBuilder {
func (b *MutableCbsdModelBuilder) WithEmptyInstallationParam() *MutableCbsdModelBuilder {
b.Payload.InstallationParam = &models.InstallationParam{}
return b
}

func (b *MutableCbsdModelBuilder) WithAntennaGain(gain float64) *MutableCbsdModelBuilder {
b.Payload.InstallationParam.AntennaGain = to_pointer.Float(gain)
return b
}

func (b *MutableCbsdModelBuilder) WithSingleStepEnabled() *MutableCbsdModelBuilder {
b.Payload.SingleStepEnabled = to_pointer.Bool(true)
return b
}

func (b *MutableCbsdModelBuilder) withCbsdCategory(c string) *MutableCbsdModelBuilder {
func (b *MutableCbsdModelBuilder) WithCbsdCategory(c string) *MutableCbsdModelBuilder {
b.Payload.CbsdCategory = c
return b
}
57 changes: 39 additions & 18 deletions dp/cloud/go/services/dp/obsidian/cbsd/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,25 +234,46 @@ func (s *HandlersTestSuite) TestFetchNonexistentCbsd() {
}

func (s *HandlersTestSuite) TestCreateCbsd() {
e := echo.New()
obsidianHandlers := cbsd.GetHandlers()
payload := b.NewMutableCbsdModelPayloadBuilder().Payload
s.cbsdServer.createResponse = &protos.CreateCbsdResponse{}
s.cbsdServer.expectedCreateRequest = &protos.CreateCbsdRequest{
NetworkId: "n1",
Data: b.NewCbsdProtoPayloadBuilder().Payload,
}
createCbsd := tests.GetHandlerByPathAndMethod(s.T(), obsidianHandlers, cbsd.ManageCbsdsPath, obsidian.POST).HandlerFunc
tc := tests.Test{
Method: http.MethodPost,
URL: "/magma/v1/dp/n1/cbsds",
Payload: payload,
ParamNames: []string{"network_id"},
ParamValues: []string{"n1"},
Handler: createCbsd,
ExpectedStatus: http.StatusCreated,
testCases := []struct {
name string
inputPayload *models.MutableCbsd
expectedPayload *protos.CbsdData
}{{
name: "test create without installation param",
inputPayload: b.NewMutableCbsdModelPayloadBuilder().Payload,
expectedPayload: b.NewCbsdProtoPayloadBuilder().Payload,
}, {
name: "test create with antenna gain",
inputPayload: b.NewMutableCbsdModelPayloadBuilder().
WithEmptyInstallationParam().
WithAntennaGain(10.5).Payload,
expectedPayload: b.NewCbsdProtoPayloadBuilder().
WithEmptyInstallationParam().
WithAntennaGain(10.5).Payload,
}}
for _, tc := range testCases {
s.Run(tc.name, func() {
e := echo.New()
obsidianHandlers := cbsd.GetHandlers()
payload := tc.inputPayload
s.cbsdServer.createResponse = &protos.CreateCbsdResponse{}
s.cbsdServer.expectedCreateRequest = &protos.CreateCbsdRequest{
NetworkId: "n1",
Data: tc.expectedPayload,
}
createCbsd := tests.GetHandlerByPathAndMethod(s.T(), obsidianHandlers, cbsd.ManageCbsdsPath, obsidian.POST).HandlerFunc
tc := tests.Test{
Method: http.MethodPost,
URL: "/magma/v1/dp/n1/cbsds",
Payload: payload,
ParamNames: []string{"network_id"},
ParamValues: []string{"n1"},
Handler: createCbsd,
ExpectedStatus: http.StatusCreated,
}
tests.RunUnitTest(s.T(), e, tc)
})
}
tests.RunUnitTest(s.T(), e, tc)
}

func (s *HandlersTestSuite) TestCreateWithDuplicateUniqueFieldsReturnsConflict() {
Expand Down
77 changes: 68 additions & 9 deletions dp/cloud/go/services/dp/obsidian/models/conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ package models

import (
"github.com/go-openapi/strfmt"
"github.com/golang/protobuf/ptypes/wrappers"
"google.golang.org/protobuf/types/known/wrapperspb"

"magma/dp/cloud/go/protos"
"magma/dp/cloud/go/services/dp/obsidian/to_pointer"
Expand All @@ -36,7 +38,8 @@ func CbsdToBackend(m *MutableCbsd) *protos.CbsdData {
BandwidthMhz: m.FrequencyPreferences.BandwidthMhz,
FrequenciesMhz: m.FrequencyPreferences.FrequenciesMhz,
},
DesiredState: m.DesiredState,
DesiredState: m.DesiredState,
InstallationParam: getProtoInstallationParam(m.InstallationParam),
}
}

Expand All @@ -62,7 +65,7 @@ func CbsdFromBackend(details *protos.CbsdDetails) *Cbsd {
UserID: details.Data.UserId,
SingleStepEnabled: details.Data.SingleStepEnabled,
CbsdCategory: details.Data.CbsdCategory,
InstallationParam: getInstallationParam(details.Data.GetInstallationParam()),
InstallationParam: getModelInstallationParam(details.Data.GetInstallationParam()),
}
}

Expand All @@ -87,20 +90,76 @@ func getGrant(grant *protos.GrantDetails) *Grant {
}
}

func getInstallationParam(params *protos.InstallationParam) *InstallationParam {
func getModelInstallationParam(params *protos.InstallationParam) *InstallationParam {
if params == nil {
return nil
}
return &InstallationParam{
AntennaGain: params.AntennaGain.Value,
Heightm: params.HeightM.Value,
HeightType: params.HeightType.Value,
IndoorDeployment: params.IndoorDeployment.Value,
LatitudeDeg: params.LatitudeDeg.Value,
LongitudeDeg: params.LongitudeDeg.Value,
AntennaGain: doubleValueToFloatOrNil(params.AntennaGain),
Heightm: doubleValueToFloatOrNil(params.HeightM),
HeightType: stringValueToStringOrNil(params.HeightType),
IndoorDeployment: boolValueToBoolOrNil(params.IndoorDeployment),
LatitudeDeg: doubleValueToFloatOrNil(params.LatitudeDeg),
LongitudeDeg: doubleValueToFloatOrNil(params.LongitudeDeg),
}
}

func getProtoInstallationParam(params *InstallationParam) *protos.InstallationParam {
if params == nil {
return nil
}
return &protos.InstallationParam{
AntennaGain: floatToDoubleValueOrNil(params.AntennaGain),
HeightM: floatToDoubleValueOrNil(params.Heightm),
HeightType: stringToStringValueOrNil(params.HeightType),
IndoorDeployment: boolToBoolValueOrNil(params.IndoorDeployment),
LatitudeDeg: floatToDoubleValueOrNil(params.LatitudeDeg),
LongitudeDeg: floatToDoubleValueOrNil(params.LongitudeDeg),
}
}

func doubleValueToFloatOrNil(v *wrappers.DoubleValue) *float64 {
if v == nil {
return nil
}
return &v.Value
}

func boolValueToBoolOrNil(v *wrappers.BoolValue) *bool {
if v == nil {
return nil
}
return &v.Value
}

func stringValueToStringOrNil(v *wrappers.StringValue) *string {
if v == nil {
return nil
}
return &v.Value
}

func floatToDoubleValueOrNil(v *float64) *wrappers.DoubleValue {
if v == nil {
return nil
}
return wrapperspb.Double(*v)
}

func boolToBoolValueOrNil(v *bool) *wrapperspb.BoolValue {
if v == nil {
return nil
}
return wrapperspb.Bool(*v)
}

func stringToStringValueOrNil(v *string) *wrapperspb.StringValue {
if v == nil {
return nil
}
return wrapperspb.String(*v)
}

type LogInterface struct {
Body string `json:"log_message"`
FccID string `json:"fcc_id"`
Expand Down

0 comments on commit 550f354

Please sign in to comment.