1
0
mirror of https://github.com/mainflux/mainflux.git synced 2025-04-24 13:48:49 +08:00
Mainflux.mainflux/things/redis/streams_test.go

651 lines
20 KiB
Go
Raw Normal View History

// Copyright (c) Mainflux
// SPDX-License-Identifier: Apache-2.0
package redis_test
import (
2019-07-18 15:01:09 +02:00
"context"
"fmt"
"math"
"strconv"
"testing"
"time"
r "github.com/go-redis/redis"
"github.com/mainflux/mainflux/pkg/errors"
"github.com/mainflux/mainflux/pkg/uuid"
"github.com/mainflux/mainflux/things"
"github.com/mainflux/mainflux/things/mocks"
"github.com/mainflux/mainflux/things/redis"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
const (
streamID = "mainflux.things"
email = "user@example.com"
token = "token"
thingPrefix = "thing."
thingCreate = thingPrefix + "create"
thingUpdate = thingPrefix + "update"
thingRemove = thingPrefix + "remove"
thingConnect = thingPrefix + "connect"
thingDisconnect = thingPrefix + "disconnect"
channelPrefix = "channel."
channelCreate = channelPrefix + "create"
channelUpdate = channelPrefix + "update"
channelRemove = channelPrefix + "remove"
)
func newService(tokens map[string]string) things.Service {
MF-932 - User API keys (#941) * Add inital Auth implementation Signed-off-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com> * Extract IssuedAt on transport layer Signed-off-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com> * Add token type Signed-off-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com> * Fix Auth service URL in Things service Signed-off-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com> * Add User Keys revocation check Signed-off-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com> * Update tests Signed-off-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com> * Remove unused tracing methods Signed-off-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com> * Fix Key retrival and parsing Signed-off-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com> * Remove unused code Signed-off-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com> * Increase test coverage Signed-off-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com> * Fix compose files Signed-off-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com> * Fix typos Simplify tests. Signed-off-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com> * Fix typos and remove useless comments Signed-off-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com> * Rename Auth to Authn Signed-off-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com> * Rename database.go to tracin.go A new name (`tracing.go`) describes better the purpose of the file. Signed-off-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com> * Increase test coverage Fix typo. Signed-off-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com> * Increase test coverage Signed-off-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com> * Remove token from Users service Signed-off-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com> * Fix identify login keys Rename token parsing method. Signed-off-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com> * Extract tokenizer to interface Signed-off-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com> * Remove pointer time Signed-off-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com> * Use pointer for expiration time in response Signed-off-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com> * Use uppercase N Signed-off-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com> * Remove unnecessary email check Signed-off-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com> * Cleanup unused code and env vars Signed-off-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com> * Rename tokenizer field Signed-off-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com> * Use slices and named fields in test cases Signed-off-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com> * Update AuthN keys naming Signed-off-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com> * Remove package-lock.json changes Signed-off-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com> * Remove Secret from issuing request Signed-off-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com>
2019-12-16 16:22:09 +01:00
auth := mocks.NewAuthService(tokens)
MF-483 - Enable channels and devices corresponding lists in backend (#520) * Add list channels by thing id endpoint Add list channels by thing id endpoint to things service. Add pagination format and logic. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add fetch channels by thing endpoint Add fetch channels by thing endpoint with pagination. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update list endpoints to contain pagination Update list endpoints to contain pagination metadata. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add tests for two new endpoints Add tests for two new endpoints and update existing ones. Also, remove connected field from channel response. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Fix tests for SDK Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add SDK methods for new endpoints Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update swagger docs for things service Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add error handling to http tests Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Fix response body in http tests Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Remove unused responses from things service Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add test cases to things postgres tests Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add test cases for event sourcing Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com>
2019-01-08 11:53:24 +01:00
conns := make(chan mocks.Connection)
thingsRepo := mocks.NewThingRepository(conns)
channelsRepo := mocks.NewChannelRepository(thingsRepo, conns)
chanCache := mocks.NewChannelCache()
thingCache := mocks.NewThingCache()
idProvider := uuid.NewMock()
return things.New(auth, thingsRepo, channelsRepo, nil, chanCache, thingCache, idProvider)
}
func TestCreateThings(t *testing.T) {
redisClient.FlushAll().Err()
svc := newService(map[string]string{token: email})
svc = redis.NewEventStoreMiddleware(svc, redisClient)
cases := []struct {
desc string
ths []things.Thing
key string
err error
event map[string]interface{}
}{
{
desc: "create things successfully",
ths: []things.Thing{{
Name: "a",
Metadata: map[string]interface{}{"test": "test"},
}},
key: token,
err: nil,
event: map[string]interface{}{
"id": "1",
"name": "a",
"owner": email,
"metadata": "{\"test\":\"test\"}",
"operation": thingCreate,
},
},
{
desc: "create things with invalid credentials",
ths: []things.Thing{{Name: "a", Metadata: map[string]interface{}{"test": "test"}}},
key: "",
err: things.ErrUnauthorizedAccess,
event: nil,
},
}
lastID := "0"
for _, tc := range cases {
_, err := svc.CreateThings(context.Background(), tc.key, tc.ths...)
assert.True(t, errors.Contains(err, tc.err), fmt.Sprintf("%s: expected %s got %s\n", tc.desc, tc.err, err))
streams := redisClient.XRead(&r.XReadArgs{
Streams: []string{streamID, lastID},
Count: 1,
Block: time.Second,
}).Val()
var event map[string]interface{}
if len(streams) > 0 && len(streams[0].Messages) > 0 {
msg := streams[0].Messages[0]
event = msg.Values
lastID = msg.ID
}
assert.Equal(t, tc.event, event, fmt.Sprintf("%s: expected %v got %v\n", tc.desc, tc.event, event))
}
}
func TestUpdateThing(t *testing.T) {
redisClient.FlushAll().Err()
svc := newService(map[string]string{token: email})
// Create thing without sending event.
th := things.Thing{Name: "a", Metadata: map[string]interface{}{"test": "test"}}
sths, err := svc.CreateThings(context.Background(), token, th)
require.Nil(t, err, fmt.Sprintf("unexpected error %s", err))
sth := sths[0]
svc = redis.NewEventStoreMiddleware(svc, redisClient)
cases := []struct {
desc string
thing things.Thing
key string
err error
event map[string]interface{}
}{
{
MF-549 - Change metadata format from JSON string to JSON object (#706) * Update metadata type in things service Update things service so that metadata has map type. Update repo implementation by adding sqlx lib. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add sqlx lib to bootstrap service Add sqlx lib to bootstrap service and update metadata field type. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update metadata in redis streams consumer Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update tests for bootstrap service Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Fix mongo reader logging and driver version Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Fix mongo reader and writer Fix mongo reader and writer by updating driver version. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update SDK with new metadata format Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update LoRa adapter with new metadata format Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update users service in order to use sqlx Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Replace anonymous struct with map Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update docs for LoRa adapter Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Fix LoRa application metadata format Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Fix metadata format in LoRa docs Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add metadata2 var to SDK things test Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com>
2019-04-16 14:58:56 +02:00
desc: "update existing thing successfully",
thing: things.Thing{
ID: sth.ID,
Name: "a",
Metadata: map[string]interface{}{"test": "test"},
},
key: token,
err: nil,
event: map[string]interface{}{
MF-475 - Replace increment ID with UUID (#490) * Update increment ID to UUID in things service Update increment ID to UUID for things and channels in things service and proto files. Also, update ID type from uint to string. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in http adapter Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in ws adapter Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in CoAP adapter Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in normalizer service Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in writer services Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in reader services Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in SDK Update increment ID to UUID in SDK. Update id type to string. Update tests. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in mqtt adapter Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Remove unnecessary case from influxdb reader Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update tests in order to increase code coverage Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update lora adapter to use string ID instead of unsigned int Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com>
2018-12-05 13:09:25 +01:00
"id": sth.ID,
"name": "a",
MF-549 - Change metadata format from JSON string to JSON object (#706) * Update metadata type in things service Update things service so that metadata has map type. Update repo implementation by adding sqlx lib. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add sqlx lib to bootstrap service Add sqlx lib to bootstrap service and update metadata field type. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update metadata in redis streams consumer Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update tests for bootstrap service Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Fix mongo reader logging and driver version Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Fix mongo reader and writer Fix mongo reader and writer by updating driver version. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update SDK with new metadata format Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update LoRa adapter with new metadata format Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update users service in order to use sqlx Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Replace anonymous struct with map Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update docs for LoRa adapter Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Fix LoRa application metadata format Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Fix metadata format in LoRa docs Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add metadata2 var to SDK things test Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com>
2019-04-16 14:58:56 +02:00
"metadata": "{\"test\":\"test\"}",
"operation": thingUpdate,
},
},
}
lastID := "0"
for _, tc := range cases {
2019-07-18 15:01:09 +02:00
err := svc.UpdateThing(context.Background(), tc.key, tc.thing)
assert.True(t, errors.Contains(err, tc.err), fmt.Sprintf("%s: expected %s got %s\n", tc.desc, tc.err, err))
streams := redisClient.XRead(&r.XReadArgs{
Streams: []string{streamID, lastID},
Count: 1,
Block: time.Second,
}).Val()
var event map[string]interface{}
if len(streams) > 0 && len(streams[0].Messages) > 0 {
msg := streams[0].Messages[0]
event = msg.Values
lastID = msg.ID
}
assert.Equal(t, tc.event, event, fmt.Sprintf("%s: expected %v got %v\n", tc.desc, tc.event, event))
}
}
MF-483 - Enable channels and devices corresponding lists in backend (#520) * Add list channels by thing id endpoint Add list channels by thing id endpoint to things service. Add pagination format and logic. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add fetch channels by thing endpoint Add fetch channels by thing endpoint with pagination. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update list endpoints to contain pagination Update list endpoints to contain pagination metadata. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add tests for two new endpoints Add tests for two new endpoints and update existing ones. Also, remove connected field from channel response. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Fix tests for SDK Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add SDK methods for new endpoints Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update swagger docs for things service Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add error handling to http tests Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Fix response body in http tests Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Remove unused responses from things service Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add test cases to things postgres tests Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add test cases for event sourcing Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com>
2019-01-08 11:53:24 +01:00
func TestViewThing(t *testing.T) {
redisClient.FlushAll().Err()
MF-483 - Enable channels and devices corresponding lists in backend (#520) * Add list channels by thing id endpoint Add list channels by thing id endpoint to things service. Add pagination format and logic. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add fetch channels by thing endpoint Add fetch channels by thing endpoint with pagination. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update list endpoints to contain pagination Update list endpoints to contain pagination metadata. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add tests for two new endpoints Add tests for two new endpoints and update existing ones. Also, remove connected field from channel response. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Fix tests for SDK Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add SDK methods for new endpoints Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update swagger docs for things service Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add error handling to http tests Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Fix response body in http tests Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Remove unused responses from things service Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add test cases to things postgres tests Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add test cases for event sourcing Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com>
2019-01-08 11:53:24 +01:00
svc := newService(map[string]string{token: email})
// Create thing without sending event.
sths, err := svc.CreateThings(context.Background(), token, things.Thing{Name: "a"})
MF-483 - Enable channels and devices corresponding lists in backend (#520) * Add list channels by thing id endpoint Add list channels by thing id endpoint to things service. Add pagination format and logic. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add fetch channels by thing endpoint Add fetch channels by thing endpoint with pagination. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update list endpoints to contain pagination Update list endpoints to contain pagination metadata. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add tests for two new endpoints Add tests for two new endpoints and update existing ones. Also, remove connected field from channel response. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Fix tests for SDK Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add SDK methods for new endpoints Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update swagger docs for things service Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add error handling to http tests Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Fix response body in http tests Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Remove unused responses from things service Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add test cases to things postgres tests Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add test cases for event sourcing Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com>
2019-01-08 11:53:24 +01:00
require.Nil(t, err, fmt.Sprintf("unexpected error %s", err))
sth := sths[0]
MF-483 - Enable channels and devices corresponding lists in backend (#520) * Add list channels by thing id endpoint Add list channels by thing id endpoint to things service. Add pagination format and logic. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add fetch channels by thing endpoint Add fetch channels by thing endpoint with pagination. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update list endpoints to contain pagination Update list endpoints to contain pagination metadata. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add tests for two new endpoints Add tests for two new endpoints and update existing ones. Also, remove connected field from channel response. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Fix tests for SDK Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add SDK methods for new endpoints Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update swagger docs for things service Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add error handling to http tests Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Fix response body in http tests Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Remove unused responses from things service Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add test cases to things postgres tests Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add test cases for event sourcing Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com>
2019-01-08 11:53:24 +01:00
essvc := redis.NewEventStoreMiddleware(svc, redisClient)
2019-07-18 15:01:09 +02:00
esth, eserr := essvc.ViewThing(context.Background(), token, sth.ID)
th, err := svc.ViewThing(context.Background(), token, sth.ID)
MF-483 - Enable channels and devices corresponding lists in backend (#520) * Add list channels by thing id endpoint Add list channels by thing id endpoint to things service. Add pagination format and logic. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add fetch channels by thing endpoint Add fetch channels by thing endpoint with pagination. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update list endpoints to contain pagination Update list endpoints to contain pagination metadata. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add tests for two new endpoints Add tests for two new endpoints and update existing ones. Also, remove connected field from channel response. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Fix tests for SDK Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add SDK methods for new endpoints Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update swagger docs for things service Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add error handling to http tests Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Fix response body in http tests Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Remove unused responses from things service Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add test cases to things postgres tests Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add test cases for event sourcing Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com>
2019-01-08 11:53:24 +01:00
assert.Equal(t, th, esth, fmt.Sprintf("event sourcing changed service behaviour: expected %v got %v", th, esth))
assert.Equal(t, err, eserr, fmt.Sprintf("event sourcing changed service behaviour: expected %v got %v", err, eserr))
}
func TestListThings(t *testing.T) {
redisClient.FlushAll().Err()
MF-483 - Enable channels and devices corresponding lists in backend (#520) * Add list channels by thing id endpoint Add list channels by thing id endpoint to things service. Add pagination format and logic. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add fetch channels by thing endpoint Add fetch channels by thing endpoint with pagination. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update list endpoints to contain pagination Update list endpoints to contain pagination metadata. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add tests for two new endpoints Add tests for two new endpoints and update existing ones. Also, remove connected field from channel response. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Fix tests for SDK Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add SDK methods for new endpoints Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update swagger docs for things service Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add error handling to http tests Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Fix response body in http tests Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Remove unused responses from things service Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add test cases to things postgres tests Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add test cases for event sourcing Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com>
2019-01-08 11:53:24 +01:00
svc := newService(map[string]string{token: email})
// Create thing without sending event.
_, err := svc.CreateThings(context.Background(), token, things.Thing{Name: "a"})
MF-483 - Enable channels and devices corresponding lists in backend (#520) * Add list channels by thing id endpoint Add list channels by thing id endpoint to things service. Add pagination format and logic. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add fetch channels by thing endpoint Add fetch channels by thing endpoint with pagination. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update list endpoints to contain pagination Update list endpoints to contain pagination metadata. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add tests for two new endpoints Add tests for two new endpoints and update existing ones. Also, remove connected field from channel response. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Fix tests for SDK Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add SDK methods for new endpoints Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update swagger docs for things service Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add error handling to http tests Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Fix response body in http tests Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Remove unused responses from things service Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add test cases to things postgres tests Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add test cases for event sourcing Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com>
2019-01-08 11:53:24 +01:00
require.Nil(t, err, fmt.Sprintf("unexpected error %s", err))
essvc := redis.NewEventStoreMiddleware(svc, redisClient)
MF-1290 - Sort Things and Channels by name (#1293) * MF-1290 - Sort Things and Channels by name Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Add sort HTTP parameter and use PageMetadata to pass filters Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Fix channels conn list Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Fix logs Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Replace sort naming by order Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Add order in responses Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Improve tests Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Improve order tests Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Improve getNameQuery func Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Fix commits Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Add name-asc and name-desc Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Fix typo Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Add dir query parameter Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Fix openapi Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Fix typo Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Fix openapi format Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Fix test messages and openapi.yml Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Fix openapi uuid format Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Check query parameters in endpoint layer Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Add asc endpoint tests Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Rename pageMeta into pageMetadata Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>
2020-12-08 21:30:47 +01:00
esths, eserr := essvc.ListThings(context.Background(), token, things.PageMetadata{Offset: 0, Limit: 10})
ths, err := svc.ListThings(context.Background(), token, things.PageMetadata{Offset: 0, Limit: 10})
MF-483 - Enable channels and devices corresponding lists in backend (#520) * Add list channels by thing id endpoint Add list channels by thing id endpoint to things service. Add pagination format and logic. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add fetch channels by thing endpoint Add fetch channels by thing endpoint with pagination. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update list endpoints to contain pagination Update list endpoints to contain pagination metadata. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add tests for two new endpoints Add tests for two new endpoints and update existing ones. Also, remove connected field from channel response. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Fix tests for SDK Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add SDK methods for new endpoints Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update swagger docs for things service Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add error handling to http tests Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Fix response body in http tests Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Remove unused responses from things service Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add test cases to things postgres tests Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add test cases for event sourcing Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com>
2019-01-08 11:53:24 +01:00
assert.Equal(t, ths, esths, fmt.Sprintf("event sourcing changed service behaviour: expected %v got %v", ths, esths))
assert.Equal(t, err, eserr, fmt.Sprintf("event sourcing changed service behaviour: expected %v got %v", err, eserr))
}
func TestListThingsByChannel(t *testing.T) {
redisClient.FlushAll().Err()
MF-483 - Enable channels and devices corresponding lists in backend (#520) * Add list channels by thing id endpoint Add list channels by thing id endpoint to things service. Add pagination format and logic. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add fetch channels by thing endpoint Add fetch channels by thing endpoint with pagination. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update list endpoints to contain pagination Update list endpoints to contain pagination metadata. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add tests for two new endpoints Add tests for two new endpoints and update existing ones. Also, remove connected field from channel response. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Fix tests for SDK Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add SDK methods for new endpoints Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update swagger docs for things service Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add error handling to http tests Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Fix response body in http tests Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Remove unused responses from things service Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add test cases to things postgres tests Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add test cases for event sourcing Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com>
2019-01-08 11:53:24 +01:00
svc := newService(map[string]string{token: email})
// Create thing without sending event.
sths, err := svc.CreateThings(context.Background(), token, things.Thing{Name: "a"})
MF-483 - Enable channels and devices corresponding lists in backend (#520) * Add list channels by thing id endpoint Add list channels by thing id endpoint to things service. Add pagination format and logic. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add fetch channels by thing endpoint Add fetch channels by thing endpoint with pagination. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update list endpoints to contain pagination Update list endpoints to contain pagination metadata. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add tests for two new endpoints Add tests for two new endpoints and update existing ones. Also, remove connected field from channel response. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Fix tests for SDK Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add SDK methods for new endpoints Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update swagger docs for things service Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add error handling to http tests Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Fix response body in http tests Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Remove unused responses from things service Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add test cases to things postgres tests Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add test cases for event sourcing Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com>
2019-01-08 11:53:24 +01:00
require.Nil(t, err, fmt.Sprintf("unexpected error %s", err))
sth := sths[0]
schs, err := svc.CreateChannels(context.Background(), token, things.Channel{Name: "a"})
MF-483 - Enable channels and devices corresponding lists in backend (#520) * Add list channels by thing id endpoint Add list channels by thing id endpoint to things service. Add pagination format and logic. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add fetch channels by thing endpoint Add fetch channels by thing endpoint with pagination. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update list endpoints to contain pagination Update list endpoints to contain pagination metadata. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add tests for two new endpoints Add tests for two new endpoints and update existing ones. Also, remove connected field from channel response. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Fix tests for SDK Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add SDK methods for new endpoints Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update swagger docs for things service Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add error handling to http tests Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Fix response body in http tests Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Remove unused responses from things service Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add test cases to things postgres tests Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add test cases for event sourcing Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com>
2019-01-08 11:53:24 +01:00
require.Nil(t, err, fmt.Sprintf("unexpected error %s", err))
sch := schs[0]
err = svc.Connect(context.Background(), token, []string{sch.ID}, []string{sth.ID})
MF-483 - Enable channels and devices corresponding lists in backend (#520) * Add list channels by thing id endpoint Add list channels by thing id endpoint to things service. Add pagination format and logic. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add fetch channels by thing endpoint Add fetch channels by thing endpoint with pagination. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update list endpoints to contain pagination Update list endpoints to contain pagination metadata. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add tests for two new endpoints Add tests for two new endpoints and update existing ones. Also, remove connected field from channel response. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Fix tests for SDK Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add SDK methods for new endpoints Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update swagger docs for things service Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add error handling to http tests Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Fix response body in http tests Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Remove unused responses from things service Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add test cases to things postgres tests Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add test cases for event sourcing Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com>
2019-01-08 11:53:24 +01:00
require.Nil(t, err, fmt.Sprintf("unexpected error %s", err))
essvc := redis.NewEventStoreMiddleware(svc, redisClient)
esths, eserr := essvc.ListThingsByChannel(context.Background(), token, sch.ID, 0, 10, true)
thps, err := svc.ListThingsByChannel(context.Background(), token, sch.ID, 0, 10, true)
assert.Equal(t, thps, esths, fmt.Sprintf("event sourcing changed service behaviour: expected %v got %v", thps, esths))
MF-483 - Enable channels and devices corresponding lists in backend (#520) * Add list channels by thing id endpoint Add list channels by thing id endpoint to things service. Add pagination format and logic. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add fetch channels by thing endpoint Add fetch channels by thing endpoint with pagination. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update list endpoints to contain pagination Update list endpoints to contain pagination metadata. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add tests for two new endpoints Add tests for two new endpoints and update existing ones. Also, remove connected field from channel response. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Fix tests for SDK Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add SDK methods for new endpoints Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update swagger docs for things service Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add error handling to http tests Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Fix response body in http tests Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Remove unused responses from things service Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add test cases to things postgres tests Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add test cases for event sourcing Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com>
2019-01-08 11:53:24 +01:00
assert.Equal(t, err, eserr, fmt.Sprintf("event sourcing changed service behaviour: expected %v got %v", err, eserr))
}
func TestRemoveThing(t *testing.T) {
redisClient.FlushAll().Err()
svc := newService(map[string]string{token: email})
// Create thing without sending event.
sths, err := svc.CreateThings(context.Background(), token, things.Thing{Name: "a"})
require.Nil(t, err, fmt.Sprintf("unexpected error %s", err))
sth := sths[0]
svc = redis.NewEventStoreMiddleware(svc, redisClient)
cases := []struct {
desc string
MF-475 - Replace increment ID with UUID (#490) * Update increment ID to UUID in things service Update increment ID to UUID for things and channels in things service and proto files. Also, update ID type from uint to string. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in http adapter Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in ws adapter Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in CoAP adapter Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in normalizer service Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in writer services Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in reader services Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in SDK Update increment ID to UUID in SDK. Update id type to string. Update tests. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in mqtt adapter Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Remove unnecessary case from influxdb reader Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update tests in order to increase code coverage Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update lora adapter to use string ID instead of unsigned int Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com>
2018-12-05 13:09:25 +01:00
id string
key string
err error
event map[string]interface{}
}{
{
desc: "delete existing thing successfully",
id: sth.ID,
key: token,
err: nil,
event: map[string]interface{}{
MF-475 - Replace increment ID with UUID (#490) * Update increment ID to UUID in things service Update increment ID to UUID for things and channels in things service and proto files. Also, update ID type from uint to string. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in http adapter Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in ws adapter Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in CoAP adapter Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in normalizer service Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in writer services Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in reader services Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in SDK Update increment ID to UUID in SDK. Update id type to string. Update tests. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in mqtt adapter Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Remove unnecessary case from influxdb reader Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update tests in order to increase code coverage Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update lora adapter to use string ID instead of unsigned int Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com>
2018-12-05 13:09:25 +01:00
"id": sth.ID,
"operation": thingRemove,
},
},
{
desc: "delete thing with invalid credentials",
MF-475 - Replace increment ID with UUID (#490) * Update increment ID to UUID in things service Update increment ID to UUID for things and channels in things service and proto files. Also, update ID type from uint to string. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in http adapter Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in ws adapter Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in CoAP adapter Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in normalizer service Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in writer services Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in reader services Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in SDK Update increment ID to UUID in SDK. Update id type to string. Update tests. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in mqtt adapter Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Remove unnecessary case from influxdb reader Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update tests in order to increase code coverage Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update lora adapter to use string ID instead of unsigned int Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com>
2018-12-05 13:09:25 +01:00
id: strconv.FormatUint(math.MaxUint64, 10),
key: "",
err: things.ErrUnauthorizedAccess,
event: nil,
},
}
lastID := "0"
for _, tc := range cases {
2019-07-18 15:01:09 +02:00
err := svc.RemoveThing(context.Background(), tc.key, tc.id)
2020-03-30 15:22:18 +02:00
assert.True(t, errors.Contains(err, tc.err), fmt.Sprintf("%s: expected %s got %s\n", tc.desc, tc.err, err))
streams := redisClient.XRead(&r.XReadArgs{
Streams: []string{streamID, lastID},
Count: 1,
Block: time.Second,
}).Val()
var event map[string]interface{}
if len(streams) > 0 && len(streams[0].Messages) > 0 {
msg := streams[0].Messages[0]
event = msg.Values
lastID = msg.ID
}
assert.Equal(t, tc.event, event, fmt.Sprintf("%s: expected %v got %v\n", tc.desc, tc.event, event))
}
}
func TestCreateChannels(t *testing.T) {
redisClient.FlushAll().Err()
svc := newService(map[string]string{token: email})
svc = redis.NewEventStoreMiddleware(svc, redisClient)
cases := []struct {
desc string
chs []things.Channel
key string
err error
event map[string]interface{}
}{
{
desc: "create channels successfully",
chs: []things.Channel{{Name: "a", Metadata: map[string]interface{}{"test": "test"}}},
key: token,
err: nil,
event: map[string]interface{}{
"id": "1",
"name": "a",
"metadata": "{\"test\":\"test\"}",
"owner": email,
"operation": channelCreate,
},
},
{
desc: "create channels with invalid credentials",
chs: []things.Channel{{Name: "a", Metadata: map[string]interface{}{"test": "test"}}},
key: "",
err: things.ErrUnauthorizedAccess,
event: nil,
},
}
lastID := "0"
for _, tc := range cases {
_, err := svc.CreateChannels(context.Background(), tc.key, tc.chs...)
assert.True(t, errors.Contains(err, tc.err), fmt.Sprintf("%s: expected %s got %s\n", tc.desc, tc.err, err))
streams := redisClient.XRead(&r.XReadArgs{
Streams: []string{streamID, lastID},
Count: 1,
Block: time.Second,
}).Val()
var event map[string]interface{}
if len(streams) > 0 && len(streams[0].Messages) > 0 {
msg := streams[0].Messages[0]
event = msg.Values
lastID = msg.ID
}
assert.Equal(t, tc.event, event, fmt.Sprintf("%s: expected %v got %v\n", tc.desc, tc.event, event))
}
}
func TestUpdateChannel(t *testing.T) {
redisClient.FlushAll().Err()
svc := newService(map[string]string{token: email})
// Create channel without sending event.
schs, err := svc.CreateChannels(context.Background(), token, things.Channel{Name: "a"})
require.Nil(t, err, fmt.Sprintf("unexpected error %s", err))
sch := schs[0]
svc = redis.NewEventStoreMiddleware(svc, redisClient)
cases := []struct {
desc string
channel things.Channel
key string
err error
event map[string]interface{}
}{
{
MF-549 - Change metadata format from JSON string to JSON object (#706) * Update metadata type in things service Update things service so that metadata has map type. Update repo implementation by adding sqlx lib. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add sqlx lib to bootstrap service Add sqlx lib to bootstrap service and update metadata field type. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update metadata in redis streams consumer Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update tests for bootstrap service Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Fix mongo reader logging and driver version Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Fix mongo reader and writer Fix mongo reader and writer by updating driver version. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update SDK with new metadata format Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update LoRa adapter with new metadata format Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update users service in order to use sqlx Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Replace anonymous struct with map Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update docs for LoRa adapter Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Fix LoRa application metadata format Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Fix metadata format in LoRa docs Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add metadata2 var to SDK things test Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com>
2019-04-16 14:58:56 +02:00
desc: "update channel successfully",
channel: things.Channel{
ID: sch.ID,
Name: "b",
Metadata: map[string]interface{}{"test": "test"},
},
key: token,
err: nil,
event: map[string]interface{}{
MF-475 - Replace increment ID with UUID (#490) * Update increment ID to UUID in things service Update increment ID to UUID for things and channels in things service and proto files. Also, update ID type from uint to string. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in http adapter Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in ws adapter Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in CoAP adapter Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in normalizer service Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in writer services Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in reader services Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in SDK Update increment ID to UUID in SDK. Update id type to string. Update tests. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in mqtt adapter Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Remove unnecessary case from influxdb reader Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update tests in order to increase code coverage Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update lora adapter to use string ID instead of unsigned int Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com>
2018-12-05 13:09:25 +01:00
"id": sch.ID,
"name": "b",
MF-549 - Change metadata format from JSON string to JSON object (#706) * Update metadata type in things service Update things service so that metadata has map type. Update repo implementation by adding sqlx lib. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add sqlx lib to bootstrap service Add sqlx lib to bootstrap service and update metadata field type. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update metadata in redis streams consumer Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update tests for bootstrap service Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Fix mongo reader logging and driver version Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Fix mongo reader and writer Fix mongo reader and writer by updating driver version. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update SDK with new metadata format Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update LoRa adapter with new metadata format Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update users service in order to use sqlx Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Replace anonymous struct with map Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update docs for LoRa adapter Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Fix LoRa application metadata format Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Fix metadata format in LoRa docs Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add metadata2 var to SDK things test Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com>
2019-04-16 14:58:56 +02:00
"metadata": "{\"test\":\"test\"}",
"operation": channelUpdate,
},
},
{
MF-475 - Replace increment ID with UUID (#490) * Update increment ID to UUID in things service Update increment ID to UUID for things and channels in things service and proto files. Also, update ID type from uint to string. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in http adapter Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in ws adapter Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in CoAP adapter Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in normalizer service Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in writer services Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in reader services Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in SDK Update increment ID to UUID in SDK. Update id type to string. Update tests. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in mqtt adapter Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Remove unnecessary case from influxdb reader Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update tests in order to increase code coverage Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update lora adapter to use string ID instead of unsigned int Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com>
2018-12-05 13:09:25 +01:00
desc: "create non-existent channel",
channel: things.Channel{
ID: strconv.FormatUint(math.MaxUint64, 10),
Name: "c",
},
key: token,
err: things.ErrNotFound,
event: nil,
},
}
lastID := "0"
for _, tc := range cases {
2019-07-18 15:01:09 +02:00
err := svc.UpdateChannel(context.Background(), tc.key, tc.channel)
assert.True(t, errors.Contains(err, tc.err), fmt.Sprintf("%s: expected %s got %s\n", tc.desc, tc.err, err))
streams := redisClient.XRead(&r.XReadArgs{
Streams: []string{streamID, lastID},
Count: 1,
Block: time.Second,
}).Val()
var event map[string]interface{}
if len(streams) > 0 && len(streams[0].Messages) > 0 {
msg := streams[0].Messages[0]
event = msg.Values
lastID = msg.ID
}
assert.Equal(t, tc.event, event, fmt.Sprintf("%s: expected %v got %v\n", tc.desc, tc.event, event))
}
}
MF-483 - Enable channels and devices corresponding lists in backend (#520) * Add list channels by thing id endpoint Add list channels by thing id endpoint to things service. Add pagination format and logic. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add fetch channels by thing endpoint Add fetch channels by thing endpoint with pagination. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update list endpoints to contain pagination Update list endpoints to contain pagination metadata. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add tests for two new endpoints Add tests for two new endpoints and update existing ones. Also, remove connected field from channel response. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Fix tests for SDK Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add SDK methods for new endpoints Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update swagger docs for things service Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add error handling to http tests Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Fix response body in http tests Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Remove unused responses from things service Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add test cases to things postgres tests Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add test cases for event sourcing Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com>
2019-01-08 11:53:24 +01:00
func TestViewChannel(t *testing.T) {
redisClient.FlushAll().Err()
MF-483 - Enable channels and devices corresponding lists in backend (#520) * Add list channels by thing id endpoint Add list channels by thing id endpoint to things service. Add pagination format and logic. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add fetch channels by thing endpoint Add fetch channels by thing endpoint with pagination. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update list endpoints to contain pagination Update list endpoints to contain pagination metadata. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add tests for two new endpoints Add tests for two new endpoints and update existing ones. Also, remove connected field from channel response. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Fix tests for SDK Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add SDK methods for new endpoints Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update swagger docs for things service Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add error handling to http tests Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Fix response body in http tests Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Remove unused responses from things service Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add test cases to things postgres tests Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add test cases for event sourcing Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com>
2019-01-08 11:53:24 +01:00
svc := newService(map[string]string{token: email})
// Create channel without sending event.
schs, err := svc.CreateChannels(context.Background(), token, things.Channel{Name: "a"})
MF-483 - Enable channels and devices corresponding lists in backend (#520) * Add list channels by thing id endpoint Add list channels by thing id endpoint to things service. Add pagination format and logic. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add fetch channels by thing endpoint Add fetch channels by thing endpoint with pagination. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update list endpoints to contain pagination Update list endpoints to contain pagination metadata. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add tests for two new endpoints Add tests for two new endpoints and update existing ones. Also, remove connected field from channel response. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Fix tests for SDK Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add SDK methods for new endpoints Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update swagger docs for things service Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add error handling to http tests Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Fix response body in http tests Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Remove unused responses from things service Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add test cases to things postgres tests Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add test cases for event sourcing Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com>
2019-01-08 11:53:24 +01:00
require.Nil(t, err, fmt.Sprintf("unexpected error %s", err))
sch := schs[0]
MF-483 - Enable channels and devices corresponding lists in backend (#520) * Add list channels by thing id endpoint Add list channels by thing id endpoint to things service. Add pagination format and logic. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add fetch channels by thing endpoint Add fetch channels by thing endpoint with pagination. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update list endpoints to contain pagination Update list endpoints to contain pagination metadata. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add tests for two new endpoints Add tests for two new endpoints and update existing ones. Also, remove connected field from channel response. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Fix tests for SDK Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add SDK methods for new endpoints Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update swagger docs for things service Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add error handling to http tests Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Fix response body in http tests Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Remove unused responses from things service Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add test cases to things postgres tests Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add test cases for event sourcing Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com>
2019-01-08 11:53:24 +01:00
essvc := redis.NewEventStoreMiddleware(svc, redisClient)
2019-07-18 15:01:09 +02:00
esch, eserr := essvc.ViewChannel(context.Background(), token, sch.ID)
ch, err := svc.ViewChannel(context.Background(), token, sch.ID)
MF-483 - Enable channels and devices corresponding lists in backend (#520) * Add list channels by thing id endpoint Add list channels by thing id endpoint to things service. Add pagination format and logic. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add fetch channels by thing endpoint Add fetch channels by thing endpoint with pagination. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update list endpoints to contain pagination Update list endpoints to contain pagination metadata. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add tests for two new endpoints Add tests for two new endpoints and update existing ones. Also, remove connected field from channel response. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Fix tests for SDK Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add SDK methods for new endpoints Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update swagger docs for things service Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add error handling to http tests Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Fix response body in http tests Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Remove unused responses from things service Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add test cases to things postgres tests Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add test cases for event sourcing Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com>
2019-01-08 11:53:24 +01:00
assert.Equal(t, ch, esch, fmt.Sprintf("event sourcing changed service behaviour: expected %v got %v", ch, esch))
assert.Equal(t, err, eserr, fmt.Sprintf("event sourcing changed service behaviour: expected %v got %v", err, eserr))
}
func TestListChannels(t *testing.T) {
redisClient.FlushAll().Err()
MF-483 - Enable channels and devices corresponding lists in backend (#520) * Add list channels by thing id endpoint Add list channels by thing id endpoint to things service. Add pagination format and logic. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add fetch channels by thing endpoint Add fetch channels by thing endpoint with pagination. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update list endpoints to contain pagination Update list endpoints to contain pagination metadata. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add tests for two new endpoints Add tests for two new endpoints and update existing ones. Also, remove connected field from channel response. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Fix tests for SDK Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add SDK methods for new endpoints Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update swagger docs for things service Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add error handling to http tests Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Fix response body in http tests Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Remove unused responses from things service Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add test cases to things postgres tests Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add test cases for event sourcing Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com>
2019-01-08 11:53:24 +01:00
svc := newService(map[string]string{token: email})
// Create thing without sending event.
_, err := svc.CreateChannels(context.Background(), token, things.Channel{Name: "a"})
MF-483 - Enable channels and devices corresponding lists in backend (#520) * Add list channels by thing id endpoint Add list channels by thing id endpoint to things service. Add pagination format and logic. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add fetch channels by thing endpoint Add fetch channels by thing endpoint with pagination. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update list endpoints to contain pagination Update list endpoints to contain pagination metadata. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add tests for two new endpoints Add tests for two new endpoints and update existing ones. Also, remove connected field from channel response. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Fix tests for SDK Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add SDK methods for new endpoints Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update swagger docs for things service Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add error handling to http tests Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Fix response body in http tests Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Remove unused responses from things service Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add test cases to things postgres tests Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add test cases for event sourcing Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com>
2019-01-08 11:53:24 +01:00
require.Nil(t, err, fmt.Sprintf("unexpected error %s", err))
essvc := redis.NewEventStoreMiddleware(svc, redisClient)
MF-1290 - Sort Things and Channels by name (#1293) * MF-1290 - Sort Things and Channels by name Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Add sort HTTP parameter and use PageMetadata to pass filters Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Fix channels conn list Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Fix logs Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Replace sort naming by order Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Add order in responses Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Improve tests Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Improve order tests Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Improve getNameQuery func Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Fix commits Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Add name-asc and name-desc Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Fix typo Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Add dir query parameter Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Fix openapi Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Fix typo Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Fix openapi format Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Fix test messages and openapi.yml Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Fix openapi uuid format Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Check query parameters in endpoint layer Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Add asc endpoint tests Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Rename pageMeta into pageMetadata Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>
2020-12-08 21:30:47 +01:00
eschs, eserr := essvc.ListChannels(context.Background(), token, things.PageMetadata{Offset: 0, Limit: 10})
chs, err := svc.ListChannels(context.Background(), token, things.PageMetadata{Offset: 0, Limit: 10})
MF-483 - Enable channels and devices corresponding lists in backend (#520) * Add list channels by thing id endpoint Add list channels by thing id endpoint to things service. Add pagination format and logic. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add fetch channels by thing endpoint Add fetch channels by thing endpoint with pagination. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update list endpoints to contain pagination Update list endpoints to contain pagination metadata. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add tests for two new endpoints Add tests for two new endpoints and update existing ones. Also, remove connected field from channel response. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Fix tests for SDK Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add SDK methods for new endpoints Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update swagger docs for things service Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add error handling to http tests Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Fix response body in http tests Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Remove unused responses from things service Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add test cases to things postgres tests Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add test cases for event sourcing Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com>
2019-01-08 11:53:24 +01:00
assert.Equal(t, chs, eschs, fmt.Sprintf("event sourcing changed service behaviour: expected %v got %v", chs, eschs))
assert.Equal(t, err, eserr, fmt.Sprintf("event sourcing changed service behaviour: expected %v got %v", err, eserr))
}
func TestListChannelsByThing(t *testing.T) {
redisClient.FlushAll().Err()
MF-483 - Enable channels and devices corresponding lists in backend (#520) * Add list channels by thing id endpoint Add list channels by thing id endpoint to things service. Add pagination format and logic. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add fetch channels by thing endpoint Add fetch channels by thing endpoint with pagination. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update list endpoints to contain pagination Update list endpoints to contain pagination metadata. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add tests for two new endpoints Add tests for two new endpoints and update existing ones. Also, remove connected field from channel response. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Fix tests for SDK Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add SDK methods for new endpoints Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update swagger docs for things service Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add error handling to http tests Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Fix response body in http tests Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Remove unused responses from things service Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add test cases to things postgres tests Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add test cases for event sourcing Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com>
2019-01-08 11:53:24 +01:00
svc := newService(map[string]string{token: email})
// Create thing without sending event.
sths, err := svc.CreateThings(context.Background(), token, things.Thing{Name: "a"})
MF-483 - Enable channels and devices corresponding lists in backend (#520) * Add list channels by thing id endpoint Add list channels by thing id endpoint to things service. Add pagination format and logic. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add fetch channels by thing endpoint Add fetch channels by thing endpoint with pagination. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update list endpoints to contain pagination Update list endpoints to contain pagination metadata. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add tests for two new endpoints Add tests for two new endpoints and update existing ones. Also, remove connected field from channel response. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Fix tests for SDK Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add SDK methods for new endpoints Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update swagger docs for things service Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add error handling to http tests Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Fix response body in http tests Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Remove unused responses from things service Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add test cases to things postgres tests Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add test cases for event sourcing Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com>
2019-01-08 11:53:24 +01:00
require.Nil(t, err, fmt.Sprintf("unexpected error %s", err))
sth := sths[0]
schs, err := svc.CreateChannels(context.Background(), token, things.Channel{Name: "a"})
MF-483 - Enable channels and devices corresponding lists in backend (#520) * Add list channels by thing id endpoint Add list channels by thing id endpoint to things service. Add pagination format and logic. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add fetch channels by thing endpoint Add fetch channels by thing endpoint with pagination. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update list endpoints to contain pagination Update list endpoints to contain pagination metadata. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add tests for two new endpoints Add tests for two new endpoints and update existing ones. Also, remove connected field from channel response. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Fix tests for SDK Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add SDK methods for new endpoints Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update swagger docs for things service Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add error handling to http tests Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Fix response body in http tests Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Remove unused responses from things service Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add test cases to things postgres tests Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add test cases for event sourcing Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com>
2019-01-08 11:53:24 +01:00
require.Nil(t, err, fmt.Sprintf("unexpected error %s", err))
sch := schs[0]
err = svc.Connect(context.Background(), token, []string{sch.ID}, []string{sth.ID})
MF-483 - Enable channels and devices corresponding lists in backend (#520) * Add list channels by thing id endpoint Add list channels by thing id endpoint to things service. Add pagination format and logic. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add fetch channels by thing endpoint Add fetch channels by thing endpoint with pagination. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update list endpoints to contain pagination Update list endpoints to contain pagination metadata. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add tests for two new endpoints Add tests for two new endpoints and update existing ones. Also, remove connected field from channel response. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Fix tests for SDK Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add SDK methods for new endpoints Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update swagger docs for things service Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add error handling to http tests Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Fix response body in http tests Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Remove unused responses from things service Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add test cases to things postgres tests Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add test cases for event sourcing Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com>
2019-01-08 11:53:24 +01:00
require.Nil(t, err, fmt.Sprintf("unexpected error %s", err))
essvc := redis.NewEventStoreMiddleware(svc, redisClient)
eschs, eserr := essvc.ListChannelsByThing(context.Background(), token, sth.ID, 0, 10, true)
chps, err := svc.ListChannelsByThing(context.Background(), token, sth.ID, 0, 10, true)
assert.Equal(t, chps, eschs, fmt.Sprintf("event sourcing changed service behaviour: expected %v got %v", chps, eschs))
MF-483 - Enable channels and devices corresponding lists in backend (#520) * Add list channels by thing id endpoint Add list channels by thing id endpoint to things service. Add pagination format and logic. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add fetch channels by thing endpoint Add fetch channels by thing endpoint with pagination. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update list endpoints to contain pagination Update list endpoints to contain pagination metadata. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add tests for two new endpoints Add tests for two new endpoints and update existing ones. Also, remove connected field from channel response. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Fix tests for SDK Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add SDK methods for new endpoints Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update swagger docs for things service Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add error handling to http tests Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Fix response body in http tests Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Remove unused responses from things service Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add test cases to things postgres tests Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Add test cases for event sourcing Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com>
2019-01-08 11:53:24 +01:00
assert.Equal(t, err, eserr, fmt.Sprintf("event sourcing changed service behaviour: expected %v got %v", err, eserr))
}
func TestRemoveChannel(t *testing.T) {
redisClient.FlushAll().Err()
svc := newService(map[string]string{token: email})
// Create channel without sending event.
schs, err := svc.CreateChannels(context.Background(), token, things.Channel{Name: "a"})
require.Nil(t, err, fmt.Sprintf("unexpected error %s", err))
sch := schs[0]
svc = redis.NewEventStoreMiddleware(svc, redisClient)
cases := []struct {
desc string
MF-475 - Replace increment ID with UUID (#490) * Update increment ID to UUID in things service Update increment ID to UUID for things and channels in things service and proto files. Also, update ID type from uint to string. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in http adapter Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in ws adapter Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in CoAP adapter Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in normalizer service Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in writer services Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in reader services Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in SDK Update increment ID to UUID in SDK. Update id type to string. Update tests. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in mqtt adapter Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Remove unnecessary case from influxdb reader Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update tests in order to increase code coverage Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update lora adapter to use string ID instead of unsigned int Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com>
2018-12-05 13:09:25 +01:00
id string
key string
err error
event map[string]interface{}
}{
{
desc: "update channel successfully",
id: sch.ID,
key: token,
err: nil,
event: map[string]interface{}{
MF-475 - Replace increment ID with UUID (#490) * Update increment ID to UUID in things service Update increment ID to UUID for things and channels in things service and proto files. Also, update ID type from uint to string. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in http adapter Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in ws adapter Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in CoAP adapter Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in normalizer service Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in writer services Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in reader services Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in SDK Update increment ID to UUID in SDK. Update id type to string. Update tests. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in mqtt adapter Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Remove unnecessary case from influxdb reader Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update tests in order to increase code coverage Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update lora adapter to use string ID instead of unsigned int Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com>
2018-12-05 13:09:25 +01:00
"id": sch.ID,
"operation": channelRemove,
},
},
{
desc: "create non-existent channel",
MF-475 - Replace increment ID with UUID (#490) * Update increment ID to UUID in things service Update increment ID to UUID for things and channels in things service and proto files. Also, update ID type from uint to string. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in http adapter Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in ws adapter Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in CoAP adapter Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in normalizer service Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in writer services Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in reader services Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in SDK Update increment ID to UUID in SDK. Update id type to string. Update tests. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in mqtt adapter Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Remove unnecessary case from influxdb reader Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update tests in order to increase code coverage Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update lora adapter to use string ID instead of unsigned int Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com>
2018-12-05 13:09:25 +01:00
id: strconv.FormatUint(math.MaxUint64, 10),
key: "",
err: things.ErrUnauthorizedAccess,
event: nil,
},
}
lastID := "0"
for _, tc := range cases {
2019-07-18 15:01:09 +02:00
err := svc.RemoveChannel(context.Background(), tc.key, tc.id)
assert.True(t, errors.Contains(err, tc.err), fmt.Sprintf("%s: expected %s got %s\n", tc.desc, tc.err, err))
streams := redisClient.XRead(&r.XReadArgs{
Streams: []string{streamID, lastID},
Count: 1,
Block: time.Second,
}).Val()
var event map[string]interface{}
if len(streams) > 0 && len(streams[0].Messages) > 0 {
msg := streams[0].Messages[0]
event = msg.Values
lastID = msg.ID
}
assert.Equal(t, tc.event, event, fmt.Sprintf("%s: expected %v got %v\n", tc.desc, tc.event, event))
}
}
func TestConnectEvent(t *testing.T) {
redisClient.FlushAll().Err()
svc := newService(map[string]string{token: email})
// Create thing and channel that will be connected.
sths, err := svc.CreateThings(context.Background(), token, things.Thing{Name: "a"})
require.Nil(t, err, fmt.Sprintf("unexpected error %s", err))
sth := sths[0]
schs, err := svc.CreateChannels(context.Background(), token, things.Channel{Name: "a"})
require.Nil(t, err, fmt.Sprintf("unexpected error %s", err))
sch := schs[0]
svc = redis.NewEventStoreMiddleware(svc, redisClient)
cases := []struct {
desc string
MF-475 - Replace increment ID with UUID (#490) * Update increment ID to UUID in things service Update increment ID to UUID for things and channels in things service and proto files. Also, update ID type from uint to string. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in http adapter Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in ws adapter Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in CoAP adapter Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in normalizer service Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in writer services Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in reader services Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in SDK Update increment ID to UUID in SDK. Update id type to string. Update tests. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in mqtt adapter Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Remove unnecessary case from influxdb reader Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update tests in order to increase code coverage Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update lora adapter to use string ID instead of unsigned int Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com>
2018-12-05 13:09:25 +01:00
thingID string
chanID string
key string
err error
event map[string]interface{}
}{
{
desc: "connect existing thing to existing channel",
thingID: sth.ID,
chanID: sch.ID,
key: token,
err: nil,
event: map[string]interface{}{
MF-475 - Replace increment ID with UUID (#490) * Update increment ID to UUID in things service Update increment ID to UUID for things and channels in things service and proto files. Also, update ID type from uint to string. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in http adapter Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in ws adapter Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in CoAP adapter Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in normalizer service Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in writer services Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in reader services Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in SDK Update increment ID to UUID in SDK. Update id type to string. Update tests. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in mqtt adapter Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Remove unnecessary case from influxdb reader Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update tests in order to increase code coverage Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update lora adapter to use string ID instead of unsigned int Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com>
2018-12-05 13:09:25 +01:00
"chan_id": sch.ID,
"thing_id": sth.ID,
"operation": thingConnect,
},
},
{
desc: "connect non-existent thing to channel",
MF-475 - Replace increment ID with UUID (#490) * Update increment ID to UUID in things service Update increment ID to UUID for things and channels in things service and proto files. Also, update ID type from uint to string. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in http adapter Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in ws adapter Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in CoAP adapter Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in normalizer service Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in writer services Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in reader services Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in SDK Update increment ID to UUID in SDK. Update id type to string. Update tests. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in mqtt adapter Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Remove unnecessary case from influxdb reader Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update tests in order to increase code coverage Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update lora adapter to use string ID instead of unsigned int Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com>
2018-12-05 13:09:25 +01:00
thingID: strconv.FormatUint(math.MaxUint64, 10),
chanID: sch.ID,
key: token,
err: things.ErrNotFound,
event: nil,
},
}
lastID := "0"
for _, tc := range cases {
err := svc.Connect(context.Background(), tc.key, []string{tc.chanID}, []string{tc.thingID})
assert.Equal(t, tc.err, err, fmt.Sprintf("%s: expected %s got %s\n", tc.desc, tc.err, err))
streams := redisClient.XRead(&r.XReadArgs{
Streams: []string{streamID, lastID},
Count: 1,
Block: time.Second,
}).Val()
var event map[string]interface{}
if len(streams) > 0 && len(streams[0].Messages) > 0 {
msg := streams[0].Messages[0]
event = msg.Values
lastID = msg.ID
}
assert.Equal(t, tc.event, event, fmt.Sprintf("%s: expected %v got %v\n", tc.desc, tc.event, event))
}
}
func TestDisconnectEvent(t *testing.T) {
redisClient.FlushAll().Err()
svc := newService(map[string]string{token: email})
// Create thing and channel that will be connected.
sths, err := svc.CreateThings(context.Background(), token, things.Thing{Name: "a"})
require.Nil(t, err, fmt.Sprintf("unexpected error %s", err))
sth := sths[0]
schs, err := svc.CreateChannels(context.Background(), token, things.Channel{Name: "a"})
require.Nil(t, err, fmt.Sprintf("unexpected error %s", err))
sch := schs[0]
err = svc.Connect(context.Background(), token, []string{sch.ID}, []string{sth.ID})
require.Nil(t, err, fmt.Sprintf("unexpected error %s", err))
svc = redis.NewEventStoreMiddleware(svc, redisClient)
cases := []struct {
desc string
MF-475 - Replace increment ID with UUID (#490) * Update increment ID to UUID in things service Update increment ID to UUID for things and channels in things service and proto files. Also, update ID type from uint to string. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in http adapter Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in ws adapter Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in CoAP adapter Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in normalizer service Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in writer services Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in reader services Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in SDK Update increment ID to UUID in SDK. Update id type to string. Update tests. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in mqtt adapter Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Remove unnecessary case from influxdb reader Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update tests in order to increase code coverage Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update lora adapter to use string ID instead of unsigned int Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com>
2018-12-05 13:09:25 +01:00
thingID string
chanID string
key string
err error
event map[string]interface{}
}{
{
desc: "disconnect thing from channel",
thingID: sth.ID,
chanID: sch.ID,
key: token,
err: nil,
event: map[string]interface{}{
MF-475 - Replace increment ID with UUID (#490) * Update increment ID to UUID in things service Update increment ID to UUID for things and channels in things service and proto files. Also, update ID type from uint to string. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in http adapter Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in ws adapter Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in CoAP adapter Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in normalizer service Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in writer services Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in reader services Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in SDK Update increment ID to UUID in SDK. Update id type to string. Update tests. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in mqtt adapter Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Remove unnecessary case from influxdb reader Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update tests in order to increase code coverage Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update lora adapter to use string ID instead of unsigned int Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com>
2018-12-05 13:09:25 +01:00
"chan_id": sch.ID,
"thing_id": sth.ID,
"operation": thingDisconnect,
},
},
{
desc: "disconnect non-existent thing from channel",
MF-475 - Replace increment ID with UUID (#490) * Update increment ID to UUID in things service Update increment ID to UUID for things and channels in things service and proto files. Also, update ID type from uint to string. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in http adapter Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in ws adapter Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in CoAP adapter Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in normalizer service Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in writer services Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in reader services Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in SDK Update increment ID to UUID in SDK. Update id type to string. Update tests. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in mqtt adapter Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Remove unnecessary case from influxdb reader Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update tests in order to increase code coverage Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update lora adapter to use string ID instead of unsigned int Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com>
2018-12-05 13:09:25 +01:00
thingID: strconv.FormatUint(math.MaxUint64, 10),
chanID: sch.ID,
key: token,
err: things.ErrNotFound,
event: nil,
},
}
lastID := "0"
for _, tc := range cases {
2019-07-18 15:01:09 +02:00
err := svc.Disconnect(context.Background(), tc.key, tc.chanID, tc.thingID)
assert.Equal(t, tc.err, err, fmt.Sprintf("%s: expected %s got %s\n", tc.desc, tc.err, err))
streams := redisClient.XRead(&r.XReadArgs{
Streams: []string{streamID, lastID},
Count: 1,
Block: time.Second,
}).Val()
var event map[string]interface{}
if len(streams) > 0 && len(streams[0].Messages) > 0 {
msg := streams[0].Messages[0]
event = msg.Values
lastID = msg.ID
}
assert.Equal(t, tc.event, event, fmt.Sprintf("%s: expected %v got %v\n", tc.desc, tc.event, event))
}
}