123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 |
- // Copyright 2021 EMQ Technologies Co., Ltd.
- //
- // Licensed under the Apache License, Version 2.0 (the "License");
- // you may not use this file except in compliance with the License.
- // You may obtain a copy of the License at
- //
- // http://www.apache.org/licenses/LICENSE-2.0
- //
- // Unless required by applicable law or agreed to in writing, software
- // distributed under the License is distributed on an "AS IS" BASIS,
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- // See the License for the specific language governing permissions and
- // limitations under the License.
- // +build edgex
- package connection
- import (
- "github.com/edgexfoundry/go-mod-messaging/v2/pkg/types"
- "reflect"
- "testing"
- )
- func TestEdgex_CfgValidate(t *testing.T) {
- type fields struct {
- mbconf types.MessageBusConfig
- }
- type args struct {
- props map[string]interface{}
- }
- tests := []struct {
- name string
- fields fields
- args args
- wantErr bool
- }{
- {
- name: "config pass",
- fields: fields{},
- args: args{props: map[string]interface{}{
- "protocol": "tcp",
- "server": "127.0.0.1",
- "port": int64(1883),
- "type": "mqtt",
- "optional": map[string]string{
- "ClientId": "client1",
- "Username": "user1",
- },
- }},
- wantErr: false,
- },
- {
- name: "config not case sensitive",
- fields: fields{},
- args: args{props: map[string]interface{}{
- "Protocol": "tcp",
- "server": "127.0.0.1",
- "Port": 1883,
- "type": "mqtt",
- "optional": map[string]string{
- "ClientId": "client1",
- "Username": "user1",
- },
- }},
- wantErr: false,
- },
- {
- name: "have unwanted config items topic",
- fields: fields{},
- args: args{props: map[string]interface{}{
- "protocol": "tcp",
- "server": "127.0.0.1",
- "port": 1883,
- "type": "mqtt",
- "optional": map[string]string{
- "ClientId": "client1",
- "Username": "user1",
- },
- "topic": "demo",
- }},
- wantErr: true,
- },
- {
- name: "config type not in zero/mqtt/redis ",
- fields: fields{},
- args: args{props: map[string]interface{}{
- "protocol": "tcp",
- "server": "127.0.0.1",
- "port": 1883,
- "type": "kafka",
- "optional": map[string]string{
- "ClientId": "client1",
- "Username": "user1",
- },
- }},
- wantErr: true,
- },
- {
- name: "do not have enough config items ",
- fields: fields{},
- args: args{props: map[string]interface{}{
- "protocol": "tcp",
- "type": "mqtt",
- "optional": map[string]string{
- "ClientId": "client1",
- "Username": "user1",
- },
- }},
- wantErr: true,
- },
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- es := &EdgexClient{
- selector: &ConSelector{
- ConnSelectorCfg: "testSelector",
- },
- mbconf: tt.fields.mbconf,
- }
- if err := es.CfgValidate(tt.args.props); (err != nil) != tt.wantErr {
- t.Errorf("CfgValidate() error = %v, wantErr %v", err, tt.wantErr)
- }
- })
- }
- }
- func TestEdgex_CorrectsConfigKey(t *testing.T) {
- var props = map[string]interface{}{
- "protocol": "tcp",
- "server": "127.0.0.1",
- "port": int64(1883),
- "type": "mqtt",
- "optional": map[string]interface{}{
- "clientid": "client1",
- "username": "user1",
- "password": "password",
- },
- }
- es := &EdgexClient{
- selector: &ConSelector{
- ConnSelectorCfg: "testSelector",
- },
- }
- err := es.CfgValidate(props)
- if err != nil {
- t.Errorf("Error %v", err)
- }
- expectOps := map[string]string{
- "ClientId": "client1",
- "Username": "user1",
- "Password": "password",
- }
- if !reflect.DeepEqual(es.mbconf.Optional, expectOps) {
- t.Errorf("CfgValidate() expect = %+v, actual %+v", expectOps, es.mbconf.Optional)
- }
- }
|