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 (
|
2021-01-26 19:23:15 +08:00
|
|
|
"encoding/json"
|
2018-08-06 23:06:55 +08:00
|
|
|
"sync"
|
|
|
|
|
2021-01-26 19:23:15 +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
|
2020-12-30 22:43:04 +08:00
|
|
|
messages map[string][]readers.Message
|
2018-08-06 23:06:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewMessageRepository returns mock implementation of message repository.
|
2021-01-26 19:23:15 +08:00
|
|
|
func NewMessageRepository(chanID string, messages []readers.Message) readers.MessageRepository {
|
|
|
|
repo := map[string][]readers.Message{
|
|
|
|
chanID: messages,
|
|
|
|
}
|
|
|
|
|
2018-08-06 23:06:55 +08:00
|
|
|
return &messageRepositoryMock{
|
|
|
|
mutex: sync.Mutex{},
|
2021-01-26 19:23:15 +08:00
|
|
|
messages: repo,
|
2018-08-06 23:06:55 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-26 19:23:15 +08:00
|
|
|
func (repo *messageRepositoryMock) ReadAll(chanID string, rpm readers.PageMetadata) (readers.MessagesPage, error) {
|
2018-08-06 23:06:55 +08:00
|
|
|
repo.mutex.Lock()
|
|
|
|
defer repo.mutex.Unlock()
|
|
|
|
|
2021-01-26 19:23:15 +08:00
|
|
|
if rpm.Format != "" && rpm.Format != "messages" {
|
|
|
|
return readers.MessagesPage{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var query map[string]interface{}
|
|
|
|
meta, _ := json.Marshal(rpm)
|
|
|
|
json.Unmarshal(meta, &query)
|
|
|
|
|
|
|
|
var msgs []readers.Message
|
|
|
|
for _, m := range repo.messages[chanID] {
|
|
|
|
senml := m.(senml.Message)
|
|
|
|
|
|
|
|
ok := true
|
|
|
|
|
|
|
|
for name := range query {
|
|
|
|
switch name {
|
|
|
|
case "subtopic":
|
|
|
|
if rpm.Subtopic != senml.Subtopic {
|
|
|
|
ok = false
|
|
|
|
}
|
|
|
|
case "publisher":
|
|
|
|
if rpm.Publisher != senml.Publisher {
|
|
|
|
ok = false
|
|
|
|
}
|
|
|
|
case "name":
|
|
|
|
if rpm.Name != senml.Name {
|
|
|
|
ok = false
|
|
|
|
}
|
|
|
|
case "protocol":
|
|
|
|
if rpm.Protocol != senml.Protocol {
|
|
|
|
ok = false
|
|
|
|
}
|
|
|
|
case "v":
|
2021-02-10 05:44:04 +08:00
|
|
|
if senml.Value == nil {
|
2021-01-26 19:23:15 +08:00
|
|
|
ok = false
|
|
|
|
}
|
2021-02-10 05:44:04 +08:00
|
|
|
|
|
|
|
val, okQuery := query["comparator"]
|
|
|
|
if okQuery {
|
|
|
|
switch val.(string) {
|
|
|
|
case readers.LowerThanKey:
|
|
|
|
if senml.Value != nil &&
|
|
|
|
*senml.Value >= rpm.Value {
|
|
|
|
ok = false
|
|
|
|
}
|
|
|
|
case readers.LowerThanEqualKey:
|
|
|
|
if senml.Value != nil &&
|
|
|
|
*senml.Value > rpm.Value {
|
|
|
|
ok = false
|
|
|
|
}
|
|
|
|
case readers.GreaterThanKey:
|
|
|
|
if senml.Value != nil &&
|
|
|
|
*senml.Value <= rpm.Value {
|
|
|
|
ok = false
|
|
|
|
}
|
|
|
|
case readers.GreaterThanEqualKey:
|
|
|
|
if senml.Value != nil &&
|
|
|
|
*senml.Value < rpm.Value {
|
|
|
|
ok = false
|
|
|
|
}
|
|
|
|
case readers.EqualKey:
|
|
|
|
default:
|
|
|
|
if senml.Value != nil &&
|
|
|
|
*senml.Value != rpm.Value {
|
|
|
|
ok = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-01-26 19:23:15 +08:00
|
|
|
case "vb":
|
|
|
|
if senml.BoolValue == nil ||
|
|
|
|
(senml.BoolValue != nil &&
|
|
|
|
*senml.BoolValue != rpm.BoolValue) {
|
|
|
|
ok = false
|
|
|
|
}
|
|
|
|
case "vs":
|
|
|
|
if senml.StringValue == nil ||
|
|
|
|
(senml.StringValue != nil &&
|
|
|
|
*senml.StringValue != rpm.StringValue) {
|
|
|
|
ok = false
|
|
|
|
}
|
|
|
|
case "vd":
|
|
|
|
if senml.DataValue == nil ||
|
|
|
|
(senml.DataValue != nil &&
|
|
|
|
*senml.DataValue != rpm.DataValue) {
|
|
|
|
ok = false
|
|
|
|
}
|
|
|
|
case "from":
|
|
|
|
if senml.Time < rpm.From {
|
|
|
|
ok = false
|
|
|
|
}
|
|
|
|
case "to":
|
|
|
|
if senml.Time >= rpm.To {
|
|
|
|
ok = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ok {
|
|
|
|
msgs = append(msgs, m)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
numOfMessages := uint64(len(msgs))
|
2018-08-06 23:06:55 +08:00
|
|
|
|
2021-01-26 19:23:15 +08:00
|
|
|
if rpm.Offset >= numOfMessages {
|
2019-05-07 21:59:18 +08:00
|
|
|
return readers.MessagesPage{}, nil
|
2018-08-06 23:06:55 +08:00
|
|
|
}
|
|
|
|
|
2021-01-26 19:23:15 +08:00
|
|
|
if rpm.Limit < 1 {
|
2019-05-07 21:59:18 +08:00
|
|
|
return readers.MessagesPage{}, nil
|
2018-08-06 23:06:55 +08:00
|
|
|
}
|
|
|
|
|
2021-01-26 19:23:15 +08:00
|
|
|
end := rpm.Offset + rpm.Limit
|
|
|
|
if rpm.Offset+rpm.Limit > numOfMessages {
|
2018-08-06 23:06:55 +08:00
|
|
|
end = numOfMessages
|
|
|
|
}
|
|
|
|
|
2019-04-25 06:18:43 +08:00
|
|
|
return readers.MessagesPage{
|
2021-01-26 19:23:15 +08:00
|
|
|
PageMetadata: rpm,
|
|
|
|
Total: uint64(len(msgs)),
|
|
|
|
Messages: msgs[rpm.Offset:end],
|
2019-05-07 21:59:18 +08:00
|
|
|
}, nil
|
2018-08-06 23:06:55 +08:00
|
|
|
}
|