mirror of
https://github.com/mainflux/mainflux.git
synced 2025-04-26 13:48:53 +08:00

* things, twins, and logger lint fixed Signed-off-by: aryan <aryangodara03@gmail.com> * all services updated, auth jwt not working, ineffectual assignment issue Signed-off-by: aryan <aryangodara03@gmail.com> * handle error from grpc server in endpointtest Signed-off-by: aryan <aryangodara03@gmail.com> * temp commit, auth/jwt needs to be resolved Signed-off-by: aryan <aryangodara03@gmail.com> * revert back to jwt v4 temporarily Signed-off-by: aryan <aryangodara03@gmail.com> * updated jwt tokenizer Signed-off-by: aryan <aryangodara03@gmail.com> * resolve EOF error for httptest requests Signed-off-by: aryan <aryangodara03@gmail.com> * fix auth jwt, update to registeredclaims Signed-off-by: aryan <aryangodara03@gmail.com> * fix ineffective assignment, auth/api/grpc endpoint failing Signed-off-by: aryan <aryangodara03@gmail.com> * temp commit, remove later Signed-off-by: aryan <aryangodara03@gmail.com> * fix grpc server setup Signed-off-by: aryan <aryangodara03@gmail.com> * resolve golangci tests, remove debug statements Signed-off-by: aryan <aryangodara03@gmail.com> * update golangci version and modify linters used Signed-off-by: aryan <aryangodara03@gmail.com> * fix failing tests Signed-off-by: aryan <aryangodara03@gmail.com> * fix grpc server for setup tests Signed-off-by: aryan <aryangodara03@gmail.com> * fix logging and errors inlined Signed-off-by: aryan <aryangodara03@gmail.com> * fix remarks, update grpc setup_test Signed-off-by: aryan <aryangodara03@gmail.com> * fix setup_test Signed-off-by: aryan <aryangodara03@gmail.com> * update setup_test grpc Signed-off-by: aryan <aryangodara03@gmail.com> * fix data race Signed-off-by: aryan <aryangodara03@gmail.com> * update setup_test grpc Signed-off-by: aryan <aryangodara03@gmail.com> * fix grpc setup down to single simple function Signed-off-by: aryan <aryangodara03@gmail.com> * fix linting issues Signed-off-by: aryan <aryangodara03@gmail.com> * resolve pr comments Signed-off-by: aryan <aryangodara03@gmail.com> * fix tests, handle returned errors, go mod tidy vendor Signed-off-by: aryan <aryangodara03@gmail.com> * fix errors from new linters Signed-off-by: aryan <aryangodara03@gmail.com> --------- Signed-off-by: aryan <aryangodara03@gmail.com>
169 lines
3.4 KiB
Go
169 lines
3.4 KiB
Go
// Copyright (c) Mainflux
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package mocks
|
|
|
|
import (
|
|
"encoding/json"
|
|
"sync"
|
|
|
|
"github.com/mainflux/mainflux/pkg/transformers/senml"
|
|
"github.com/mainflux/mainflux/readers"
|
|
)
|
|
|
|
var _ readers.MessageRepository = (*messageRepositoryMock)(nil)
|
|
|
|
type messageRepositoryMock struct {
|
|
mutex sync.Mutex
|
|
messages map[string][]readers.Message
|
|
}
|
|
|
|
// NewMessageRepository returns mock implementation of message repository.
|
|
func NewMessageRepository(chanID string, messages []readers.Message) readers.MessageRepository {
|
|
repo := map[string][]readers.Message{
|
|
chanID: messages,
|
|
}
|
|
|
|
return &messageRepositoryMock{
|
|
mutex: sync.Mutex{},
|
|
messages: repo,
|
|
}
|
|
}
|
|
|
|
func (repo *messageRepositoryMock) ReadAll(chanID string, rpm readers.PageMetadata) (readers.MessagesPage, error) {
|
|
repo.mutex.Lock()
|
|
defer repo.mutex.Unlock()
|
|
|
|
if rpm.Format != "" && rpm.Format != "messages" {
|
|
return readers.MessagesPage{}, nil
|
|
}
|
|
|
|
var query map[string]interface{}
|
|
meta, err := json.Marshal(rpm)
|
|
if err != nil {
|
|
return readers.MessagesPage{}, err
|
|
}
|
|
if err := json.Unmarshal(meta, &query); err != nil {
|
|
return readers.MessagesPage{}, err
|
|
}
|
|
|
|
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":
|
|
if senml.Value == nil {
|
|
ok = false
|
|
}
|
|
|
|
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
|
|
}
|
|
}
|
|
}
|
|
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))
|
|
|
|
if rpm.Offset >= numOfMessages {
|
|
return readers.MessagesPage{}, nil
|
|
}
|
|
|
|
if rpm.Limit < 1 {
|
|
return readers.MessagesPage{}, nil
|
|
}
|
|
|
|
end := rpm.Offset + rpm.Limit
|
|
if rpm.Offset+rpm.Limit > numOfMessages {
|
|
end = numOfMessages
|
|
}
|
|
|
|
return readers.MessagesPage{
|
|
PageMetadata: rpm,
|
|
Total: uint64(len(msgs)),
|
|
Messages: msgs[rpm.Offset:end],
|
|
}, nil
|
|
}
|