2018-08-26 13:15:48 +02:00
|
|
|
//
|
|
|
|
// Copyright (c) 2018
|
|
|
|
// Mainflux
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
//
|
|
|
|
|
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-21 12:51:46 +02:00
|
|
|
mu sync.Mutex
|
|
|
|
counter uint64
|
|
|
|
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-21 12:51:46 +02:00
|
|
|
func (trm *thingRepositoryMock) Save(thing things.Thing) (uint64, error) {
|
2018-05-15 17:13:09 +02:00
|
|
|
trm.mu.Lock()
|
|
|
|
defer trm.mu.Unlock()
|
|
|
|
|
2018-05-21 12:51:46 +02:00
|
|
|
trm.counter++
|
|
|
|
thing.ID = trm.counter
|
2018-05-15 17:13:09 +02:00
|
|
|
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-21 12:51:46 +02:00
|
|
|
func (trm *thingRepositoryMock) RetrieveByID(owner string, id uint64) (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-10-24 11:21:03 +02:00
|
|
|
func (trm *thingRepositoryMock) RetrieveAll(owner string, offset, limit uint64) []things.Thing {
|
2018-05-15 17:13:09 +02:00
|
|
|
things := make([]things.Thing, 0)
|
|
|
|
|
|
|
|
if offset < 0 || limit <= 0 {
|
|
|
|
return things
|
|
|
|
}
|
|
|
|
|
2018-05-21 12:51:46 +02:00
|
|
|
first := uint64(offset) + 1
|
|
|
|
last := first + uint64(limit)
|
2018-05-15 17:13:09 +02:00
|
|
|
|
2018-05-21 12:51:46 +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)
|
2018-05-15 17:13:09 +02:00
|
|
|
for k, v := range trm.things {
|
2018-05-21 12:51:46 +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
|
|
|
|
}
|
|
|
|
|
2018-05-21 12:51:46 +02:00
|
|
|
func (trm *thingRepositoryMock) Remove(owner string, id uint64) error {
|
2018-05-15 17:13:09 +02:00
|
|
|
delete(trm.things, key(owner, id))
|
|
|
|
return nil
|
|
|
|
}
|
2018-05-17 20:17:02 +02:00
|
|
|
|
2018-05-21 12:51:46 +02:00
|
|
|
func (trm *thingRepositoryMock) RetrieveByKey(key string) (uint64, error) {
|
2018-05-17 20:17:02 +02:00
|
|
|
for _, thing := range trm.things {
|
|
|
|
if thing.Key == key {
|
|
|
|
return thing.ID, nil
|
|
|
|
}
|
|
|
|
}
|
2018-05-21 12:51:46 +02:00
|
|
|
return 0, things.ErrNotFound
|
2018-05-17 20:17:02 +02:00
|
|
|
}
|
2018-09-04 22:19:43 +02:00
|
|
|
|
|
|
|
type thingCacheMock struct {
|
|
|
|
mu sync.Mutex
|
|
|
|
things map[string]uint64
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewThingCache returns mock cache instance.
|
|
|
|
func NewThingCache() things.ThingCache {
|
|
|
|
return &thingCacheMock{
|
|
|
|
things: make(map[string]uint64),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tcm *thingCacheMock) Save(key string, id uint64) error {
|
|
|
|
tcm.mu.Lock()
|
|
|
|
defer tcm.mu.Unlock()
|
|
|
|
|
|
|
|
tcm.things[key] = id
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tcm *thingCacheMock) ID(key string) (uint64, error) {
|
|
|
|
tcm.mu.Lock()
|
|
|
|
defer tcm.mu.Unlock()
|
|
|
|
|
|
|
|
id, ok := tcm.things[key]
|
|
|
|
if !ok {
|
|
|
|
return 0, things.ErrNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
return id, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tcm *thingCacheMock) Remove(id uint64) error {
|
|
|
|
tcm.mu.Lock()
|
|
|
|
defer tcm.mu.Unlock()
|
|
|
|
|
|
|
|
for key, val := range tcm.things {
|
|
|
|
if val == id {
|
|
|
|
delete(tcm.things, key)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return things.ErrNotFound
|
|
|
|
}
|