dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts@v1.0.2/dtos/systemevent_test.go (about) 1 /******************************************************************************* 2 * Copyright 2022 Intel Corp. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 5 * in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the License 10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 11 * or implied. See the License for the specific language governing permissions and limitations under 12 * the License. 13 *******************************************************************************/ 14 15 package dtos 16 17 import ( 18 "encoding/json" 19 "testing" 20 "time" 21 22 "github.com/stretchr/testify/assert" 23 "github.com/stretchr/testify/require" 24 25 "dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts/common" 26 ) 27 28 var expectedApiVersion = common.ApiVersion 29 var expectedType = common.DeviceSystemEventType 30 var expectedAction = common.SystemEventActionAdd 31 var expectedSoonerTimestamp = time.Now().UnixNano() 32 var expectedSource = "core-metadata" 33 var expectedOwner = "device-onvif-camera" 34 var expectedTags = map[string]string{"device-profile": "onvif-camera"} 35 var expectedDetails = Device{ 36 Id: TestUUID, 37 Name: "My-Camera-Device", 38 ServiceName: "device-onvif-camera", 39 ProfileName: "onvif-camera", 40 Protocols: map[string]ProtocolProperties{ 41 "Onvif": { 42 "Address": "192.168.12.123", 43 "Port": "80", 44 }, 45 }, 46 } 47 48 func TestNewSystemEvent(t *testing.T) { 49 actual := NewSystemEvent(expectedType, expectedAction, expectedSource, expectedOwner, expectedTags, expectedDetails) 50 51 assert.Equal(t, expectedApiVersion, actual.ApiVersion) 52 assert.Equal(t, expectedType, actual.Type) 53 assert.Equal(t, expectedAction, actual.Action) 54 assert.Equal(t, expectedSource, actual.Source) 55 assert.Equal(t, expectedOwner, actual.Owner) 56 assert.Equal(t, expectedTags, actual.Tags) 57 assert.Equal(t, expectedDetails, actual.Details) 58 59 expectedLaterTimestamp := time.Now().UnixNano() 60 assert.LessOrEqual(t, expectedSoonerTimestamp, actual.Timestamp) 61 assert.GreaterOrEqual(t, expectedLaterTimestamp, actual.Timestamp) 62 } 63 64 func TestDecodeDetails(t *testing.T) { 65 systemEvent := NewSystemEvent(expectedType, expectedAction, expectedSource, expectedOwner, expectedTags, expectedDetails) 66 67 // Simulate the System Event was received as encoded JSON and has been decoded which results in the Details being 68 // decoded to a map[string]interface{} since decoder doesn't know the actual type. 69 data, err := json.Marshal(systemEvent) 70 require.NoError(t, err) 71 target := &SystemEvent{} 72 err = json.Unmarshal(data, target) 73 require.NoError(t, err) 74 75 actual := &Device{} 76 err = target.DecodeDetails(actual) 77 require.NoError(t, err) 78 assert.Equal(t, expectedDetails, *actual) 79 } 80 81 func TestDecodeDetailsError(t *testing.T) { 82 tests := []struct { 83 Name string 84 Details any 85 expectedError string 86 }{ 87 {"Nil details", nil, "Details are nil"}, 88 {"string details", "...", "unable to decode System Event details from JSON"}, 89 } 90 91 for _, test := range tests { 92 t.Run(test.Name, func(t *testing.T) { 93 target := NewSystemEvent(expectedType, expectedAction, expectedSource, expectedOwner, expectedTags, test.Details) 94 actual := &Device{} 95 err := target.DecodeDetails(actual) 96 require.Error(t, err) 97 require.Contains(t, err.Error(), test.expectedError) 98 }) 99 } 100 }