github.com/siglens/siglens@v0.0.0-20240328180423-f7ce9ae441ed/pkg/utils/jsonutils_test.go (about) 1 /* 2 Copyright 2023. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package utils 18 19 import ( 20 "encoding/json" 21 "fmt" 22 "reflect" 23 "testing" 24 ) 25 26 func TestJson(t *testing.T) { 27 cases := []struct { 28 input string 29 want map[string]interface{} 30 }{ 31 { // case#1 32 `{ 33 "key1": "val1", 34 "key2": 1.456, 35 "key3": true, 36 "key4": "John" 37 }`, 38 map[string]interface{}{ 39 "key1": "val1", 40 "key2": 1.456, 41 "key3": true, 42 "key4": "John", 43 }, 44 }, 45 { //case 2 46 `{ 47 "foo": { 48 "jim":"bean" 49 }, 50 "fee": "bar" 51 }`, 52 map[string]interface{}{ 53 "foo.jim": "bean", 54 "fee": "bar", 55 }, 56 }, 57 { // case 3 58 `{ "a": { "b" : { "c" : { "d" : "e" } } }, "number": 1.4567, "bool": true }`, 59 map[string]interface{}{ 60 "a.b.c.d": "e", 61 "number": 1.4567, 62 "bool": true, 63 }, 64 }, 65 { // case 4 66 `{ "a": ["v1", "v2"], "b": [0.0,1.1,2.2], "bool": true }`, 67 map[string]interface{}{ 68 "a.0": "v1", 69 "a.1": "v2", 70 "b.0": 0.0, 71 "b.1": 1.1, 72 "b.2": 2.2, 73 "bool": true, 74 }, 75 }, 76 } 77 78 for i, test := range cases { 79 var m map[string]interface{} 80 err := json.Unmarshal([]byte(test.input), &m) 81 if err != nil { 82 t.Errorf("testid: %d: Failed to parse json err:%v", i+1, err) 83 continue 84 } 85 86 got := Flatten(m) 87 88 // t.Logf("got: %v", got) 89 if !reflect.DeepEqual(got, test.want) { 90 t.Errorf("case: %d: mismatch: got: [%v], wanted: [%v]", i+1, got, test.want) 91 } 92 } 93 94 } 95 96 func Benchmark_flatten(b *testing.B) { 97 b.ReportAllocs() 98 b.ResetTimer() 99 input := `{ "a": { "b" : { "c" : { "d" : "e" } } }, "number": 1.4567, "bool": true }` 100 101 var m map[string]interface{} 102 err := json.Unmarshal([]byte(input), &m) 103 if err != nil { 104 b.Errorf("Failed to parse json err:%v", err) 105 } 106 107 for i := 0; i < b.N; i++ { 108 Flatten(m) 109 } 110 } 111 func unmarshalString(t *testing.T, data string) map[string]interface{} { 112 var m map[string]interface{} 113 err := json.Unmarshal([]byte(data), &m) 114 if err != nil { 115 return nil 116 } 117 fmt.Println(m) 118 return m 119 } 120 121 func TestFlatten(t *testing.T) { 122 data1 := `{"responseElements": {"gOEZw": {"CToqk": "vynlmwGIgk", "VfWbM": "lGuFJDkDip"}, "AjMej": {"vGKXa": "uIzuwIpvfg", "iIVEs": "hgssPSNtMD"}, "rjAON": [{"NECnv": "KuwTanjNIz", "frnTG": "yoVLglyswf"}, {"Lgzrq": "MKpSdGRjCk", "ZkYEt": "ogbPVfdWCp"}, {"cmUmd": {"fxtub": "LKXvMmqDPn", "lpElF": "TWcFwOfntu"}, "GADuH": {"GloWr": "wrMUHqSLnP", "aWRAk": "HbkHXlfiZF"}, "LBiSQ": {"lcaYz": "uAwADPJonD", "EHdTD": "vRTEyiylDO"}, "eoJsl": [{"nyCIH": "AQdoctvPin", "GGBIc": "VFXotQcQaY"}]}]}}` 123 data2 := `{"eventTime": "2022-03-31T04:04:24Z", "eventVersion": "1.2", "userIdentity": {"type": "IAMUser", "principalId": "A9YRGIZJ7LTYX7E6ZBR9", "arn": "arn:aws:iam::327525440610:user/Tina_Long", "accountId": "327525440610", "accessKeyId": "BFWANGMQK15TDRHEDMDD", "userName": "Tina_Long"}, "eventSource": "ebs.amazonaws.com", "eventName": "SignOut", "awsRegion": "us-west-2", "sourceIPAddress": "80.99.17.33", "userAgent": "aws-sdk-ruby", "errorCode": "Often.", "errorMessage": "Most peace material.", "requestParameters": {"JftbB": {"Eptxk": "qPrPxRooyD"}, "xuqKB": {"FHcUk": "rYYfGwtBqR"}, "FBQGY": {"ncDnH": "LYBLkKkaZg"}, "naqJE": {"ISzCV": "HCugGPfDuG"}, "rcXQZ": []}, "responseElements": {"gOEZw": {"CToqk": "vynlmwGIgk", "VfWbM": "lGuFJDkDip"}, "AjMej": {"vGKXa": "uIzuwIpvfg", "iIVEs": "hgssPSNtMD"}, "rjAON": [{"NECnv": "KuwTanjNIz", "frnTG": "yoVLglyswf"}, {"Lgzrq": "MKpSdGRjCk", "ZkYEt": "ogbPVfdWCp"}]}, "requestID": "0d82f6e4-a7ca-4b76-a542-a4a5182490f2", "eventID": "6ca824c8-81c4-41d3-9de4-58ab902d1158", "eventType": "AwsConsoleSignIn", "apiVersion": "6.1", "managementEvent": "AwsConsoleAction", "resources": [{"ARN": "arn:aws:iam::647294929174:role/SXkLH", "accountId": "647294929174", "type": "AWS::s3::SXkLH"}, {"ARN": "arn:aws:iam::935171735452:role/Auqhr", "accountId": "935171735452", "type": "AWS::lambda::Auqhr"}, {"ARN": "arn:aws:iam::914027419284:role/gXxts", "accountId": "914027419284", "type": "AWS::ebs::gXxts"}], "recipientAccountId": "446069363933", "sharedEventID": "f434d6a1-22f2-4bea-89b6-0505d9e30b5d", "vpcEndpointId": "vpc-18328331", "eventCategory": "Insight"}` 124 data3 := `{"eventTime": "2022-03-31T04:04:24Z", "eventVersion": "1.2", "userIdentity": {"type": "IAMUser", "principalId": "A9YRGIZJ7LTYX7E6ZBR9", "arn": "arn:aws:iam::327525440610:user/Tina_Long", "accountId": "327525440610", "accessKeyId": "BFWANGMQK15TDRHEDMDD", "userName": "Tina_Long"}, "eventSource": "ebs.amazonaws.com", "eventName": "SignOut", "awsRegion": "us-west-2", "sourceIPAddress": "80.99.17.33", "userAgent": "aws-sdk-ruby", "errorCode": "Often.", "errorMessage": "Most peace material.", "requestParameters": {"JftbB": {"Eptxk": "qPrPxRooyD"}, "xuqKB": {"FHcUk": "rYYfGwtBqR"}, "FBQGY": {"ncDnH": "LYBLkKkaZg"}, "naqJE": {"ISzCV": "HCugGPfDuG"}, "rcXQZ": []}, "responseElements": {"gOEZw": {"CToqk": "vynlmwGIgk", "VfWbM": "lGuFJDkDip"}, "AjMej": {"vGKXa": "uIzuwIpvfg", "iIVEs": "hgssPSNtMD"}, "rjAON": [{"NECnv": "KuwTanjNIz", "frnTG": "yoVLglyswf"}, {"Lgzrq": "MKpSdGRjCk", "ZkYEt": "ogbPVfdWCp"}, {"cmUmd": {"fxtub": "LKXvMmqDPn", "lpElF": "TWcFwOfntu"}, "GADuH": {"GloWr": "wrMUHqSLnP", "aWRAk": "HbkHXlfiZF"}, "LBiSQ": {"lcaYz": "uAwADPJonD", "EHdTD": "vRTEyiylDO"}, "eoJsl": [{"nyCIH": "AQdoctvPin", "GGBIc": "VFXotQcQaY"}]}]}, "requestID": "0d82f6e4-a7ca-4b76-a542-a4a5182490f2", "eventID": "6ca824c8-81c4-41d3-9de4-58ab902d1158", "eventType": "AwsConsoleSignIn", "apiVersion": "6.1", "managementEvent": "AwsConsoleAction", "resources": [{"ARN": "arn:aws:iam::647294929174:role/SXkLH", "accountId": "647294929174", "type": "AWS::s3::SXkLH"}, {"ARN": "arn:aws:iam::935171735452:role/Auqhr", "accountId": "935171735452", "type": "AWS::lambda::Auqhr"}, {"ARN": "arn:aws:iam::914027419284:role/gXxts", "accountId": "914027419284", "type": "AWS::ebs::gXxts"}], "recipientAccountId": "446069363933", "sharedEventID": "f434d6a1-22f2-4bea-89b6-0505d9e30b5d", "vpcEndpointId": "vpc-18328331", "eventCategory": "Insight"}` 125 126 type args struct { 127 m map[string]interface{} 128 } 129 tests := []struct { 130 name string 131 args args 132 want map[string]interface{} 133 }{ 134 { 135 name: "Test Valid Case Data1", 136 args: args{ 137 m: unmarshalString(t, data1), 138 }, 139 want: map[string]interface{}{ 140 "responseElements.AjMej.iIVEs": "hgssPSNtMD", 141 "responseElements.AjMej.vGKXa": "uIzuwIpvfg", 142 "responseElements.gOEZw.CToqk": "vynlmwGIgk", 143 "responseElements.gOEZw.VfWbM": "lGuFJDkDip", 144 "responseElements.rjAON.0.NECnv": "KuwTanjNIz", 145 "responseElements.rjAON.0.frnTG": "yoVLglyswf", 146 "responseElements.rjAON.1.Lgzrq": "MKpSdGRjCk", 147 "responseElements.rjAON.1.ZkYEt": "ogbPVfdWCp", 148 "responseElements.rjAON.2.GADuH.GloWr": "wrMUHqSLnP", 149 "responseElements.rjAON.2.GADuH.aWRAk": "HbkHXlfiZF", 150 "responseElements.rjAON.2.LBiSQ.EHdTD": "vRTEyiylDO", 151 "responseElements.rjAON.2.LBiSQ.lcaYz": "uAwADPJonD", 152 "responseElements.rjAON.2.cmUmd.fxtub": "LKXvMmqDPn", 153 "responseElements.rjAON.2.cmUmd.lpElF": "TWcFwOfntu", 154 "responseElements.rjAON.2.eoJsl.0.GGBIc": "VFXotQcQaY", 155 "responseElements.rjAON.2.eoJsl.0.nyCIH": "AQdoctvPin", 156 }, 157 }, 158 { 159 name: "Test Valid Case Data2", 160 args: args{ 161 m: unmarshalString(t, data2), 162 }, 163 want: map[string]interface{}{ 164 "apiVersion": "6.1", 165 "awsRegion": "us-west-2", 166 "errorCode": "Often.", 167 "errorMessage": "Most peace material.", 168 "eventCategory": "Insight", 169 "eventID": "6ca824c8-81c4-41d3-9de4-58ab902d1158", 170 "eventName": "SignOut", 171 "eventSource": "ebs.amazonaws.com", 172 "eventTime": "2022-03-31T04:04:24Z", 173 "eventType": "AwsConsoleSignIn", 174 "eventVersion": "1.2", 175 "managementEvent": "AwsConsoleAction", 176 "recipientAccountId": "446069363933", 177 "requestID": "0d82f6e4-a7ca-4b76-a542-a4a5182490f2", 178 "requestParameters.FBQGY.ncDnH": "LYBLkKkaZg", 179 "requestParameters.JftbB.Eptxk": "qPrPxRooyD", 180 "requestParameters.naqJE.ISzCV": "HCugGPfDuG", 181 "requestParameters.xuqKB.FHcUk": "rYYfGwtBqR", 182 "resources.0.ARN": "arn:aws:iam::647294929174:role/SXkLH", 183 "resources.0.accountId": "647294929174", 184 "resources.0.type": "AWS::s3::SXkLH", 185 "resources.1.ARN": "arn:aws:iam::935171735452:role/Auqhr", 186 "resources.1.accountId": "935171735452", 187 "resources.1.type": "AWS::lambda::Auqhr", 188 "resources.2.ARN": "arn:aws:iam::914027419284:role/gXxts", 189 "resources.2.accountId": "914027419284", 190 "resources.2.type": "AWS::ebs::gXxts", 191 "responseElements.AjMej.iIVEs": "hgssPSNtMD", 192 "responseElements.AjMej.vGKXa": "uIzuwIpvfg", 193 "responseElements.gOEZw.CToqk": "vynlmwGIgk", 194 "responseElements.gOEZw.VfWbM": "lGuFJDkDip", 195 "responseElements.rjAON.0.NECnv": "KuwTanjNIz", 196 "responseElements.rjAON.0.frnTG": "yoVLglyswf", 197 "responseElements.rjAON.1.Lgzrq": "MKpSdGRjCk", 198 "responseElements.rjAON.1.ZkYEt": "ogbPVfdWCp", 199 "sharedEventID": "f434d6a1-22f2-4bea-89b6-0505d9e30b5d", 200 "sourceIPAddress": "80.99.17.33", 201 "userAgent": "aws-sdk-ruby", 202 "userIdentity.accessKeyId": "BFWANGMQK15TDRHEDMDD", 203 "userIdentity.accountId": "327525440610", 204 "userIdentity.arn": "arn:aws:iam::327525440610:user/Tina_Long", 205 "userIdentity.principalId": "A9YRGIZJ7LTYX7E6ZBR9", 206 "userIdentity.type": "IAMUser", 207 "userIdentity.userName": "Tina_Long", 208 "vpcEndpointId": "vpc-18328331", 209 }, 210 }, 211 { 212 name: "Test Valid Case Data3", 213 args: args{ 214 m: unmarshalString(t, data3), 215 }, 216 want: map[string]interface{}{ 217 "apiVersion": "6.1", 218 "awsRegion": "us-west-2", 219 "errorCode": "Often.", 220 "errorMessage": "Most peace material.", 221 "eventCategory": "Insight", 222 "eventID": "6ca824c8-81c4-41d3-9de4-58ab902d1158", 223 "eventName": "SignOut", 224 "eventSource": "ebs.amazonaws.com", 225 "eventTime": "2022-03-31T04:04:24Z", 226 "eventType": "AwsConsoleSignIn", 227 "eventVersion": "1.2", 228 "managementEvent": "AwsConsoleAction", 229 "recipientAccountId": "446069363933", 230 "requestID": "0d82f6e4-a7ca-4b76-a542-a4a5182490f2", 231 "requestParameters.FBQGY.ncDnH": "LYBLkKkaZg", 232 "requestParameters.JftbB.Eptxk": "qPrPxRooyD", 233 "requestParameters.naqJE.ISzCV": "HCugGPfDuG", 234 "requestParameters.xuqKB.FHcUk": "rYYfGwtBqR", 235 "resources.0.ARN": "arn:aws:iam::647294929174:role/SXkLH", 236 "resources.0.accountId": "647294929174", 237 "resources.0.type": "AWS::s3::SXkLH", 238 "resources.1.ARN": "arn:aws:iam::935171735452:role/Auqhr", 239 "resources.1.accountId": "935171735452", 240 "resources.1.type": "AWS::lambda::Auqhr", 241 "resources.2.ARN": "arn:aws:iam::914027419284:role/gXxts", 242 "resources.2.accountId": "914027419284", 243 "resources.2.type": "AWS::ebs::gXxts", 244 "responseElements.AjMej.iIVEs": "hgssPSNtMD", 245 "responseElements.AjMej.vGKXa": "uIzuwIpvfg", 246 "responseElements.gOEZw.CToqk": "vynlmwGIgk", 247 "responseElements.gOEZw.VfWbM": "lGuFJDkDip", 248 "responseElements.rjAON.0.NECnv": "KuwTanjNIz", 249 "responseElements.rjAON.0.frnTG": "yoVLglyswf", 250 "responseElements.rjAON.1.Lgzrq": "MKpSdGRjCk", 251 "responseElements.rjAON.1.ZkYEt": "ogbPVfdWCp", 252 "responseElements.rjAON.2.GADuH.GloWr": "wrMUHqSLnP", 253 "responseElements.rjAON.2.GADuH.aWRAk": "HbkHXlfiZF", 254 "responseElements.rjAON.2.LBiSQ.EHdTD": "vRTEyiylDO", 255 "responseElements.rjAON.2.LBiSQ.lcaYz": "uAwADPJonD", 256 "responseElements.rjAON.2.cmUmd.fxtub": "LKXvMmqDPn", 257 "responseElements.rjAON.2.cmUmd.lpElF": "TWcFwOfntu", 258 "responseElements.rjAON.2.eoJsl.0.GGBIc": "VFXotQcQaY", 259 "responseElements.rjAON.2.eoJsl.0.nyCIH": "AQdoctvPin", 260 "sharedEventID": "f434d6a1-22f2-4bea-89b6-0505d9e30b5d", 261 "sourceIPAddress": "80.99.17.33", 262 "userAgent": "aws-sdk-ruby", 263 "userIdentity.accessKeyId": "BFWANGMQK15TDRHEDMDD", 264 "userIdentity.accountId": "327525440610", 265 "userIdentity.arn": "arn:aws:iam::327525440610:user/Tina_Long", 266 "userIdentity.principalId": "A9YRGIZJ7LTYX7E6ZBR9", 267 "userIdentity.type": "IAMUser", 268 "userIdentity.userName": "Tina_Long", 269 "vpcEndpointId": "vpc-18328331", 270 }, 271 }, 272 } 273 for _, tt := range tests { 274 t.Run(tt.name, func(t *testing.T) { 275 if got := Flatten(tt.args.m); !reflect.DeepEqual(got, tt.want) { 276 t.Errorf("Flatten() = %v, want %v", got, tt.want) 277 } 278 }) 279 } 280 }