Skip to content

Commit

Permalink
fix(notifications): Remove name constraints on category value
Browse files Browse the repository at this point in the history
Signed-off-by: Felix Ting <felix@iotechsys.com>
  • Loading branch information
FelixTing committed Feb 25, 2021
1 parent ad44d54 commit a60e686
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 59 deletions.
6 changes: 3 additions & 3 deletions v2/dtos/notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ type Notification struct {
Id string `json:"id,omitempty" validate:"omitempty,uuid"`
Created int64 `json:"created,omitempty"`
Modified int64 `json:"modified,omitempty"`
Category string `json:"category,omitempty" validate:"required_without=Labels,omitempty,oneof='SECURITY' 'SW_HEALTH' 'HW_HEALTH'"`
Labels []string `json:"labels,omitempty" validate:"required_without=NotificationCategory,omitempty,gt=0,dive,edgex-dto-none-empty-string,edgex-dto-rfc3986-unreserved-chars"`
Category string `json:"category,omitempty" validate:"required_without=Labels,omitempty,edgex-dto-none-empty-string,edgex-dto-rfc3986-unreserved-chars"`
Labels []string `json:"labels,omitempty" validate:"required_without=Category,omitempty,gt=0,dive,edgex-dto-none-empty-string,edgex-dto-rfc3986-unreserved-chars"`
Content string `json:"content" validate:"required,edgex-dto-none-empty-string"`
ContentType string `json:"contentType,omitempty"`
Description string `json:"description,omitempty"`
Expand All @@ -33,7 +33,7 @@ func ToNotificationModel(n Notification) models.Notification {
m.Id = n.Id
m.Created = n.Created
m.Modified = n.Modified
m.Category = models.NotificationCategory(n.Category)
m.Category = n.Category
m.Labels = n.Labels
m.Content = n.Content
m.ContentType = n.ContentType
Expand Down
10 changes: 5 additions & 5 deletions v2/dtos/requests/notification_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
)

var (
testNotificationCategory = models.SoftwareHealth
testNotificationCategory = "category"
testNotificationLabels = []string{"label1", "label2"}
testNotificationContent = "content"
testNotificationContentType = "text/plain"
Expand Down Expand Up @@ -50,8 +50,8 @@ func TestAddNotification_Validate(t *testing.T) {
noCategoryAndLabel := NewAddNotificationRequest(testAddNotification)
noCategoryAndLabel.Notification.Category = ""
noCategoryAndLabel.Notification.Labels = nil
invalidCategory := NewAddNotificationRequest(testAddNotification)
invalidCategory.Notification.Category = "foo"
categoryNameWithReservedChar := NewAddNotificationRequest(testAddNotification)
categoryNameWithReservedChar.Notification.Category = namesWithReservedChar[0]

noContent := NewAddNotificationRequest(testAddNotification)
noContent.Notification.Content = ""
Expand All @@ -75,7 +75,7 @@ func TestAddNotification_Validate(t *testing.T) {
{"valid", NewAddNotificationRequest(testAddNotification), false},
{"invalid, request ID is not an UUID", invalidReqId, true},
{"invalid, no category and labels", noCategoryAndLabel, true},
{"invalid, unsupported category", invalidCategory, true},
{"invalid, category name containing reserved chars", categoryNameWithReservedChar, true},
{"invalid, no content", noContent, true},
{"invalid, no sender", noSender, true},
{"invalid, no severity", noSeverity, true},
Expand Down Expand Up @@ -122,7 +122,7 @@ func TestAddNotificationReqToNotificationModels(t *testing.T) {
requests := []AddNotificationRequest{addNotificationRequest}
expectedNotificationModel := []models.Notification{
{
Category: models.NotificationCategory(testNotificationCategory),
Category: testNotificationCategory,
Content: testNotificationContent,
ContentType: testNotificationContentType,
Description: testNotificationDescription,
Expand Down
2 changes: 1 addition & 1 deletion v2/dtos/requests/subscription.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func ReplaceSubscriptionModelFieldsWithDTO(s *models.Subscription, patch dtos.Up
s.Channels = dtos.ToChannelModels(patch.Channels)
}
if patch.Categories != nil {
s.Categories = dtos.ToCategoryModels(patch.Categories)
s.Categories = patch.Categories
}
if patch.Labels != nil {
s.Labels = patch.Labels
Expand Down
23 changes: 11 additions & 12 deletions v2/dtos/requests/subscription_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (

var (
testSubscriptionName = "subscriptionName"
testSubscriptionCategories = []string{models.SoftwareHealth}
testSubscriptionCategories = []string{"category1", "category2"}
testSubscriptionLabels = []string{"label"}
testSubscriptionChannels = []dtos.Channel{
{Type: models.Email, EmailAddresses: []string{"test@example.com"}},
Expand All @@ -29,7 +29,6 @@ var (
testSubscriptionResendLimit = int64(5)
testSubscriptionResendInterval = "10s"
unsupportedChannelType = "unsupportedChannelType"
unsupportedCategory = "unsupportedCategory"
)

func addSubscriptionRequestData() AddSubscriptionRequest {
Expand Down Expand Up @@ -119,10 +118,10 @@ func TestAddSubscriptionRequest_Validate(t *testing.T) {
noLabels := addSubscriptionRequestData()
noLabels.Subscription.Labels = nil
noCategoriesAndLabels := addSubscriptionRequestData()
noCategoriesAndLabels.Subscription.Categories = nil
noCategoriesAndLabels.Subscription.Labels = nil
invalidCategory := addSubscriptionRequestData()
invalidCategory.Subscription.Categories = []string{unsupportedCategory}
noCategoriesAndLabels.Subscription.Categories = []string{}
noCategoriesAndLabels.Subscription.Labels = []string{}
categoryNameWithReservedChar := addSubscriptionRequestData()
categoryNameWithReservedChar.Subscription.Categories = []string{namesWithReservedChar[0]}

noReceiver := addSubscriptionRequestData()
noReceiver.Subscription.Receiver = emptyString
Expand All @@ -149,7 +148,7 @@ func TestAddSubscriptionRequest_Validate(t *testing.T) {
{"invalid, email address is invalid", invalidEmailAddress, true},
{"invalid, url is invalid", invalidUrl, true},
{"invalid, no categories and labels specified", noCategoriesAndLabels, true},
{"invalid, unsupported category type", invalidCategory, true},
{"invalid, unsupported category type", categoryNameWithReservedChar, true},
{"invalid, no receiver specified", noReceiver, true},
{"invalid, receiver name containing reserved chars", receiverNameWithReservedChars, true},
{"invalid, resendInterval is not specified in ISO8601 format", invalidResendInterval, true},
Expand Down Expand Up @@ -194,7 +193,7 @@ func TestAddSubscriptionReqToSubscriptionModels(t *testing.T) {
expectedSubscriptionModel := []models.Subscription{
{
Name: testSubscriptionName,
Categories: dtos.ToCategoryModels(testSubscriptionCategories),
Categories: testSubscriptionCategories,
Labels: testSubscriptionLabels,
Channels: dtos.ToChannelModels(testSubscriptionChannels),
Description: testSubscriptionDescription,
Expand Down Expand Up @@ -241,8 +240,8 @@ func TestUpdateSubscriptionRequest_Validate(t *testing.T) {
{Type: models.Rest, Url: "http127.0.0.1"},
}

invalidCategory := updateSubscriptionRequestData()
invalidCategory.Subscription.Categories = []string{unsupportedCategory}
categoryNameWithReservedChar := updateSubscriptionRequestData()
categoryNameWithReservedChar.Subscription.Categories = []string{namesWithReservedChar[0]}

receiverNameWithReservedChars := updateSubscriptionRequestData()
receiverNameWithReservedChars.Subscription.Receiver = &invalidReceiverName
Expand All @@ -268,7 +267,7 @@ func TestUpdateSubscriptionRequest_Validate(t *testing.T) {
{"invalid, unsupported channel type", invalidChannelType, true},
{"invalid, email address is invalid", invalidEmailAddress, true},
{"invalid, url is invalid", invalidUrl, true},
{"invalid, unsupported category type", invalidCategory, true},
{"invalid, category name containing reserved chars", categoryNameWithReservedChar, true},
{"invalid, receiver name containing reserved chars", receiverNameWithReservedChars, true},
{"invalid, resendInterval is not specified in ISO8601 format", invalidResendInterval, true},
}
Expand Down Expand Up @@ -316,7 +315,7 @@ func TestReplaceSubscriptionModelFieldsWithDTO(t *testing.T) {

ReplaceSubscriptionModelFieldsWithDTO(&subscription, patch)

assert.Equal(t, dtos.ToCategoryModels(testSubscriptionCategories), subscription.Categories)
assert.Equal(t, testSubscriptionCategories, subscription.Categories)
assert.Equal(t, testSubscriptionLabels, subscription.Labels)
assert.Equal(t, dtos.ToChannelModels(testSubscriptionChannels), subscription.Channels)
assert.Equal(t, testSubscriptionDescription, subscription.Description)
Expand Down
25 changes: 4 additions & 21 deletions v2/dtos/subscription.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type Subscription struct {
Name string `json:"name" validate:"required,edgex-dto-none-empty-string,edgex-dto-rfc3986-unreserved-chars"`
Channels []Channel `json:"channels" validate:"required,gt=0,dive"`
Receiver string `json:"receiver" validate:"required,edgex-dto-none-empty-string,edgex-dto-rfc3986-unreserved-chars"`
Categories []string `json:"categories,omitempty" validate:"required_without=Labels,omitempty,gt=0,dive,oneof='SECURITY' 'SW_HEALTH' 'HW_HEALTH'"`
Categories []string `json:"categories,omitempty" validate:"required_without=Labels,omitempty,gt=0,dive,edgex-dto-none-empty-string,edgex-dto-rfc3986-unreserved-chars"`
Labels []string `json:"labels,omitempty" validate:"required_without=Categories,omitempty,gt=0,dive,edgex-dto-none-empty-string,edgex-dto-rfc3986-unreserved-chars"`
Created int64 `json:"created,omitempty"`
Modified int64 `json:"modified,omitempty"`
Expand All @@ -35,7 +35,7 @@ type UpdateSubscription struct {
Name *string `json:"name,omitempty" validate:"required_without=Id,edgex-dto-none-empty-string,edgex-dto-rfc3986-unreserved-chars"`
Channels []Channel `json:"channels,omitempty" validate:"omitempty,dive"`
Receiver *string `json:"receiver,omitempty" validate:"omitempty,edgex-dto-none-empty-string,edgex-dto-rfc3986-unreserved-chars"`
Categories []string `json:"categories,omitempty" validate:"omitempty,dive,oneof='SECURITY' 'SW_HEALTH' 'HW_HEALTH'"`
Categories []string `json:"categories,omitempty" validate:"omitempty,dive,gt=0,edgex-dto-none-empty-string,edgex-dto-rfc3986-unreserved-chars"`
Labels []string `json:"labels,omitempty" validate:"omitempty,dive,edgex-dto-none-empty-string,edgex-dto-rfc3986-unreserved-chars"`
Description *string `json:"description,omitempty"`
ResendLimit *int64 `json:"resendLimit,omitempty"`
Expand All @@ -53,7 +53,7 @@ type Channel struct {
// ToSubscriptionModel transforms the Subscription DTO to the Subscription Model
func ToSubscriptionModel(s Subscription) models.Subscription {
var m models.Subscription
m.Categories = ToCategoryModels(s.Categories)
m.Categories = s.Categories
m.Labels = s.Labels
m.Channels = ToChannelModels(s.Channels)
m.Created = s.Created
Expand All @@ -80,7 +80,7 @@ func ToSubscriptionModels(subs []Subscription) []models.Subscription {
func FromSubscriptionModelToDTO(s models.Subscription) Subscription {
return Subscription{
Versionable: common.NewVersionable(),
Categories: FromCategoryModelsToStrings(s.Categories),
Categories: s.Categories,
Labels: s.Labels,
Channels: FromChannelModelsToDTOs(s.Channels),
Created: s.Created,
Expand Down Expand Up @@ -136,20 +136,3 @@ func FromChannelModelToDTO(c models.Channel) Channel {
Url: c.Url,
}
}

func ToCategoryModels(categories []string) []models.NotificationCategory {
categoryModels := make([]models.NotificationCategory, len(categories))
for i, c := range categories {
categoryModels[i] = models.NotificationCategory(c)
}
return categoryModels
}

// FromCategoryModelsToDTOs transforms the NotificationCategory model array to string array
func FromCategoryModelsToStrings(cs []models.NotificationCategory) []string {
s := make([]string, len(cs))
for i, c := range cs {
s[i] = string(c)
}
return s
}
15 changes: 0 additions & 15 deletions v2/models/category.go

This file was deleted.

2 changes: 1 addition & 1 deletion v2/models/notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ package models
// Model fields are same as the DTOs documented by this swagger. Exceptions, if any, are noted below.
type Notification struct {
Timestamps
Category NotificationCategory
Category string
Content string
ContentType string
Description string
Expand Down
2 changes: 1 addition & 1 deletion v2/models/subscription.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ package models
// Model fields are same as the DTOs documented by this swagger. Exceptions, if any, are noted below.
type Subscription struct {
Timestamps
Categories []NotificationCategory
Categories []string
Labels []string
Channels []Channel
Created int64
Expand Down

0 comments on commit a60e686

Please sign in to comment.