github.com/futurehomeno/fimpgo@v1.14.0/message_test.go (about)

     1  package fimpgo
     2  
     3  import (
     4  	"strings"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  )
    10  
    11  func TestFimpMessage_SerializeToJson(t *testing.T) {
    12  	tcs := []struct {
    13  		name    string
    14  		message *FimpMessage
    15  	}{
    16  		{
    17  			name:    "Null message",
    18  			message: NewNullMessage("test_type", "test_service", nil, nil, nil),
    19  		},
    20  		{
    21  			name: "Null message with storage strategy",
    22  			message: NewNullMessage("test_type", "test_service", nil, nil, nil).
    23  				WithStorageStrategy(StorageStrategySkip, ""),
    24  		},
    25  		{
    26  			name: "Null message with storage strategy, property and tag",
    27  			message: NewNullMessage("test_type", "test_service", nil, nil, nil).
    28  				WithProperty("prop1", "val1").
    29  				WithTag("tag1").
    30  				WithStorageStrategy(StorageStrategyAggregate, "val1"),
    31  		},
    32  	}
    33  
    34  	for _, tc := range tcs {
    35  		tc := tc
    36  
    37  		t.Run(tc.name, func(t *testing.T) {
    38  			json, err := tc.message.SerializeToJson()
    39  			assert.NoError(t, err)
    40  
    41  			newMsg, err := NewMessageFromBytes(json)
    42  			assert.NoError(t, err)
    43  
    44  			assert.Equal(t, tc.message, newMsg)
    45  		})
    46  	}
    47  }
    48  
    49  func TestNewBoolMessage(t *testing.T) {
    50  	msg := NewBoolMessage("cmd.binary.set", "out_bin_switch", true, nil, nil, nil)
    51  	val, err := msg.GetBoolValue()
    52  	if err != nil {
    53  		t.Error(err)
    54  	}
    55  	if val == false {
    56  		t.Error("Wrong value")
    57  	}
    58  	t.Log("ok")
    59  }
    60  
    61  func TestNewFloatMessage(t *testing.T) {
    62  
    63  	msg := NewFloatMessage("evt.sensor.report", "temp_sensor", 35.5, nil, nil, nil)
    64  	val, err := msg.GetFloatValue()
    65  
    66  	if err != nil {
    67  		t.Error(err)
    68  	}
    69  	if val != 35.5 {
    70  		t.Error("Wrong value")
    71  	}
    72  	t.Log("ok")
    73  }
    74  
    75  func TestNewObjectMessage(t *testing.T) {
    76  
    77  	type Event struct {
    78  		Field1 int
    79  		Field2 int
    80  	}
    81  	var obj []Event
    82  
    83  	obj = append(obj, Event{
    84  		Field1: 1,
    85  		Field2: 2,
    86  	})
    87  	msg := NewMessage("evt.timeline.report", "kind-owl", VTypeObject, obj, nil, nil, nil)
    88  	bObj, _ := msg.SerializeToJson()
    89  	t.Log("ok", string(bObj))
    90  }
    91  
    92  func TestFimpMessage_SerializeBool(t *testing.T) {
    93  	msg := NewBoolMessage("cmd.binary.set", "out_bin_switch", true, nil, nil, nil)
    94  	serVal, err := msg.SerializeToJson()
    95  	if err != nil {
    96  		t.Error(err)
    97  	}
    98  	t.Log(string(serVal))
    99  }
   100  
   101  func TestFimpMessage_SerializeFloat(t *testing.T) {
   102  	props := Props{}
   103  	props["unit"] = "C"
   104  	msg := NewFloatMessage("evt.sensor.report", "temp_sensor", 35.5, props, nil, nil)
   105  	serVal, err := msg.SerializeToJson()
   106  	if err != nil {
   107  		t.Error(err)
   108  	}
   109  	t.Log(string(serVal))
   110  
   111  }
   112  
   113  func BenchmarkFimpMessage_Serialize(b *testing.B) {
   114  	for i := 0; i < b.N; i++ {
   115  		msg := NewBoolMessage("cmd.binary.set", "out_bin_switch", true, nil, nil, nil)
   116  		_, err := msg.SerializeToJson()
   117  		if err != nil {
   118  			b.Error(err)
   119  		}
   120  	}
   121  }
   122  
   123  func BenchmarkFimpMessage_Serialize2(b *testing.B) {
   124  	props := make(map[string]string)
   125  	props["param1"] = "val1"
   126  	for i := 0; i < b.N; i++ {
   127  		msg := NewStrMapMessage("cmd.config.set", "dev_sys", props, nil, nil, nil)
   128  		_, err := msg.SerializeToJson()
   129  		if err != nil {
   130  			b.Error(err)
   131  		}
   132  	}
   133  }
   134  
   135  func TestNewMessageFromBytes_CorruptedPayload1(t *testing.T) {
   136  	msgString := "{123456789-=#$%"
   137  	_, err := NewMessageFromBytes([]byte(msgString))
   138  	if err != nil {
   139  		t.Log(err)
   140  	}
   141  	t.Log("ok")
   142  }
   143  
   144  func TestNewMessageFromBytes_BoolValue(t *testing.T) {
   145  	msgString := `{"serv":"out_bin_switch","type":"cmd.binary.set","val_t":"bool","val":true,"props":{"p1":"pv1"},"tags":null}`
   146  	fimp, err := NewMessageFromBytes([]byte(msgString))
   147  	if err != nil {
   148  		t.Error(err)
   149  	}
   150  	val, err := fimp.GetBoolValue()
   151  	if val != true {
   152  		t.Error("Wrong value")
   153  	}
   154  	if fimp.Properties["p1"] != "pv1" {
   155  		t.Error("Wrong props value")
   156  	}
   157  	t.Log("ok")
   158  }
   159  
   160  func TestNewMessageFromBytes_BoolInt(t *testing.T) {
   161  	msgString := `{"serv":"out_bin_switch","type":"cmd.binary.set","val_t":"int","val":1234,"props":null,"tags":null}`
   162  	fimp, err := NewMessageFromBytes([]byte(msgString))
   163  	if err != nil {
   164  		t.Error(err)
   165  	}
   166  	val, err := fimp.GetIntValue()
   167  	if val != 1234 {
   168  		t.Error("Wrong value ", val)
   169  	}
   170  	t.Log("ok")
   171  }
   172  
   173  func TestNewMessageFromBytesWithProps(t *testing.T) {
   174  	msgString := `{"serv":"out_bin_switch","type":"cmd.binary.set","val_t":"int","val":1234,"props":{"prop1":"val1"},"tags":null}`
   175  	fimp, err := NewMessageFromBytes([]byte(msgString))
   176  	if err != nil {
   177  		t.Error(err)
   178  	}
   179  	val, err := fimp.GetIntValue()
   180  	if val != 1234 {
   181  		t.Error("Wrong value ", val)
   182  	}
   183  	t.Log("ok")
   184  }
   185  
   186  func TestFimpMessage_GetStrArrayValue(t *testing.T) {
   187  	msgString := `{"serv":"dev_sys","type":"cmd.config.set","val_t":"str_array","val":["val1","val2"],"props":null,"tags":null}`
   188  	fimp, err := NewMessageFromBytes([]byte(msgString))
   189  	if err != nil {
   190  		t.Error(err)
   191  	}
   192  
   193  	val, err := fimp.GetStrArrayValue()
   194  	if err != nil {
   195  		t.Error(err)
   196  	}
   197  	if val[1] != "val2" {
   198  		t.Error("Wrong map result : ", val[1])
   199  	}
   200  }
   201  
   202  func TestFimpMessage_GetIntArrayValue(t *testing.T) {
   203  	msgString := `{"serv":"dev_sys","type":"cmd.config.set","val_t":"int_array","val":[123,1234],"props":null,"tags":null}`
   204  	fimp, err := NewMessageFromBytes([]byte(msgString))
   205  	if err != nil {
   206  		t.Error(err)
   207  	}
   208  
   209  	val, err := fimp.GetIntArrayValue()
   210  	if err != nil {
   211  		t.Error(err)
   212  	}
   213  	if val[1] != 1234 {
   214  		t.Error("Wrong map result : ", val[1])
   215  	}
   216  }
   217  
   218  func TestFimpMessage_GetFloatArrayValue(t *testing.T) {
   219  	msgString := `{"serv":"dev_sys","type":"cmd.config.set","val_t":"float_array","val":[1.5,2.5],"props":null,"tags":null}`
   220  	fimp, err := NewMessageFromBytes([]byte(msgString))
   221  	if err != nil {
   222  		t.Error(err)
   223  	}
   224  
   225  	val, err := fimp.GetFloatArrayValue()
   226  	if err != nil {
   227  		t.Error(err)
   228  	}
   229  	if val[1] != 2.5 {
   230  		t.Error("Wrong map result : ", val[1])
   231  	}
   232  }
   233  
   234  func TestFimpMessage_GetBoolArrayValue(t *testing.T) {
   235  	msgString := `{"serv":"dev_sys","type":"cmd.config.set","val_t":"bool_array","val":[true,true],"props":null,"tags":null}`
   236  	fimp, err := NewMessageFromBytes([]byte(msgString))
   237  	if err != nil {
   238  		t.Error(err)
   239  	}
   240  
   241  	val, err := fimp.GetBoolArrayValue()
   242  	if err != nil {
   243  		t.Error(err)
   244  	}
   245  	if val[1] != true {
   246  		t.Error("Wrong map result : ", val[1])
   247  	}
   248  }
   249  
   250  func TestFimpMessage_GetStrMapValue(t *testing.T) {
   251  	msgString := `{"serv":"dev_sys","type":"cmd.config.set","val_t":"str_map","val":{"param1":"val1","param2":"val2"},"props":null,"tags":null}`
   252  	fimp, err := NewMessageFromBytes([]byte(msgString))
   253  	if err != nil {
   254  		t.Error(err)
   255  	}
   256  
   257  	val, err := fimp.GetStrMapValue()
   258  	if err != nil {
   259  		t.Error(err)
   260  	}
   261  	if val["param2"] != "val2" {
   262  		t.Error("Wrong map result")
   263  	}
   264  }
   265  
   266  func TestFimpMessage_GetIntMapValue(t *testing.T) {
   267  	msgString := `{"serv":"dev_sys","type":"cmd.config.set","val_t":"int_map","val":{"param1":1,"param2":2},"props":null,"tags":null}`
   268  	fimp, err := NewMessageFromBytes([]byte(msgString))
   269  	if err != nil {
   270  		t.Error(err)
   271  	}
   272  
   273  	val, err := fimp.GetIntMapValue()
   274  	if err != nil {
   275  		t.Error(err)
   276  	}
   277  	if val["param2"] != 2 {
   278  		t.Error("Wrong map result")
   279  	}
   280  }
   281  
   282  func TestFimpMessage_GetFloatMapValue(t *testing.T) {
   283  	msgString := `{"serv":"dev_sys","type":"cmd.config.set","val_t":"float_map","val":{"param1":0.5,"param2":2.5,"param3":5},"props":null,"tags":null}`
   284  	fimp, err := NewMessageFromBytes([]byte(msgString))
   285  	if err != nil {
   286  		t.Error(err)
   287  	}
   288  
   289  	val, err := fimp.GetFloatMapValue()
   290  	if err != nil {
   291  		t.Error(err)
   292  	}
   293  	if val["param2"] != 2.5 {
   294  		t.Error("Wrong map result")
   295  	}
   296  	if val["param3"] == 5 {
   297  		t.Log("OK")
   298  	}
   299  }
   300  
   301  func TestFimpMessage_GetBoolMapValue(t *testing.T) {
   302  	msgString := `{"serv":"dev_sys","type":"cmd.config.set","val_t":"bool_map","val":{"param1":true,"param2":true},"props":null,"tags":null}`
   303  	fimp, err := NewMessageFromBytes([]byte(msgString))
   304  	if err != nil {
   305  		t.Error(err)
   306  	}
   307  
   308  	val, err := fimp.GetBoolMapValue()
   309  	if err != nil {
   310  		t.Error(err)
   311  	}
   312  	if val["param2"] != true {
   313  		t.Error("Wrong map result")
   314  	}
   315  }
   316  
   317  func TestProps_GetIntValue(t *testing.T) {
   318  	msgString := `{"serv":"dev_sys","type":"cmd.config.set","val_t":"int","val":1234,"props":{"param1":1,"param2":2},"tags":null}`
   319  	fimp, err := NewMessageFromBytes([]byte(msgString))
   320  	if err != nil {
   321  		t.Error(err)
   322  	}
   323  
   324  	props := fimp.Properties
   325  	val, _, err := props.GetIntValue("param1")
   326  	if err != nil {
   327  		t.Error(err)
   328  	}
   329  	if val != 1 {
   330  		t.Error("Wrong map result")
   331  	}
   332  }
   333  
   334  func TestProps_GetStringValue(t *testing.T) {
   335  	msgString := `{"serv":"dev_sys","type":"cmd.config.set","val_t":"str","val":"val1","props":{"param1":"val1","param2":"val2"},"tags":null}`
   336  	fimp, err := NewMessageFromBytes([]byte(msgString))
   337  	if err != nil {
   338  		t.Error(err)
   339  	}
   340  
   341  	props := fimp.Properties
   342  	val, _ := props.GetStringValue("param1")
   343  
   344  	if val != "val1" {
   345  		t.Error("Wrong map result")
   346  	}
   347  }
   348  
   349  func TestProps_GetFloatValue(t *testing.T) {
   350  	msgString := `{"serv":"dev_sys","type":"cmd.config.set","val_t":"float","val":1.5,"props":{"param1":1.5,"param2":2.5},"tags":null}`
   351  	fimp, err := NewMessageFromBytes([]byte(msgString))
   352  	if err != nil {
   353  		t.Error(err)
   354  	}
   355  
   356  	props := fimp.Properties
   357  	val, _, err := props.GetFloatValue("param1")
   358  	if err != nil {
   359  		t.Error(err)
   360  	}
   361  	if val != 1.5 {
   362  		t.Error("Wrong map result")
   363  	}
   364  }
   365  
   366  func TestProps_GetBoolValue(t *testing.T) {
   367  	msgString := `{"serv":"dev_sys","type":"cmd.config.set","val_t":"bool","val":true,"props":{"param1":true,"param2":false},"tags":null}`
   368  	fimp, err := NewMessageFromBytes([]byte(msgString))
   369  	if err != nil {
   370  		t.Error(err)
   371  	}
   372  
   373  	props := fimp.Properties
   374  	val, _, err := props.GetBoolValue("param1")
   375  	if err != nil {
   376  		t.Error(err)
   377  	}
   378  	if val != true {
   379  		t.Error("Wrong map result")
   380  	}
   381  }
   382  
   383  func BenchmarkFimpMessage_GetStrMapValue(b *testing.B) {
   384  	msgString := []byte(`{"serv":"dev_sys","type":"cmd.config.set","val_t":"str_map","val":{"param1":"val1","param2":"val2"},"props":null,"tags":null}`)
   385  	b.ResetTimer()
   386  	for i := 0; i < b.N; i++ {
   387  		fimp, err := NewMessageFromBytes(msgString)
   388  		if err != nil {
   389  			b.Error(err)
   390  		}
   391  
   392  		val, err := fimp.GetStrMapValue()
   393  		if err != nil {
   394  			b.Error(err)
   395  		}
   396  		if val["param2"] != "val2" {
   397  			b.Error("Wrong map result")
   398  		}
   399  	}
   400  }
   401  
   402  func TestFimpMessage_GetObjectValue(t *testing.T) {
   403  	type Config struct {
   404  		Param1 string
   405  		Param2 string
   406  	}
   407  	msgString := `{"serv":"dev_sys","type":"cmd.config.set","val_t":"object","val":{"param1":"val1","param2":"val2"},"props":{"test":"1"},"tags":null}`
   408  	fimp, err := NewMessageFromBytes([]byte(msgString))
   409  	if err != nil {
   410  		t.Error(err)
   411  	}
   412  	config := Config{}
   413  	err = fimp.GetObjectValue(&config)
   414  	if err != nil {
   415  		t.Error(err)
   416  	}
   417  	if config.Param2 != "val2" {
   418  		t.Error("Wrong map result")
   419  	}
   420  	binMsg, err := fimp.SerializeToJson()
   421  	if err != nil {
   422  		t.Error(err)
   423  	}
   424  	strMsg := string(binMsg)
   425  	t.Log(strMsg)
   426  	if strings.Contains(strMsg, "\"param2\":\"val2\"") {
   427  		t.Log("All good")
   428  	} else {
   429  		t.Error("Something wrong witgh serialization")
   430  	}
   431  
   432  }
   433  
   434  func BenchmarkFimpMessage_GetObjectValue(b *testing.B) {
   435  	type Config struct {
   436  		Param1 string
   437  		Param2 string
   438  	}
   439  	msgString := []byte(`{"serv":"dev_sys","type":"cmd.config.set","val_t":"object","val":{"param1":"val1","param2":"val2"},"props":null,"tags":null}`)
   440  	b.ResetTimer()
   441  	for i := 0; i < b.N; i++ {
   442  		fimp, err := NewMessageFromBytes(msgString)
   443  		if err != nil {
   444  			b.Error(err)
   445  		}
   446  		config := Config{}
   447  		err = fimp.GetObjectValue(&config)
   448  		if err != nil {
   449  			b.Error(err)
   450  		}
   451  		if config.Param2 != "val2" {
   452  			b.Error("Wrong map result")
   453  		}
   454  	}
   455  }
   456  
   457  func TestParseTime(t *testing.T) {
   458  	t.Parallel()
   459  
   460  	tt := []struct {
   461  		timestamp string
   462  		want      time.Time
   463  	}{
   464  		{timestamp: "2022-08-12T06:58:50.551867Z", want: time.Date(2022, 8, 12, 6, 58, 50, 551867000, time.UTC)},
   465  		{timestamp: "2022-08-12T06:58:50Z", want: time.Date(2022, 8, 12, 6, 58, 50, 0, time.UTC)},
   466  		{timestamp: "2022-08-12T08:58:53.383+02:00", want: time.Date(2022, 8, 12, 8, 58, 53, 383000000, time.FixedZone("", 7200))},
   467  		{timestamp: "2022-08-12T08:58:53+02:00", want: time.Date(2022, 8, 12, 8, 58, 53, 0, time.FixedZone("", 7200))},
   468  		{timestamp: "2022-08-12T08:58:53.551867+02:00", want: time.Date(2022, 8, 12, 8, 58, 53, 551867000, time.FixedZone("", 7200))},
   469  		{timestamp: "2022-08-12T08:58:51+0200", want: time.Date(2022, 8, 12, 8, 58, 51, 0, time.FixedZone("", 7200))},
   470  		{timestamp: "2022-08-12T08:58:51.383+0200", want: time.Date(2022, 8, 12, 8, 58, 51, 383000000, time.FixedZone("", 7200))},
   471  		{timestamp: "2022-07-21 12:09:49 +0200", want: time.Date(2022, 7, 21, 12, 9, 49, 0, time.FixedZone("", 7200))},
   472  		{timestamp: "2022-07-21 12:09:49.383 +0200", want: time.Date(2022, 7, 21, 12, 9, 49, 383000000, time.FixedZone("", 7200))},
   473  		{timestamp: "2022-07-21 12:09:49 +02:00", want: time.Date(2022, 7, 21, 12, 9, 49, 0, time.FixedZone("", 7200))},
   474  		{timestamp: "2022-07-21 12:09:49.383 +02:00", want: time.Date(2022, 7, 21, 12, 9, 49, 383000000, time.FixedZone("", 7200))},
   475  		{timestamp: "invalid_format", want: time.Time{}},
   476  		{timestamp: "", want: time.Time{}},
   477  	}
   478  
   479  	for _, tc := range tt {
   480  		tc := tc
   481  
   482  		t.Run(tc.timestamp, func(t *testing.T) {
   483  			t.Parallel()
   484  
   485  			got := ParseTime(tc.timestamp)
   486  
   487  			assert.True(t, tc.want.Equal(got))
   488  		})
   489  	}
   490  }