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

SonarCloud: add tests to increase code coverage (2) #287

Merged
merged 1 commit into from
Sep 21, 2022
Merged
Show file tree
Hide file tree
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
79 changes: 79 additions & 0 deletions schema/device/device_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package device_test

import (
"testing"

"github.com/plgd-dev/device/schema/device"
"github.com/stretchr/testify/require"
)

func TestDeviceGetManufacturerName(t *testing.T) {
type fields struct {
ManufacturerName []device.LocalizedString
}
tests := []struct {
name string
fields fields
want string
}{
{
name: "Empty",
fields: fields{
ManufacturerName: nil,
},
want: "",
},
{
name: "Slovak",
fields: fields{
ManufacturerName: []device.LocalizedString{
{
Language: "sk",
Value: "zariadenie",
},
},
},
want: "",
},
{
name: "English",
fields: fields{
ManufacturerName: []device.LocalizedString{
{
Language: "en",
Value: "device",
},
},
},
want: "device",
},
{
name: "Multiple",
fields: fields{
ManufacturerName: []device.LocalizedString{
{
Language: "sk",
Value: "zariadenie",
},
{
Language: "en",
Value: "device",
},
{
Language: "de",
Value: "Gerät",
},
},
},
want: "device",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
d := device.Device{
ManufacturerName: tt.fields.ManufacturerName,
}
require.Equal(t, d.GetManufacturerName(), tt.want)
})
}
}
198 changes: 198 additions & 0 deletions schema/link_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
package schema_test

import (
"testing"

"github.com/plgd-dev/device/schema"
"github.com/plgd-dev/device/schema/acl"
"github.com/plgd-dev/device/schema/ael"
"github.com/plgd-dev/device/schema/credential"
kitNet "github.com/plgd-dev/kit/v2/net"
"github.com/stretchr/testify/require"
)

func TestBitMaskHas(t *testing.T) {
b := schema.BitMask(0)
require.False(t, b.Has(schema.Discoverable))
require.False(t, b.Has(schema.Observable))

b = schema.Discoverable
require.True(t, b.Has(schema.Discoverable))
require.False(t, b.Has(schema.Observable))

b = schema.Discoverable | schema.Observable
require.True(t, b.Has(schema.Discoverable))
require.True(t, b.Has(schema.Observable))
}

func TestResourceLinkHasType(t *testing.T) {
type fields struct {
resourceTypes []string
}
type args struct {
resourceType string
}
tests := []struct {
name string
fields fields
args args
want bool
}{
{
name: "Empty",
fields: fields{
resourceTypes: []string{credential.ResourceType, acl.ResourceType},
},
args: args{
resourceType: "",
},
want: false,
},
{
name: "Mismatch",
fields: fields{
resourceTypes: []string{credential.ResourceType, acl.ResourceType},
},
args: args{
resourceType: ael.ResourceType,
},
want: false,
},
{
name: "Match",
fields: fields{
resourceTypes: []string{credential.ResourceType, acl.ResourceType},
},
args: args{
resourceType: acl.ResourceType,
},
want: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
r := schema.ResourceLink{
ResourceTypes: tt.fields.resourceTypes,
}
require.Equal(t, tt.want, r.HasType(tt.args.resourceType))
})
}
}

func TestResourceLinkPatchEndpoint(t *testing.T) {
type args struct {
addr kitNet.Addr
}
tests := []struct {
name string
args args
want schema.Endpoints
}{
{
name: "IPv4",
args: args{
addr: kitNet.MakeAddr("coap", "127.0.0.1", 5683),
},
want: schema.Endpoints{
schema.Endpoint{URI: "coap://127.0.0.1:5683"},
schema.Endpoint{URI: "coap+tcp://127.0.0.1:5683"},
schema.Endpoint{URI: "coaps+tcp://127.0.0.1:5684"},
},
},
{
name: "IPv6",
args: args{
addr: kitNet.MakeAddr("coap", "fe80::1", 5683),
},
want: schema.Endpoints{
schema.Endpoint{URI: "coap://[fe80::1]:5683"},
schema.Endpoint{URI: "coap+tcp://[fe80::1]:5683"},
schema.Endpoint{URI: "coaps+tcp://[fe80::1]:5684"},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
r := schema.ResourceLink{
Policy: &schema.Policy{
UDPPort: 5683,
TCPPort: 5683,
TCPTLSPort: 5684,
},
}
got := r.PatchEndpoint(tt.args.addr, nil)
require.Len(t, got.Endpoints, len(tt.want))
if len(tt.want) > 0 {
got.Endpoints = got.Endpoints.Sort()
tt.want = tt.want.Sort()
for i := range got.Endpoints {
require.Equal(t, got.Endpoints[i], tt.want[i])
}
}
})
}
}

func TestResourceLinkPatchEndpointLinkLocal(t *testing.T) {
type fields struct {
Endpoints schema.Endpoints
}
type args struct {
addr kitNet.Addr
}
tests := []struct {
name string
fields fields
args args
want schema.Endpoints
}{
{
name: "IPv4",
fields: fields{
Endpoints: schema.Endpoints{
schema.Endpoint{
URI: "coap://169.254.0.1:5683",
},
},
},
args: args{
addr: kitNet.MakeAddr("coap", "169.254.0.1", 5683),
},
want: schema.Endpoints{
schema.Endpoint{URI: "coap://169.254.0.1:5683"},
},
},
{
name: "IPv6",
fields: fields{
Endpoints: schema.Endpoints{
schema.Endpoint{
URI: "coap://[fe80::1]:5683",
},
},
},
args: args{
addr: kitNet.MakeAddr("coap", "fe80::1%eth0", 0),
},
want: schema.Endpoints{
schema.Endpoint{URI: "coap://[fe80::1%eth0]:5683"},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
r := schema.ResourceLink{
Endpoints: tt.fields.Endpoints,
}
got := r.PatchEndpoint(tt.args.addr, nil)
require.Len(t, got.Endpoints, len(tt.want))
if len(tt.want) > 0 {
got.Endpoints = got.Endpoints.Sort()
tt.want = tt.want.Sort()
for i := range got.Endpoints {
require.Equal(t, got.Endpoints[i], tt.want[i])
}
}
})
}
}
2 changes: 1 addition & 1 deletion schema/pstat/pstat.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func (s OperationalState) String() string {
case OperationalState_RFNOP:
return "RFNOP"
case OperationalState_SRESET:
return "OperationalState_SRESET"
return "SRESET"
default:
return fmt.Sprintf("unknown %v", int(s))
}
Expand Down
2 changes: 1 addition & 1 deletion schema/pstat/pstat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func TestOperationalStateString(t *testing.T) {
pstat.OperationalState_RFOTM: "RFOTM",
pstat.OperationalState_RFPRO: "RFPRO",
pstat.OperationalState_RFNOP: "RFNOP",
pstat.OperationalState_SRESET: "OperationalState_SRESET",
pstat.OperationalState_SRESET: "SRESET",
}

for k, v := range states {
Expand Down