Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(notifications): Add factory method for Notification DTO #531

Merged
merged 1 commit into from
Mar 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions v2/dtos/notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ package dtos
import (
"github.com/edgexfoundry/go-mod-core-contracts/v2/v2/dtos/common"
"github.com/edgexfoundry/go-mod-core-contracts/v2/v2/models"

"github.com/google/uuid"
)

// Notification and its properties are defined in the APIv2 specification:
Expand All @@ -27,6 +29,19 @@ type Notification struct {
Status string `json:"status,omitempty" validate:"omitempty,oneof='NEW' 'PROCESSED' 'ESCALATED'"`
}

// NewNotification creates and returns a Notification DTO
func NewNotification(labels []string, category, content, sender, severity string) Notification {
return Notification{
Versionable: common.NewVersionable(),
Id: uuid.NewString(),
Labels: labels,
Category: category,
Content: content,
Sender: sender,
Severity: severity,
}
}

// ToNotificationModel transforms the Notification DTO to the Notification Model
func ToNotificationModel(n Notification) models.Notification {
var m models.Notification
Expand Down
34 changes: 34 additions & 0 deletions v2/dtos/notification_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package dtos

import (
"testing"

"github.com/edgexfoundry/go-mod-core-contracts/v2/v2"
"github.com/edgexfoundry/go-mod-core-contracts/v2/v2/models"

"github.com/stretchr/testify/assert"
)

func TestNewNotification(t *testing.T) {
expectedApiVersion := v2.ApiVersion
expectedLabels := []string{"label1", "label2"}
expectedCategory := "category"
expectedContent := "content"
expectedSender := "sender"
expectedSeverity := models.Normal

actual := NewNotification(expectedLabels, expectedCategory, expectedContent, expectedSender, expectedSeverity)

assert.Equal(t, expectedApiVersion, actual.ApiVersion)
assert.NotEmpty(t, actual.Id)
assert.Equal(t, expectedLabels, actual.Labels)
assert.Equal(t, expectedCategory, actual.Category)
assert.Equal(t, expectedContent, actual.Content)
assert.Equal(t, expectedSender, actual.Sender)
assert.Equal(t, expectedSeverity, actual.Severity)
assert.Empty(t, actual.ContentType)
assert.Empty(t, actual.Description)
assert.Empty(t, actual.Status)
assert.Zero(t, actual.Created)
assert.Zero(t, actual.Modified)
}
44 changes: 20 additions & 24 deletions v2/dtos/requests/notification_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"testing"

"github.com/edgexfoundry/go-mod-core-contracts/v2/v2/dtos"
"github.com/edgexfoundry/go-mod-core-contracts/v2/v2/dtos/common"
"github.com/edgexfoundry/go-mod-core-contracts/v2/v2/models"

"github.com/stretchr/testify/assert"
Expand All @@ -28,51 +27,47 @@ var (
testNotificationStatus = models.New
)

var testAddNotification = dtos.Notification{
Versionable: common.NewVersionable(),
Category: testNotificationCategory,
Labels: testNotificationLabels,
Content: testNotificationContent,
ContentType: testNotificationContentType,
Description: testNotificationDescription,
Sender: testNotificationSender,
Severity: testNotificationSeverity,
Status: testNotificationStatus,
func buildTestAddNotificationRequest() AddNotificationRequest {
notification := dtos.NewNotification(testNotificationLabels, testNotificationCategory, testNotificationContent,
testNotificationSender, testNotificationSeverity)
notification.ContentType = testNotificationContentType
notification.Description = testNotificationDescription
notification.Status = testNotificationStatus
return NewAddNotificationRequest(notification)
}

func TestAddNotification_Validate(t *testing.T) {

noReqId := NewAddNotificationRequest(testAddNotification)
noReqId := buildTestAddNotificationRequest()
noReqId.RequestId = ""
invalidReqId := NewAddNotificationRequest(testAddNotification)
invalidReqId := buildTestAddNotificationRequest()
invalidReqId.RequestId = "abc"

noCategoryAndLabel := NewAddNotificationRequest(testAddNotification)
noCategoryAndLabel := buildTestAddNotificationRequest()
noCategoryAndLabel.Notification.Category = ""
noCategoryAndLabel.Notification.Labels = nil
categoryNameWithReservedChar := NewAddNotificationRequest(testAddNotification)
categoryNameWithReservedChar := buildTestAddNotificationRequest()
categoryNameWithReservedChar.Notification.Category = namesWithReservedChar[0]

noContent := NewAddNotificationRequest(testAddNotification)
noContent := buildTestAddNotificationRequest()
noContent.Notification.Content = ""

noSender := NewAddNotificationRequest(testAddNotification)
noSender := buildTestAddNotificationRequest()
noSender.Notification.Sender = ""

noSeverity := NewAddNotificationRequest(testAddNotification)
noSeverity := buildTestAddNotificationRequest()
noSeverity.Notification.Severity = ""
invalidSeverity := NewAddNotificationRequest(testAddNotification)
invalidSeverity := buildTestAddNotificationRequest()
invalidSeverity.Notification.Severity = "foo"

invalidStatus := NewAddNotificationRequest(testAddNotification)
invalidStatus := buildTestAddNotificationRequest()
invalidStatus.Notification.Status = "foo"

tests := []struct {
name string
request AddNotificationRequest
expectError bool
}{
{"valid", NewAddNotificationRequest(testAddNotification), false},
{"valid", buildTestAddNotificationRequest(), false},
{"invalid, request ID is not an UUID", invalidReqId, true},
{"invalid, no category and labels", noCategoryAndLabel, true},
{"invalid, category name containing reserved chars", categoryNameWithReservedChar, true},
Expand All @@ -91,7 +86,7 @@ func TestAddNotification_Validate(t *testing.T) {
}

func TestAddNotification_UnmarshalJSON(t *testing.T) {
addNotificationRequest := NewAddNotificationRequest(testAddNotification)
addNotificationRequest := buildTestAddNotificationRequest()
jsonData, _ := json.Marshal(addNotificationRequest)
tests := []struct {
name string
Expand All @@ -118,10 +113,11 @@ func TestAddNotification_UnmarshalJSON(t *testing.T) {
}

func TestAddNotificationReqToNotificationModels(t *testing.T) {
addNotificationRequest := NewAddNotificationRequest(testAddNotification)
addNotificationRequest := buildTestAddNotificationRequest()
requests := []AddNotificationRequest{addNotificationRequest}
expectedNotificationModel := []models.Notification{
{
Id: addNotificationRequest.Notification.Id,
Category: testNotificationCategory,
Content: testNotificationContent,
ContentType: testNotificationContentType,
Expand Down