dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts@v1.0.2/dtos/address_test.go (about) 1 // 2 // Copyright (C) 2021 IOTech Ltd 3 // 4 // SPDX-License-Identifier: Apache-2.0 5 6 package dtos_test 7 8 import ( 9 "encoding/json" 10 "fmt" 11 "net/http" 12 "testing" 13 14 "dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts/common" 15 "dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts/dtos" 16 "dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts/models" 17 18 "github.com/stretchr/testify/assert" 19 "github.com/stretchr/testify/require" 20 ) 21 22 const ( 23 testHost = "testHost" 24 testPort = 123 25 testPath = "testPath" 26 testHTTPMethod = "GET" 27 testPublisher = "testPublisher" 28 testTopic = "testTopic" 29 testEmail = "test@example.com" 30 ) 31 32 var testRESTAddress = dtos.Address{ 33 Type: common.REST, 34 Host: testHost, 35 Port: testPort, 36 RESTAddress: dtos.RESTAddress{ 37 Path: testPath, 38 HTTPMethod: testHTTPMethod, 39 }, 40 } 41 42 var testMQTTPubAddress = dtos.Address{ 43 Type: common.MQTT, 44 Host: testHost, 45 Port: testPort, 46 MQTTPubAddress: dtos.MQTTPubAddress{ 47 Publisher: testPublisher, 48 Topic: testTopic, 49 }, 50 } 51 52 var testEmailAddress = dtos.Address{ 53 Type: common.EMAIL, 54 EmailAddress: dtos.EmailAddress{ 55 Recipients: []string{testEmail}, 56 }, 57 } 58 59 func TestAddress_UnmarshalJSON(t *testing.T) { 60 restJsonStr := fmt.Sprintf( 61 `{"type":"%s","host":"%s","port":%d,"path":"%s","httpMethod":"%s"}`, 62 testRESTAddress.Type, testRESTAddress.Host, testRESTAddress.Port, 63 testRESTAddress.Path, testRESTAddress.HTTPMethod, 64 ) 65 mqttJsonStr := fmt.Sprintf( 66 `{"type":"%s","host":"%s","port":%d,"Publisher":"%s","Topic":"%s"}`, 67 testMQTTPubAddress.Type, testMQTTPubAddress.Host, testMQTTPubAddress.Port, 68 testMQTTPubAddress.Publisher, testMQTTPubAddress.Topic, 69 ) 70 emailJsonStr := fmt.Sprintf(`{"type":"%s","Recipients":["%s"]}`, testEmailAddress.Type, testEmail) 71 72 tests := []struct { 73 name string 74 expected dtos.Address 75 data []byte 76 wantErr bool 77 }{ 78 {"unmarshal RESTAddress with success", testRESTAddress, []byte(restJsonStr), false}, 79 {"unmarshal MQTTPubAddress with success", testMQTTPubAddress, []byte(mqttJsonStr), false}, 80 {"unmarshal EmailAddress with success", testEmailAddress, []byte(emailJsonStr), false}, 81 {"unmarshal invalid Address, empty data", dtos.Address{}, []byte{}, true}, 82 {"unmarshal invalid Address, string data", dtos.Address{}, []byte("Invalid address"), true}, 83 } 84 85 for _, tt := range tests { 86 t.Run(tt.name, func(t *testing.T) { 87 var result dtos.Address 88 err := json.Unmarshal(tt.data, &result) 89 if tt.wantErr { 90 require.Error(t, err) 91 } else { 92 require.NoError(t, err) 93 assert.Equal(t, tt.expected, result, "Unmarshal did not result in expected Address.", err) 94 } 95 }) 96 } 97 } 98 99 func TestAddress_Validate(t *testing.T) { 100 validRest := testRESTAddress 101 validRestPatch := testRESTAddress 102 validRestPatch.HTTPMethod = http.MethodPatch 103 noRestHttpMethod := testRESTAddress 104 noRestHttpMethod.HTTPMethod = "" 105 106 validMQTT := testMQTTPubAddress 107 noMQTTPublisher := testMQTTPubAddress 108 noMQTTPublisher.Publisher = "" 109 noMQTTTopic := testMQTTPubAddress 110 noMQTTTopic.Topic = "" 111 112 validEmail := testEmailAddress 113 invalidEmailAddress := testEmailAddress 114 invalidEmailAddress.Recipients = []string{"test.example.com"} 115 116 tests := []struct { 117 name string 118 dto dtos.Address 119 expectError bool 120 }{ 121 {"valid RESTAddress", validRest, false}, 122 {"valid RESTAddress, PATCH http method", validRestPatch, false}, 123 {"invalid RESTAddress, no HTTP method", noRestHttpMethod, true}, 124 {"valid MQTTPubAddress", validMQTT, false}, 125 {"invalid MQTTPubAddress, no MQTT publisher", noMQTTPublisher, true}, 126 {"invalid MQTTPubAddress, no MQTT Topic", noMQTTTopic, true}, 127 {"valid EmailAddress", validEmail, false}, 128 {"invalid EmailAddress", invalidEmailAddress, true}, 129 } 130 for _, tt := range tests { 131 t.Run(tt.name, func(t *testing.T) { 132 err := tt.dto.Validate() 133 if tt.expectError { 134 require.Error(t, err) 135 } else { 136 require.NoError(t, err) 137 } 138 }) 139 } 140 } 141 142 func TestEmailAddressModelToDTO(t *testing.T) { 143 recipients := []string{"test@example.com"} 144 m := models.EmailAddress{Recipients: recipients} 145 dto := dtos.FromAddressModelToDTO(m) 146 assert.Equal(t, recipients, dto.Recipients) 147 } 148 149 func TestEmailAddressDTOtoModel(t *testing.T) { 150 recipients := []string{"test@example.com"} 151 dto := dtos.NewEmailAddress(recipients) 152 m := dtos.ToAddressModel(dto) 153 require.IsType(t, models.EmailAddress{}, m) 154 assert.Equal(t, recipients, m.(models.EmailAddress).Recipients) 155 } 156 157 func TestAddress_marshalJSON(t *testing.T) { 158 restAddress := dtos.Address{ 159 Type: common.REST, 160 Host: testHost, Port: testPort, 161 RESTAddress: dtos.RESTAddress{HTTPMethod: testHTTPMethod}, 162 } 163 expectedRESTJsonStr := fmt.Sprintf( 164 `{"type":"%s","host":"%s","port":%d,"httpMethod":"%s"}`, 165 restAddress.Type, restAddress.Host, restAddress.Port, restAddress.HTTPMethod, 166 ) 167 mattAddress := dtos.Address{ 168 Type: common.MQTT, 169 Host: testHost, Port: testPort, 170 MQTTPubAddress: dtos.MQTTPubAddress{ 171 Publisher: testPublisher, 172 Topic: testTopic, 173 }, 174 } 175 expectedMQTTJsonStr := fmt.Sprintf( 176 `{"type":"%s","host":"%s","port":%d,"publisher":"%s","topic":"%s"}`, 177 mattAddress.Type, mattAddress.Host, mattAddress.Port, mattAddress.Publisher, mattAddress.Topic, 178 ) 179 emailAddress := dtos.Address{ 180 Type: common.EMAIL, 181 EmailAddress: dtos.EmailAddress{ 182 Recipients: []string{testEmail}, 183 }, 184 } 185 expectedEmailJsonStr := fmt.Sprintf( 186 `{"type":"%s","host":"","port":0,"recipients":["%s"]}`, 187 emailAddress.Type, emailAddress.Recipients[0], 188 ) 189 190 tests := []struct { 191 name string 192 address dtos.Address 193 expectedJSONStr string 194 }{ 195 {"marshal REST address", restAddress, expectedRESTJsonStr}, 196 {"marshal MQTT address", mattAddress, expectedMQTTJsonStr}, 197 {"marshal Email address", emailAddress, expectedEmailJsonStr}, 198 } 199 200 for _, tt := range tests { 201 t.Run(tt.name, func(t *testing.T) { 202 jsonBytes, err := json.Marshal(tt.address) 203 require.NoError(t, err) 204 assert.Equal(t, tt.expectedJSONStr, string(jsonBytes), "Unmarshal did not result in expected JSON string.", err) 205 }) 206 } 207 }