dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts@v1.0.2/dtos/requests/notification_test.go (about) 1 // 2 // Copyright (C) 2021 IOTech Ltd 3 // 4 // SPDX-License-Identifier: Apache-2.0 5 6 package requests 7 8 import ( 9 "encoding/json" 10 "testing" 11 12 "dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts/dtos" 13 "dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts/models" 14 15 "github.com/stretchr/testify/assert" 16 "github.com/stretchr/testify/require" 17 ) 18 19 var ( 20 testNotificationCategory = "category" 21 testNotificationLabels = []string{"label1", "label2"} 22 testNotificationContent = "content" 23 testNotificationContentType = "text/plain" 24 testNotificationDescription = "description" 25 testNotificationSender = "sender" 26 testNotificationSeverity = models.Normal 27 testNotificationStatus = models.New 28 ) 29 30 func buildTestAddNotificationRequest() AddNotificationRequest { 31 notification := dtos.NewNotification(testNotificationLabels, testNotificationCategory, testNotificationContent, 32 testNotificationSender, testNotificationSeverity) 33 notification.ContentType = testNotificationContentType 34 notification.Description = testNotificationDescription 35 notification.Status = testNotificationStatus 36 return NewAddNotificationRequest(notification) 37 } 38 39 func TestAddNotification_Validate(t *testing.T) { 40 noReqId := buildTestAddNotificationRequest() 41 noReqId.RequestId = "" 42 invalidReqId := buildTestAddNotificationRequest() 43 invalidReqId.RequestId = "abc" 44 45 noCategoryAndLabel := buildTestAddNotificationRequest() 46 noCategoryAndLabel.Notification.Category = "" 47 noCategoryAndLabel.Notification.Labels = nil 48 categoryNameWithReservedChar := buildTestAddNotificationRequest() 49 categoryNameWithReservedChar.Notification.Category = namesWithReservedChar[0] 50 51 noContent := buildTestAddNotificationRequest() 52 noContent.Notification.Content = "" 53 54 noSender := buildTestAddNotificationRequest() 55 noSender.Notification.Sender = "" 56 57 noSeverity := buildTestAddNotificationRequest() 58 noSeverity.Notification.Severity = "" 59 invalidSeverity := buildTestAddNotificationRequest() 60 invalidSeverity.Notification.Severity = "foo" 61 62 invalidStatus := buildTestAddNotificationRequest() 63 invalidStatus.Notification.Status = "foo" 64 65 tests := []struct { 66 name string 67 request AddNotificationRequest 68 expectError bool 69 }{ 70 {"valid", buildTestAddNotificationRequest(), false}, 71 {"invalid, request ID is not an UUID", invalidReqId, true}, 72 {"invalid, no category and labels", noCategoryAndLabel, true}, 73 {"invalid, category name containing reserved chars", categoryNameWithReservedChar, true}, 74 {"invalid, no content", noContent, true}, 75 {"invalid, no sender", noSender, true}, 76 {"invalid, no severity", noSeverity, true}, 77 {"invalid, unsupported severity level", invalidSeverity, true}, 78 {"invalid, unsupported status", invalidStatus, true}, 79 } 80 for _, tt := range tests { 81 t.Run(tt.name, func(t *testing.T) { 82 err := tt.request.Validate() 83 assert.Equal(t, tt.expectError, err != nil, "Unexpected AddNotificationRequest validation result.", err) 84 }) 85 } 86 } 87 88 func TestAddNotification_UnmarshalJSON(t *testing.T) { 89 addNotificationRequest := buildTestAddNotificationRequest() 90 jsonData, _ := json.Marshal(addNotificationRequest) 91 tests := []struct { 92 name string 93 expected AddNotificationRequest 94 data []byte 95 wantErr bool 96 }{ 97 {"unmarshal AddNotificationRequest with success", addNotificationRequest, jsonData, false}, 98 {"unmarshal invalid AddNotificationRequest, empty data", AddNotificationRequest{}, []byte{}, true}, 99 {"unmarshal invalid AddNotificationRequest, string data", AddNotificationRequest{}, []byte("Invalid AddNotificationRequest"), true}, 100 } 101 for _, tt := range tests { 102 t.Run(tt.name, func(t *testing.T) { 103 var result AddNotificationRequest 104 err := result.UnmarshalJSON(tt.data) 105 if tt.wantErr { 106 require.Error(t, err) 107 } else { 108 require.NoError(t, err) 109 assert.Equal(t, tt.expected, result, "Unmarshal did not result in expected AddNotificationRequest.") 110 } 111 }) 112 } 113 } 114 115 func TestAddNotificationReqToNotificationModels(t *testing.T) { 116 addNotificationRequest := buildTestAddNotificationRequest() 117 requests := []AddNotificationRequest{addNotificationRequest} 118 expectedNotificationModel := []models.Notification{ 119 { 120 Id: addNotificationRequest.Notification.Id, 121 Category: testNotificationCategory, 122 Content: testNotificationContent, 123 ContentType: testNotificationContentType, 124 Description: testNotificationDescription, 125 Labels: testNotificationLabels, 126 Sender: testNotificationSender, 127 Severity: models.NotificationSeverity(testNotificationSeverity), 128 Status: models.NotificationStatus(testNotificationStatus), 129 }, 130 } 131 resultModels := AddNotificationReqToNotificationModels(requests) 132 assert.Equal(t, expectedNotificationModel, resultModels, "AddNotificationReqToNotificationModels did not result in expected Notification model.") 133 }