2019-10-07 22:14:47 +08:00
|
|
|
// Copyright (c) Mainflux
|
2018-08-26 19:15:48 +08:00
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
2018-08-06 23:06:55 +08:00
|
|
|
package mocks
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
|
2020-06-03 21:16:19 +08:00
|
|
|
"github.com/mainflux/mainflux/pkg/transformers/senml"
|
2018-08-06 23:06:55 +08:00
|
|
|
"github.com/mainflux/mainflux/readers"
|
|
|
|
)
|
|
|
|
|
|
|
|
var _ readers.MessageRepository = (*messageRepositoryMock)(nil)
|
|
|
|
|
|
|
|
type messageRepositoryMock struct {
|
|
|
|
mutex sync.Mutex
|
2019-11-05 18:57:16 +08:00
|
|
|
messages map[string][]senml.Message
|
2018-08-06 23:06:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewMessageRepository returns mock implementation of message repository.
|
2019-11-05 18:57:16 +08:00
|
|
|
func NewMessageRepository(messages map[string][]senml.Message) readers.MessageRepository {
|
2018-08-06 23:06:55 +08:00
|
|
|
return &messageRepositoryMock{
|
|
|
|
mutex: sync.Mutex{},
|
|
|
|
messages: messages,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-07 21:59:18 +08:00
|
|
|
func (repo *messageRepositoryMock) ReadAll(chanID string, offset, limit uint64, query map[string]string) (readers.MessagesPage, error) {
|
2018-08-06 23:06:55 +08:00
|
|
|
repo.mutex.Lock()
|
|
|
|
defer repo.mutex.Unlock()
|
|
|
|
|
|
|
|
end := offset + limit
|
|
|
|
|
|
|
|
numOfMessages := uint64(len(repo.messages[chanID]))
|
|
|
|
if offset < 0 || offset >= numOfMessages {
|
2019-05-07 21:59:18 +08:00
|
|
|
return readers.MessagesPage{}, nil
|
2018-08-06 23:06:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if limit < 1 {
|
2019-05-07 21:59:18 +08:00
|
|
|
return readers.MessagesPage{}, nil
|
2018-08-06 23:06:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if offset+limit > numOfMessages {
|
|
|
|
end = numOfMessages
|
|
|
|
}
|
|
|
|
|
2019-04-25 06:18:43 +08:00
|
|
|
return readers.MessagesPage{
|
|
|
|
Total: numOfMessages,
|
|
|
|
Limit: limit,
|
|
|
|
Offset: offset,
|
|
|
|
Messages: repo.messages[chanID][offset:end],
|
2019-05-07 21:59:18 +08:00
|
|
|
}, nil
|
2018-08-06 23:06:55 +08:00
|
|
|
}
|