Skip to content

Commit bdaef7f

Browse files
feat: Added ODPManager implementation (#322)
* Initial ODP Manager * Outline unit tests * WIP Unit tests + changes to CUD * Implement fetch, identify, and send events with Builder pattern * WIP Fixing final unit test * Finish unit tests * Add documentation * Lint fixes * Pull request code revisions + additional unit tests * Lint fixes * Change to use string array over List * Clean usings * Add new OdpConfig test * Small refactors * Pull request code review changes * Code review changes * Mark _odpConfig as volatile * Return `volatile` denotation
1 parent 3cd6745 commit bdaef7f

12 files changed

+879
-78
lines changed
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/*
2+
* Copyright 2022 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+
* https://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 NUnit.Framework;
18+
using OptimizelySDK.Odp;
19+
using System.Collections.Generic;
20+
21+
namespace OptimizelySDK.Tests.OdpTests
22+
{
23+
[TestFixture]
24+
public class OdpConfigTest
25+
{
26+
private const string API_KEY = "UrAp1k3Y";
27+
private const string API_HOST = "https://not-real-odp-host.example.com";
28+
29+
private static readonly List<string> segmentsToCheck = new List<string>
30+
{
31+
"UPPER-CASE-AUDIENCE",
32+
"lower-case-audience",
33+
};
34+
35+
private readonly OdpConfig _goodOdpConfig =
36+
new OdpConfig(API_KEY, API_HOST, segmentsToCheck);
37+
38+
[Test]
39+
public void ShouldNotEqualWithNullInApiKey()
40+
{
41+
var nullKeyConfig =
42+
new OdpConfig(null, API_HOST, segmentsToCheck);
43+
44+
Assert.IsFalse(_goodOdpConfig.Equals(nullKeyConfig));
45+
Assert.IsFalse(nullKeyConfig.Equals(_goodOdpConfig));
46+
}
47+
48+
[Test]
49+
public void ShouldNotEqualWithNullInApiHost()
50+
{
51+
var nullHostConfig =
52+
new OdpConfig(API_KEY, null, segmentsToCheck);
53+
54+
Assert.IsFalse(_goodOdpConfig.Equals(nullHostConfig));
55+
Assert.IsFalse(nullHostConfig.Equals(_goodOdpConfig));
56+
}
57+
58+
[Test]
59+
public void ShouldNotEqualWithNullSegmentsCollection()
60+
{
61+
var nullSegmentsConfig =
62+
new OdpConfig(API_KEY, API_HOST, null);
63+
64+
Assert.IsFalse(_goodOdpConfig.Equals(nullSegmentsConfig));
65+
Assert.IsFalse(nullSegmentsConfig.Equals(_goodOdpConfig));
66+
}
67+
68+
[Test]
69+
public void ShouldNotEqualWithSegmentsWithNull()
70+
{
71+
var segmentsWithANullValue =
72+
new OdpConfig(API_KEY, API_HOST, new List<string>
73+
{
74+
"good-value",
75+
null,
76+
});
77+
78+
Assert.IsFalse(_goodOdpConfig.Equals(segmentsWithANullValue));
79+
Assert.IsFalse(segmentsWithANullValue.Equals(_goodOdpConfig));
80+
}
81+
82+
[Test]
83+
public void ShouldNotEqualIfCaseDifferenceInApiKey()
84+
{
85+
const string CASE_DIFFERENCE_IN_FIRST_LETTER_OF_API_KEY = "urAp1k3Y";
86+
var apiKeyCaseDifferentConfig =
87+
new OdpConfig(CASE_DIFFERENCE_IN_FIRST_LETTER_OF_API_KEY, API_HOST,
88+
segmentsToCheck);
89+
90+
Assert.IsFalse(_goodOdpConfig.Equals(apiKeyCaseDifferentConfig));
91+
Assert.IsFalse(apiKeyCaseDifferentConfig.Equals(_goodOdpConfig));
92+
}
93+
94+
[Test]
95+
public void ShouldEqualDespiteCaseDifferenceInApiHost()
96+
{
97+
var apiHostUpperCasedConfig =
98+
new OdpConfig(API_KEY, API_HOST.ToUpper(), segmentsToCheck);
99+
100+
Assert.IsTrue(_goodOdpConfig.Equals(apiHostUpperCasedConfig));
101+
Assert.IsTrue(apiHostUpperCasedConfig.Equals(_goodOdpConfig));
102+
}
103+
104+
[Test]
105+
public void ShouldEqualDespiteCaseDifferenceInSegments()
106+
{
107+
var wrongCaseSegmentsToCheck = new List<string>
108+
{
109+
"upper-case-audience",
110+
"LOWER-CASE-AUDIENCE",
111+
};
112+
var wrongCaseConfig = new OdpConfig(API_KEY, API_HOST, wrongCaseSegmentsToCheck);
113+
114+
Assert.IsTrue(_goodOdpConfig.Equals(wrongCaseConfig));
115+
Assert.IsTrue(wrongCaseConfig.Equals(_goodOdpConfig));
116+
}
117+
}
118+
}

OptimizelySDK.Tests/OdpTests/OdpEventManagerTests.cs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -480,30 +480,22 @@ public void ShouldPrepareCorrectPayloadForIdentifyUser()
480480
[Test]
481481
public void ShouldApplyUpdatedOdpConfigurationWhenAvailable()
482482
{
483-
var apiKeyCollector = new List<string>();
484-
var apiHostCollector = new List<string>();
485-
var segmentsToCheckCollector = new List<List<string>>();
486483
var apiKey = "testing-api-key";
487484
var apiHost = "https://some.other.example.com";
488485
var segmentsToCheck = new List<string>
489486
{
490487
"empty-cart",
491488
"1-item-cart",
492489
};
493-
var mockOdpConfig = new Mock<OdpConfig>(API_KEY, API_HOST, segmentsToCheck);
494-
mockOdpConfig.Setup(m => m.Update(Capture.In(apiKeyCollector),
495-
Capture.In(apiHostCollector), Capture.In(segmentsToCheckCollector)));
496490
var differentOdpConfig = new OdpConfig(apiKey, apiHost, segmentsToCheck);
497-
var eventManager = new OdpEventManager.Builder().WithOdpConfig(mockOdpConfig.Object).
491+
var eventManager = new OdpEventManager.Builder().WithOdpConfig(_odpConfig).
498492
WithOdpEventApiManager(_mockApiManager.Object).
499493
WithLogger(_mockLogger.Object).
500494
Build();
501495

502496
eventManager.UpdateSettings(differentOdpConfig);
503497

504-
Assert.AreEqual(apiKey, apiKeyCollector[0]);
505-
Assert.AreEqual(apiHost, apiHostCollector[0]);
506-
Assert.AreEqual(segmentsToCheck, segmentsToCheckCollector[0]);
498+
Assert.IsFalse(_odpConfig.Equals(eventManager._readOdpConfigForTesting()));
507499
}
508500

509501
private static OdpEvent MakeEvent(int id) =>

0 commit comments

Comments
 (0)