2018-08-26 19:15:48 +08:00
|
|
|
//
|
|
|
|
// Copyright (c) 2018
|
|
|
|
// Mainflux
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
//
|
|
|
|
|
2018-08-06 23:06:55 +08:00
|
|
|
package mocks
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/mainflux/mainflux"
|
|
|
|
"github.com/mainflux/mainflux/readers"
|
|
|
|
)
|
|
|
|
|
|
|
|
var _ readers.MessageRepository = (*messageRepositoryMock)(nil)
|
|
|
|
|
|
|
|
type messageRepositoryMock struct {
|
|
|
|
mutex sync.Mutex
|
2018-12-05 20:09:25 +08:00
|
|
|
messages map[string][]mainflux.Message
|
2018-08-06 23:06:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewMessageRepository returns mock implementation of message repository.
|
2018-12-05 20:09:25 +08:00
|
|
|
func NewMessageRepository(messages map[string][]mainflux.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
|
|
|
}
|