Skip to content

Commit

Permalink
feat: Added ODPManager implementation (#322)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
mikechu-optimizely authored Dec 7, 2022
1 parent 3cd6745 commit bdaef7f
Show file tree
Hide file tree
Showing 12 changed files with 879 additions and 78 deletions.
118 changes: 118 additions & 0 deletions OptimizelySDK.Tests/OdpTests/OdpConfigTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/*
* Copyright 2022 Optimizely
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

using NUnit.Framework;
using OptimizelySDK.Odp;
using System.Collections.Generic;

namespace OptimizelySDK.Tests.OdpTests
{
[TestFixture]
public class OdpConfigTest
{
private const string API_KEY = "UrAp1k3Y";
private const string API_HOST = "https://not-real-odp-host.example.com";

private static readonly List<string> segmentsToCheck = new List<string>
{
"UPPER-CASE-AUDIENCE",
"lower-case-audience",
};

private readonly OdpConfig _goodOdpConfig =
new OdpConfig(API_KEY, API_HOST, segmentsToCheck);

[Test]
public void ShouldNotEqualWithNullInApiKey()
{
var nullKeyConfig =
new OdpConfig(null, API_HOST, segmentsToCheck);

Assert.IsFalse(_goodOdpConfig.Equals(nullKeyConfig));
Assert.IsFalse(nullKeyConfig.Equals(_goodOdpConfig));
}

[Test]
public void ShouldNotEqualWithNullInApiHost()
{
var nullHostConfig =
new OdpConfig(API_KEY, null, segmentsToCheck);

Assert.IsFalse(_goodOdpConfig.Equals(nullHostConfig));
Assert.IsFalse(nullHostConfig.Equals(_goodOdpConfig));
}

[Test]
public void ShouldNotEqualWithNullSegmentsCollection()
{
var nullSegmentsConfig =
new OdpConfig(API_KEY, API_HOST, null);

Assert.IsFalse(_goodOdpConfig.Equals(nullSegmentsConfig));
Assert.IsFalse(nullSegmentsConfig.Equals(_goodOdpConfig));
}

[Test]
public void ShouldNotEqualWithSegmentsWithNull()
{
var segmentsWithANullValue =
new OdpConfig(API_KEY, API_HOST, new List<string>
{
"good-value",
null,
});

Assert.IsFalse(_goodOdpConfig.Equals(segmentsWithANullValue));
Assert.IsFalse(segmentsWithANullValue.Equals(_goodOdpConfig));
}

[Test]
public void ShouldNotEqualIfCaseDifferenceInApiKey()
{
const string CASE_DIFFERENCE_IN_FIRST_LETTER_OF_API_KEY = "urAp1k3Y";
var apiKeyCaseDifferentConfig =
new OdpConfig(CASE_DIFFERENCE_IN_FIRST_LETTER_OF_API_KEY, API_HOST,
segmentsToCheck);

Assert.IsFalse(_goodOdpConfig.Equals(apiKeyCaseDifferentConfig));
Assert.IsFalse(apiKeyCaseDifferentConfig.Equals(_goodOdpConfig));
}

[Test]
public void ShouldEqualDespiteCaseDifferenceInApiHost()
{
var apiHostUpperCasedConfig =
new OdpConfig(API_KEY, API_HOST.ToUpper(), segmentsToCheck);

Assert.IsTrue(_goodOdpConfig.Equals(apiHostUpperCasedConfig));
Assert.IsTrue(apiHostUpperCasedConfig.Equals(_goodOdpConfig));
}

[Test]
public void ShouldEqualDespiteCaseDifferenceInSegments()
{
var wrongCaseSegmentsToCheck = new List<string>
{
"upper-case-audience",
"LOWER-CASE-AUDIENCE",
};
var wrongCaseConfig = new OdpConfig(API_KEY, API_HOST, wrongCaseSegmentsToCheck);

Assert.IsTrue(_goodOdpConfig.Equals(wrongCaseConfig));
Assert.IsTrue(wrongCaseConfig.Equals(_goodOdpConfig));
}
}
}
12 changes: 2 additions & 10 deletions OptimizelySDK.Tests/OdpTests/OdpEventManagerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -480,30 +480,22 @@ public void ShouldPrepareCorrectPayloadForIdentifyUser()
[Test]
public void ShouldApplyUpdatedOdpConfigurationWhenAvailable()
{
var apiKeyCollector = new List<string>();
var apiHostCollector = new List<string>();
var segmentsToCheckCollector = new List<List<string>>();
var apiKey = "testing-api-key";
var apiHost = "https://some.other.example.com";
var segmentsToCheck = new List<string>
{
"empty-cart",
"1-item-cart",
};
var mockOdpConfig = new Mock<OdpConfig>(API_KEY, API_HOST, segmentsToCheck);
mockOdpConfig.Setup(m => m.Update(Capture.In(apiKeyCollector),
Capture.In(apiHostCollector), Capture.In(segmentsToCheckCollector)));
var differentOdpConfig = new OdpConfig(apiKey, apiHost, segmentsToCheck);
var eventManager = new OdpEventManager.Builder().WithOdpConfig(mockOdpConfig.Object).
var eventManager = new OdpEventManager.Builder().WithOdpConfig(_odpConfig).
WithOdpEventApiManager(_mockApiManager.Object).
WithLogger(_mockLogger.Object).
Build();

eventManager.UpdateSettings(differentOdpConfig);

Assert.AreEqual(apiKey, apiKeyCollector[0]);
Assert.AreEqual(apiHost, apiHostCollector[0]);
Assert.AreEqual(segmentsToCheck, segmentsToCheckCollector[0]);
Assert.IsFalse(_odpConfig.Equals(eventManager._readOdpConfigForTesting()));
}

private static OdpEvent MakeEvent(int id) =>
Expand Down
Loading

0 comments on commit bdaef7f

Please sign in to comment.