github.com/mdaxf/iac@v0.0.0-20240519030858-58a061660378/com/com_test.go (about)

     1  package com
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  )
     7  
     8  type Person struct {
     9  	Name    string
    10  	Age     int
    11  	Address string
    12  }
    13  
    14  type Message struct {
    15  	Id      string
    16  	UUID    string
    17  	Retry   int
    18  	Execute int
    19  	Topic   string
    20  	PayLoad interface{}
    21  	Handler string
    22  }
    23  
    24  func Test_ConvertstructToMap(t *testing.T) {
    25  	person := Person{
    26  		Name:    "John Doe",
    27  		Age:     30,
    28  		Address: "123 Main St",
    29  	}
    30  	msg := Message{
    31  		Id:      "1",
    32  		UUID:    "2",
    33  		Retry:   3,
    34  		Execute: 4,
    35  		Topic:   "iac/gauge/values",
    36  		PayLoad: `[{
    37              "gauge":"Gauge001",
    38              "parameter":"CircuitBreaker",
    39  			"value":"1"
    40              },
    41              {
    42              "gauge":"Gauge001",
    43              "parameter":"ElectricMeter",
    44  			"value":"110"
    45              }]`,
    46  		Handler: "test",
    47  	}
    48  	type args struct {
    49  		input interface{}
    50  	}
    51  	tests := []struct {
    52  		name    string
    53  		args    args
    54  		want    map[string]interface{}
    55  		wantErr bool
    56  	}{
    57  		{name: "test1",
    58  			args:    args{input: person},
    59  			want:    map[string]interface{}{"Name": "John Doe", "Age": 30, "Address": "123 Main St"},
    60  			wantErr: false,
    61  		},
    62  		{name: "test2",
    63  			args: args{input: msg},
    64  			want: map[string]interface{}{"Id": "1", "UUID": "2", "Retry": 3, "Execute": 4, "Topic": "iac/gauge/values", "PayLoad": `[{
    65  			}]`, "Handler": "test"},
    66  			wantErr: false,
    67  		},
    68  	}
    69  	for _, tt := range tests {
    70  		t.Run(tt.name, func(t *testing.T) {
    71  			if got := ConvertstructToMap(tt.args.input); !reflect.DeepEqual(got, tt.want) {
    72  				t.Errorf("ConvertstructToMap() = %v, want %v", got, tt.want)
    73  			}
    74  		})
    75  	}
    76  }
    77  
    78  func Test_Convertbase64ToMap(t *testing.T) {
    79  	type args struct {
    80  		input string
    81  	}
    82  	tests := []struct {
    83  		name    string
    84  		args    args
    85  		want    map[string]interface{}
    86  		wantErr bool
    87  	}{
    88  		{
    89  			name: "test1",
    90  			args: args{
    91  				input: "W3sKICAgICAgICAgICAgImdhdWdlIjoiR2F1Z2UwMDEiLAogICAgICAgICAgICAicGFyYW1ldGVyIjoiQ2lyY3VpdEJyZWFrZXIiLAoJCQkidmFsdWUiOiIxIgogICAgICAgICAgICB9LAogICAgICAgICAgICB7CiAgICAgICAgICAgICJnYXVnZSI6IkdhdWdlMDAxIiwKICAgICAgICAgICAgInBhcmFtZXRlciI6IkVsZWN0cmljTWV0ZXIiLAoJCQkidmFsdWUiOiIxMTAiCiAgICAgICAgICAgIH1dCg==",
    92  			},
    93  			want: map[string]interface{}{
    94  				"gauge":     "Gauge001",
    95  				"parameter": "CircuitBreaker",
    96  				"value":     "1",
    97  			},
    98  			wantErr: false,
    99  		},
   100  		{
   101  			name: "test2",
   102  			args: args{
   103  				input: "",
   104  			},
   105  			want:    nil,
   106  			wantErr: true,
   107  		},
   108  	}
   109  	for _, tt := range tests {
   110  		t.Run(tt.name, func(t *testing.T) {
   111  			got, err := Convertbase64ToMap(tt.args.input)
   112  			if (err != nil) != tt.wantErr {
   113  				t.Errorf("Convertbase64ToMap() error = %v, wantErr %v", err, tt.wantErr)
   114  				return
   115  			}
   116  			if !reflect.DeepEqual(got, tt.want) {
   117  				t.Errorf("Convertbase64ToMap() = %v, want %v", got, tt.want)
   118  			}
   119  		})
   120  	}
   121  }
   122  
   123  func Test_ConvertbytesToMap(t *testing.T) {
   124  	type args struct {
   125  		data []byte
   126  	}
   127  	tests := []struct {
   128  		name    string
   129  		args    args
   130  		want    map[string]interface{}
   131  		wantErr bool
   132  	}{
   133  		// TODO: Add test cases.
   134  		{
   135  			name: "test1",
   136  			args: args{
   137  				data: []byte(`{"gauge":"Gauge001","parameter":"CircuitBreaker","value":"1"}`),
   138  			},
   139  			want: map[string]interface{}{
   140  				"gauge":     "Gauge001",
   141  				"parameter": "CircuitBreaker",
   142  				"value":     "1",
   143  			},
   144  			wantErr: false,
   145  		},
   146  		{
   147  			name: "test2",
   148  			args: args{
   149  				data: []byte{91, 123, 10, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 34, 103, 97, 117, 103, 101, 34, 58, 34, 71, 97, 117, 103, 101, 48, 48, 49, 34, 44, 10, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 34, 112, 97, 114, 97, 109, 101, 116, 101, 114, 34, 58, 34, 67, 105, 114, 99, 117, 105, 116, 66, 114, 101, 97, 107, 101, 114, 34, 44, 10, 9, 9, 9, 34, 118, 97, 108, 117, 101, 34, 58, 34, 49, 34, 10, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 125, 44, 10, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 123, 10, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 34, 103, 97, 117, 103, 101, 34, 58, 34, 71, 97, 117, 103, 101, 48, 48, 49, 34, 44, 10, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 34, 112, 97, 114, 97, 109, 101, 116, 101, 114, 34, 58, 34, 69, 108, 101, 99, 116, 114, 105, 99, 77, 101, 116, 101, 114, 34, 44, 10, 9, 9, 9, 34, 118, 97, 108, 117, 101, 34, 58, 34, 49, 49, 48, 34, 10, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 125, 93, 10},
   150  			},
   151  			want: map[string]interface{}{
   152  				"gauge":     "Gauge001",
   153  				"parameter": "CircuitBreaker",
   154  				"value":     "1",
   155  			},
   156  			wantErr: false,
   157  		},
   158  	}
   159  	for _, tt := range tests {
   160  		t.Run(tt.name, func(t *testing.T) {
   161  			got, err := ConvertbytesToMap(tt.args.data)
   162  			if (err != nil) != tt.wantErr {
   163  				t.Errorf("ConvertbytesToMap() error = %v, wantErr %v", err, tt.wantErr)
   164  				return
   165  			}
   166  			if !reflect.DeepEqual(got, tt.want) {
   167  				t.Errorf("ConvertbytesToMap() = %v, want %v", got, tt.want)
   168  			}
   169  		})
   170  	}
   171  }