Skip to content

Commit

Permalink
removes cassandra from tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mflilian committed Oct 5, 2022
1 parent ec645de commit 085a300
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 154 deletions.
22 changes: 1 addition & 21 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -42,7 +42,6 @@ type App struct {
NewRelic newrelic.Application
NumberOfDaysToSearch int
DDStatsD *extnethttpmiddleware.DogStatsD
Cassandra cassandra.DataStore
Defaults *models.Defaults
Bucket *models.Bucket
}
Expand Down Expand Up @@ -89,9 +88,6 @@ func (app *App) configureStorage() {
}

app.configureBucket()
if app.Defaults.CassandraEnabled {
app.configureCassandra()
}
}

func (app *App) configureDefaults() {
Expand All @@ -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)
Expand Down
10 changes: 1 addition & 9 deletions app/histories.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
104 changes: 6 additions & 98 deletions app/histories_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"net/http"
"strings"
"testing"
"time"

goblin "github.com/franela/goblin"
. "github.com/onsi/gomega"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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() {
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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\"}")
})
})
})
Expand Down
10 changes: 1 addition & 9 deletions app/history.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
25 changes: 8 additions & 17 deletions app/history_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"net/http"
"strings"
"testing"
"time"

goblin "github.com/franela/goblin"
. "github.com/onsi/gomega"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 085a300

Please sign in to comment.