mirror of
https://github.com/mainflux/mainflux.git
synced 2025-05-09 19:29:29 +08:00

* Add ListTwins test Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Remove monotonic time from twins, definitions and attributes creation and update Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Separate twins and states endpoint tests in two files Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add state generation helper funcs to state endpoint tests Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add createStateResponse() to states test Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add states test cases Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Simplify RetrieveAll twins and states methods Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add service.go to mocks Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Rename mocks.NewService to mocks.New Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add error checking to endpoint state tests Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Fix method comment Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add json response decode success check Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Remove created and updated fields from twin and state res Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Remove definition fields from twin req and res Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add Create funcs to mocks package Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add service save state tests Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add service list states test Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com>
114 lines
2.4 KiB
Go
114 lines
2.4 KiB
Go
// Copyright (c) Mainflux
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package mocks
|
|
|
|
import (
|
|
"context"
|
|
"sort"
|
|
"strings"
|
|
"sync"
|
|
|
|
"github.com/mainflux/mainflux/twins"
|
|
)
|
|
|
|
var _ twins.StateRepository = (*stateRepositoryMock)(nil)
|
|
|
|
type stateRepositoryMock struct {
|
|
mu sync.Mutex
|
|
states map[string]twins.State
|
|
}
|
|
|
|
// NewStateRepository creates in-memory twin repository.
|
|
func NewStateRepository() twins.StateRepository {
|
|
return &stateRepositoryMock{
|
|
states: make(map[string]twins.State),
|
|
}
|
|
}
|
|
|
|
// SaveState persists the state
|
|
func (srm *stateRepositoryMock) Save(ctx context.Context, st twins.State) error {
|
|
srm.mu.Lock()
|
|
defer srm.mu.Unlock()
|
|
|
|
srm.states[key(st.TwinID, string(st.ID))] = st
|
|
|
|
return nil
|
|
}
|
|
|
|
// UpdateState updates the state
|
|
func (srm *stateRepositoryMock) Update(ctx context.Context, st twins.State) error {
|
|
srm.mu.Lock()
|
|
defer srm.mu.Unlock()
|
|
|
|
srm.states[key(st.TwinID, string(st.ID))] = st
|
|
|
|
return nil
|
|
}
|
|
|
|
// CountStates returns the number of states related to twin
|
|
func (srm *stateRepositoryMock) Count(ctx context.Context, tw twins.Twin) (int64, error) {
|
|
return int64(len(srm.states)), nil
|
|
}
|
|
|
|
func (srm *stateRepositoryMock) RetrieveAll(ctx context.Context, offset uint64, limit uint64, twinID string) (twins.StatesPage, error) {
|
|
srm.mu.Lock()
|
|
defer srm.mu.Unlock()
|
|
|
|
items := make([]twins.State, 0)
|
|
|
|
if limit <= 0 {
|
|
return twins.StatesPage{}, nil
|
|
}
|
|
|
|
for k, v := range srm.states {
|
|
if (uint64)(len(items)) >= limit {
|
|
break
|
|
}
|
|
if !strings.HasPrefix(k, twinID) {
|
|
continue
|
|
}
|
|
id := uint64(v.ID)
|
|
if id >= offset && id < offset+limit {
|
|
items = append(items, v)
|
|
}
|
|
}
|
|
|
|
sort.SliceStable(items, func(i, j int) bool {
|
|
return items[i].ID < items[j].ID
|
|
})
|
|
|
|
total := uint64(len(srm.states))
|
|
page := twins.StatesPage{
|
|
States: items,
|
|
PageMetadata: twins.PageMetadata{
|
|
Total: total,
|
|
Offset: offset,
|
|
Limit: limit,
|
|
},
|
|
}
|
|
|
|
return page, nil
|
|
}
|
|
|
|
// RetrieveLast returns the last state related to twin spec by id
|
|
func (srm *stateRepositoryMock) RetrieveLast(ctx context.Context, id string) (twins.State, error) {
|
|
srm.mu.Lock()
|
|
defer srm.mu.Unlock()
|
|
|
|
items := make([]twins.State, 0)
|
|
for _, v := range srm.states {
|
|
if v.TwinID == id {
|
|
items = append(items, v)
|
|
}
|
|
}
|
|
sort.SliceStable(items, func(i, j int) bool {
|
|
return items[i].ID < items[j].ID
|
|
})
|
|
|
|
if len(items) > 0 {
|
|
return items[len(items)-1], nil
|
|
}
|
|
return twins.State{}, nil
|
|
}
|