Skip to content

Commit 05e60bd

Browse files
[FSSDK-8948] fix(odp): check odp identifiers not null or empty before sending (#342)
* fix(odp): check odp identifiers not null or empty before sending * fixed as per comment * refactored and removed additional message for identifiers --------- Co-authored-by: mnoman09 <m.nomanshoaib09@gmail.com>
1 parent c5592e7 commit 05e60bd

File tree

7 files changed

+56
-9
lines changed

7 files changed

+56
-9
lines changed

OptimizelySDK.Tests/OdpTests/OdpEventManagerTests.cs

+37
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,43 @@ public void ShouldDiscardEventsWithInvalidData()
271271
Times.Exactly(2));
272272
}
273273

274+
[Test]
275+
public void ShouldDiscardEventsWithInvalidIdentifiersDictionary()
276+
{
277+
var eventWithemptyIdentifiers = new OdpEvent("t3", "a3",
278+
new Dictionary<string, string>(),
279+
new Dictionary<string, object>
280+
{
281+
{
282+
"data1", $"data1-value1-1"
283+
},
284+
{
285+
"data2", 1
286+
},
287+
});
288+
var eventWithNullIdentifiers = new OdpEvent("t3", "a3",
289+
null, new Dictionary<string, object>
290+
{
291+
{
292+
"data1", $"data1-value1-1"
293+
},
294+
{
295+
"data2", 1
296+
},
297+
});
298+
var eventManager = new OdpEventManager.Builder().
299+
WithOdpEventApiManager(_mockApiManager.Object).
300+
WithLogger(_mockLogger.Object).
301+
Build();
302+
eventManager.UpdateSettings(_odpConfig);
303+
304+
eventManager.SendEvent(eventWithemptyIdentifiers);
305+
eventManager.SendEvent(eventWithNullIdentifiers);
306+
307+
_mockLogger.Verify(l => l.Log(LogLevel.ERROR, Constants.ODP_INVALID_DATA_MESSAGE),
308+
Times.Exactly(2));
309+
}
310+
274311
[Test]
275312
public void ShouldAddAdditionalInformationToEachEvent()
276313
{

OptimizelySDK/Odp/Constants.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2022 Optimizely
2+
* Copyright 2022-2023 Optimizely
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -65,7 +65,7 @@ public static class Constants
6565
/// <summary>
6666
/// Default message to log when an ODP Event contains invalid data
6767
/// </summary>
68-
public const string ODP_INVALID_DATA_MESSAGE = "ODP data is not valid.";
68+
public const string ODP_INVALID_DATA_MESSAGE = "ODP event send failed.";
6969

7070
/// <summary>
7171
/// Default message to log when sending ODP event fails

OptimizelySDK/Odp/Entity/OdpEvent.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2022 Optimizely
2+
* Copyright 2022-2023 Optimizely
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -34,7 +34,7 @@ public class OdpEvent
3434
public string Action { get; }
3535

3636
/// <summary>
37-
/// Key-value map of user identifiers
37+
/// Dictionary for identifiers. The caller must provide at least one key-value pair.
3838
/// </summary>
3939
public Dictionary<string, string> Identifiers { get; }
4040

OptimizelySDK/Odp/IOdpManager.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2022 Optimizely
2+
* Copyright 2022-2023 Optimizely
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -51,7 +51,7 @@ public interface IOdpManager
5151
/// </summary>
5252
/// <param name="type">Type of event (typically `fullstack` from server-side SDK events)</param>
5353
/// <param name="action">Subcategory of the event type</param>
54-
/// <param name="identifiers">Key-value map of user identifiers</param>
54+
/// <param name="identifiers">Dictionary for identifiers. The caller must provide at least one key-value pair.</param>
5555
/// <param name="data">Event data in a key-value pair format</param>
5656
void SendEvent(string type, string action, Dictionary<string, string> identifiers,
5757
Dictionary<string, object> data

OptimizelySDK/Odp/OdpEventManager.cs

+11-1
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ public void SendEvent(OdpEvent odpEvent)
296296
return;
297297
}
298298

299-
if (InvalidDataFound(odpEvent.Data))
299+
if (InvalidDataFound(odpEvent.Data) || !IsIdentifiersValid(odpEvent.Identifiers))
300300
{
301301
_logger.Log(LogLevel.ERROR, Constants.ODP_INVALID_DATA_MESSAGE);
302302
return;
@@ -380,6 +380,16 @@ public void UpdateSettings(OdpConfig odpConfig)
380380
}
381381
}
382382

383+
/// <summary>
384+
/// ODP event identifiers should not be null or empty
385+
/// </summary>
386+
/// <param name="Identifiers">Identifiers to be analyzed</param>
387+
/// <returns>True if identifiers dictionary is not null or empty otherwise False</returns>
388+
private bool IsIdentifiersValid(Dictionary<string, string> Identifiers)
389+
{
390+
return Identifiers?.Count > 0;
391+
}
392+
383393
/// <summary>
384394
/// ODP event data has invalid types
385395
/// </summary>

OptimizelySDK/Odp/OdpManager.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ public void IdentifyUser(string userId)
116116
/// </summary>
117117
/// <param name="type">Type of event (typically `fullstack` from server-side SDK events)</param>
118118
/// <param name="action">Subcategory of the event type</param>
119-
/// <param name="identifiers">Key-value map of user identifiers</param>
119+
/// <param name="identifiers">Dictionary for identifiers. The caller must provide at least one key-value pair.</param>
120120
/// <param name="data">Event data in a key-value pair format</param>
121121
public void SendEvent(string type, string action, Dictionary<string, string> identifiers,
122122
Dictionary<string, object> data

OptimizelySDK/Optimizely.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1356,7 +1356,7 @@ internal void IdentifyUser(string userId)
13561356
/// </summary>
13571357
/// <param name="type">Type of event (typically `fullstack` from server-side SDK events)</param>
13581358
/// <param name="action">Subcategory of the event type</param>
1359-
/// <param name="identifiers">Key-value map of user identifiers</param>
1359+
/// <param name="identifiers">Dictionary for identifiers. The caller must provide at least one key-value pair.</param>
13601360
/// <param name="data">Event data in a key-value pair format</param>
13611361
public void SendOdpEvent(string type, string action, Dictionary<string, string> identifiers,
13621362
Dictionary<string, object> data

0 commit comments

Comments
 (0)