From 0f856f5667e51e33e8619c17d6ac2614b3ea3462 Mon Sep 17 00:00:00 2001 From: Manuel Imperiale Date: Sun, 17 Jan 2021 23:12:45 +0100 Subject: [PATCH] NOISSUE - Rename package aliases uuidProvider into uuid (#1323) * NOISSUE - Rename pkg aliases uuidProvider into uuid and fix authn typo Signed-off-by: Manuel Imperiale * Add missing error checks Signed-off-by: Manuel Imperiale * Use global uuidProvider Signed-off-by: Manuel Imperiale * Use expTime globally Signed-off-by: Manuel Imperiale * Fix user uuid provider Signed-off-by: Manuel Imperiale * Fix review Signed-off-by: Manuel Imperiale * Use idProvider name Signed-off-by: Manuel Imperiale * Use idProvider instead of uuidProvider Signed-off-by: Manuel Imperiale * Use idProvider instead of uuidProvider Signed-off-by: Manuel Imperiale --- auth/api/grpc/endpoint_test.go | 4 +- auth/api/http/endpoint_test.go | 4 +- auth/postgres/key_test.go | 32 +++++++----- auth/service.go | 8 +-- auth/service_test.go | 4 +- cmd/auth/main.go | 7 +-- cmd/things/main.go | 30 +++++------ cmd/twins/main.go | 6 +-- cmd/users/main.go | 5 +- pkg/sdk/go/things_test.go | 4 +- pkg/sdk/go/users_test.go | 4 +- readers/cassandra/messages_test.go | 8 +-- readers/influxdb/messages_test.go | 11 ++-- readers/postgres/messages_test.go | 12 +++-- things/api/auth/grpc/setup_test.go | 4 +- things/api/auth/http/endpoint_test.go | 4 +- things/api/things/http/endpoint_test.go | 4 +- things/postgres/channels_test.go | 69 ++++++++++++------------- things/postgres/things_test.go | 62 +++++++++++----------- things/redis/streams_test.go | 4 +- things/redis/things_test.go | 8 +-- things/service.go | 12 ++--- things/service_test.go | 16 +++--- twins/mocks/service.go | 5 +- twins/mongodb/states_test.go | 7 ++- twins/mongodb/twins_test.go | 24 ++++----- twins/service.go | 34 ++++++------ users/api/endpoint_test.go | 4 +- users/postgres/groups_test.go | 42 +++++++-------- users/postgres/users_test.go | 12 +++-- users/service.go | 29 ++++++----- users/service_test.go | 10 ++-- uuid.go | 2 +- 33 files changed, 260 insertions(+), 231 deletions(-) diff --git a/auth/api/grpc/endpoint_test.go b/auth/api/grpc/endpoint_test.go index bf361d0c..7efd870e 100644 --- a/auth/api/grpc/endpoint_test.go +++ b/auth/api/grpc/endpoint_test.go @@ -35,10 +35,10 @@ var svc auth.Service func newService() auth.Service { repo := mocks.NewKeyRepository() groupRepo := mocks.NewGroupRepository() - uuidProvider := uuid.NewMock() + idProvider := uuid.NewMock() t := jwt.New(secret) - return auth.New(repo, groupRepo, uuidProvider, t) + return auth.New(repo, groupRepo, idProvider, t) } func startGRPCServer(svc auth.Service, port int) { diff --git a/auth/api/http/endpoint_test.go b/auth/api/http/endpoint_test.go index 1e077eee..edde694e 100644 --- a/auth/api/http/endpoint_test.go +++ b/auth/api/http/endpoint_test.go @@ -65,9 +65,9 @@ func (tr testRequest) make() (*http.Response, error) { func newService() auth.Service { repo := mocks.NewKeyRepository() groupRepo := mocks.NewGroupRepository() - uuidProvider := uuid.NewMock() + idProvider := uuid.NewMock() t := jwt.New(secret) - return auth.New(repo, groupRepo, uuidProvider, t) + return auth.New(repo, groupRepo, idProvider, t) } func newServer(svc auth.Service) *httptest.Server { diff --git a/auth/postgres/key_test.go b/auth/postgres/key_test.go index 98cd38c1..63919a37 100644 --- a/auth/postgres/key_test.go +++ b/auth/postgres/key_test.go @@ -12,18 +12,26 @@ import ( "github.com/mainflux/mainflux/auth" "github.com/mainflux/mainflux/auth/postgres" "github.com/mainflux/mainflux/pkg/errors" - uuidProvider "github.com/mainflux/mainflux/pkg/uuid" + "github.com/mainflux/mainflux/pkg/uuid" "github.com/opentracing/opentracing-go" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +const email = "user-save@example.com" + +var ( + expTime = time.Now().Add(5 * time.Minute) + idProvider = uuid.New() ) func TestKeySave(t *testing.T) { dbMiddleware := postgres.NewDatabase(db) repo := postgres.New(dbMiddleware) - email := "user-save@example.com" - expTime := time.Now().Add(5 * time.Minute) - id, _ := uuidProvider.New().ID() + id, err := idProvider.ID() + require.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err)) + cases := []struct { desc string key auth.Key @@ -63,9 +71,9 @@ func TestKeyRetrieve(t *testing.T) { dbMiddleware := postgres.NewDatabase(db) repo := postgres.New(dbMiddleware) - email := "user-save@example.com" - expTime := time.Now().Add(5 * time.Minute) - id, _ := uuidProvider.New().ID() + id, err := idProvider.ID() + require.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err)) + key := auth.Key{ Subject: email, IssuedAt: time.Now(), @@ -73,7 +81,7 @@ func TestKeyRetrieve(t *testing.T) { ID: id, IssuerID: id, } - _, err := repo.Save(context.Background(), key) + _, err = repo.Save(context.Background(), key) assert.Nil(t, err, fmt.Sprintf("Storing Key expected to succeed: %s", err)) cases := []struct { desc string @@ -111,9 +119,9 @@ func TestKeyRemove(t *testing.T) { dbMiddleware := postgres.NewDatabase(db) repo := postgres.New(dbMiddleware) - email := "user-save@example.com" - expTime := time.Now().Add(5 * time.Minute) - id, _ := uuidProvider.New().ID() + id, err := idProvider.ID() + require.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err)) + key := auth.Key{ Subject: email, IssuedAt: time.Now(), @@ -121,7 +129,7 @@ func TestKeyRemove(t *testing.T) { ID: id, IssuerID: id, } - _, err := repo.Save(opentracing.ContextWithSpan(context.Background(), opentracing.StartSpan("")), key) + _, err = repo.Save(opentracing.ContextWithSpan(context.Background(), opentracing.StartSpan("")), key) assert.Nil(t, err, fmt.Sprintf("Storing Key expected to succeed: %s", err)) cases := []struct { desc string diff --git a/auth/service.go b/auth/service.go index b343236a..279889bb 100644 --- a/auth/service.go +++ b/auth/service.go @@ -91,18 +91,18 @@ var _ Service = (*service)(nil) type service struct { keys KeyRepository groups groups.Repository - uuidProvider mainflux.IDProvider + idProvider mainflux.IDProvider ulidProvider mainflux.IDProvider tokenizer Tokenizer } // New instantiates the auth service implementation. -func New(keys KeyRepository, groups groups.Repository, up mainflux.IDProvider, tokenizer Tokenizer) Service { +func New(keys KeyRepository, groups groups.Repository, idp mainflux.IDProvider, tokenizer Tokenizer) Service { return &service{ tokenizer: tokenizer, keys: keys, groups: groups, - uuidProvider: up, + idProvider: idp, ulidProvider: ulid.New(), } } @@ -184,7 +184,7 @@ func (svc service) userKey(ctx context.Context, token string, key Key) (Key, str key.Subject = sub } - keyID, err := svc.uuidProvider.ID() + keyID, err := svc.idProvider.ID() if err != nil { return Key{}, "", errors.Wrap(errIssueUser, err) } diff --git a/auth/service_test.go b/auth/service_test.go index b7aa35da..b0024c4d 100644 --- a/auth/service_test.go +++ b/auth/service_test.go @@ -26,9 +26,9 @@ const ( func newService() auth.Service { repo := mocks.NewKeyRepository() groupRepo := mocks.NewGroupRepository() - uuidProvider := uuid.NewMock() + idProvider := uuid.NewMock() t := jwt.New(secret) - return auth.New(repo, groupRepo, uuidProvider, t) + return auth.New(repo, groupRepo, idProvider, t) } func TestIssue(t *testing.T) { diff --git a/cmd/auth/main.go b/cmd/auth/main.go index 214f44c9..015a0c25 100644 --- a/cmd/auth/main.go +++ b/cmd/auth/main.go @@ -22,7 +22,7 @@ import ( "github.com/mainflux/mainflux/auth/postgres" "github.com/mainflux/mainflux/auth/tracing" "github.com/mainflux/mainflux/logger" - uuidProvider "github.com/mainflux/mainflux/pkg/uuid" + "github.com/mainflux/mainflux/pkg/uuid" "github.com/opentracing/opentracing-go" stdprometheus "github.com/prometheus/client_golang/prometheus" jconfig "github.com/uber/jaeger-client-go/config" @@ -182,9 +182,10 @@ func newService(db *sqlx.DB, tracer opentracing.Tracer, secret string, logger lo groupsRepo := postgres.NewGroupRepo(database) groupsRepo = tracing.GroupRepositoryMiddleware(tracer, groupsRepo) - up := uuidProvider.New() + idProvider := uuid.New() t := jwt.New(secret) - svc := auth.New(keysRepo, groupsRepo, up, t) + + svc := auth.New(keysRepo, groupsRepo, idProvider, t) svc = api.LoggingMiddleware(svc, logger) svc = api.MetricsMiddleware( svc, diff --git a/cmd/things/main.go b/cmd/things/main.go index 1b3afd91..1aaa5e50 100644 --- a/cmd/things/main.go +++ b/cmd/things/main.go @@ -27,7 +27,7 @@ import ( "github.com/mainflux/mainflux" authapi "github.com/mainflux/mainflux/auth/api/grpc" "github.com/mainflux/mainflux/logger" - uuidProvider "github.com/mainflux/mainflux/pkg/uuid" + "github.com/mainflux/mainflux/pkg/uuid" "github.com/mainflux/mainflux/things" "github.com/mainflux/mainflux/things/api" authgrpcapi "github.com/mainflux/mainflux/things/api/auth/grpc" @@ -68,8 +68,8 @@ const ( defSingleUserEmail = "" defSingleUserToken = "" defJaegerURL = "" - defAuthnURL = "localhost:8181" - defAuthnTimeout = "1s" + defAuthURL = "localhost:8181" + defAuthTimeout = "1s" envLogLevel = "MF_THINGS_LOG_LEVEL" envDBHost = "MF_THINGS_DB_HOST" @@ -97,8 +97,8 @@ const ( envSingleUserEmail = "MF_THINGS_SINGLE_USER_EMAIL" envSingleUserToken = "MF_THINGS_SINGLE_USER_TOKEN" envJaegerURL = "MF_JAEGER_URL" - envAuthnURL = "MF_AUTH_GRPC_URL" - envAuthnTimeout = "MF_AUTH_GRPC_TIMEOUT" + envAuthURL = "MF_AUTH_GRPC_URL" + envAuthTimeout = "MF_AUTH_GRPC_TIMEOUT" ) type config struct { @@ -120,8 +120,8 @@ type config struct { singleUserEmail string singleUserToken string jaegerURL string - authnURL string - authnTimeout time.Duration + authURL string + authTimeout time.Duration } func main() { @@ -179,9 +179,9 @@ func loadConfig() config { log.Fatalf("Invalid value passed for %s\n", envClientTLS) } - authnTimeout, err := time.ParseDuration(mainflux.Env(envAuthnTimeout, defAuthnTimeout)) + authTimeout, err := time.ParseDuration(mainflux.Env(envAuthTimeout, defAuthTimeout)) if err != nil { - log.Fatalf("Invalid %s value: %s", envAuthnTimeout, err.Error()) + log.Fatalf("Invalid %s value: %s", envAuthTimeout, err.Error()) } dbConfig := postgres.Config{ @@ -215,8 +215,8 @@ func loadConfig() config { singleUserEmail: mainflux.Env(envSingleUserEmail, defSingleUserEmail), singleUserToken: mainflux.Env(envSingleUserToken, defSingleUserToken), jaegerURL: mainflux.Env(envJaegerURL, defJaegerURL), - authnURL: mainflux.Env(envAuthnURL, defAuthnURL), - authnTimeout: authnTimeout, + authURL: mainflux.Env(envAuthURL, defAuthURL), + authTimeout: authTimeout, } } @@ -273,7 +273,7 @@ func createAuthClient(cfg config, tracer opentracing.Tracer, logger logger.Logge } conn := connectToAuth(cfg, logger) - return authapi.NewClient(tracer, conn, cfg.authnTimeout), conn.Close + return authapi.NewClient(tracer, conn, cfg.authTimeout), conn.Close } func connectToAuth(cfg config, logger logger.Logger) *grpc.ClientConn { @@ -292,7 +292,7 @@ func connectToAuth(cfg config, logger logger.Logger) *grpc.ClientConn { logger.Info("gRPC communication is not encrypted") } - conn, err := grpc.Dial(cfg.authnURL, opts...) + conn, err := grpc.Dial(cfg.authURL, opts...) if err != nil { logger.Error(fmt.Sprintf("Failed to connect to auth service: %s", err)) os.Exit(1) @@ -318,9 +318,9 @@ func newService(auth mainflux.AuthServiceClient, dbTracer opentracing.Tracer, ca thingCache := rediscache.NewThingCache(cacheClient) thingCache = tracing.ThingCacheMiddleware(cacheTracer, thingCache) - up := uuidProvider.New() + idProvider := uuid.New() - svc := things.New(auth, thingsRepo, channelsRepo, groupsRepo, chanCache, thingCache, up) + svc := things.New(auth, thingsRepo, channelsRepo, groupsRepo, chanCache, thingCache, idProvider) svc = rediscache.NewEventStoreMiddleware(svc, esClient) svc = api.LoggingMiddleware(svc, logger) svc = api.MetricsMiddleware( diff --git a/cmd/twins/main.go b/cmd/twins/main.go index 36b493ee..8cfcc280 100644 --- a/cmd/twins/main.go +++ b/cmd/twins/main.go @@ -22,7 +22,7 @@ import ( "github.com/mainflux/mainflux/logger" "github.com/mainflux/mainflux/pkg/messaging" "github.com/mainflux/mainflux/pkg/messaging/nats" - uuidProvider "github.com/mainflux/mainflux/pkg/uuid" + "github.com/mainflux/mainflux/pkg/uuid" localusers "github.com/mainflux/mainflux/things/users" "github.com/mainflux/mainflux/twins" "github.com/mainflux/mainflux/twins/api" @@ -269,11 +269,11 @@ func newService(ps messaging.PubSub, chanID string, users mainflux.AuthServiceCl stateRepo := twmongodb.NewStateRepository(db) stateRepo = tracing.StateRepositoryMiddleware(dbTracer, stateRepo) - up := uuidProvider.New() + idProvider := uuid.New() twinCache := rediscache.NewTwinCache(cacheClient) twinCache = tracing.TwinCacheMiddleware(cacheTracer, twinCache) - svc := twins.New(ps, users, twinRepo, twinCache, stateRepo, up, chanID, logger) + svc := twins.New(ps, users, twinRepo, twinCache, stateRepo, idProvider, chanID, logger) svc = api.LoggingMiddleware(svc, logger) svc = api.MetricsMiddleware( svc, diff --git a/cmd/users/main.go b/cmd/users/main.go index 35b28871..77dac684 100644 --- a/cmd/users/main.go +++ b/cmd/users/main.go @@ -17,6 +17,7 @@ import ( "time" "github.com/mainflux/mainflux/internal/email" + "github.com/mainflux/mainflux/pkg/uuid" "github.com/mainflux/mainflux/users" "github.com/mainflux/mainflux/users/bcrypt" "github.com/mainflux/mainflux/users/emailer" @@ -284,7 +285,9 @@ func newService(db *sqlx.DB, tracer opentracing.Tracer, auth mainflux.AuthServic logger.Error(fmt.Sprintf("Failed to configure e-mailing util: %s", err.Error())) } - svc := users.New(userRepo, groupRepo, hasher, auth, emailer) + idProvider := uuid.New() + + svc := users.New(userRepo, groupRepo, hasher, auth, emailer, idProvider) svc = api.LoggingMiddleware(svc, logger) svc = api.MetricsMiddleware( svc, diff --git a/pkg/sdk/go/things_test.go b/pkg/sdk/go/things_test.go index 30d754f3..96d057f1 100644 --- a/pkg/sdk/go/things_test.go +++ b/pkg/sdk/go/things_test.go @@ -47,9 +47,9 @@ func newThingsService(tokens map[string]string) things.Service { channelsRepo := mocks.NewChannelRepository(thingsRepo, conns) chanCache := mocks.NewChannelCache() thingCache := mocks.NewThingCache() - uuidProvider := uuid.NewMock() + idProvider := uuid.NewMock() - return things.New(auth, thingsRepo, channelsRepo, nil, chanCache, thingCache, uuidProvider) + return things.New(auth, thingsRepo, channelsRepo, nil, chanCache, thingCache, idProvider) } func newThingsServer(svc things.Service) *httptest.Server { diff --git a/pkg/sdk/go/users_test.go b/pkg/sdk/go/users_test.go index 6f3021d8..377dce95 100644 --- a/pkg/sdk/go/users_test.go +++ b/pkg/sdk/go/users_test.go @@ -12,6 +12,7 @@ import ( "github.com/mainflux/mainflux" sdk "github.com/mainflux/mainflux/pkg/sdk/go" + "github.com/mainflux/mainflux/pkg/uuid" "github.com/mainflux/mainflux/users" "github.com/mainflux/mainflux/users/api" "github.com/mainflux/mainflux/users/mocks" @@ -29,8 +30,9 @@ func newUserService() users.Service { hasher := mocks.NewHasher() auth := mocks.NewAuthService(map[string]string{"user@example.com": "user@example.com"}) emailer := mocks.NewEmailer() + idProvider := uuid.New() - return users.New(usersRepo, groupsRepo, hasher, auth, emailer) + return users.New(usersRepo, groupsRepo, hasher, auth, emailer, idProvider) } func newUserServer(svc users.Service) *httptest.Server { diff --git a/readers/cassandra/messages_test.go b/readers/cassandra/messages_test.go index 36769e22..d2f9aa64 100644 --- a/readers/cassandra/messages_test.go +++ b/readers/cassandra/messages_test.go @@ -36,6 +36,8 @@ var ( vb = true vd = "base64" sum float64 = 42 + + idProvider = uuid.New() ) func TestReadSenml(t *testing.T) { @@ -47,11 +49,11 @@ func TestReadSenml(t *testing.T) { defer session.Close() writer := writer.New(session) - chanID, err := uuid.New().ID() + chanID, err := idProvider.ID() require.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err)) - pubID, err := uuid.New().ID() + pubID, err := idProvider.ID() require.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err)) - pub2ID, err := uuid.New().ID() + pub2ID, err := idProvider.ID() require.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err)) m := senml.Message{ diff --git a/readers/influxdb/messages_test.go b/readers/influxdb/messages_test.go index e6eb3f1e..66dc2ec4 100644 --- a/readers/influxdb/messages_test.go +++ b/readers/influxdb/messages_test.go @@ -8,7 +8,7 @@ import ( influxdata "github.com/influxdata/influxdb/client/v2" iwriter "github.com/mainflux/mainflux/consumers/writers/influxdb" "github.com/mainflux/mainflux/pkg/transformers/senml" - uuidProvider "github.com/mainflux/mainflux/pkg/uuid" + "github.com/mainflux/mainflux/pkg/uuid" "github.com/mainflux/mainflux/readers" ireader "github.com/mainflux/mainflux/readers/influxdb" @@ -34,17 +34,18 @@ var ( vd = "dataValue" sum float64 = 42 - client influxdata.Client + client influxdata.Client + idProvider = uuid.New() ) func TestReadAll(t *testing.T) { writer := iwriter.New(client, testDB) - chanID, err := uuidProvider.New().ID() + chanID, err := idProvider.ID() require.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err)) - pubID, err := uuidProvider.New().ID() + pubID, err := idProvider.ID() require.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err)) - pub2ID, err := uuidProvider.New().ID() + pub2ID, err := idProvider.ID() require.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err)) m := senml.Message{ diff --git a/readers/postgres/messages_test.go b/readers/postgres/messages_test.go index 8f775efe..505f107e 100644 --- a/readers/postgres/messages_test.go +++ b/readers/postgres/messages_test.go @@ -10,7 +10,7 @@ import ( pwriter "github.com/mainflux/mainflux/consumers/writers/postgres" "github.com/mainflux/mainflux/pkg/transformers/senml" - uuidProvider "github.com/mainflux/mainflux/pkg/uuid" + "github.com/mainflux/mainflux/pkg/uuid" "github.com/mainflux/mainflux/readers" preader "github.com/mainflux/mainflux/readers/postgres" "github.com/stretchr/testify/assert" @@ -33,18 +33,20 @@ var ( vb = true vd = "dataValue" sum float64 = 42 + + idProvider = uuid.New() ) func TestReadSenml(t *testing.T) { writer := pwriter.New(db) - chanID, err := uuidProvider.New().ID() + chanID, err := idProvider.ID() require.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err)) - pubID, err := uuidProvider.New().ID() + pubID, err := idProvider.ID() require.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err)) - pub2ID, err := uuidProvider.New().ID() + pub2ID, err := idProvider.ID() require.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err)) - wrongID, err := uuidProvider.New().ID() + wrongID, err := idProvider.ID() require.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err)) m := senml.Message{ diff --git a/things/api/auth/grpc/setup_test.go b/things/api/auth/grpc/setup_test.go index 7307f172..36e4b609 100644 --- a/things/api/auth/grpc/setup_test.go +++ b/things/api/auth/grpc/setup_test.go @@ -48,7 +48,7 @@ func newService(tokens map[string]string) things.Service { channelsRepo := mocks.NewChannelRepository(thingsRepo, conns) chanCache := mocks.NewChannelCache() thingCache := mocks.NewThingCache() - uuidProvider := uuid.NewMock() + idProvider := uuid.NewMock() - return things.New(auth, thingsRepo, channelsRepo, nil, chanCache, thingCache, uuidProvider) + return things.New(auth, thingsRepo, channelsRepo, nil, chanCache, thingCache, idProvider) } diff --git a/things/api/auth/http/endpoint_test.go b/things/api/auth/http/endpoint_test.go index 735b942b..8e7851a0 100644 --- a/things/api/auth/http/endpoint_test.go +++ b/things/api/auth/http/endpoint_test.go @@ -73,9 +73,9 @@ func newService(tokens map[string]string) things.Service { channelsRepo := mocks.NewChannelRepository(thingsRepo, conns) chanCache := mocks.NewChannelCache() thingCache := mocks.NewThingCache() - uuidProvider := uuid.NewMock() + idProvider := uuid.NewMock() - return things.New(auth, thingsRepo, channelsRepo, nil, chanCache, thingCache, uuidProvider) + return things.New(auth, thingsRepo, channelsRepo, nil, chanCache, thingCache, idProvider) } func newServer(svc things.Service) *httptest.Server { diff --git a/things/api/things/http/endpoint_test.go b/things/api/things/http/endpoint_test.go index dad0d0e7..433bdfcd 100644 --- a/things/api/things/http/endpoint_test.go +++ b/things/api/things/http/endpoint_test.go @@ -78,9 +78,9 @@ func newService(tokens map[string]string) things.Service { channelsRepo := mocks.NewChannelRepository(thingsRepo, conns) chanCache := mocks.NewChannelCache() thingCache := mocks.NewThingCache() - uuidProvider := uuid.NewMock() + idProvider := uuid.NewMock() - return things.New(auth, thingsRepo, channelsRepo, nil, chanCache, thingCache, uuidProvider) + return things.New(auth, thingsRepo, channelsRepo, nil, chanCache, thingCache, idProvider) } func newServer(svc things.Service) *httptest.Server { diff --git a/things/postgres/channels_test.go b/things/postgres/channels_test.go index 8aab58c0..e4162eac 100644 --- a/things/postgres/channels_test.go +++ b/things/postgres/channels_test.go @@ -11,7 +11,6 @@ import ( "github.com/stretchr/testify/require" "github.com/mainflux/mainflux/pkg/errors" - uuidProvider "github.com/mainflux/mainflux/pkg/uuid" "github.com/mainflux/mainflux/things" "github.com/mainflux/mainflux/things/postgres" "github.com/stretchr/testify/assert" @@ -25,7 +24,7 @@ func TestChannelsSave(t *testing.T) { chs := []things.Channel{} for i := 1; i <= 5; i++ { - id, err := uuidProvider.New().ID() + id, err := idProvider.ID() require.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err)) ch := things.Channel{ @@ -85,7 +84,7 @@ func TestChannelUpdate(t *testing.T) { dbMiddleware := postgres.NewDatabase(db) chanRepo := postgres.NewChannelRepository(dbMiddleware) - id, err := uuidProvider.New().ID() + id, err := idProvider.ID() require.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err)) ch := things.Channel{ ID: id, @@ -96,7 +95,7 @@ func TestChannelUpdate(t *testing.T) { require.Nil(t, err, fmt.Sprintf("unexpected error: %s\n", err)) ch.ID = chs[0].ID - nonexistentChanID, err := uuidProvider.New().ID() + nonexistentChanID, err := idProvider.ID() require.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err)) cases := []struct { @@ -147,9 +146,9 @@ func TestSingleChannelRetrieval(t *testing.T) { chanRepo := postgres.NewChannelRepository(dbMiddleware) thingRepo := postgres.NewThingRepository(dbMiddleware) - thid, err := uuidProvider.New().ID() + thid, err := idProvider.ID() require.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err)) - thkey, err := uuidProvider.New().ID() + thkey, err := idProvider.ID() require.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err)) th := things.Thing{ ID: thid, @@ -159,7 +158,7 @@ func TestSingleChannelRetrieval(t *testing.T) { ths, _ := thingRepo.Save(context.Background(), th) th.ID = ths[0].ID - chid, err := uuidProvider.New().ID() + chid, err := idProvider.ID() require.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err)) ch := things.Channel{ ID: chid, @@ -169,7 +168,7 @@ func TestSingleChannelRetrieval(t *testing.T) { ch.ID = chs[0].ID chanRepo.Connect(context.Background(), email, []string{ch.ID}, []string{th.ID}) - nonexistentChanID, err := uuidProvider.New().ID() + nonexistentChanID, err := idProvider.ID() require.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err)) cases := map[string]struct { @@ -225,7 +224,7 @@ func TestMultiChannelRetrieval(t *testing.T) { n := uint64(10) for i := uint64(0); i < n; i++ { - chid, err := uuidProvider.New().ID() + chid, err := idProvider.ID() require.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err)) ch := things.Channel{ @@ -251,8 +250,8 @@ func TestMultiChannelRetrieval(t *testing.T) { } cases := map[string]struct { - owner string - size uint64 + owner string + size uint64 pageMetadata things.PageMetadata }{ "retrieve all channels with existing owner": { @@ -387,7 +386,7 @@ func TestRetrieveByThing(t *testing.T) { chanRepo := postgres.NewChannelRepository(dbMiddleware) thingRepo := postgres.NewThingRepository(dbMiddleware) - thid, err := uuidProvider.New().ID() + thid, err := idProvider.ID() require.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err)) ths, err := thingRepo.Save(context.Background(), things.Thing{ ID: thid, @@ -400,7 +399,7 @@ func TestRetrieveByThing(t *testing.T) { chsDisconNum := uint64(1) for i := uint64(0); i < n; i++ { - chid, err := uuidProvider.New().ID() + chid, err := idProvider.ID() require.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err)) ch := things.Channel{ ID: chid, @@ -419,7 +418,7 @@ func TestRetrieveByThing(t *testing.T) { require.Nil(t, err, fmt.Sprintf("unexpected error: %s", err)) } - nonexistentThingID, err := uuidProvider.New().ID() + nonexistentThingID, err := idProvider.ID() require.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err)) cases := map[string]struct { @@ -495,7 +494,7 @@ func TestChannelRemoval(t *testing.T) { dbMiddleware := postgres.NewDatabase(db) chanRepo := postgres.NewChannelRepository(dbMiddleware) - chid, err := uuidProvider.New().ID() + chid, err := idProvider.ID() require.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err)) chs, err := chanRepo.Save(context.Background(), things.Channel{ ID: chid, @@ -520,9 +519,9 @@ func TestConnect(t *testing.T) { dbMiddleware := postgres.NewDatabase(db) thingRepo := postgres.NewThingRepository(dbMiddleware) - thid, err := uuidProvider.New().ID() + thid, err := idProvider.ID() require.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err)) - thkey, err := uuidProvider.New().ID() + thkey, err := idProvider.ID() require.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err)) th := things.Thing{ @@ -537,7 +536,7 @@ func TestConnect(t *testing.T) { chanRepo := postgres.NewChannelRepository(dbMiddleware) - chid, err := uuidProvider.New().ID() + chid, err := idProvider.ID() require.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err)) chs, err := chanRepo.Save(context.Background(), things.Channel{ ID: chid, @@ -546,10 +545,10 @@ func TestConnect(t *testing.T) { require.Nil(t, err, fmt.Sprintf("unexpected error: %s\n", err)) chid = chs[0].ID - nonexistentThingID, err := uuidProvider.New().ID() + nonexistentThingID, err := idProvider.ID() require.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err)) - nonexistentChanID, err := uuidProvider.New().ID() + nonexistentChanID, err := idProvider.ID() require.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err)) cases := []struct { @@ -607,9 +606,9 @@ func TestDisconnect(t *testing.T) { dbMiddleware := postgres.NewDatabase(db) thingRepo := postgres.NewThingRepository(dbMiddleware) - thid, err := uuidProvider.New().ID() + thid, err := idProvider.ID() require.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err)) - thkey, err := uuidProvider.New().ID() + thkey, err := idProvider.ID() require.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err)) th := things.Thing{ ID: thid, @@ -622,7 +621,7 @@ func TestDisconnect(t *testing.T) { thid = ths[0].ID chanRepo := postgres.NewChannelRepository(dbMiddleware) - chid, err := uuidProvider.New().ID() + chid, err := idProvider.ID() require.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err)) chs, err := chanRepo.Save(context.Background(), things.Channel{ ID: chid, @@ -632,10 +631,10 @@ func TestDisconnect(t *testing.T) { chid = chs[0].ID chanRepo.Connect(context.Background(), email, []string{chid}, []string{thid}) - nonexistentThingID, err := uuidProvider.New().ID() + nonexistentThingID, err := idProvider.ID() require.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err)) - nonexistentChanID, err := uuidProvider.New().ID() + nonexistentChanID, err := idProvider.ID() require.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err)) cases := []struct { @@ -693,9 +692,9 @@ func TestHasThing(t *testing.T) { dbMiddleware := postgres.NewDatabase(db) thingRepo := postgres.NewThingRepository(dbMiddleware) - thid, err := uuidProvider.New().ID() + thid, err := idProvider.ID() require.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err)) - thkey, err := uuidProvider.New().ID() + thkey, err := idProvider.ID() require.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err)) th := things.Thing{ @@ -708,7 +707,7 @@ func TestHasThing(t *testing.T) { thid = ths[0].ID chanRepo := postgres.NewChannelRepository(dbMiddleware) - chid, err := uuidProvider.New().ID() + chid, err := idProvider.ID() require.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err)) chs, err := chanRepo.Save(context.Background(), things.Channel{ ID: chid, @@ -718,7 +717,7 @@ func TestHasThing(t *testing.T) { chid = chs[0].ID chanRepo.Connect(context.Background(), email, []string{chid}, []string{thid}) - nonexistentChanID, err := uuidProvider.New().ID() + nonexistentChanID, err := idProvider.ID() require.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err)) cases := map[string]struct { @@ -755,9 +754,9 @@ func TestHasThingByID(t *testing.T) { dbMiddleware := postgres.NewDatabase(db) thingRepo := postgres.NewThingRepository(dbMiddleware) - thid, err := uuidProvider.New().ID() + thid, err := idProvider.ID() require.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err)) - thkey, err := uuidProvider.New().ID() + thkey, err := idProvider.ID() require.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err)) th := things.Thing{ ID: thid, @@ -768,9 +767,9 @@ func TestHasThingByID(t *testing.T) { require.Nil(t, err, fmt.Sprintf("unexpected error: %s\n", err)) thid = ths[0].ID - disconnectedThID, err := uuidProvider.New().ID() + disconnectedThID, err := idProvider.ID() require.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err)) - disconnectedThKey, err := uuidProvider.New().ID() + disconnectedThKey, err := idProvider.ID() require.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err)) disconnectedThing := things.Thing{ @@ -783,7 +782,7 @@ func TestHasThingByID(t *testing.T) { disconnectedThingID := ths[0].ID chanRepo := postgres.NewChannelRepository(dbMiddleware) - chid, err := uuidProvider.New().ID() + chid, err := idProvider.ID() require.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err)) chs, err := chanRepo.Save(context.Background(), things.Channel{ ID: chid, @@ -793,7 +792,7 @@ func TestHasThingByID(t *testing.T) { chid = chs[0].ID chanRepo.Connect(context.Background(), email, []string{chid}, []string{thid}) - nonexistentChanID, err := uuidProvider.New().ID() + nonexistentChanID, err := idProvider.ID() require.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err)) cases := map[string]struct { diff --git a/things/postgres/things_test.go b/things/postgres/things_test.go index 7a94c010..dc5d2876 100644 --- a/things/postgres/things_test.go +++ b/things/postgres/things_test.go @@ -10,7 +10,7 @@ import ( "testing" "github.com/mainflux/mainflux/pkg/errors" - uuidProvider "github.com/mainflux/mainflux/pkg/uuid" + "github.com/mainflux/mainflux/pkg/uuid" "github.com/mainflux/mainflux/things" "github.com/mainflux/mainflux/things/postgres" "github.com/stretchr/testify/assert" @@ -19,7 +19,10 @@ import ( const maxNameSize = 1024 -var invalidName = strings.Repeat("m", maxNameSize+1) +var ( + invalidName = strings.Repeat("m", maxNameSize+1) + idProvider = uuid.New() +) func TestThingsSave(t *testing.T) { dbMiddleware := postgres.NewDatabase(db) @@ -27,14 +30,14 @@ func TestThingsSave(t *testing.T) { email := "thing-save@example.com" - nonexistentThingKey, err := uuidProvider.New().ID() + nonexistentThingKey, err := idProvider.ID() require.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err)) ths := []things.Thing{} for i := 1; i <= 5; i++ { - thid, err := uuidProvider.New().ID() + thid, err := idProvider.ID() require.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err)) - thkey, err := uuidProvider.New().ID() + thkey, err := idProvider.ID() require.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err)) thing := things.Thing{ @@ -103,9 +106,9 @@ func TestThingUpdate(t *testing.T) { email := "thing-update@example.com" validName := "mfx_device" - thid, err := uuidProvider.New().ID() + thid, err := idProvider.ID() require.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err)) - thkey, err := uuidProvider.New().ID() + thkey, err := idProvider.ID() require.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err)) thing := things.Thing{ @@ -119,7 +122,7 @@ func TestThingUpdate(t *testing.T) { thing.ID = sths[0].ID - nonexistentThingID, err := uuidProvider.New().ID() + nonexistentThingID, err := idProvider.ID() require.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err)) cases := []struct { @@ -190,9 +193,9 @@ func TestUpdateKey(t *testing.T) { dbMiddleware := postgres.NewDatabase(db) thingRepo := postgres.NewThingRepository(dbMiddleware) - id, err := uuidProvider.New().ID() + id, err := idProvider.ID() require.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err)) - key, err := uuidProvider.New().ID() + key, err := idProvider.ID() require.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err)) th1 := things.Thing{ ID: id, @@ -203,9 +206,9 @@ func TestUpdateKey(t *testing.T) { require.Nil(t, err, fmt.Sprintf("unexpected error: %s\n", err)) th1.ID = ths[0].ID - id, err = uuidProvider.New().ID() + id, err = idProvider.ID() require.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err)) - key, err = uuidProvider.New().ID() + key, err = idProvider.ID() require.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err)) th2 := things.Thing{ ID: id, @@ -216,7 +219,7 @@ func TestUpdateKey(t *testing.T) { require.Nil(t, err, fmt.Sprintf("unexpected error: %s\n", err)) th2.ID = ths[0].ID - nonexistentThingID, err := uuidProvider.New().ID() + nonexistentThingID, err := idProvider.ID() require.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err)) cases := []struct { @@ -274,9 +277,9 @@ func TestSingleThingRetrieval(t *testing.T) { dbMiddleware := postgres.NewDatabase(db) thingRepo := postgres.NewThingRepository(dbMiddleware) - id, err := uuidProvider.New().ID() + id, err := idProvider.ID() require.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err)) - key, err := uuidProvider.New().ID() + key, err := idProvider.ID() require.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err)) th := things.Thing{ ID: id, @@ -288,7 +291,7 @@ func TestSingleThingRetrieval(t *testing.T) { require.Nil(t, err, fmt.Sprintf("unexpected error: %s\n", err)) th.ID = ths[0].ID - nonexistentThingID, err := uuidProvider.New().ID() + nonexistentThingID, err := idProvider.ID() require.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err)) cases := map[string]struct { @@ -329,9 +332,9 @@ func TestThingRetrieveByKey(t *testing.T) { dbMiddleware := postgres.NewDatabase(db) thingRepo := postgres.NewThingRepository(dbMiddleware) - id, err := uuidProvider.New().ID() + id, err := idProvider.ID() require.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err)) - key, err := uuidProvider.New().ID() + key, err := idProvider.ID() require.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err)) th := things.Thing{ @@ -381,7 +384,6 @@ func TestMultiThingRetrieval(t *testing.T) { "wrong": "wrong", } - up := uuidProvider.New() offset := uint64(1) nameNum := uint64(3) metaNum := uint64(3) @@ -389,9 +391,9 @@ func TestMultiThingRetrieval(t *testing.T) { n := uint64(10) for i := uint64(0); i < n; i++ { - id, err := up.ID() + id, err := idProvider.ID() require.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err)) - key, err := up.ID() + key, err := idProvider.ID() require.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err)) th := things.Thing{ Owner: email, @@ -418,9 +420,9 @@ func TestMultiThingRetrieval(t *testing.T) { } cases := map[string]struct { - owner string + owner string pageMetadata things.PageMetadata - size uint64 + size uint64 }{ "retrieve all things with existing owner": { owner: email, @@ -551,7 +553,7 @@ func TestMultiThingRetrieval(t *testing.T) { func TestMultiThingRetrievalByChannel(t *testing.T) { email := "thing-multi-retrieval-by-channel@example.com" - up := uuidProvider.New() + dbMiddleware := postgres.NewDatabase(db) thingRepo := postgres.NewThingRepository(dbMiddleware) channelRepo := postgres.NewChannelRepository(dbMiddleware) @@ -559,7 +561,7 @@ func TestMultiThingRetrievalByChannel(t *testing.T) { n := uint64(10) thsDisconNum := uint64(1) - chid, err := up.ID() + chid, err := idProvider.ID() require.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err)) chs, err := channelRepo.Save(context.Background(), things.Channel{ @@ -569,9 +571,9 @@ func TestMultiThingRetrievalByChannel(t *testing.T) { require.Nil(t, err, fmt.Sprintf("unexpected error: %s", err)) cid := chs[0].ID for i := uint64(0); i < n; i++ { - thid, err := up.ID() + thid, err := idProvider.ID() require.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err)) - thkey, err := up.ID() + thkey, err := idProvider.ID() require.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err)) th := things.Thing{ ID: thid, @@ -592,7 +594,7 @@ func TestMultiThingRetrievalByChannel(t *testing.T) { require.Nil(t, err, fmt.Sprintf("unexpected error: %s", err)) } - nonexistentChanID, err := up.ID() + nonexistentChanID, err := idProvider.ID() require.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err)) cases := map[string]struct { @@ -668,9 +670,9 @@ func TestThingRemoval(t *testing.T) { dbMiddleware := postgres.NewDatabase(db) thingRepo := postgres.NewThingRepository(dbMiddleware) - id, err := uuidProvider.New().ID() + id, err := idProvider.ID() require.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err)) - key, err := uuidProvider.New().ID() + key, err := idProvider.ID() require.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err)) thing := things.Thing{ ID: id, diff --git a/things/redis/streams_test.go b/things/redis/streams_test.go index 2c5389b3..d9d04726 100644 --- a/things/redis/streams_test.go +++ b/things/redis/streams_test.go @@ -45,9 +45,9 @@ func newService(tokens map[string]string) things.Service { channelsRepo := mocks.NewChannelRepository(thingsRepo, conns) chanCache := mocks.NewChannelCache() thingCache := mocks.NewThingCache() - uuidProvider := uuid.NewMock() + idProvider := uuid.NewMock() - return things.New(auth, thingsRepo, channelsRepo, nil, chanCache, thingCache, uuidProvider) + return things.New(auth, thingsRepo, channelsRepo, nil, chanCache, thingCache, idProvider) } func TestCreateThings(t *testing.T) { diff --git a/things/redis/things_test.go b/things/redis/things_test.go index a88eefe3..6176624f 100644 --- a/things/redis/things_test.go +++ b/things/redis/things_test.go @@ -16,9 +16,11 @@ import ( "github.com/stretchr/testify/require" ) +var idProvider = uuid.New() + func TestThingSave(t *testing.T) { thingCache := redis.NewThingCache(redisClient) - key, err := uuid.New().ID() + key, err := idProvider.ID() require.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err)) id := "123" id2 := "124" @@ -56,7 +58,7 @@ func TestThingSave(t *testing.T) { func TestThingID(t *testing.T) { thingCache := redis.NewThingCache(redisClient) - key, err := uuid.New().ID() + key, err := idProvider.ID() require.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err)) id := "123" err = thingCache.Save(context.Background(), key, id) @@ -89,7 +91,7 @@ func TestThingID(t *testing.T) { func TestThingRemove(t *testing.T) { thingCache := redis.NewThingCache(redisClient) - key, err := uuid.New().ID() + key, err := idProvider.ID() require.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err)) id := "123" id2 := "321" diff --git a/things/service.go b/things/service.go index 6c772bca..b3bd3418 100644 --- a/things/service.go +++ b/things/service.go @@ -147,12 +147,12 @@ type thingsService struct { groups groups.Repository channelCache ChannelCache thingCache ThingCache - uuidProvider mainflux.IDProvider + idProvider mainflux.IDProvider ulidProvider mainflux.IDProvider } // New instantiates the things service implementation. -func New(auth mainflux.AuthServiceClient, things ThingRepository, channels ChannelRepository, groups groups.Repository, ccache ChannelCache, tcache ThingCache, up mainflux.IDProvider) Service { +func New(auth mainflux.AuthServiceClient, things ThingRepository, channels ChannelRepository, groups groups.Repository, ccache ChannelCache, tcache ThingCache, idp mainflux.IDProvider) Service { return &thingsService{ auth: auth, things: things, @@ -160,7 +160,7 @@ func New(auth mainflux.AuthServiceClient, things ThingRepository, channels Chann channels: channels, channelCache: ccache, thingCache: tcache, - uuidProvider: up, + idProvider: idp, ulidProvider: ulid.New(), } } @@ -172,7 +172,7 @@ func (ts *thingsService) CreateThings(ctx context.Context, token string, things } for i := range things { - things[i].ID, err = ts.uuidProvider.ID() + things[i].ID, err = ts.idProvider.ID() if err != nil { return []Thing{}, errors.Wrap(ErrCreateUUID, err) } @@ -180,7 +180,7 @@ func (ts *thingsService) CreateThings(ctx context.Context, token string, things things[i].Owner = res.GetEmail() if things[i].Key == "" { - things[i].Key, err = ts.uuidProvider.ID() + things[i].Key, err = ts.idProvider.ID() if err != nil { return []Thing{}, errors.Wrap(ErrCreateUUID, err) } @@ -258,7 +258,7 @@ func (ts *thingsService) CreateChannels(ctx context.Context, token string, chann } for i := range channels { - channels[i].ID, err = ts.uuidProvider.ID() + channels[i].ID, err = ts.idProvider.ID() if err != nil { return []Channel{}, errors.Wrap(ErrCreateUUID, err) } diff --git a/things/service_test.go b/things/service_test.go index 41aea009..26835220 100644 --- a/things/service_test.go +++ b/things/service_test.go @@ -36,9 +36,9 @@ func newService(tokens map[string]string) things.Service { channelsRepo := mocks.NewChannelRepository(thingsRepo, conns) chanCache := mocks.NewChannelCache() thingCache := mocks.NewThingCache() - uuidProvider := uuid.NewMock() + idProvider := uuid.NewMock() - return things.New(auth, thingsRepo, channelsRepo, nil, chanCache, thingCache, uuidProvider) + return things.New(auth, thingsRepo, channelsRepo, nil, chanCache, thingCache, idProvider) } func TestCreateThings(t *testing.T) { @@ -200,10 +200,10 @@ func TestListThings(t *testing.T) { } cases := map[string]struct { - token string + token string pageMetadata things.PageMetadata - size uint64 - err error + size uint64 + err error }{ "list all things": { token: token, @@ -559,10 +559,10 @@ func TestListChannels(t *testing.T) { svc.CreateChannels(context.Background(), token, channel) } cases := map[string]struct { - token string + token string pageMetadata things.PageMetadata - size uint64 - err error + size uint64 + err error }{ "list all channels": { token: token, diff --git a/twins/mocks/service.go b/twins/mocks/service.go index 0f6f02c0..b6dd40d6 100644 --- a/twins/mocks/service.go +++ b/twins/mocks/service.go @@ -21,10 +21,11 @@ func NewService(tokens map[string]string) twins.Service { twinsRepo := NewTwinRepository() twinCache := NewTwinCache() statesRepo := NewStateRepository() - uuidProvider := uuid.NewMock() + idProvider := uuid.NewMock() subs := map[string]string{"chanID": "chanID"} broker := NewBroker(subs) - return twins.New(broker, auth, twinsRepo, twinCache, statesRepo, uuidProvider, "chanID", nil) + + return twins.New(broker, auth, twinsRepo, twinCache, statesRepo, idProvider, "chanID", nil) } // CreateDefinition creates twin definition diff --git a/twins/mongodb/states_test.go b/twins/mongodb/states_test.go index 381f1576..b36b535a 100644 --- a/twins/mongodb/states_test.go +++ b/twins/mongodb/states_test.go @@ -11,7 +11,6 @@ import ( "github.com/mainflux/mainflux/twins" "github.com/mainflux/mainflux/twins/mongodb" - uuidProvider "github.com/mainflux/mainflux/pkg/uuid" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "go.mongodb.org/mongo-driver/bson" @@ -26,7 +25,7 @@ func TestStateSave(t *testing.T) { db := client.Database(testDB) repo := mongodb.NewStateRepository(db) - twid, err := uuidProvider.New().ID() + twid, err := idProvider.ID() require.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err)) var id int64 @@ -63,7 +62,7 @@ func TestStatesRetrieveAll(t *testing.T) { repo := mongodb.NewStateRepository(db) - twid, err := uuidProvider.New().ID() + twid, err := idProvider.ID() require.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err)) n := uint64(10) @@ -125,7 +124,7 @@ func TestStatesRetrieveLast(t *testing.T) { repo := mongodb.NewStateRepository(db) - twid, err := uuidProvider.New().ID() + twid, err := idProvider.ID() require.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err)) n := int64(10) diff --git a/twins/mongodb/twins_test.go b/twins/mongodb/twins_test.go index 3ba90fa0..679a6377 100644 --- a/twins/mongodb/twins_test.go +++ b/twins/mongodb/twins_test.go @@ -11,7 +11,7 @@ import ( "testing" log "github.com/mainflux/mainflux/logger" - uuidProvider "github.com/mainflux/mainflux/pkg/uuid" + "github.com/mainflux/mainflux/pkg/uuid" "github.com/mainflux/mainflux/twins" "github.com/mainflux/mainflux/twins/mocks" "github.com/mainflux/mainflux/twins/mongodb" @@ -36,7 +36,7 @@ var ( port string addr string testLog, _ = log.New(os.Stdout, log.Info.String()) - uuid = uuidProvider.New() + idProvider = uuid.New() db mongo.Database invalidName = strings.Repeat("m", maxNameSize+1) ) @@ -48,10 +48,10 @@ func TestTwinsSave(t *testing.T) { db := client.Database(testDB) repo := mongodb.NewTwinRepository(db) - twid, err := uuid.ID() + twid, err := idProvider.ID() require.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err)) - nonexistentTwinID, err := uuid.ID() + nonexistentTwinID, err := idProvider.ID() require.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err)) twin := twins.Twin{ @@ -93,10 +93,10 @@ func TestTwinsUpdate(t *testing.T) { db := client.Database(testDB) repo := mongodb.NewTwinRepository(db) - twid, err := uuid.ID() + twid, err := idProvider.ID() require.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err)) - nonexistentTwinID, err := uuid.ID() + nonexistentTwinID, err := idProvider.ID() require.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err)) twin := twins.Twin{ @@ -150,10 +150,10 @@ func TestTwinsRetrieveByID(t *testing.T) { db := client.Database(testDB) repo := mongodb.NewTwinRepository(db) - twid, err := uuid.ID() + twid, err := idProvider.ID() require.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err)) - nonexistentTwinID, err := uuid.ID() + nonexistentTwinID, err := idProvider.ID() require.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err)) twin := twins.Twin{ @@ -194,7 +194,7 @@ func TestTwinsRetrieveByAttribute(t *testing.T) { db := client.Database(testDB) repo := mongodb.NewTwinRepository(db) - chID, err := uuid.ID() + chID, err := idProvider.ID() require.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err)) empty := mocks.CreateTwin([]string{chID}, []string{""}) @@ -256,7 +256,7 @@ func TestTwinsRetrieveAll(t *testing.T) { n := uint64(10) for i := uint64(0); i < n; i++ { - twid, err := uuidProvider.New().ID() + twid, err := idProvider.ID() require.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err)) tw := twins.Twin{ @@ -349,10 +349,10 @@ func TestTwinsRemove(t *testing.T) { db := client.Database(testDB) repo := mongodb.NewTwinRepository(db) - twid, err := uuid.ID() + twid, err := idProvider.ID() require.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err)) - nonexistentTwinID, err := uuid.ID() + nonexistentTwinID, err := idProvider.ID() require.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err)) twin := twins.Twin{ diff --git a/twins/service.go b/twins/service.go index 7520fba4..839cea18 100644 --- a/twins/service.go +++ b/twins/service.go @@ -89,14 +89,14 @@ var crudOp = map[string]string{ } type twinsService struct { - publisher messaging.Publisher - auth mainflux.AuthServiceClient - twins TwinRepository - states StateRepository - uuidProvider mainflux.IDProvider - channelID string - twinCache TwinCache - logger logger.Logger + publisher messaging.Publisher + auth mainflux.AuthServiceClient + twins TwinRepository + states StateRepository + idProvider mainflux.IDProvider + channelID string + twinCache TwinCache + logger logger.Logger } var _ Service = (*twinsService)(nil) @@ -104,14 +104,14 @@ var _ Service = (*twinsService)(nil) // New instantiates the twins service implementation. func New(publisher messaging.Publisher, auth mainflux.AuthServiceClient, twins TwinRepository, tcache TwinCache, sr StateRepository, idp mainflux.IDProvider, chann string, logger logger.Logger) Service { return &twinsService{ - publisher: publisher, - auth: auth, - twins: twins, - twinCache: tcache, - states: sr, - uuidProvider: idp, - channelID: chann, - logger: logger, + publisher: publisher, + auth: auth, + twins: twins, + twinCache: tcache, + states: sr, + idProvider: idp, + channelID: chann, + logger: logger, } } @@ -125,7 +125,7 @@ func (ts *twinsService) AddTwin(ctx context.Context, token string, twin Twin, de return Twin{}, ErrUnauthorizedAccess } - twin.ID, err = ts.uuidProvider.ID() + twin.ID, err = ts.idProvider.ID() if err != nil { return Twin{}, err } diff --git a/users/api/endpoint_test.go b/users/api/endpoint_test.go index fad8fd4a..70238cfc 100644 --- a/users/api/endpoint_test.go +++ b/users/api/endpoint_test.go @@ -15,6 +15,7 @@ import ( "testing" "github.com/mainflux/mainflux" + "github.com/mainflux/mainflux/pkg/uuid" "github.com/mainflux/mainflux/users" "github.com/mainflux/mainflux/users/api" "github.com/mainflux/mainflux/users/bcrypt" @@ -70,8 +71,9 @@ func newService() users.Service { hasher := bcrypt.New() auth := mocks.NewAuthService(map[string]string{user.Email: user.Email}) email := mocks.NewEmailer() + idProvider := uuid.New() - return users.New(usersRepo, groupRepo, hasher, auth, email) + return users.New(usersRepo, groupRepo, hasher, auth, email, idProvider) } func newServer(svc users.Service) *httptest.Server { diff --git a/users/postgres/groups_test.go b/users/postgres/groups_test.go index 656c93cc..42e08802 100644 --- a/users/postgres/groups_test.go +++ b/users/postgres/groups_test.go @@ -10,8 +10,6 @@ import ( "testing" "github.com/mainflux/mainflux/pkg/errors" - "github.com/mainflux/mainflux/pkg/uuid" - uuidProvider "github.com/mainflux/mainflux/pkg/uuid" "github.com/mainflux/mainflux/users" "github.com/mainflux/mainflux/users/postgres" "github.com/stretchr/testify/assert" @@ -25,14 +23,16 @@ const ( password = "12345678" ) -var invalidName = strings.Repeat("m", maxNameSize+1) -var invalidDesc = strings.Repeat("m", maxDescSize+1) +var ( + invalidName = strings.Repeat("m", maxNameSize+1) + invalidDesc = strings.Repeat("m", maxDescSize+1) +) func TestGroupSave(t *testing.T) { dbMiddleware := postgres.NewDatabase(db) repo := postgres.NewGroupRepo(dbMiddleware) userRepo := postgres.NewUserRepo(dbMiddleware) - uid, err := uuid.New().ID() + uid, err := idProvider.ID() require.Nil(t, err, fmt.Sprintf("user id unexpected error: %s", err)) user := users.User{ ID: uid, @@ -45,7 +45,7 @@ func TestGroupSave(t *testing.T) { user, err = userRepo.RetrieveByEmail(context.Background(), user.Email) require.Nil(t, err, fmt.Sprintf("retrieve got unexpected error: %s", err)) - uid, err = uuid.New().ID() + uid, err = idProvider.ID() require.Nil(t, err, fmt.Sprintf("group id unexpected error: %s", err)) group := users.Group{ ID: uid, @@ -87,7 +87,7 @@ func TestGroupRetrieveByID(t *testing.T) { dbMiddleware := postgres.NewDatabase(db) repo := postgres.NewGroupRepo(dbMiddleware) userRepo := postgres.NewUserRepo(dbMiddleware) - uid, err := uuid.New().ID() + uid, err := idProvider.ID() require.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err)) user := users.User{ ID: uid, @@ -100,7 +100,7 @@ func TestGroupRetrieveByID(t *testing.T) { user, err = userRepo.RetrieveByEmail(context.Background(), user.Email) require.Nil(t, err, fmt.Sprintf("retrieve got unexpected error: %s", err)) - gid, err := uuid.New().ID() + gid, err := idProvider.ID() require.Nil(t, err, fmt.Sprintf("group id unexpected error: %s", err)) group1 := users.Group{ ID: gid, @@ -108,7 +108,7 @@ func TestGroupRetrieveByID(t *testing.T) { OwnerID: user.ID, } - gid, err = uuid.New().ID() + gid, err = idProvider.ID() require.Nil(t, err, fmt.Sprintf("group id unexpected error: %s", err)) group2 := users.Group{ ID: gid, @@ -122,7 +122,7 @@ func TestGroupRetrieveByID(t *testing.T) { g2, err := repo.Save(context.Background(), group2) require.Nil(t, err, fmt.Sprintf("group save got unexpected error: %s", err)) - g2.ID, err = uuidProvider.New().ID() + g2.ID, err = idProvider.ID() require.Nil(t, err, fmt.Sprintf("failed to generate id error: %s", err)) cases := []struct { @@ -152,7 +152,7 @@ func TestGroupUpdate(t *testing.T) { dbMiddleware := postgres.NewDatabase(db) groupRepo := postgres.NewGroupRepo(dbMiddleware) - gid, err := uuid.New().ID() + gid, err := idProvider.ID() require.Nil(t, err, fmt.Sprintf("group id unexpected error: %s", err)) group := users.Group{ ID: gid, @@ -211,7 +211,7 @@ func TestGroupDelete(t *testing.T) { dbMiddleware := postgres.NewDatabase(db) repo := postgres.NewGroupRepo(dbMiddleware) userRepo := postgres.NewUserRepo(dbMiddleware) - uid, err := uuid.New().ID() + uid, err := idProvider.ID() require.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err)) user := users.User{ ID: uid, @@ -224,7 +224,7 @@ func TestGroupDelete(t *testing.T) { user, err = userRepo.RetrieveByEmail(context.Background(), user.Email) require.Nil(t, err, fmt.Sprintf("retrieve got unexpected error: %s", err)) - gid, err := uuid.New().ID() + gid, err := idProvider.ID() require.Nil(t, err, fmt.Sprintf("group id unexpected error: %s", err)) group1 := users.Group{ ID: gid, @@ -238,7 +238,7 @@ func TestGroupDelete(t *testing.T) { err = repo.Assign(context.Background(), user.ID, g1.ID) require.Nil(t, err, fmt.Sprintf("failed to assign user to a group: %s", err)) - gid, err = uuid.New().ID() + gid, err = idProvider.ID() require.Nil(t, err, fmt.Sprintf("group id unexpected error: %s", err)) group2 := users.Group{ ID: gid, @@ -276,7 +276,7 @@ func TestAssignUser(t *testing.T) { dbMiddleware := postgres.NewDatabase(db) repo := postgres.NewGroupRepo(dbMiddleware) userRepo := postgres.NewUserRepo(dbMiddleware) - uid, err := uuid.New().ID() + uid, err := idProvider.ID() require.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err)) user := users.User{ ID: uid, @@ -290,7 +290,7 @@ func TestAssignUser(t *testing.T) { user, err = userRepo.RetrieveByEmail(context.Background(), user.Email) require.Nil(t, err, fmt.Sprintf("retrieve got unexpected error: %s", err)) - gid, err := uuid.New().ID() + gid, err := idProvider.ID() require.Nil(t, err, fmt.Sprintf("group id unexpected error: %s", err)) group1 := users.Group{ ID: gid, @@ -301,7 +301,7 @@ func TestAssignUser(t *testing.T) { g1, err := repo.Save(context.Background(), group1) require.Nil(t, err, fmt.Sprintf("group save got unexpected error: %s", err)) - gid, err = uuid.New().ID() + gid, err = idProvider.ID() require.Nil(t, err, fmt.Sprintf("group id unexpected error: %s", err)) group2 := users.Group{ ID: gid, @@ -312,7 +312,7 @@ func TestAssignUser(t *testing.T) { g2, err := repo.Save(context.Background(), group2) require.Nil(t, err, fmt.Sprintf("group save got unexpected error: %s", err)) - gid, err = uuidProvider.New().ID() + gid, err = idProvider.ID() require.Nil(t, err, fmt.Sprintf("group id generating error: %s", err)) g3 := users.Group{ ID: gid, @@ -352,7 +352,7 @@ func TestUnassignUser(t *testing.T) { repo := postgres.NewGroupRepo(dbMiddleware) userRepo := postgres.NewUserRepo(dbMiddleware) - uid, err := uuid.New().ID() + uid, err := idProvider.ID() require.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err)) user := users.User{ ID: uid, @@ -366,7 +366,7 @@ func TestUnassignUser(t *testing.T) { user1, err := userRepo.RetrieveByEmail(context.Background(), user.Email) require.Nil(t, err, fmt.Sprintf("retrieve got unexpected error: %s", err)) - uid, err = uuid.New().ID() + uid, err = idProvider.ID() require.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err)) user = users.User{ ID: uid, @@ -380,7 +380,7 @@ func TestUnassignUser(t *testing.T) { user2, err := userRepo.RetrieveByEmail(context.Background(), user.Email) require.Nil(t, err, fmt.Sprintf("retrieve got unexpected error: %s", err)) - gid, err := uuid.New().ID() + gid, err := idProvider.ID() require.Nil(t, err, fmt.Sprintf("group id unexpected error: %s", err)) group1 := users.Group{ ID: gid, diff --git a/users/postgres/users_test.go b/users/postgres/users_test.go index b2ec22c0..04051037 100644 --- a/users/postgres/users_test.go +++ b/users/postgres/users_test.go @@ -16,10 +16,12 @@ import ( "github.com/stretchr/testify/require" ) +var idProvider = uuid.New() + func TestUserSave(t *testing.T) { email := "user-save@example.com" - uid, err := uuid.New().ID() + uid, err := idProvider.ID() require.Nil(t, err, fmt.Sprintf("unexpected error: %s", err)) cases := []struct { @@ -62,7 +64,7 @@ func TestSingleUserRetrieval(t *testing.T) { email := "user-retrieval@example.com" - uid, err := uuid.New().ID() + uid, err := idProvider.ID() require.Nil(t, err, fmt.Sprintf("unexpected error: %s", err)) user := users.User{ @@ -95,7 +97,7 @@ func TestRetrieveMembers(t *testing.T) { var nUsers = uint64(10) var usrs []users.User for i := uint64(0); i < nUsers; i++ { - uid, err := uuid.New().ID() + uid, err := idProvider.ID() require.Nil(t, err, fmt.Sprintf("unexpected error: %s", err)) email := fmt.Sprintf("TestRetrieveMembers%d@example.com", i) user := users.User{ @@ -108,7 +110,7 @@ func TestRetrieveMembers(t *testing.T) { u, _ := userRepo.RetrieveByEmail(context.Background(), user.Email) usrs = append(usrs, u) } - uid, err := uuid.New().ID() + uid, err := idProvider.ID() require.Nil(t, err, fmt.Sprintf("user uuid error: %s", err)) group := users.Group{ ID: uid, @@ -155,7 +157,7 @@ func TestRetrieveAll(t *testing.T) { var nUsers = uint64(10) for i := uint64(0); i < nUsers; i++ { - uid, err := uuid.New().ID() + uid, err := idProvider.ID() require.Nil(t, err, fmt.Sprintf("unexpected error: %s", err)) email := fmt.Sprintf("TestRetrieveAll%d@example.com", i) user := users.User{ diff --git a/users/service.go b/users/service.go index eb935705..61c59b6d 100644 --- a/users/service.go +++ b/users/service.go @@ -10,7 +10,6 @@ import ( "github.com/mainflux/mainflux" "github.com/mainflux/mainflux/auth" "github.com/mainflux/mainflux/pkg/errors" - uuidProvider "github.com/mainflux/mainflux/pkg/uuid" ) var ( @@ -159,21 +158,23 @@ type UserPage struct { var _ Service = (*usersService)(nil) type usersService struct { - users UserRepository - groups GroupRepository - hasher Hasher - email Emailer - auth mainflux.AuthServiceClient + users UserRepository + groups GroupRepository + hasher Hasher + email Emailer + auth mainflux.AuthServiceClient + idProvider mainflux.IDProvider } // New instantiates the users service implementation -func New(users UserRepository, groups GroupRepository, hasher Hasher, auth mainflux.AuthServiceClient, m Emailer) Service { +func New(users UserRepository, groups GroupRepository, hasher Hasher, auth mainflux.AuthServiceClient, m Emailer, idp mainflux.IDProvider) Service { return &usersService{ - users: users, - groups: groups, - hasher: hasher, - auth: auth, - email: m, + users: users, + groups: groups, + hasher: hasher, + auth: auth, + email: m, + idProvider: idp, } } @@ -186,7 +187,7 @@ func (svc usersService) Register(ctx context.Context, user User) (string, error) return "", errors.Wrap(ErrMalformedEntity, err) } user.Password = hash - uid, err := uuidProvider.New().ID() + uid, err := svc.idProvider.ID() if err != nil { return "", errors.Wrap(ErrCreateUser, err) } @@ -335,7 +336,7 @@ func (svc usersService) CreateGroup(ctx context.Context, token string, group Gro return Group{}, err } - uid, err := uuidProvider.New().ID() + uid, err := svc.idProvider.ID() if err != nil { return Group{}, errors.Wrap(ErrCreateUser, err) } diff --git a/users/service_test.go b/users/service_test.go index 10fe4ede..c316db3d 100644 --- a/users/service_test.go +++ b/users/service_test.go @@ -10,7 +10,7 @@ import ( "github.com/mainflux/mainflux" "github.com/mainflux/mainflux/pkg/errors" - uuidProvider "github.com/mainflux/mainflux/pkg/uuid" + "github.com/mainflux/mainflux/pkg/uuid" "github.com/mainflux/mainflux/users" "github.com/mainflux/mainflux/users/mocks" @@ -25,6 +25,8 @@ var ( nonExistingUser = users.User{Email: "non-ex-user@example.com", Password: "password", Metadata: map[string]interface{}{"role": "user"}} host = "example.com" groupName = "Mainflux" + + idProvider = uuid.New() ) func newService() users.Service { @@ -34,7 +36,7 @@ func newService() users.Service { auth := mocks.NewAuthService(map[string]string{user.Email: user.Email}) e := mocks.NewEmailer() - return users.New(userRepo, groupRepo, hasher, auth, e) + return users.New(userRepo, groupRepo, hasher, auth, e, idProvider) } func TestRegister(t *testing.T) { @@ -374,11 +376,11 @@ func TestCreateGroup(t *testing.T) { token, err := svc.Login(context.Background(), user) assert.Nil(t, err, fmt.Sprintf("authenticating user expected to succeed: %s", err)) - uuid, err := uuidProvider.New().ID() + id, err := idProvider.ID() assert.Nil(t, err, fmt.Sprintf("generating uuid expected to succeed: %s", err)) group := users.Group{ - ID: uuid, + ID: id, Name: groupName, } diff --git a/uuid.go b/uuid.go index 08927caa..08fb802c 100644 --- a/uuid.go +++ b/uuid.go @@ -3,7 +3,7 @@ package mainflux -// UUIDProvider specifies an API for generating unique identifiers. +// IDProvider specifies an API for generating unique identifiers. type IDProvider interface { // ID generates the unique identifier. ID() (string, error)