From ff9303905beb2e2f28d2a33e3fc232cd846b5963 Mon Sep 17 00:00:00 2001 From: Sida Chen Date: Tue, 11 Sep 2018 14:24:09 -0400 Subject: [PATCH] database: changed Notification interface name --- database/database.go | 4 ++-- database/mock.go | 20 ++++++++++---------- database/pgsql/notification.go | 10 +++++----- database/pgsql/notification_test.go | 12 ++++++------ database/pgsql/queries.go | 2 +- database/pgsql/vulnerability.go | 12 ------------ notifier.go | 6 +++--- 7 files changed, 27 insertions(+), 39 deletions(-) diff --git a/database/database.go b/database/database.go index e4125381..e5efcf13 100644 --- a/database/database.go +++ b/database/database.go @@ -167,9 +167,9 @@ type Session interface { // always considered first page. FindVulnerabilityNotification(name string, limit int, oldVulnerabilityPage pagination.Token, newVulnerabilityPage pagination.Token) (noti VulnerabilityNotificationWithVulnerable, found bool, err error) - // MarkNotificationNotified marks a Notification as notified now, assuming + // MarkNotificationAsRead marks a Notification as notified now, assuming // the requested notification is in the database. - MarkNotificationNotified(name string) error + MarkNotificationAsRead(name string) error // DeleteNotification removes a Notification in the database. DeleteNotification(name string) error diff --git a/database/mock.go b/database/mock.go index a5c35aa5..883283b5 100644 --- a/database/mock.go +++ b/database/mock.go @@ -43,13 +43,13 @@ type MockSession struct { FctFindNewNotification func(lastNotified time.Time) (NotificationHook, bool, error) FctFindVulnerabilityNotification func(name string, limit int, oldPage pagination.Token, newPage pagination.Token) ( vuln VulnerabilityNotificationWithVulnerable, ok bool, err error) - FctMarkNotificationNotified func(name string) error - FctDeleteNotification func(name string) error - FctUpdateKeyValue func(key, value string) error - FctFindKeyValue func(key string) (string, bool, error) - FctLock func(name string, owner string, duration time.Duration, renew bool) (bool, time.Time, error) - FctUnlock func(name, owner string) error - FctFindLock func(name string) (string, time.Time, bool, error) + FctMarkNotificationAsRead func(name string) error + FctDeleteNotification func(name string) error + FctUpdateKeyValue func(key, value string) error + FctFindKeyValue func(key string) (string, bool, error) + FctLock func(name string, owner string, duration time.Duration, renew bool) (bool, time.Time, error) + FctUnlock func(name, owner string) error + FctFindLock func(name string) (string, time.Time, bool, error) } func (ms *MockSession) Commit() error { @@ -186,9 +186,9 @@ func (ms *MockSession) FindVulnerabilityNotification(name string, limit int, old panic("required mock function not implemented") } -func (ms *MockSession) MarkNotificationNotified(name string) error { - if ms.FctMarkNotificationNotified != nil { - return ms.FctMarkNotificationNotified(name) +func (ms *MockSession) MarkNotificationAsRead(name string) error { + if ms.FctMarkNotificationAsRead != nil { + return ms.FctMarkNotificationAsRead(name) } panic("required mock function not implemented") } diff --git a/database/pgsql/notification.go b/database/pgsql/notification.go index 3a27fb3c..4ddf042f 100644 --- a/database/pgsql/notification.go +++ b/database/pgsql/notification.go @@ -289,23 +289,23 @@ func (tx *pgSession) FindVulnerabilityNotification(name string, limit int, oldPa return noti, true, nil } -func (tx *pgSession) MarkNotificationNotified(name string) error { +func (tx *pgSession) MarkNotificationAsRead(name string) error { if name == "" { return commonerr.NewBadRequestError("Empty notification name is not allowed") } - r, err := tx.Exec(updatedNotificationNotified, name) + r, err := tx.Exec(updatedNotificationAsRead, name) if err != nil { - return handleError("updatedNotificationNotified", err) + return handleError("updatedNotificationAsRead", err) } affected, err := r.RowsAffected() if err != nil { - return handleError("updatedNotificationNotified", err) + return handleError("updatedNotificationAsRead", err) } if affected <= 0 { - return handleError("updatedNotificationNotified", errNotificationNotFound) + return handleError("updatedNotificationAsRead", errNotificationNotFound) } return nil } diff --git a/database/pgsql/notification_test.go b/database/pgsql/notification_test.go index ec119e99..9d36f4cb 100644 --- a/database/pgsql/notification_test.go +++ b/database/pgsql/notification_test.go @@ -200,7 +200,7 @@ func TestFindNewNotification(t *testing.T) { } // can't find the notified - assert.Nil(t, tx.MarkNotificationNotified("test")) + assert.Nil(t, tx.MarkNotificationAsRead("test")) // if the notified time is before noti, ok, err = tx.FindNewNotification(time.Now().Add(-time.Duration(10 * time.Second))) assert.Nil(t, err) @@ -225,16 +225,16 @@ func TestFindNewNotification(t *testing.T) { assert.False(t, ok) } -func TestMarkNotificationNotified(t *testing.T) { - datastore, tx := openSessionForTest(t, "MarkNotificationNotified", true) +func TestMarkNotificationAsRead(t *testing.T) { + datastore, tx := openSessionForTest(t, "MarkNotificationAsRead", true) defer closeTest(t, datastore, tx) // invalid case: notification doesn't exist - assert.NotNil(t, tx.MarkNotificationNotified("non-existing")) + assert.NotNil(t, tx.MarkNotificationAsRead("non-existing")) // valid case - assert.Nil(t, tx.MarkNotificationNotified("test")) + assert.Nil(t, tx.MarkNotificationAsRead("test")) // valid case - assert.Nil(t, tx.MarkNotificationNotified("test")) + assert.Nil(t, tx.MarkNotificationAsRead("test")) } func TestDeleteNotification(t *testing.T) { diff --git a/database/pgsql/queries.go b/database/pgsql/queries.go index 0a99365d..ce60ca91 100644 --- a/database/pgsql/queries.go +++ b/database/pgsql/queries.go @@ -168,7 +168,7 @@ const ( INSERT INTO Vulnerability_Notification(name, created_at, old_vulnerability_id, new_vulnerability_id) VALUES ($1, $2, $3, $4)` - updatedNotificationNotified = ` + updatedNotificationAsRead = ` UPDATE Vulnerability_Notification SET notified_at = CURRENT_TIMESTAMP WHERE name = $1` diff --git a/database/pgsql/vulnerability.go b/database/pgsql/vulnerability.go index ab92c0e9..fb483cfe 100644 --- a/database/pgsql/vulnerability.go +++ b/database/pgsql/vulnerability.go @@ -16,7 +16,6 @@ package pgsql import ( "database/sql" - "encoding/json" "errors" "time" @@ -220,17 +219,6 @@ func (tx *pgSession) insertVulnerabilities(vulnerabilities []database.Vulnerabil return vulnIDs, nil } -// castMetadata marshals the given database.MetadataMap and unmarshals it again to make sure that -// everything has the interface{} type. -// It is required when comparing crafted MetadataMap against MetadataMap that we get from the -// database. -func castMetadata(m database.MetadataMap) database.MetadataMap { - c := make(database.MetadataMap) - j, _ := json.Marshal(m) - json.Unmarshal(j, &c) - return c -} - func (tx *pgSession) lockFeatureVulnerabilityCache() error { _, err := tx.Exec(lockVulnerabilityAffects) if err != nil { diff --git a/notifier.go b/notifier.go index 3b4d5f49..9f78977f 100644 --- a/notifier.go +++ b/notifier.go @@ -93,7 +93,7 @@ func RunNotifier(config *notification.Config, datastore database.Datastore, stop go func() { success, interrupted := handleTask(*notification, stopper, config.Attempts) if success { - err := markNotificationNotified(datastore, notification.Name) + err := markNotificationAsRead(datastore, notification.Name) if err != nil { log.WithError(err).Error("Failed to mark notification notified") } @@ -196,14 +196,14 @@ func findNewNotification(datastore database.Datastore, renotifyInterval time.Dur return tx.FindNewNotification(time.Now().Add(-renotifyInterval)) } -func markNotificationNotified(datastore database.Datastore, name string) error { +func markNotificationAsRead(datastore database.Datastore, name string) error { tx, err := datastore.Begin() if err != nil { log.WithError(err).Error("an error happens when beginning database transaction") } defer tx.Rollback() - if err := tx.MarkNotificationNotified(name); err != nil { + if err := tx.MarkNotificationAsRead(name); err != nil { return err } return tx.Commit()