|
1 | 1 | /*
|
2 |
| - * Copyright 2017, Optimizely |
| 2 | + * Copyright 2017-2018, Optimizely |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
22 | 22 | using OptimizelySDK.Exceptions;
|
23 | 23 | using NUnit.Framework;
|
24 | 24 | using OptimizelySDK.Entity;
|
| 25 | +using Newtonsoft.Json; |
| 26 | +using OptimizelySDK.Event.Builder; |
| 27 | +using OptimizelySDK.Utils; |
25 | 28 |
|
26 | 29 | namespace OptimizelySDK.Tests
|
27 | 30 | {
|
@@ -51,14 +54,14 @@ public static Dictionary<string, object> CreateDictionary(string name, object en
|
51 | 54 | public void TestInit()
|
52 | 55 | {
|
53 | 56 | // Check Version
|
54 |
| - Assert.AreEqual(4, Config.Version); |
| 57 | + Assert.AreEqual("4", Config.Version); |
55 | 58 |
|
56 | 59 | // Check Account ID
|
57 | 60 | Assert.AreEqual("1592310167", Config.AccountId);
|
58 | 61 | // Check Project ID
|
59 | 62 | Assert.AreEqual("7720880029", Config.ProjectId);
|
60 | 63 | // Check Revision
|
61 |
| - Assert.AreEqual(15, Config.Revision); |
| 64 | + Assert.AreEqual("15", Config.Revision); |
62 | 65 |
|
63 | 66 | // Check Group ID Map
|
64 | 67 | var expectedGroupId = CreateDictionary("7722400015", Config.GetGroup("7722400015"));
|
@@ -810,5 +813,55 @@ public void TestVariationFeatureEnabledProperty()
|
810 | 813 | var variation = Config.GetVariationFromKey("test_experiment", "control");
|
811 | 814 | Assert.IsFalse(variation.IsFeatureEnabled);
|
812 | 815 | }
|
| 816 | + |
| 817 | + [Test] |
| 818 | + public void TestBotFilteringValues() |
| 819 | + { |
| 820 | + // Verify that bot filtering value is true as defined in Config data. |
| 821 | + Assert.True(Config.BotFiltering.GetValueOrDefault()); |
| 822 | + |
| 823 | + // Remove botFilering node and verify returned value in null. |
| 824 | + JObject projConfig = JObject.Parse(TestData.Datafile); |
| 825 | + if (projConfig.TryGetValue("botFiltering", out JToken token)) |
| 826 | + { |
| 827 | + projConfig.Property("botFiltering").Remove(); |
| 828 | + var configWithoutBotFilter = ProjectConfig.Create(JsonConvert.SerializeObject(projConfig), |
| 829 | + LoggerMock.Object, ErrorHandlerMock.Object); |
| 830 | + |
| 831 | + // Verify that bot filtering is null when not defined in datafile. |
| 832 | + Assert.Null(configWithoutBotFilter.BotFiltering); |
| 833 | + } |
| 834 | + } |
| 835 | + |
| 836 | + [Test] |
| 837 | + public void TestGetAttributeIdWithReservedPrefix() |
| 838 | + { |
| 839 | + // Verify that attribute key is returned for reserved attribute key. |
| 840 | + Assert.AreEqual(Config.GetAttributeId(ControlAttributes.USER_AGENT_ATTRIBUTE), ControlAttributes.USER_AGENT_ATTRIBUTE); |
| 841 | + |
| 842 | + // Verify that attribute Id is returned for attribute key with reserved prefix that does not exist in datafile. |
| 843 | + Assert.AreEqual(Config.GetAttributeId("$opt_reserved_prefix_attribute"), "$opt_reserved_prefix_attribute"); |
| 844 | + |
| 845 | + // Create config file copy with additional resered prefix attribute. |
| 846 | + string reservedPrefixAttrKey = "$opt_user_defined_attribute"; |
| 847 | + JObject projConfig = JObject.Parse(TestData.Datafile); |
| 848 | + var attributes = (JArray)projConfig["attributes"]; |
| 849 | + |
| 850 | + var reservedAttr = new Entity.Attribute { Id = "7723348204", Key = reservedPrefixAttrKey }; |
| 851 | + attributes.Add((JObject)JToken.FromObject(reservedAttr)); |
| 852 | + |
| 853 | + // Verify that attribute Id is returned and warning is logged for attribute key with reserved prefix that exists in datafile. |
| 854 | + var reservedAttrConfig = ProjectConfig.Create(JsonConvert.SerializeObject(projConfig), LoggerMock.Object, ErrorHandlerMock.Object); |
| 855 | + Assert.AreEqual(reservedAttrConfig.GetAttributeId(reservedPrefixAttrKey), reservedAttrConfig.GetAttribute(reservedPrefixAttrKey).Id); |
| 856 | + LoggerMock.Verify(l => l.Log(LogLevel.WARN, $@"Attribute {reservedPrefixAttrKey} unexpectedly has reserved prefix {ProjectConfig.RESERVED_ATTRIBUTE_PREFIX}; using attribute ID instead of reserved attribute name.")); |
| 857 | + } |
| 858 | + |
| 859 | + [Test] |
| 860 | + public void TestGetAttributeIdWithInvalidAttributeKey() |
| 861 | + { |
| 862 | + // Verify that null is returned when provided attribute key is invalid. |
| 863 | + Assert.Null(Config.GetAttributeId("invalid_attribute")); |
| 864 | + LoggerMock.Verify(l => l.Log(LogLevel.ERROR, @"Attribute key ""invalid_attribute"" is not in datafile.")); |
| 865 | + } |
813 | 866 | }
|
814 | 867 | }
|
0 commit comments