2018-05-15 17:13:09 +02:00
|
|
|
package mocks
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2018-05-16 14:28:41 +02:00
|
|
|
"sort"
|
2018-05-15 17:13:09 +02:00
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/mainflux/mainflux/things"
|
|
|
|
)
|
|
|
|
|
|
|
|
var _ things.ThingRepository = (*thingRepositoryMock)(nil)
|
|
|
|
|
|
|
|
type thingRepositoryMock struct {
|
2018-05-16 14:28:41 +02:00
|
|
|
mu sync.Mutex
|
|
|
|
things map[string]things.Thing
|
2018-05-15 17:13:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewThingRepository creates in-memory thing repository.
|
|
|
|
func NewThingRepository() things.ThingRepository {
|
|
|
|
return &thingRepositoryMock{
|
|
|
|
things: make(map[string]things.Thing),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-16 14:28:41 +02:00
|
|
|
func (trm *thingRepositoryMock) Save(thing things.Thing) (string, error) {
|
2018-05-15 17:13:09 +02:00
|
|
|
trm.mu.Lock()
|
|
|
|
defer trm.mu.Unlock()
|
|
|
|
|
|
|
|
trm.things[key(thing.Owner, thing.ID)] = thing
|
|
|
|
|
2018-05-16 14:28:41 +02:00
|
|
|
return thing.ID, nil
|
2018-05-15 17:13:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (trm *thingRepositoryMock) Update(thing things.Thing) error {
|
|
|
|
trm.mu.Lock()
|
|
|
|
defer trm.mu.Unlock()
|
|
|
|
|
|
|
|
dbKey := key(thing.Owner, thing.ID)
|
|
|
|
|
|
|
|
if _, ok := trm.things[dbKey]; !ok {
|
|
|
|
return things.ErrNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
trm.things[dbKey] = thing
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-05-17 20:17:02 +02:00
|
|
|
func (trm *thingRepositoryMock) RetrieveByID(owner, id string) (things.Thing, error) {
|
2018-05-15 17:13:09 +02:00
|
|
|
if c, ok := trm.things[key(owner, id)]; ok {
|
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return things.Thing{}, things.ErrNotFound
|
|
|
|
}
|
|
|
|
|
2018-05-17 20:17:02 +02:00
|
|
|
func (trm *thingRepositoryMock) RetrieveAll(owner string, offset, limit int) []things.Thing {
|
2018-05-15 17:13:09 +02:00
|
|
|
// This obscure way to examine map keys is enforced by the key structure
|
|
|
|
// itself (see mocks/commons.go).
|
|
|
|
prefix := fmt.Sprintf("%s-", owner)
|
|
|
|
things := make([]things.Thing, 0)
|
|
|
|
|
|
|
|
if offset < 0 || limit <= 0 {
|
|
|
|
return things
|
|
|
|
}
|
|
|
|
|
2018-05-16 14:28:41 +02:00
|
|
|
// Since both ID and key are generated via the identity provider mock, all
|
|
|
|
// identifiers will be at "odd" positions. The following loop skips all
|
|
|
|
// values used for keys. Starting value of 1 indicates the first usable
|
|
|
|
// UUID produced by mocked identity provider.
|
|
|
|
skip := 1
|
|
|
|
for i := 0; i < offset; i++ {
|
|
|
|
skip += 2
|
|
|
|
}
|
|
|
|
|
|
|
|
first := fmt.Sprintf("%s%012d", startID, skip)
|
|
|
|
last := fmt.Sprintf("%s%012d", startID, skip+2*(limit-1))
|
2018-05-15 17:13:09 +02:00
|
|
|
|
|
|
|
for k, v := range trm.things {
|
2018-05-16 14:28:41 +02:00
|
|
|
if strings.HasPrefix(k, prefix) && v.ID >= first && v.ID <= last {
|
2018-05-15 17:13:09 +02:00
|
|
|
things = append(things, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-16 14:28:41 +02:00
|
|
|
sort.SliceStable(things, func(i, j int) bool {
|
|
|
|
return things[i].ID < things[j].ID
|
|
|
|
})
|
|
|
|
|
2018-05-15 17:13:09 +02:00
|
|
|
return things
|
|
|
|
}
|
|
|
|
|
|
|
|
func (trm *thingRepositoryMock) Remove(owner, id string) error {
|
|
|
|
delete(trm.things, key(owner, id))
|
|
|
|
return nil
|
|
|
|
}
|
2018-05-17 20:17:02 +02:00
|
|
|
|
|
|
|
func (trm *thingRepositoryMock) RetrieveByKey(key string) (string, error) {
|
|
|
|
for _, thing := range trm.things {
|
|
|
|
if thing.Key == key {
|
|
|
|
return thing.ID, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "", things.ErrNotFound
|
|
|
|
}
|