123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- package connection
- import (
- "os"
- "reflect"
- "testing"
- )
- func Test_getConnectionConf(t *testing.T) {
- type args struct {
- connectionSelector string
- }
- tests := []struct {
- name string
- args args
- want map[string]interface{}
- wantErr bool
- }{
- {
- name: "mqtt:localConnection",
- args: args{
- connectionSelector: "mqtt.localConnection",
- },
- want: map[string]interface{}{
- "servers": []interface{}{"tcp://127.0.0.1:1883"},
- "username": "ekuiper",
- "password": "password",
- "clientid": "ekuiper",
- },
- wantErr: false,
- },
- {
- name: "mqtt:cloudConnection",
- args: args{
- connectionSelector: "mqtt.cloudConnection",
- },
- want: map[string]interface{}{
- "servers": []interface{}{"tcp://broker.emqx.io:1883"},
- "username": "user1",
- "password": "password",
- },
- wantErr: false,
- },
- {
- name: "mqtt:mqtt_conf3 not exist",
- args: args{
- connectionSelector: "mqtt.mqtt_conf3",
- },
- wantErr: true,
- },
- {
- name: "edgex:redisMsgBus",
- args: args{
- connectionSelector: "edgex.redisMsgBus",
- },
- want: map[string]interface{}{
- "protocol": "redis",
- "server": "127.0.0.1",
- "port": 6379,
- "type": "redis",
- },
- wantErr: false,
- },
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- c := ConSelector{
- ConnSelectorCfg: tt.args.connectionSelector,
- }
- _ = c.Init()
- got, err := c.ReadCfgFromYaml()
- if (err != nil) != tt.wantErr {
- t.Errorf("getConnectionConf() error = %v, wantErr %v", err, tt.wantErr)
- return
- }
- if !reflect.DeepEqual(got, tt.want) {
- t.Errorf("getConnectionConf() got = %v, want %v", got, tt.want)
- }
- })
- }
- }
- func Test_getConnectionConfWithEnv(t *testing.T) {
- mqttServerKey := "CONNECTION__MQTT__LOCALCONNECTION__SERVERS"
- mqttServerValue := "[tcp://broker.emqx.io:1883]"
- edgexPortKey := "CONNECTION__EDGEX__REDISMSGBUS__PORT"
- edgexPortValue := "6666"
- err := os.Setenv(mqttServerKey, mqttServerValue)
- if err != nil {
- t.Error(err)
- }
- err = os.Setenv(edgexPortKey, edgexPortValue)
- if err != nil {
- t.Error(err)
- }
- type args struct {
- connectionSelector string
- }
- tests := []struct {
- name string
- args args
- want map[string]interface{}
- wantErr bool
- }{
- {
- name: "mqtt:localConnection",
- args: args{
- connectionSelector: "mqtt.localConnection",
- },
- want: map[string]interface{}{
- "servers": []interface{}{"tcp://broker.emqx.io:1883"},
- "username": "ekuiper",
- "password": "password",
- "clientid": "ekuiper",
- },
- wantErr: false,
- },
- {
- name: "edgex:redisMsgBus",
- args: args{
- connectionSelector: "edgex.redisMsgBus",
- },
- want: map[string]interface{}{
- "protocol": "redis",
- "server": "127.0.0.1",
- "port": int64(6666),
- "type": "redis",
- },
- wantErr: false,
- },
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- c := ConSelector{
- ConnSelectorCfg: tt.args.connectionSelector,
- }
- _ = c.Init()
- got, err := c.ReadCfgFromYaml()
- if (err != nil) != tt.wantErr {
- t.Errorf("getConnectionConf() error = %v, wantErr %v", err, tt.wantErr)
- return
- }
- if !reflect.DeepEqual(got, tt.want) {
- t.Errorf("getConnectionConf() got = %v, want %v", got, tt.want)
- }
- })
- }
- }
|