|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Net.Http; |
| 5 | +using System.Text; |
| 6 | +using System.Threading.Tasks; |
| 7 | +using Newtonsoft.Json; |
| 8 | +using OptimizelySDK; |
| 9 | +using OptimizelySDK.Config; |
| 10 | +using OptimizelySDK.Entity; |
| 11 | +using OptimizelySDK.ErrorHandler; |
| 12 | +using OptimizelySDK.Event; |
| 13 | +using OptimizelySDK.Logger; |
| 14 | +using OptimizelySDK.Notifications; |
| 15 | +using OptimizelySDK.Odp; |
| 16 | +using OptimizelySDK.OptimizelyDecisions; |
| 17 | + |
| 18 | +namespace DemoConsoleApp |
| 19 | +{ |
| 20 | + class Program |
| 21 | + { |
| 22 | + private static void FetchAndDecide(OptimizelyUserContext user) |
| 23 | + { |
| 24 | + //========================================= |
| 25 | + // Fetch Qualified Segments + decide |
| 26 | + // ========================================= |
| 27 | + |
| 28 | + user.FetchQualifiedSegments(); // to test segment options add one or both as argument: ['RESET_CACHE', 'IGNORE_CACHE'] |
| 29 | + |
| 30 | + var options = new OptimizelyDecideOption[] { OptimizelyDecideOption.INCLUDE_REASONS }; |
| 31 | + |
| 32 | + OptimizelyDecision decision = user.Decide("flag1", options); |
| 33 | + string decisionString = JsonConvert.SerializeObject(decision); |
| 34 | + Console.WriteLine(" >>> DECISION " + decisionString); |
| 35 | + |
| 36 | + var segments = user.GetQualifiedSegments(); |
| 37 | + if (segments != null) |
| 38 | + { |
| 39 | + Console.WriteLine(" >>> SEGMENTS: [{0}]", string.Join(", ", segments)); |
| 40 | + |
| 41 | + foreach (string segment in segments) |
| 42 | + { |
| 43 | + bool isQualified = user.IsQualifiedFor(segment); |
| 44 | + Console.WriteLine(" >>> IS QUALIFIED for " + segment + " : " + isQualified); |
| 45 | + } |
| 46 | + } |
| 47 | + else |
| 48 | + { |
| 49 | + Console.WriteLine(" >>> SEGMENTS: null"); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + private static void SendEvent(Optimizely optimizely) |
| 54 | + { |
| 55 | + |
| 56 | + var identifiers = new Dictionary<string, string>(); |
| 57 | + identifiers.Add("fs-user-id", "fs-id-12"); |
| 58 | + identifiers.Add("'email'", "fs-bash-x@optimizely.com"); |
| 59 | + |
| 60 | + // valid case |
| 61 | + optimizely.SendOdpEvent("any", "any", identifiers, null); |
| 62 | + |
| 63 | + // // test missing/ empty identifiers should not be allowed |
| 64 | + // optimizely.SendOdpEvent("any", "any", null, null); |
| 65 | + // optimizely.SendOdpEvent("any", "any", new Dictionary<string, string>(), null); |
| 66 | + // |
| 67 | + // // test missing/ empty action should not be allowed |
| 68 | + // optimizely.SendOdpEvent("", "any", identifiers, null); |
| 69 | + // optimizely.SendOdpEvent(null, "any", identifiers, null); |
| 70 | + } |
| 71 | + |
| 72 | + static void Main(string[] args) |
| 73 | + { |
| 74 | + /** ============================================ |
| 75 | + # CONFIG TYPE 1: |
| 76 | + # default config, minimal settings |
| 77 | + ============================================ |
| 78 | + """ |
| 79 | + TEST THE FOLLOWING: |
| 80 | + - test creating user context with regular attributes |
| 81 | + - test creating user context with prebuilt segments (no odp list), should get segments back, experiment should evaluate to true, decide response should look correct |
| 82 | + - truth table - TBD |
| 83 | + - test creating user context with prebuilt segment list, should get back a list of segments, experiment should evaluate to true, decide response should look correct |
| 84 | + - may not need truth table test here (check) |
| 85 | + - add RESET_CACHE and/or IGNORE_CACHE to fetch qualified segments call and repeat |
| 86 | + - test send event |
| 87 | + - verify events on ODP event inspector |
| 88 | + - in send_event function uncomment lines of code that test missing identifiers and action keys, verify appropriate error is produced |
| 89 | + - test audience segments (see spreadsheet |
| 90 | + - test implicit/explicit ODP events? |
| 91 | + - test integrations (no ODP integration added to project, integration is on, is off) |
| 92 | + """ |
| 93 | + **/ |
| 94 | + var optimizely = OptimizelyFactory.NewDefaultInstance("TbrfRLeKvLyWGusqANoeR"); |
| 95 | + |
| 96 | + var optimizelyConfig = optimizely.GetOptimizelyConfig(); |
| 97 | + |
| 98 | + var attributes = new UserAttributes(); |
| 99 | + attributes.Add("laptop_os", "mac"); |
| 100 | + |
| 101 | + var user = optimizely.CreateUserContext("fs-id-6", attributes); |
| 102 | + |
| 103 | + FetchAndDecide(user); |
| 104 | + SendEvent(optimizely); |
| 105 | + |
| 106 | + optimizely.Dispose(); |
| 107 | + |
| 108 | + /** ============================================ |
| 109 | + # CONFIG TYPE 2: |
| 110 | + # with ODP integration changed at app.optimizely.com - changed public key or host url |
| 111 | + # VALID API key/HOST url- should work, INVALID KEY/URL-should get errors |
| 112 | + # ============================================ |
| 113 | + """ |
| 114 | + TEST THE FOLLOWING: |
| 115 | + - test the same as in "CONFIG TYPE 1" but use invalid API key or HOST url |
| 116 | + - TODO clarify with Jae what to test here !!! |
| 117 | + """ **/ |
| 118 | + /*optimizely = OptimizelyFactory.NewDefaultInstance("TbrfRLeKvLyWGusqANoeR"); |
| 119 | +
|
| 120 | + attributes = new UserAttributes(); |
| 121 | + attributes.Add("laptop_os", "mac"); |
| 122 | +
|
| 123 | + // CASE 1 - REGULAR ATTRIBUTES, NO ODP |
| 124 | + user = optimizely.CreateUserContext("user123", attributes); |
| 125 | +
|
| 126 | + // CASE 2 - PREBUILT SEGMENTS, NO LIST SEGMENTS, valid user id is fs-id-12 (matehces DOB) |
| 127 | + // user = optimizely.CreateUserContext("fs-id-12", attributes); |
| 128 | +
|
| 129 | + // CASE 3 - SEGMENT LIST/ARRAY, valid user id is fs-id-6 |
| 130 | + // user = optimizely.CreateUserContext("fs-id-6", attributes); |
| 131 | +
|
| 132 | + FetchAndDecide(user); |
| 133 | + SendEvent(optimizely); |
| 134 | +
|
| 135 | + optimizely.Dispose();*/ |
| 136 | + |
| 137 | + /** ============================================ |
| 138 | + # CONFIG TYPE 3: |
| 139 | + # with different ODP configuration options (odpdisabled, segments_cache_size etc) |
| 140 | + # ============================================ |
| 141 | + """ |
| 142 | + TEST THE FOLLOWING: |
| 143 | + same as in "CONFIG TYPE 1", but add config options to fetch qualified segments function, for example: |
| 144 | + odp_disabled |
| 145 | + segments_cache_size |
| 146 | + segments_cache_timeout_in_secs |
| 147 | + odp_segments_cache |
| 148 | + odp_segment_manager |
| 149 | + odp_event_manager |
| 150 | + odp_segment_request_timeout |
| 151 | + odp_event_request_timeout |
| 152 | + odp_flush_interval |
| 153 | +
|
| 154 | + Observe responses and verity the correct behavior. |
| 155 | + """ |
| 156 | + **/ |
| 157 | + var logger = new DefaultLogger(); |
| 158 | + var errorHandler = new DefaultErrorHandler(logger, false); |
| 159 | + var notificationCenter = new NotificationCenter(); |
| 160 | + var builder = new HttpProjectConfigManager.Builder(); |
| 161 | + var configManager = builder.WithSdkKey("TbrfRLeKvLyWGusqANoeR"). |
| 162 | + WithLogger(logger). |
| 163 | + WithErrorHandler(errorHandler). |
| 164 | + WithNotificationCenter(notificationCenter). |
| 165 | + Build(false); |
| 166 | + var handler = HttpProjectConfigManager.HttpClient.GetHttpClientHandler(); |
| 167 | + handler.UseProxy = false; |
| 168 | + |
| 169 | + var fetchSegmentHttpClient = new HttpClient(handler); |
| 170 | + fetchSegmentHttpClient.Timeout = TimeSpan.FromMilliseconds(10000); |
| 171 | + var sendEventHttpClient = new HttpClient(handler); |
| 172 | + sendEventHttpClient.Timeout = TimeSpan.FromMilliseconds(10000); |
| 173 | + |
| 174 | + var odpEventApiManager = new OdpEventApiManager(logger, errorHandler, sendEventHttpClient); |
| 175 | + var odpSegmentApiManager = new OdpSegmentApiManager(logger, errorHandler, fetchSegmentHttpClient); |
| 176 | + |
| 177 | + var eventManagerBuilder = new OdpEventManager.Builder() |
| 178 | + .WithOdpEventApiManager(odpEventApiManager) |
| 179 | + .WithFlushInterval(TimeSpan.FromMilliseconds(1)); |
| 180 | + |
| 181 | + OdpSegmentManager odpSegmentManager = new OdpSegmentManager(odpSegmentApiManager, cacheSize: 1, itemTimeout: TimeSpan.FromSeconds(10), logger: logger); |
| 182 | + |
| 183 | + bool disableODP = false; // Set this to true to disable odp and false to enable odp, There is no other way to disable odp, like passing any variable. |
| 184 | + OdpManager odpManager = null; |
| 185 | + if (!disableODP) |
| 186 | + { |
| 187 | + odpManager = new OdpManager.Builder() |
| 188 | + .WithEventManager(eventManagerBuilder.Build()) |
| 189 | + .WithSegmentManager(odpSegmentManager) |
| 190 | + .WithErrorHandler(errorHandler) |
| 191 | + .WithLogger(logger) |
| 192 | + .Build(); |
| 193 | + } |
| 194 | + optimizely = new Optimizely(configManager, notificationCenter, null, logger, errorHandler, null, null, null, odpManager); |
| 195 | + |
| 196 | + attributes = new UserAttributes(); |
| 197 | + attributes.Add("laptop_os", "mac"); |
| 198 | + |
| 199 | + // CASE 1 - REGULAR ATTRIBUTES, NO ODP |
| 200 | + user = optimizely.CreateUserContext("user123", attributes); |
| 201 | + |
| 202 | + // CASE 2 - PREBUILT SEGMENTS, NO LIST SEGMENTS, valid user id is fs-id-12 (matches DOB) |
| 203 | + // user = optimizely.CreateUserContext("fs-id-12", attributes); |
| 204 | + |
| 205 | + // CASE 3 - SEGMENT LIST/ARRAY, valid user id is fs-id-6 |
| 206 | + // user = optimizely.CreateUserContext("fs-id-6", attributes); |
| 20
341A
7 | + |
| 208 | + FetchAndDecide(user); |
| 209 | + SendEvent(optimizely); |
| 210 | + |
| 211 | + optimizely.Dispose(); |
| 212 | + |
| 213 | + Console.ReadLine(); |
| 214 | + } |
| 215 | + |
| 216 | + |
| 217 | + } |
| 218 | +} |
0 commit comments