Skip to content

Commit

Permalink
database: changed Notification interface name
Browse files Browse the repository at this point in the history
  • Loading branch information
KeyboardNerd committed Sep 11, 2018
1 parent 6c69377 commit ff93039
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 39 deletions.
4 changes: 2 additions & 2 deletions database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 10 additions & 10 deletions database/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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")
}
Expand Down
10 changes: 5 additions & 5 deletions database/pgsql/notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
12 changes: 6 additions & 6 deletions database/pgsql/notification_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion database/pgsql/queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
12 changes: 0 additions & 12 deletions database/pgsql/vulnerability.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package pgsql

import (
"database/sql"
"encoding/json"
"errors"
"time"

Expand Down Expand Up @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions notifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit ff93039

Please sign in to comment.