mirror of
https://github.com/mainflux/mainflux.git
synced 2025-05-02 22:17:10 +08:00

* Use normalizer as stream source Renamed 'writer' service to 'normalizer' and dropped Cassandra facilities from it. Extracted the common dependencies to 'mainflux' package for easier sharing. Fixed the API docs and unified environment variables. Signed-off-by: Dejan Mijic <dejan@mainflux.com> * Use docker build arguments to specify build Signed-off-by: Dejan Mijic <dejan@mainflux.com> * Remove cassandra libraries Signed-off-by: Dejan Mijic <dejan@mainflux.com> * Update go-kit version to 0.6.0 Signed-off-by: Dejan Mijic <dejan@mainflux.com> * Fix manager configuration Signed-off-by: Dejan Mijic <dejan@mainflux.com> * Refactor docker-compose Merged individual compose files and dropped external links. Remove CoAP container since it is not referenced from NginX config at the moment. Update port mapping in compose and nginx.conf. Dropped bin scripts. Updated service documentation. Signed-off-by: Dejan Mijic <dejan@mainflux.com> * Drop content-type check Signed-off-by: Dejan Mijic <dejan@mainflux.com> * Implement users data access layer in PostgreSQL Signed-off-by: Dejan Mijic <dejan@mainflux.com> * Bump version to 0.1.0 Signed-off-by: Dejan Mijic <dejan@mainflux.com> * Use go-kit logger everywhere (except CoAP) Signed-off-by: Dejan Mijic <dejan@mainflux.com> * Improve factory methods naming Signed-off-by: Dejan Mijic <dejan@mainflux.com> * Implement clients data access layer on PostgreSQL Signed-off-by: Dejan Mijic <dejan@mainflux.com> * Make tests stateless All tests are refactored to use map-based table-driven tests. No cross-tests dependencies is present anymore. Signed-off-by: Dejan Mijic <dejan@mainflux.com> * Remove gitignore Signed-off-by: Dejan Mijic <dejan@mainflux.com> * Fix nginx proxying Signed-off-by: Dejan Mijic <dejan@mainflux.com> * Mark client-user FK explicit Signed-off-by: Dejan Mijic <dejan@mainflux.com> * Update API documentation Signed-off-by: Dejan Mijic <dejan@mainflux.com> * Update channel model Signed-off-by: Dejan Mijic <dejan@mainflux.com> * Add channel PostgreSQL repository tests Signed-off-by: Dejan Mijic <dejan@mainflux.com> * Implement PostgreSQL channels DAO Replaced update queries with raw SQL. Explicitly defined M2M table due to difficulties of ensuring the referential integrity through GORM. Signed-off-by: Dejan Mijic <dejan@mainflux.com> * Expose connection endpoints Signed-off-by: Dejan Mijic <dejan@mainflux.com> * Fix swagger docs and remove DB logging Signed-off-by: Dejan Mijic <dejan@mainflux.com> * Fix nested query remarks Signed-off-by: Dejan Mijic <dejan@mainflux.com> * Add unique indices Signed-off-by: Dejan Mijic <dejan@mainflux.com>
134 lines
3.0 KiB
Go
134 lines
3.0 KiB
Go
package mocks
|
|
|
|
import (
|
|
"fmt"
|
|
"strconv"
|
|
"strings"
|
|
"sync"
|
|
|
|
"github.com/mainflux/mainflux/manager"
|
|
)
|
|
|
|
var _ manager.ChannelRepository = (*channelRepositoryMock)(nil)
|
|
|
|
type channelRepositoryMock struct {
|
|
mu sync.Mutex
|
|
counter int
|
|
channels map[string]manager.Channel
|
|
}
|
|
|
|
// NewChannelRepository creates in-memory channel repository.
|
|
func NewChannelRepository() manager.ChannelRepository {
|
|
return &channelRepositoryMock{
|
|
channels: make(map[string]manager.Channel),
|
|
}
|
|
}
|
|
|
|
func (crm *channelRepositoryMock) Save(channel manager.Channel) (string, error) {
|
|
crm.mu.Lock()
|
|
defer crm.mu.Unlock()
|
|
|
|
crm.counter += 1
|
|
channel.ID = strconv.Itoa(crm.counter)
|
|
|
|
crm.channels[key(channel.Owner, channel.ID)] = channel
|
|
|
|
return channel.ID, nil
|
|
}
|
|
|
|
func (crm *channelRepositoryMock) Update(channel manager.Channel) error {
|
|
crm.mu.Lock()
|
|
defer crm.mu.Unlock()
|
|
|
|
dbKey := key(channel.Owner, channel.ID)
|
|
|
|
if _, ok := crm.channels[dbKey]; !ok {
|
|
return manager.ErrNotFound
|
|
}
|
|
|
|
crm.channels[dbKey] = channel
|
|
return nil
|
|
}
|
|
|
|
func (crm *channelRepositoryMock) One(owner, id string) (manager.Channel, error) {
|
|
if c, ok := crm.channels[key(owner, id)]; ok {
|
|
return c, nil
|
|
}
|
|
|
|
return manager.Channel{}, manager.ErrNotFound
|
|
}
|
|
|
|
func (crm *channelRepositoryMock) All(owner string) []manager.Channel {
|
|
// This obscure way to examine map keys is enforced by the key structure
|
|
// itself (see mocks/commons.go).
|
|
prefix := fmt.Sprintf("%s-", owner)
|
|
|
|
channels := make([]manager.Channel, 0)
|
|
|
|
for k, v := range crm.channels {
|
|
if strings.HasPrefix(k, prefix) {
|
|
channels = append(channels, v)
|
|
}
|
|
}
|
|
|
|
return channels
|
|
}
|
|
|
|
func (crm *channelRepositoryMock) Remove(owner, id string) error {
|
|
delete(crm.channels, key(owner, id))
|
|
return nil
|
|
}
|
|
|
|
func (crm *channelRepositoryMock) Connect(owner, chanId, clientId string) error {
|
|
channel, err := crm.One(owner, chanId)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// Since the current implementation has no way to retrieve a real client
|
|
// instance, the implementation will assume client always exist and create
|
|
// a dummy one, containing only the provided ID.
|
|
channel.Clients = append(channel.Clients, manager.Client{ID: clientId})
|
|
return crm.Update(channel)
|
|
}
|
|
|
|
func (crm *channelRepositoryMock) Disconnect(owner, chanId, clientId string) error {
|
|
channel, err := crm.One(owner, chanId)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if !crm.HasClient(chanId, clientId) {
|
|
return manager.ErrNotFound
|
|
}
|
|
|
|
connected := make([]manager.Client, len(channel.Clients)-1)
|
|
for _, client := range channel.Clients {
|
|
if client.ID != clientId {
|
|
connected = append(connected, client)
|
|
}
|
|
}
|
|
|
|
channel.Clients = connected
|
|
return crm.Update(channel)
|
|
}
|
|
|
|
func (crm *channelRepositoryMock) HasClient(channel, client string) bool {
|
|
// This obscure way to examine map keys is enforced by the key structure
|
|
// itself (see mocks/commons.go).
|
|
suffix := fmt.Sprintf("-%s", channel)
|
|
|
|
for k, v := range crm.channels {
|
|
if strings.HasSuffix(k, suffix) {
|
|
for _, c := range v.Clients {
|
|
if c.ID == client {
|
|
return true
|
|
}
|
|
}
|
|
break
|
|
}
|
|
}
|
|
|
|
return false
|
|
}
|