2017-09-23 01:03:27 +02:00
|
|
|
package mocks
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2018-05-16 14:28:41 +02:00
|
|
|
"sort"
|
2017-09-23 01:03:27 +02:00
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
|
2018-05-15 17:13:09 +02:00
|
|
|
"github.com/mainflux/mainflux/things"
|
2017-09-23 01:03:27 +02:00
|
|
|
)
|
|
|
|
|
2018-05-15 17:13:09 +02:00
|
|
|
var _ things.ChannelRepository = (*channelRepositoryMock)(nil)
|
2017-09-23 01:03:27 +02:00
|
|
|
|
|
|
|
type channelRepositoryMock struct {
|
|
|
|
mu sync.Mutex
|
|
|
|
counter int
|
2018-05-15 17:13:09 +02:00
|
|
|
channels map[string]things.Channel
|
|
|
|
things things.ThingRepository
|
2017-09-23 01:03:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewChannelRepository creates in-memory channel repository.
|
2018-05-15 17:13:09 +02:00
|
|
|
func NewChannelRepository(repo things.ThingRepository) things.ChannelRepository {
|
2017-09-23 01:03:27 +02:00
|
|
|
return &channelRepositoryMock{
|
2018-05-15 17:13:09 +02:00
|
|
|
channels: make(map[string]things.Channel),
|
|
|
|
things: repo,
|
2017-09-23 01:03:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-15 17:13:09 +02:00
|
|
|
func (crm *channelRepositoryMock) Save(channel things.Channel) (string, error) {
|
2018-03-11 18:06:01 +01:00
|
|
|
crm.mu.Lock()
|
|
|
|
defer crm.mu.Unlock()
|
2017-09-23 01:03:27 +02:00
|
|
|
|
2018-03-11 18:06:01 +01:00
|
|
|
crm.channels[key(channel.Owner, channel.ID)] = channel
|
2017-09-23 01:03:27 +02:00
|
|
|
|
|
|
|
return channel.ID, nil
|
|
|
|
}
|
|
|
|
|
2018-05-15 17:13:09 +02:00
|
|
|
func (crm *channelRepositoryMock) Update(channel things.Channel) error {
|
2018-03-11 18:06:01 +01:00
|
|
|
crm.mu.Lock()
|
|
|
|
defer crm.mu.Unlock()
|
2017-09-23 01:03:27 +02:00
|
|
|
|
|
|
|
dbKey := key(channel.Owner, channel.ID)
|
|
|
|
|
2018-03-11 18:06:01 +01:00
|
|
|
if _, ok := crm.channels[dbKey]; !ok {
|
2018-05-15 17:13:09 +02:00
|
|
|
return things.ErrNotFound
|
2017-09-23 01:03:27 +02:00
|
|
|
}
|
|
|
|
|
2018-03-11 18:06:01 +01:00
|
|
|
crm.channels[dbKey] = channel
|
2017-09-23 01:03:27 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-05-15 17:13:09 +02:00
|
|
|
func (crm *channelRepositoryMock) One(owner, id string) (things.Channel, error) {
|
2018-03-11 18:06:01 +01:00
|
|
|
if c, ok := crm.channels[key(owner, id)]; ok {
|
2017-09-23 01:03:27 +02:00
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
2018-05-15 17:13:09 +02:00
|
|
|
return things.Channel{}, things.ErrNotFound
|
2017-09-23 01:03:27 +02:00
|
|
|
}
|
|
|
|
|
2018-05-15 17:13:09 +02:00
|
|
|
func (crm *channelRepositoryMock) All(owner string, offset, limit int) []things.Channel {
|
2018-03-11 18:06:01 +01:00
|
|
|
// This obscure way to examine map keys is enforced by the key structure
|
|
|
|
// itself (see mocks/commons.go).
|
2017-09-23 01:03:27 +02:00
|
|
|
prefix := fmt.Sprintf("%s-", owner)
|
2018-05-15 17:13:09 +02:00
|
|
|
channels := make([]things.Channel, 0)
|
2017-09-23 01:03:27 +02:00
|
|
|
|
2018-04-18 22:36:24 +02:00
|
|
|
if offset < 0 || limit <= 0 {
|
|
|
|
return channels
|
|
|
|
}
|
|
|
|
|
2018-04-23 15:17:30 +02:00
|
|
|
// Since IDs starts from 1, shift everything by one.
|
2018-05-16 14:28:41 +02:00
|
|
|
first := fmt.Sprintf("%s%012d", startID, offset+1)
|
|
|
|
last := fmt.Sprintf("%s%012d", startID, offset+limit+1)
|
2018-04-18 22:36:24 +02:00
|
|
|
|
2018-03-11 18:06:01 +01:00
|
|
|
for k, v := range crm.channels {
|
2018-04-23 15:17:30 +02:00
|
|
|
if strings.HasPrefix(k, prefix) && v.ID >= first && v.ID < last {
|
2017-09-23 01:03:27 +02:00
|
|
|
channels = append(channels, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-16 14:28:41 +02:00
|
|
|
sort.SliceStable(channels, func(i, j int) bool {
|
|
|
|
return channels[i].ID < channels[j].ID
|
|
|
|
})
|
|
|
|
|
2017-09-23 01:03:27 +02:00
|
|
|
return channels
|
|
|
|
}
|
|
|
|
|
2018-03-11 18:06:01 +01:00
|
|
|
func (crm *channelRepositoryMock) Remove(owner, id string) error {
|
|
|
|
delete(crm.channels, key(owner, id))
|
2017-09-23 01:03:27 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-05-15 17:13:09 +02:00
|
|
|
func (crm *channelRepositoryMock) Connect(owner, chanID, thingID string) error {
|
2018-05-10 23:53:25 +02:00
|
|
|
channel, err := crm.One(owner, chanID)
|
2018-03-11 18:06:01 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-05-15 17:13:09 +02:00
|
|
|
thing, err := crm.things.One(owner, thingID)
|
2018-04-08 22:57:56 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-05-15 17:13:09 +02:00
|
|
|
channel.Things = append(channel.Things, thing)
|
2018-03-11 18:06:01 +01:00
|
|
|
return crm.Update(channel)
|
|
|
|
}
|
|
|
|
|
2018-05-15 17:13:09 +02:00
|
|
|
func (crm *channelRepositoryMock) Disconnect(owner, chanID, thingID string) error {
|
2018-05-10 23:53:25 +02:00
|
|
|
channel, err := crm.One(owner, chanID)
|
2018-03-11 18:06:01 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-05-16 14:28:41 +02:00
|
|
|
for _, t := range channel.Things {
|
|
|
|
if t.ID == thingID {
|
|
|
|
connected := make([]things.Thing, len(channel.Things)-1)
|
|
|
|
for _, thing := range channel.Things {
|
|
|
|
if thing.ID != thingID {
|
|
|
|
connected = append(connected, thing)
|
|
|
|
}
|
|
|
|
}
|
2018-03-11 18:06:01 +01:00
|
|
|
|
2018-05-16 14:28:41 +02:00
|
|
|
channel.Things = connected
|
|
|
|
return crm.Update(channel)
|
2018-03-11 18:06:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-16 14:28:41 +02:00
|
|
|
return things.ErrNotFound
|
2018-03-11 18:06:01 +01:00
|
|
|
}
|
|
|
|
|
2018-05-16 14:28:41 +02:00
|
|
|
func (crm *channelRepositoryMock) HasThing(chanID, key string) (string, error) {
|
2018-03-11 18:06:01 +01:00
|
|
|
// This obscure way to examine map keys is enforced by the key structure
|
|
|
|
// itself (see mocks/commons.go).
|
2018-05-16 14:28:41 +02:00
|
|
|
suffix := fmt.Sprintf("-%s", chanID)
|
2017-09-23 01:03:27 +02:00
|
|
|
|
2018-03-11 18:06:01 +01:00
|
|
|
for k, v := range crm.channels {
|
2017-09-23 01:03:27 +02:00
|
|
|
if strings.HasSuffix(k, suffix) {
|
2018-05-16 14:28:41 +02:00
|
|
|
for _, t := range v.Things {
|
|
|
|
if t.Key == key {
|
|
|
|
return t.ID, nil
|
2017-09-23 01:03:27 +02:00
|
|
|
}
|
|
|
|
}
|
2018-03-11 18:06:01 +01:00
|
|
|
break
|
2017-09-23 01:03:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-16 14:28:41 +02:00
|
|
|
return "", things.ErrNotFound
|
2017-09-23 01:03:27 +02:00
|
|
|
}
|