Skip to content

Commit

Permalink
fixup! snippet-service: add ResourceChanged event subscription
Browse files Browse the repository at this point in the history
  • Loading branch information
Danielius1922 committed Jun 12, 2024
1 parent 61ae294 commit 2467abf
Show file tree
Hide file tree
Showing 34 changed files with 636 additions and 262 deletions.
4 changes: 2 additions & 2 deletions cloud2cloud-gateway/test/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"crypto/tls"
"net"
"slices"
"sync"
"testing"
"time"
Expand All @@ -15,7 +16,6 @@ import (
"github.com/plgd-dev/hub/v2/pkg/mongodb"
"github.com/plgd-dev/hub/v2/pkg/net/http"
"github.com/plgd-dev/hub/v2/pkg/security/certManager/server"
pkgStrings "github.com/plgd-dev/hub/v2/pkg/strings"
"github.com/plgd-dev/hub/v2/test/config"
testHttp "github.com/plgd-dev/hub/v2/test/http"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -98,7 +98,7 @@ func C2CURI(uri string) string {
func GetUniqueSubscriptionID(subIDS ...string) string {
id := uuid.NewString()
for {
if !pkgStrings.Contains(subIDS, id) {
if !slices.Contains(subIDS, id) {
break
}
id = uuid.NewString()
Expand Down
4 changes: 2 additions & 2 deletions coap-gateway/service/clientObserveHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"errors"
"fmt"
"io"
"slices"
"sync"
"sync/atomic"

Expand All @@ -17,7 +18,6 @@ import (
"github.com/plgd-dev/hub/v2/coap-gateway/service/message"
"github.com/plgd-dev/hub/v2/grpc-gateway/pb"
"github.com/plgd-dev/hub/v2/grpc-gateway/subscription"
"github.com/plgd-dev/hub/v2/pkg/strings"
"github.com/plgd-dev/hub/v2/resource-aggregate/commands"
"github.com/plgd-dev/hub/v2/resource-aggregate/events"
)
Expand Down Expand Up @@ -119,7 +119,7 @@ func (s *resourceSubscription) isDuplicateEvent(ev *events.ResourceChanged) bool
func (s *resourceSubscription) eventHandler(e *pb.Event) error {
switch {
case e.GetResourceUnpublished() != nil:
if !strings.Contains(e.GetResourceUnpublished().GetHrefs(), s.href) {
if !slices.Contains(e.GetResourceUnpublished().GetHrefs(), s.href) {
return nil
}
s.cancelSubscription(coapCodes.ServiceUnavailable)
Expand Down
4 changes: 2 additions & 2 deletions coap-gateway/service/observation/deviceObserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"io"
"net"
"slices"

"github.com/plgd-dev/device/v2/schema"
"github.com/plgd-dev/device/v2/schema/interfaces"
Expand All @@ -16,7 +17,6 @@ import (
"github.com/plgd-dev/hub/v2/grpc-gateway/pb"
"github.com/plgd-dev/hub/v2/pkg/log"
"github.com/plgd-dev/hub/v2/pkg/net/coap"
pkgStrings "github.com/plgd-dev/hub/v2/pkg/strings"
"github.com/plgd-dev/hub/v2/resource-aggregate/commands"
pbRD "github.com/plgd-dev/hub/v2/resource-directory/pb"
"google.golang.org/grpc"
Expand Down Expand Up @@ -308,7 +308,7 @@ func IsDiscoveryResourceObservable(links schema.ResourceLinks) (bool, error) {
return observable, nil
}

return pkgStrings.Contains(res.Interfaces, observeInterface), nil
return slices.Contains(res.Interfaces, observeInterface), nil
}

func detectObservationType(links schema.ResourceLinks) (ObservationType, error) {
Expand Down
4 changes: 2 additions & 2 deletions coap-gateway/service/signIn.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"slices"
"time"

"github.com/plgd-dev/go-coap/v3/message"
Expand All @@ -14,7 +15,6 @@ import (
grpcgwClient "github.com/plgd-dev/hub/v2/grpc-gateway/client"
"github.com/plgd-dev/hub/v2/identity-store/events"
kitNetGrpc "github.com/plgd-dev/hub/v2/pkg/net/grpc"
"github.com/plgd-dev/hub/v2/pkg/strings"
pkgTime "github.com/plgd-dev/hub/v2/pkg/time"
"github.com/plgd-dev/hub/v2/resource-aggregate/commands"
"github.com/plgd-dev/kit/v2/codec/cbor"
Expand Down Expand Up @@ -173,7 +173,7 @@ func subscribeToDeviceEvents(client *session, owner, deviceID string) error {
if evt.GetOwner() != owner {
return
}
if !strings.Contains(evt.GetDeviceIds(), deviceID) {
if !slices.Contains(evt.GetDeviceIds(), deviceID) {
return
}
client.Close()
Expand Down
21 changes: 0 additions & 21 deletions grpc-gateway/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,16 @@ package client_test

import (
"context"
"crypto/tls"
"crypto/x509"
"testing"

"github.com/plgd-dev/device/v2/schema/device"
"github.com/plgd-dev/go-coap/v3/message"
"github.com/plgd-dev/hub/v2/grpc-gateway/client"
"github.com/plgd-dev/hub/v2/grpc-gateway/pb"
"github.com/plgd-dev/hub/v2/pkg/net/grpc/server"
"github.com/plgd-dev/hub/v2/resource-aggregate/commands"
"github.com/plgd-dev/hub/v2/resource-aggregate/events"
"github.com/plgd-dev/hub/v2/test"
"github.com/plgd-dev/hub/v2/test/config"
"github.com/plgd-dev/kit/v2/codec/cbor"
"github.com/stretchr/testify/require"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
Expand All @@ -26,22 +21,6 @@ const (
TestManufacturer = "Test Manufacturer"
)

func NewTestClient(t *testing.T) *client.Client {
rootCAs := x509.NewCertPool()
for _, c := range test.GetRootCertificateAuthorities(t) {
rootCAs.AddCert(c)
}
tlsCfg := tls.Config{
RootCAs: rootCAs,
}
clientConfig := client.Config{
GatewayAddress: config.GRPC_GW_HOST,
}
c, err := client.NewFromConfig(&clientConfig, &tlsCfg)
require.NoError(t, err)
return c
}

func NewGateway(addr string) (*server.Server, error) {
s, err := server.NewServer(addr)
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion grpc-gateway/client/createResource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"time"

"github.com/plgd-dev/device/v2/schema/device"
grpcgwTest "github.com/plgd-dev/hub/v2/grpc-gateway/test"
kitNetGrpc "github.com/plgd-dev/hub/v2/pkg/net/grpc"
"github.com/plgd-dev/hub/v2/test"
"github.com/plgd-dev/hub/v2/test/config"
Expand Down Expand Up @@ -67,7 +68,7 @@ func TestClient_CreateResource(t *testing.T) {

ctx = kitNetGrpc.CtxWithToken(ctx, oauthTest.GetDefaultAccessToken(t))

c := NewTestClient(t)
c := grpcgwTest.NewTestClient(t)
defer func() {
err := c.Close()
require.NoError(t, err)
Expand Down
5 changes: 3 additions & 2 deletions grpc-gateway/client/deleteResource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/plgd-dev/device/v2/schema/resources"
"github.com/plgd-dev/hub/v2/grpc-gateway/client"
extCodes "github.com/plgd-dev/hub/v2/grpc-gateway/pb/codes"
grpcgwTest "github.com/plgd-dev/hub/v2/grpc-gateway/test"
kitNetGrpc "github.com/plgd-dev/hub/v2/pkg/net/grpc"
"github.com/plgd-dev/hub/v2/test"
"github.com/plgd-dev/hub/v2/test/config"
Expand Down Expand Up @@ -83,7 +84,7 @@ func TestClientDeleteResource(t *testing.T) {

ctx = kitNetGrpc.CtxWithToken(ctx, oauthTest.GetDefaultAccessToken(t))

c := NewTestClient(t)
c := grpcgwTest.NewTestClient(t)
defer func() {
err := c.Close()
require.NoError(t, err)
Expand Down Expand Up @@ -172,7 +173,7 @@ func TestClientBatchDeleteResource(t *testing.T) {

ctx = kitNetGrpc.CtxWithToken(ctx, oauthTest.GetDefaultAccessToken(t))

c := NewTestClient(t)
c := grpcgwTest.NewTestClient(t)
defer func() {
err := c.Close()
require.NoError(t, err)
Expand Down
7 changes: 4 additions & 3 deletions grpc-gateway/client/deviceSubscriptions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/plgd-dev/device/v2/schema/platform"
"github.com/plgd-dev/go-coap/v3/message"
"github.com/plgd-dev/hub/v2/grpc-gateway/pb"
grpcgwTest "github.com/plgd-dev/hub/v2/grpc-gateway/test"
"github.com/plgd-dev/hub/v2/pkg/fsnotify"
"github.com/plgd-dev/hub/v2/pkg/log"
kitNetGrpc "github.com/plgd-dev/hub/v2/pkg/net/grpc"
Expand Down Expand Up @@ -82,7 +83,7 @@ func TestObserveDeviceResourcesRetrieve(t *testing.T) {
}()
rac := raservice.NewResourceAggregateClient(raConn.GRPC())

c := NewTestClient(t)
c := grpcgwTest.NewTestClient(t)
defer func() {
errC := c.Close()
require.NoError(t, errC)
Expand Down Expand Up @@ -177,7 +178,7 @@ func TestObserveDeviceResourcesUpdate(t *testing.T) {
}()
rac := raservice.NewResourceAggregateClient(raConn.GRPC())

c := NewTestClient(t)
c := grpcgwTest.NewTestClient(t)
defer func() {
errC := c.Close()
require.NoError(t, errC)
Expand Down Expand Up @@ -306,7 +307,7 @@ func TestObserveDeviceResourcesCreateAndDelete(t *testing.T) {
}()
rac := raservice.NewResourceAggregateClient(raConn.GRPC())

c := NewTestClient(t)
c := grpcgwTest.NewTestClient(t)
defer func() {
errC := c.Close()
require.NoError(t, errC)
Expand Down
3 changes: 2 additions & 1 deletion grpc-gateway/client/getDevice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/plgd-dev/device/v2/test/resource/types"
"github.com/plgd-dev/hub/v2/grpc-gateway/client"
"github.com/plgd-dev/hub/v2/grpc-gateway/pb"
grpcgwTest "github.com/plgd-dev/hub/v2/grpc-gateway/test"
kitNetGrpc "github.com/plgd-dev/hub/v2/pkg/net/grpc"
"github.com/plgd-dev/hub/v2/resource-aggregate/commands"
test "github.com/plgd-dev/hub/v2/test"
Expand Down Expand Up @@ -92,7 +93,7 @@ func TestClient_GetDevice(t *testing.T) {

ctx = kitNetGrpc.CtxWithToken(ctx, oauthTest.GetDefaultAccessToken(t))

c := NewTestClient(t)
c := grpcgwTest.NewTestClient(t)
defer func() {
err := c.Close()
require.NoError(t, err)
Expand Down
3 changes: 2 additions & 1 deletion grpc-gateway/client/getDevices_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"time"

"github.com/plgd-dev/hub/v2/grpc-gateway/client"
grpcgwTest "github.com/plgd-dev/hub/v2/grpc-gateway/test"
kitNetGrpc "github.com/plgd-dev/hub/v2/pkg/net/grpc"
test "github.com/plgd-dev/hub/v2/test"
"github.com/plgd-dev/hub/v2/test/config"
Expand Down Expand Up @@ -69,7 +70,7 @@ func TestClient_GetDevices(t *testing.T) {

ctx = kitNetGrpc.CtxWithToken(ctx, oauthTest.GetDefaultAccessToken(t))

c := NewTestClient(t)
c := grpcgwTest.NewTestClient(t)
defer func() {
err := c.Close()
require.NoError(t, err)
Expand Down
3 changes: 2 additions & 1 deletion grpc-gateway/client/getResource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/plgd-dev/device/v2/schema/configuration"
"github.com/plgd-dev/device/v2/schema/interfaces"
"github.com/plgd-dev/hub/v2/grpc-gateway/client"
grpcgwTest "github.com/plgd-dev/hub/v2/grpc-gateway/test"
kitNetGrpc "github.com/plgd-dev/hub/v2/pkg/net/grpc"
"github.com/plgd-dev/hub/v2/test"
"github.com/plgd-dev/hub/v2/test/config"
Expand Down Expand Up @@ -100,7 +101,7 @@ func TestClientGetResource(t *testing.T) {

ctx = kitNetGrpc.CtxWithToken(ctx, oauthTest.GetDefaultAccessToken(t))

c := NewTestClient(t)
c := grpcgwTest.NewTestClient(t)
defer func() {
err := c.Close()
require.NoError(t, err)
Expand Down
4 changes: 2 additions & 2 deletions grpc-gateway/client/maintenance.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package client
import (
"context"
"net/http"
"slices"

"github.com/plgd-dev/device/v2/schema/maintenance"
"github.com/plgd-dev/hub/v2/pkg/net/grpc"
"github.com/plgd-dev/hub/v2/pkg/strings"
"github.com/plgd-dev/hub/v2/resource-aggregate/events"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
Expand Down Expand Up @@ -65,7 +65,7 @@ func (c *Client) updateMaintenanceResource(
}
var href string
for _, r := range v.GetResources() {
if r.GetDeviceId() == deviceID && strings.Contains(r.GetResourceTypes(), maintenance.ResourceType) {
if r.GetDeviceId() == deviceID && slices.Contains(r.GetResourceTypes(), maintenance.ResourceType) {
href = r.GetHref()
break
}
Expand Down
5 changes: 3 additions & 2 deletions grpc-gateway/client/maintenance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"testing"
"time"

grpcgwTest "github.com/plgd-dev/hub/v2/grpc-gateway/test"
kitNetGrpc "github.com/plgd-dev/hub/v2/pkg/net/grpc"
test "github.com/plgd-dev/hub/v2/test"
"github.com/plgd-dev/hub/v2/test/config"
Expand Down Expand Up @@ -50,7 +51,7 @@ func TestClientFactoryReset(t *testing.T) {

ctx = kitNetGrpc.CtxWithToken(ctx, oauthTest.GetDefaultAccessToken(t))

c := NewTestClient(t)
c := grpcgwTest.NewTestClient(t)
defer func() {
err := c.Close()
require.NoError(t, err)
Expand Down Expand Up @@ -104,7 +105,7 @@ func TestClientReboot(t *testing.T) {

ctx = kitNetGrpc.CtxWithToken(ctx, oauthTest.GetDefaultAccessToken(t))

c := NewTestClient(t)
c := grpcgwTest.NewTestClient(t)
defer func() {
err := c.Close()
require.NoError(t, err)
Expand Down
3 changes: 2 additions & 1 deletion grpc-gateway/client/observeDeviceResources_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"testing"
"time"

grpcgwTest "github.com/plgd-dev/hub/v2/grpc-gateway/test"
kitNetGrpc "github.com/plgd-dev/hub/v2/pkg/net/grpc"
"github.com/plgd-dev/hub/v2/resource-aggregate/events"
test "github.com/plgd-dev/hub/v2/test"
Expand All @@ -24,7 +25,7 @@ func TestObserveDeviceResourcesPublish(t *testing.T) {
defer tearDown()
ctx = kitNetGrpc.CtxWithToken(ctx, oauthTest.GetDefaultAccessToken(t))

c := NewTestClient(t)
c := grpcgwTest.NewTestClient(t)
defer func() {
err := c.Close()
require.NoError(t, err)
Expand Down
3 changes: 2 additions & 1 deletion grpc-gateway/client/observeDevices_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"testing"

client "github.com/plgd-dev/hub/v2/grpc-gateway/client"
grpcgwTest "github.com/plgd-dev/hub/v2/grpc-gateway/test"
kitNetGrpc "github.com/plgd-dev/hub/v2/pkg/net/grpc"
"github.com/plgd-dev/hub/v2/test"
"github.com/plgd-dev/hub/v2/test/config"
Expand All @@ -23,7 +24,7 @@ func TestObserveDevices(t *testing.T) {
defer tearDown()
ctx = kitNetGrpc.CtxWithToken(ctx, oauthTest.GetDefaultAccessToken(t))

c := NewTestClient(t)
c := grpcgwTest.NewTestClient(t)
defer func() {
err := c.Close()
require.NoError(t, err)
Expand Down
3 changes: 2 additions & 1 deletion grpc-gateway/client/observeResource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"testing"

"github.com/plgd-dev/device/v2/schema/configuration"
grpcgwTest "github.com/plgd-dev/hub/v2/grpc-gateway/test"
kitNetGrpc "github.com/plgd-dev/hub/v2/pkg/net/grpc"
"github.com/plgd-dev/hub/v2/test"
"github.com/plgd-dev/hub/v2/test/config"
Expand All @@ -25,7 +26,7 @@ func TestObservingResource(t *testing.T) {
defer tearDown()
ctx = kitNetGrpc.CtxWithToken(ctx, oauthTest.GetDefaultAccessToken(t))

c := NewTestClient(t)
c := grpcgwTest.NewTestClient(t)
defer func() {
errC := c.Close()
require.NoError(t, errC)
Expand Down
5 changes: 3 additions & 2 deletions grpc-gateway/client/updateResource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/plgd-dev/device/v2/schema/configuration"
"github.com/plgd-dev/device/v2/schema/interfaces"
"github.com/plgd-dev/hub/v2/grpc-gateway/client"
grpcgwTest "github.com/plgd-dev/hub/v2/grpc-gateway/test"
kitNetGrpc "github.com/plgd-dev/hub/v2/pkg/net/grpc"
"github.com/plgd-dev/hub/v2/test"
"github.com/plgd-dev/hub/v2/test/config"
Expand Down Expand Up @@ -94,7 +95,7 @@ func TestClientUpdateResource(t *testing.T) {

ctx = kitNetGrpc.CtxWithToken(ctx, oauthTest.GetDefaultAccessToken(t))

c := NewTestClient(t)
c := grpcgwTest.NewTestClient(t)
defer func() {
errC := c.Close()
require.NoError(t, errC)
Expand Down Expand Up @@ -131,7 +132,7 @@ func TestUpdateConfigurationName(t *testing.T) {
defer tearDown()
ctx = kitNetGrpc.CtxWithToken(ctx, oauthTest.GetDefaultAccessToken(t))

c := NewTestClient(t)
c := grpcgwTest.NewTestClient(t)
defer func() {
err := c.Close()
require.NoError(t, err)
Expand Down
Loading

0 comments on commit 2467abf

Please sign in to comment.