From 085a300117cc068452457f5d8ff6de37829de863 Mon Sep 17 00:00:00 2001 From: mflilian Date: Wed, 5 Oct 2022 16:17:15 -0300 Subject: [PATCH] removes cassandra from tests --- app/app.go | 22 +-------- app/histories.go | 10 +--- app/histories_test.go | 104 +++--------------------------------------- app/history.go | 10 +--- app/history_test.go | 25 ++++------ 5 files changed, 17 insertions(+), 154 deletions(-) diff --git a/app/app.go b/app/app.go index 91391c0..50c97f4 100644 --- a/app/app.go +++ b/app/app.go @@ -21,7 +21,7 @@ import ( "github.com/labstack/echo/engine/standard" "github.com/spf13/viper" "github.com/topfreegames/extensions/echo" - "github.com/topfreegames/mqtt-history/cassandra" + "github.com/topfreegames/mqtt-history/logger" "github.com/topfreegames/mqtt-history/models" "github.com/uber-go/zap" @@ -42,7 +42,6 @@ type App struct { NewRelic newrelic.Application NumberOfDaysToSearch int DDStatsD *extnethttpmiddleware.DogStatsD - Cassandra cassandra.DataStore Defaults *models.Defaults Bucket *models.Bucket } @@ -89,9 +88,6 @@ func (app *App) configureStorage() { } app.configureBucket() - if app.Defaults.CassandraEnabled { - app.configureCassandra() - } } func (app *App) configureDefaults() { @@ -104,22 +100,6 @@ func (app *App) configureDefaults() { } } -func (app *App) configureCassandra() { - logger.Logger.Infof("Connecting to Cassandra") - cassandra, err := cassandra.GetCassandra( - logger.Logger, - app.Config, - app.DDStatsD, - ) - if err != nil { - logger.Logger.Error("Failed to initialize Cassandra.", zap.Error(err)) - panic(fmt.Sprintf("Could not initialize Cassandra, err: %s", err)) - } - - logger.Logger.Info("Initialized Cassandra successfully.") - app.Cassandra = cassandra -} - func (app *App) configureNewRelic() { newRelicKey := app.Config.GetString("newrelic.key") config := newrelic.NewConfig("mqtt-history", newRelicKey) diff --git a/app/histories.go b/app/histories.go index bdad048..db82815 100644 --- a/app/histories.go +++ b/app/histories.go @@ -50,14 +50,6 @@ func HistoriesHandler(app *App) func(c echo.Context) error { return c.JSON(http.StatusOK, messages) } - bucketQnt := app.Defaults.BucketQuantityOnSelect - currentBucket := app.Bucket.Get(from) - - for _, topic := range authorizedTopics { - topicMessages := selectFromBuckets(c.StdContext(), bucketQnt, int(limit), currentBucket, topic, app.Cassandra) - messages = append(messages, topicMessages...) - } - - return c.JSON(http.StatusOK, messages) + return c.JSON(http.StatusOK, nil) } } diff --git a/app/histories_test.go b/app/histories_test.go index c404613..0554091 100644 --- a/app/histories_test.go +++ b/app/histories_test.go @@ -13,7 +13,6 @@ import ( "net/http" "strings" "testing" - "time" goblin "github.com/franela/goblin" . "github.com/onsi/gomega" @@ -50,48 +49,6 @@ func TestHistoriesHandler(t *testing.T) { g.Assert(status).Equal(http.StatusUnauthorized) }) - g.It("It should return 200 if the user is authorized into the topics", func() { - testID := strings.Replace(uuid.NewV4().String(), "-", "", -1) - testID2 := strings.Replace(uuid.NewV4().String(), "-", "", -1) - topic := fmt.Sprintf("chat/test/%s", testID) - topic2 := fmt.Sprintf("chat/test/%s", testID2) - - authorizedTopics := []string{topic, topic2} - err := AuthorizeTestUserInTopics(ctx, authorizedTopics) - Expect(err).To(BeNil()) - - testMessage := models.Message{ - Timestamp: time.Now().AddDate(0, 0, -1), - Payload: "{\"test1\":\"test2\"}", - Topic: topic, - } - - testMessage2 := models.Message{ - Timestamp: time.Now(), - Payload: "{\"test3\":\"test4\"}", - Topic: topic2, - } - - bucket := a.Bucket.Get(testMessage.Timestamp.Unix()) - err = a.Cassandra.InsertWithTTL(ctx, testMessage.Topic, testMessage.Payload, bucket) - Expect(err).To(BeNil()) - - bucket = a.Bucket.Get(testMessage2.Timestamp.Unix()) - err = a.Cassandra.InsertWithTTL(ctx, testMessage2.Topic, testMessage2.Payload, bucket) - Expect(err).To(BeNil()) - - path := fmt.Sprintf("/histories/chat/test?userid=test:test&topics=%s,%s", testID, testID2) - status, body := Get(a, path, t) - g.Assert(status).Equal(http.StatusOK) - - var messages []models.Message - err = json.Unmarshal([]byte(body), &messages) - Expect(err).To(BeNil()) - g.Assert(len(messages)).Equal(2) - g.Assert(messages[0].Payload).Equal("{\"test1\":\"test2\"}") - g.Assert(messages[1].Payload).Equal("{\"test3\":\"test4\"}") - }) - g.It("It should return 200 if the user is authorized into the topics and mongo is used as message storage", func() { testID := strings.Replace(uuid.NewV4().String(), "-", "", -1) testID2 := strings.Replace(uuid.NewV4().String(), "-", "", -1) @@ -132,25 +89,10 @@ func TestHistoriesHandler(t *testing.T) { err := AuthorizeTestUserInTopics(ctx, authorizedTopics) Expect(err).To(BeNil()) - testMessage := models.Message{ - Timestamp: time.Now().AddDate(0, 0, -1), - Payload: "{\"test1\":\"test2\"}", - Topic: topic, - } - - testMessage2 := models.Message{ - Timestamp: time.Now(), - Payload: "{\"test3\":\"test4\"}", - Topic: topic2, - } - - bucket := a.Bucket.Get(testMessage.Timestamp.Unix()) - err = a.Cassandra.InsertWithTTL(ctx, testMessage.Topic, testMessage.Payload, bucket) + err = InsertMongoMessages(ctx, []string{topic, topic2}) Expect(err).To(BeNil()) - bucket = a.Bucket.Get(testMessage2.Timestamp.Unix()) - err = a.Cassandra.InsertWithTTL(ctx, testMessage2.Topic, testMessage2.Payload, bucket) - Expect(err).To(BeNil()) + a.Defaults.MongoEnabled = true path := fmt.Sprintf("/histories/chat/test?userid=test:test&topics=%s,%s", testID, testID2) status, body := Get(a, path, t) @@ -160,7 +102,7 @@ func TestHistoriesHandler(t *testing.T) { err = json.Unmarshal([]byte(body), &messages) Expect(err).To(BeNil()) g.Assert(len(messages)).Equal(1) - g.Assert(messages[0].Payload).Equal("{\"test1\":\"test2\"}") + g.Assert(messages[0].Payload).Equal("{\"test 0\":\"test 1\"}") }) g.It("It should return 401 if the user is not authorized in any topic", func() { @@ -182,24 +124,7 @@ func TestHistoriesHandler(t *testing.T) { err = query(mongoCollection) Expect(err).To(BeNil()) - testMessage := models.Message{ - Timestamp: time.Now().AddDate(0, 0, -1), - Payload: "{\"test1\":\"test2\"}", - Topic: topic, - } - - testMessage2 := models.Message{ - Timestamp: time.Now(), - Payload: "{\"test3\":\"test4\"}", - Topic: topic2, - } - - bucket := a.Bucket.Get(testMessage.Timestamp.Unix()) - err = a.Cassandra.InsertWithTTL(ctx, testMessage.Topic, testMessage.Payload, bucket) - Expect(err).To(BeNil()) - - bucket = a.Bucket.Get(testMessage2.Timestamp.Unix()) - err = a.Cassandra.InsertWithTTL(ctx, testMessage2.Topic, testMessage2.Payload, bucket) + err = InsertMongoMessages(ctx, []string{topic, topic2}) Expect(err).To(BeNil()) path := fmt.Sprintf("/histories/chat/test?userid=test:test&topics=%s,%s", testID, testID2) @@ -217,25 +142,10 @@ func TestHistoriesHandler(t *testing.T) { err := AuthorizeTestUserInTopics(ctx, authorizedTopics) Expect(err).To(BeNil()) - testMessage := models.Message{ - Timestamp: time.Now().AddDate(0, 0, -1), - Payload: "{\"test1\":\"test2\"}", - Topic: topic, - } - - testMessage2 := models.Message{ - Timestamp: time.Now(), - Payload: "{\"test3\":\"test4\"}", - Topic: topic2, - } - - bucket := a.Bucket.Get(testMessage.Timestamp.Unix()) - err = a.Cassandra.InsertWithTTL(ctx, testMessage.Topic, testMessage.Payload, bucket) + err = InsertMongoMessages(ctx, []string{topic, topic2}) Expect(err).To(BeNil()) - bucket = a.Bucket.Get(testMessage2.Timestamp.Unix()) - err = a.Cassandra.InsertWithTTL(ctx, testMessage2.Topic, testMessage2.Payload, bucket) - Expect(err).To(BeNil()) + a.Defaults.MongoEnabled = true path := fmt.Sprintf("/histories/chat/test?userid=test:test&topics=%s,%s", testID, testID2) status, body := Get(a, path, t) @@ -245,8 +155,6 @@ func TestHistoriesHandler(t *testing.T) { err = json.Unmarshal([]byte(body), &messages) Expect(err).To(BeNil()) g.Assert(len(messages)).Equal(2) - g.Assert(messages[0].Payload).Equal("{\"test1\":\"test2\"}") - g.Assert(messages[1].Payload).Equal("{\"test3\":\"test4\"}") }) }) }) diff --git a/app/history.go b/app/history.go index b7d15b7..54661e5 100644 --- a/app/history.go +++ b/app/history.go @@ -41,15 +41,7 @@ func HistoryHandler(app *App) func(c echo.Context) error { ) return c.JSON(http.StatusOK, messages) } + return c.JSON(http.StatusOK, nil) - bucketQnt := app.Defaults.BucketQuantityOnSelect - currentBucket := app.Bucket.Get(from) - - messages := selectFromBuckets(c.StdContext(), - bucketQnt, int(limit), currentBucket, - topic, - app.Cassandra) - - return c.JSON(http.StatusOK, messages) } } diff --git a/app/history_test.go b/app/history_test.go index d104e02..6e76bbf 100644 --- a/app/history_test.go +++ b/app/history_test.go @@ -13,7 +13,6 @@ import ( "net/http" "strings" "testing" - "time" goblin "github.com/franela/goblin" . "github.com/onsi/gomega" @@ -62,16 +61,12 @@ func TestHistoryHandler(t *testing.T) { err := AuthorizeTestUserInTopics(ctx, []string{topic}) Expect(err).To(BeNil()) - testMessage := models.Message{ - Timestamp: time.Now(), - Payload: "{\"test1\":\"test2\"}", - Topic: topic, - } - - bucket := a.Bucket.Get(testMessage.Timestamp.Unix()) - err = a.Cassandra.InsertWithTTL(context.TODO(), testMessage.Topic, testMessage.Payload, bucket) + err = InsertMongoMessages(ctx, []string{topic}) Expect(err).To(BeNil()) + // enable mongo as message store + a.Defaults.MongoEnabled = true + path := fmt.Sprintf("/history/%s?userid=test:test", topic) status, body := Get(a, path, t) g.Assert(status).Equal(http.StatusOK) @@ -131,16 +126,12 @@ func TestHistoryHandler(t *testing.T) { err := AuthorizeTestUserInTopics(ctx, authorizedTopics) Expect(err).To(BeNil()) - testMessage := models.Message{ - Timestamp: time.Now(), - Payload: "{\"test1\":\"test2\"}", - Topic: topic, - } - - bucket := a.Bucket.Get(testMessage.Timestamp.Unix()) - err = a.Cassandra.InsertWithTTL(context.TODO(), testMessage.Topic, testMessage.Payload, bucket) + err = InsertMongoMessages(ctx, []string{topic}) Expect(err).To(BeNil()) + // enable mongo as message store + a.Defaults.MongoEnabled = true + path := fmt.Sprintf("/history/%s?userid=test:test", topic) status, body := Get(a, path, t) g.Assert(status).Equal(http.StatusOK)