1- using NUnit . Framework ;
2- using OptimizelySDK . Bucketing ;
3- using OptimizelySDK . Entity ;
4- using OptimizelySDK . Event ;
5- using OptimizelySDK . Event . Builder ;
1+ /*
2+ * Copyright 2017-2018, Optimizely
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+ using Moq ;
18+ using NUnit . Framework ;
619using OptimizelySDK . Logger ;
7- using OptimizelySDK . Tests . EventTests ;
820using OptimizelySDK . Utils ;
9- using System ;
1021using System . Collections . Generic ;
11- using System . Linq ;
12- using System . Text ;
13- using System . Threading . Tasks ;
1422
1523namespace OptimizelySDK . Tests . UtilsTests
1624{
1725 [ TestFixture ]
1826 class EventTagUtilsTest
1927 {
20- private string TestUserId = string . Empty ;
21- private ProjectConfig Config ;
22- private EventBuilder EventBuilder ;
28+ private Mock < ILogger > LoggerMock ;
29+ private ILogger Logger ;
2330
24- [ TestFixtureSetUp ]
31+ [ SetUp ]
2532 public void Setup ( )
2633 {
27- TestUserId = "testUserId" ;
28- var logger = new NoOpLogger ( ) ;
29- Config = ProjectConfig . Create ( TestData . Datafile , logger , new ErrorHandler . NoOpErrorHandler ( ) ) ;
30- EventBuilder = new EventBuilder ( new Bucketer ( logger ) ) ;
34+ LoggerMock = new Mock < ILogger > ( ) ;
35+ LoggerMock . Setup ( i => i . Log ( It . IsAny < LogLevel > ( ) , It . IsAny < string > ( ) ) ) ;
36+
37+ Logger = LoggerMock . Object ;
3138 }
3239
3340 [ Test ]
@@ -61,16 +68,25 @@ public void TestGetRevenueValue()
6168 } ;
6269
6370 // Invalid data.
64- Assert . Null ( EventTagUtils . GetRevenueValue ( null ) ) ;
65- Assert . Null ( EventTagUtils . GetRevenueValue ( invalidTag ) ) ;
66- Assert . Null ( EventTagUtils . GetRevenueValue ( nullValue ) ) ;
67- Assert . Null ( EventTagUtils . GetRevenueValue ( invalidValue ) ) ;
68- Assert . Null ( EventTagUtils . GetRevenueValue ( invalidTagNonRevenue ) ) ;
71+ Assert . Null ( EventTagUtils . GetRevenueValue ( null , Logger ) ) ;
72+ Assert . Null ( EventTagUtils . GetRevenueValue ( invalidTag , Logger ) ) ;
73+ Assert . Null ( EventTagUtils . GetRevenueValue ( nullValue , Logger ) ) ;
74+ Assert . Null ( EventTagUtils . GetRevenueValue ( invalidValue , Logger ) ) ;
75+ Assert . Null ( EventTagUtils . GetRevenueValue ( invalidTagNonRevenue , Logger ) ) ;
76+
77+ LoggerMock . Verify ( l => l . Log ( LogLevel . DEBUG , "Event tags is undefined." ) , Times . Once ) ;
78+ LoggerMock . Verify ( l => l . Log ( LogLevel . DEBUG , "The revenue key is not defined in the event tags." ) , Times . Exactly ( 2 ) ) ;
79+ LoggerMock . Verify ( l => l . Log ( LogLevel . ERROR , "The revenue key value is not defined in event tags." ) , Times . Once ) ;
80+ LoggerMock . Verify ( l => l . Log ( LogLevel . ERROR , "Revenue value is not an integer or couldn't be parsed as an integer." ) , Times . Once ) ;
6981
7082 // Valid data.
71- Assert . AreEqual ( EventTagUtils . GetRevenueValue ( validTag ) , expectedValue ) ;
72- Assert . AreEqual ( EventTagUtils . GetRevenueValue ( validTag2 ) , expectedValue2 ) ;
73- Assert . AreEqual ( EventTagUtils . GetRevenueValue ( validTagStringValue ) , expectedValueString ) ;
83+ Assert . AreEqual ( EventTagUtils . GetRevenueValue ( validTag , Logger ) , expectedValue ) ;
84+ Assert . AreEqual ( EventTagUtils . GetRevenueValue ( validTag2 , Logger ) , expectedValue2 ) ;
85+ Assert . AreEqual ( EventTagUtils . GetRevenueValue ( validTagStringValue , Logger ) , expectedValueString ) ;
86+
87+ LoggerMock . Verify ( l => l . Log ( LogLevel . INFO , $ "The revenue value { expectedValue } will be sent to results.") , Times . Once ) ;
88+ LoggerMock . Verify ( l => l . Log ( LogLevel . INFO , $ "The revenue value { expectedValue2 } will be sent to results.") , Times . Once ) ;
89+ LoggerMock . Verify ( l => l . Log ( LogLevel . INFO , $ "The revenue value { expectedValueString } will be sent to results.") , Times . Once ) ;
7490 }
7591
7692 [ Test ]
@@ -106,23 +122,32 @@ public void TestGetEventValue()
106122 } ;
107123
108124 // Invalid data.
109- Assert . Null ( EventTagUtils . GetNumericValue ( null , new Logger . DefaultLogger ( ) ) ) ;
110- Assert . Null ( EventTagUtils . GetNumericValue ( invalidTag , new Logger . DefaultLogger ( ) ) ) ;
111- Assert . Null ( EventTagUtils . GetNumericValue ( nullValue , new Logger . DefaultLogger ( ) ) ) ;
125+ Assert . Null ( EventTagUtils . GetNumericValue ( null , Logger ) ) ;
126+ Assert . Null ( EventTagUtils . GetNumericValue ( invalidTag , Logger ) ) ;
127+ Assert . Null ( EventTagUtils . GetNumericValue ( nullValue , Logger ) ) ;
112128
129+ LoggerMock . Verify ( l => l . Log ( LogLevel . ERROR , "Event tags is undefined." ) , Times . Once ) ;
130+ LoggerMock . Verify ( l => l . Log ( LogLevel . ERROR , "The numeric metric key is not in event tags." ) , Times . Once ) ;
131+ LoggerMock . Verify ( l => l . Log ( LogLevel . ERROR , "The numeric metric key value is not defined in event tags." ) , Times . Once ) ;
113132
114133 // Valid data.
115- Assert . AreEqual ( 42 , EventTagUtils . GetNumericValue ( validTagStr , new Logger . DefaultLogger ( ) ) ) ;
116- Assert . AreEqual ( "42.3" , EventTagUtils . GetNumericValue ( validTagStr1 , new Logger . DefaultLogger ( ) ) . ToString ( ) ) ;
117- Assert . AreEqual ( EventTagUtils . GetNumericValue ( validTag , new Logger . DefaultLogger ( ) ) , expectedValue ) ;
118- Assert . AreEqual ( EventTagUtils . GetNumericValue ( validTag2 , new Logger . DefaultLogger ( ) ) , expectedValue2 ) ;
119- Assert . AreEqual ( EventTagUtils . GetNumericValue ( validTag3 , new Logger . DefaultLogger ( ) ) . ToString ( ) , expectedValue3 . ToString ( ) ) ;
134+ Assert . AreEqual ( 42 , EventTagUtils . GetNumericValue ( validTagStr , Logger ) ) ;
135+ Assert . AreEqual ( "42.3" , EventTagUtils . GetNumericValue ( validTagStr1 , Logger ) . ToString ( ) ) ;
136+ Assert . AreEqual ( EventTagUtils . GetNumericValue ( validTag , Logger ) , expectedValue ) ;
137+ Assert . AreEqual ( EventTagUtils . GetNumericValue ( validTag2 , Logger ) , expectedValue2 ) ;
138+ Assert . AreEqual ( EventTagUtils . GetNumericValue ( validTag3 , Logger ) . ToString ( ) , expectedValue3 . ToString ( ) ) ;
139+
140+ LoggerMock . Verify ( l => l . Log ( LogLevel . INFO , "The numeric metric value 42.3 will be sent to results." ) , Times . Once ) ;
141+ LoggerMock . Verify ( l => l . Log ( LogLevel . INFO , $ "The numeric metric value { expectedValue } will be sent to results.") , Times . Exactly ( 2 ) ) ;
142+ LoggerMock . Verify ( l => l . Log ( LogLevel . INFO , $ "The numeric metric value { expectedValue2 } will be sent to results.") , Times . Once ) ;
143+ LoggerMock . Verify ( l => l . Log ( LogLevel . INFO , $ "The numeric metric value { expectedValue3 } will be sent to results.") , Times . Once ) ;
120144 }
121145
122146 [ Test ]
123147 public void TestGetNumericMetricInvalidArgs ( )
124148 {
125- Assert . IsNull ( EventTagUtils . GetNumericValue ( null , new Logger . DefaultLogger ( ) ) ) ;
149+ Assert . IsNull ( EventTagUtils . GetNumericValue ( null , Logger ) ) ;
150+ LoggerMock . Verify ( l => l . Log ( LogLevel . ERROR , "Event tags is undefined." ) , Times . Once ) ;
126151
127152 //Errors for all, because it accepts only dictionary//
128153 // Not valid test cases in C#
@@ -139,8 +164,10 @@ public void TestGetNumericMetricInvalidArgs()
139164 public void TestGetNumericMetricNoValueTag ( )
140165 {
141166 // Test that numeric value is not returned when there's no numeric event tag.
142- Assert . IsNull ( EventTagUtils . GetNumericValue ( new Dictionary < string , object > { } , new Logger . DefaultLogger ( ) ) ) ;
143- Assert . IsNull ( EventTagUtils . GetNumericValue ( new Dictionary < string , object > { { "non-value" , 42 } } , new Logger . DefaultLogger ( ) ) ) ;
167+ Assert . IsNull ( EventTagUtils . GetNumericValue ( new Dictionary < string , object > { } , Logger ) ) ;
168+ Assert . IsNull ( EventTagUtils . GetNumericValue ( new Dictionary < string , object > { { "non-value" , 42 } } , Logger ) ) ;
169+
170+ LoggerMock . Verify ( l => l . Log ( LogLevel . ERROR , "The numeric metric key is not in event tags." ) , Times . Exactly ( 2 ) ) ;
144171
145172 //Errors for all, because it accepts only dictionary//
146173 //Assert.IsNull(EventTagUtils.GetEventValue(new object[] { }));
@@ -152,14 +179,16 @@ public void TestGetNumericMetricInvalidValueTag()
152179
153180 // Test that numeric value is not returned when revenue event tag has invalid data type.
154181
155- Assert . IsNull ( EventTagUtils . GetNumericValue ( new Dictionary < string , object > { { "non-value" , null } } , new Logger . DefaultLogger ( ) ) ) ;
156- Assert . IsNull ( EventTagUtils . GetNumericValue ( new Dictionary < string , object > { { "non-value" , 0.5 } } , new Logger . DefaultLogger ( ) ) ) ;
157- Assert . IsNull ( EventTagUtils . GetNumericValue ( new Dictionary < string , object > { { "non-value" , 12345 } } , new Logger . DefaultLogger ( ) ) ) ;
158- Assert . IsNull ( EventTagUtils . GetNumericValue ( new Dictionary < string , object > { { "non-value" , "65536" } } , new Logger . DefaultLogger ( ) ) ) ;
159- Assert . IsNull ( EventTagUtils . GetNumericValue ( new Dictionary < string , object > { { "non-value" , true } } , new Logger . DefaultLogger ( ) ) ) ;
160- Assert . IsNull ( EventTagUtils . GetNumericValue ( new Dictionary < string , object > { { "non-value" , false } } , new Logger . DefaultLogger ( ) ) ) ;
161- Assert . IsNull ( EventTagUtils . GetNumericValue ( new Dictionary < string , object > { { "non-value" , new object [ ] { 1 , 2 , 3 } } } , new Logger . DefaultLogger ( ) ) ) ;
162- Assert . IsNull ( EventTagUtils . GetNumericValue ( new Dictionary < string , object > { { "non-value" , new object [ ] { 'a' , 'b' , 'c' } } } , new Logger . DefaultLogger ( ) ) ) ;
182+ Assert . IsNull ( EventTagUtils . GetNumericValue ( new Dictionary < string , object > { { "non-value" , null } } , Logger ) ) ;
183+ Assert . IsNull ( EventTagUtils . GetNumericValue ( new Dictionary < string , object > { { "non-value" , 0.5 } } , Logger ) ) ;
184+ Assert . IsNull ( EventTagUtils . GetNumericValue ( new Dictionary < string , object > { { "non-value" , 12345 } } , Logger ) ) ;
185+ Assert . IsNull ( EventTagUtils . GetNumericValue ( new Dictionary < string , object > { { "non-value" , "65536" } } , Logger ) ) ;
186+ Assert . IsNull ( EventTagUtils . GetNumericValue ( new Dictionary < string , object > { { "non-value" , true } } , Logger ) ) ;
187+ Assert . IsNull ( EventTagUtils . GetNumericValue ( new Dictionary < string , object > { { "non-value" , false } } , Logger ) ) ;
188+ Assert . IsNull ( EventTagUtils . GetNumericValue ( new Dictionary < string , object > { { "non-value" , new object [ ] { 1 , 2 , 3 } } } , Logger ) ) ;
189+ Assert . IsNull ( EventTagUtils . GetNumericValue ( new Dictionary < string , object > { { "non-value" , new object [ ] { 'a' , 'b' , 'c' } } } , Logger ) ) ;
190+
191+ LoggerMock . Verify ( l => l . Log ( LogLevel . ERROR , "The numeric metric key is not in event tags." ) , Times . Exactly ( 8 ) ) ;
163192 }
164193
165194 [ Test ]
@@ -168,43 +197,50 @@ public void TestGetNumericMetricValueTag()
168197 {
169198
170199 // An integer should be cast to a float
171- Assert . AreEqual ( 12345.0 , EventTagUtils . GetNumericValue ( new Dictionary < string , object > { { "value" , 12345 } } , new Logger . DefaultLogger ( ) ) ) ;
200+ Assert . AreEqual ( 12345.0 , EventTagUtils . GetNumericValue ( new Dictionary < string , object > { { "value" , 12345 } } , Logger ) ) ;
172201
173202 // A string should be cast to a float
174- Assert . AreEqual ( 12345.0 , EventTagUtils . GetNumericValue ( new Dictionary < string , object > { { "value" , "12345" } } , new Logger . DefaultLogger ( ) ) ) ;
203+ Assert . AreEqual ( 12345.0 , EventTagUtils . GetNumericValue ( new Dictionary < string , object > { { "value" , "12345" } } , Logger ) ) ;
175204
176205 // Valid float values
177206 float someFloat = 1.2345F ;
178207
179- Assert . AreEqual ( someFloat , EventTagUtils . GetNumericValue ( new Dictionary < string , object > { { "value" , someFloat } } , new Logger . DefaultLogger ( ) ) ) ;
208+ Assert . AreEqual ( someFloat , EventTagUtils . GetNumericValue ( new Dictionary < string , object > { { "value" , someFloat } } , Logger ) ) ;
180209
181210 float maxFloat = float . MaxValue ;
182- Assert . AreEqual ( maxFloat , EventTagUtils . GetNumericValue ( new Dictionary < string , object > { { "value" , maxFloat } } , new Logger . DefaultLogger ( ) ) ) ;
211+ Assert . AreEqual ( maxFloat , EventTagUtils . GetNumericValue ( new Dictionary < string , object > { { "value" , maxFloat } } , Logger ) ) ;
183212
184213
185214 float minFloat = float . MinValue ;
186- Assert . AreEqual ( minFloat , EventTagUtils . GetNumericValue ( new Dictionary < string , object > { { "value" , minFloat } } , new Logger . DefaultLogger ( ) ) ) ;
215+ Assert . AreEqual ( minFloat , EventTagUtils . GetNumericValue ( new Dictionary < string , object > { { "value" , minFloat } } , Logger ) ) ;
187216
188217 // Invalid values
189- Assert . IsNull ( EventTagUtils . GetNumericValue ( new Dictionary < string , object > { { "value" , false } } , new Logger . DefaultLogger ( ) ) ) ;
190- Assert . IsNull ( EventTagUtils . GetNumericValue ( new Dictionary < string , object > { { "value" , null } } , new Logger . DefaultLogger ( ) ) ) ;
218+ Assert . IsNull ( EventTagUtils . GetNumericValue ( new Dictionary < string , object > { { "value" , false } } , Logger ) ) ;
219+ Assert . IsNull ( EventTagUtils . GetNumericValue ( new Dictionary < string , object > { { "value" , null } } , Logger ) ) ;
191220
192- Assert . IsNull ( EventTagUtils . GetNumericValue ( new Dictionary < string , object > { { "value" , true } } , new Logger . DefaultLogger ( ) ) ) ;
193- Assert . IsNull ( EventTagUtils . GetNumericValue ( new Dictionary < string , object > { { "value" , new int [ ] { } } } , new Logger . DefaultLogger ( ) ) ) ;
221+ Assert . IsNull ( EventTagUtils . GetNumericValue ( new Dictionary < string , object > { { "value" , true } } , Logger ) ) ;
222+ Assert . IsNull ( EventTagUtils . GetNumericValue ( new Dictionary < string , object > { { "value" , new int [ ] { } } } , Logger ) ) ;
194223
195- var numericValueArray = EventTagUtils . GetNumericValue ( new Dictionary < string , object > { { "value" , new object [ ] { } } } , new Logger . DefaultLogger ( ) ) ;
224+ var numericValueArray = EventTagUtils . GetNumericValue ( new Dictionary < string , object > { { "value" , new object [ ] { } } } , Logger ) ;
196225 Assert . IsNull ( numericValueArray , string . Format ( "Array numeric value is {0}" , numericValueArray ) ) ;
197226
198227
199- var numericValueNone = EventTagUtils . GetNumericValue ( new Dictionary < string , object > { { "value" , null } } , new Logger . DefaultLogger ( ) ) ;
228+ var numericValueNone = EventTagUtils . GetNumericValue ( new Dictionary < string , object > { { "value" , null } } , Logger ) ;
200229 Assert . IsNull ( numericValueNone , string . Format ( "None numeric value is {0}" , numericValueNone ) ) ;
201230
202231
203- var numericValueOverflow = EventTagUtils . GetNumericValue ( new Dictionary < string , object > { { "value" , float . MaxValue * 10 } } , new Logger . DefaultLogger ( ) ) ;
232+ var numericValueOverflow = EventTagUtils . GetNumericValue ( new Dictionary < string , object > { { "value" , float . MaxValue * 10 } } , Logger ) ;
204233 Assert . IsNull ( numericValueOverflow , string . Format ( "Max numeric value is {0}" , float . MaxValue * 10 ) ) ;
205234
206- Assert . AreEqual ( 0.0 , EventTagUtils . GetNumericValue ( new Dictionary < string , object > { { "value" , 0.0 } } , new Logger . DefaultLogger ( ) ) ) ;
235+ Assert . AreEqual ( 0.0 , EventTagUtils . GetNumericValue ( new Dictionary < string , object > { { "value" , 0.0 } } , Logger ) ) ;
207236
237+ LoggerMock . Verify ( l => l . Log ( LogLevel . INFO , "The numeric metric value 12345 will be sent to results." ) , Times . Exactly ( 2 ) ) ;
238+ LoggerMock . Verify ( l => l . Log ( LogLevel . INFO , $ "The numeric metric value { maxFloat } will be sent to results.") , Times . Once ) ;
239+ LoggerMock . Verify ( l => l . Log ( LogLevel . INFO , $ "The numeric metric value { minFloat } will be sent to results.") , Times . Once ) ;
240+ LoggerMock . Verify ( l => l . Log ( LogLevel . ERROR , $ "Provided numeric value { float . PositiveInfinity } is in an invalid format.") , Times . Once ) ;
241+ LoggerMock . Verify ( l => l . Log ( LogLevel . INFO , "The numeric metric value 0 will be sent to results." ) , Times . Once ) ;
242+ LoggerMock . Verify ( l => l . Log ( LogLevel . ERROR , "Provided numeric value is boolean which is an invalid format." ) , Times . Exactly ( 2 ) ) ;
243+ LoggerMock . Verify ( l => l . Log ( LogLevel . ERROR , "Numeric metric value is not in integer, float, or string form." ) , Times . Exactly ( 2 ) ) ;
208244
209245 /* Value is converted into 1234F */
210246 //var numericValueInvalidLiteral = EventTagUtils.GetEventValue(new Dictionary<string, object> { { "value", "1,234" } });
0 commit comments