2021-04-15 04:26:49 +08:00
|
|
|
// Copyright (c) Mainflux
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
package mocks
|
|
|
|
|
|
|
|
import (
|
2021-05-21 02:53:56 +08:00
|
|
|
"context"
|
2021-04-15 04:26:49 +08:00
|
|
|
"errors"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/mainflux/mainflux/lora"
|
|
|
|
)
|
|
|
|
|
|
|
|
type routeMapMock struct {
|
|
|
|
mu sync.Mutex
|
|
|
|
routes map[string]string
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewRouteMap returns mock route-map instance.
|
|
|
|
func NewRouteMap() lora.RouteMapRepository {
|
|
|
|
return &routeMapMock{
|
|
|
|
routes: make(map[string]string),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-21 02:53:56 +08:00
|
|
|
func (trm *routeMapMock) Save(_ context.Context, mfxID, extID string) error {
|
2021-04-15 04:26:49 +08:00
|
|
|
trm.mu.Lock()
|
|
|
|
defer trm.mu.Unlock()
|
|
|
|
|
|
|
|
trm.routes[extID] = mfxID
|
|
|
|
trm.routes[mfxID] = extID
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-05-21 02:53:56 +08:00
|
|
|
func (trm *routeMapMock) Get(_ context.Context, extID string) (string, error) {
|
2021-04-15 04:26:49 +08:00
|
|
|
trm.mu.Lock()
|
|
|
|
defer trm.mu.Unlock()
|
|
|
|
|
|
|
|
id, ok := trm.routes[extID]
|
|
|
|
if !ok {
|
|
|
|
return "", errors.New("route-map not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
return id, nil
|
|
|
|
}
|
|
|
|
|
2021-05-21 02:53:56 +08:00
|
|
|
func (trm *routeMapMock) Remove(_ context.Context, extID string) error {
|
2021-04-15 04:26:49 +08:00
|
|
|
trm.mu.Lock()
|
|
|
|
defer trm.mu.Unlock()
|
|
|
|
|
|
|
|
var mfxID string
|
|
|
|
for i, val := range trm.routes {
|
|
|
|
if val == extID {
|
|
|
|
mfxID = val
|
|
|
|
delete(trm.routes, i)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, val := range trm.routes {
|
|
|
|
if val == mfxID {
|
|
|
|
delete(trm.routes, i)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|