|
| 1 | +// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 2 | +// or more contributor license agreements. Licensed under the Elastic License; |
| 3 | +// you may not use this file except in compliance with the Elastic License. |
| 4 | + |
| 5 | +//go:build !aix |
| 6 | +// +build !aix |
| 7 | + |
| 8 | +package azureeventhub |
| 9 | + |
| 10 | +import ( |
| 11 | + "fmt" |
| 12 | + "testing" |
| 13 | + |
| 14 | + "github.com/stretchr/testify/assert" |
| 15 | + |
| 16 | + "github.com/elastic/elastic-agent-libs/logp" |
| 17 | +) |
| 18 | + |
| 19 | +func TestParseMultipleMessagesSanitization(t *testing.T) { |
| 20 | + msg := "{\"records\":[{'test':\"this is some message\",\n\n\"time\":\"2019-12-17T13:43:44.4946995Z\"}," + |
| 21 | + "{\"test\":\"this is '2nd' message\",\"time\":\"2019-12-17T13:43:44.4946995Z\"}," + |
| 22 | + "{\"time\": \"2023-04-11T13:35:20Z\", \"resourceId\": \"/SUBSCRIPTIONS/REDACTED/RESOURCEGROUPS/ELASTIC-FUNCTION-TEST/PROVIDERS/MICROSOFT.WEB/SITES/REDACTED\", \"category\": \"FunctionAppLogs\", \"operationName\": \"Microsoft.Web/sites/functions/log\", \"level\": \"Informational\", \"location\": \"West Europe\", \"properties\": {'appName':'REDACTED','roleInstance':'REDACTED','message':'Elastic Test Function Trigger. ---- West Europe West Europe West Europe West Europe West Europe ','category':'Function.HttpTriggerJava.User','hostVersion':'4.16.5.5','functionInvocationId':'REDACTED','functionName':'HttpTriggerJava','hostInstanceId':'REDACTED','level':'Information','levelId':2,'processId':62}}]}" |
| 23 | + msgs := []string{ |
| 24 | + "{\"test\":\"this is some message\",\"time\":\"2019-12-17T13:43:44.4946995Z\"}", |
| 25 | + "{\"test\":\"this is '2nd' message\",\"time\":\"2019-12-17T13:43:44.4946995Z\"}", |
| 26 | + "{\"category\":\"FunctionAppLogs\",\"level\":\"Informational\",\"location\":\"West Europe\",\"operationName\":\"Microsoft.Web/sites/functions/log\",\"properties\":{\"appName\":\"REDACTED\",\"category\":\"Function.HttpTriggerJava.User\",\"functionInvocationId\":\"REDACTED\",\"functionName\":\"HttpTriggerJava\",\"hostInstanceId\":\"REDACTED\",\"hostVersion\":\"4.16.5.5\",\"level\":\"Information\",\"levelId\":2,\"message\":\"Elastic Test Function Trigger. ---- West Europe West Europe West Europe West Europe West Europe \",\"processId\":62,\"roleInstance\":\"REDACTED\"},\"resourceId\":\"/SUBSCRIPTIONS/REDACTED/RESOURCEGROUPS/ELASTIC-FUNCTION-TEST/PROVIDERS/MICROSOFT.WEB/SITES/REDACTED\",\"time\":\"2023-04-11T13:35:20Z\"}", |
| 27 | + } |
| 28 | + |
| 29 | + input := azureInput{ |
| 30 | + log: logp.NewLogger(fmt.Sprintf("%s test for input", inputName)), |
| 31 | + config: azureInputConfig{ |
| 32 | + SanitizeOptions: []string{"SINGLE_QUOTES", "NEW_LINES"}, |
| 33 | + }, |
| 34 | + } |
| 35 | + |
| 36 | + messages := input.parseMultipleMessages([]byte(msg)) |
| 37 | + assert.NotNil(t, messages) |
| 38 | + assert.Equal(t, len(messages), 3) |
| 39 | + for _, ms := range messages { |
| 40 | + assert.Contains(t, msgs, ms) |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +func TestSanitize(t *testing.T) { |
| 45 | + jsonByte := []byte("{'test':\"this is 'some' message\n\",\n\"time\":\"2019-12-17T13:43:44.4946995Z\"}") |
| 46 | + |
| 47 | + testCases := []struct { |
| 48 | + name string |
| 49 | + opts []string |
| 50 | + expected []byte |
| 51 | + }{ |
| 52 | + { |
| 53 | + name: "no options", |
| 54 | + opts: []string{}, |
| 55 | + expected: jsonByte, |
| 56 | + }, |
| 57 | + { |
| 58 | + name: "NEW_LINES option", |
| 59 | + opts: []string{"NEW_LINES"}, |
| 60 | + expected: []byte("{'test':\"this is 'some' message\",\"time\":\"2019-12-17T13:43:44.4946995Z\"}"), |
| 61 | + }, |
| 62 | + { |
| 63 | + name: "SINGLE_QUOTES option", |
| 64 | + opts: []string{"SINGLE_QUOTES"}, |
| 65 | + expected: []byte("{\"test\":\"this is 'some' message\n\",\n\"time\":\"2019-12-17T13:43:44.4946995Z\"}"), |
| 66 | + }, |
| 67 | + { |
| 68 | + name: "both options", |
| 69 | + opts: []string{"NEW_LINES", "SINGLE_QUOTES"}, |
| 70 | + expected: []byte("{\"test\":\"this is 'some' message\",\"time\":\"2019-12-17T13:43:44.4946995Z\"}"), |
| 71 | + }, |
| 72 | + } |
| 73 | + |
| 74 | + // Run test cases |
| 75 | + for _, tc := range testCases { |
| 76 | + tc := tc |
| 77 | + t.Run(tc.name, func(t *testing.T) { |
| 78 | + res := sanitize(jsonByte, tc.opts...) |
| 79 | + assert.Equal(t, tc.expected, res) |
| 80 | + }) |
| 81 | + } |
| 82 | +} |
0 commit comments