11using System . Text . Json ;
22using FluentAssertions ;
33using FluentAssertions . Execution ;
4- using Microsoft . Extensions . Logging . Abstractions ;
54using OpenFeature . Constant ;
65using OpenFeature . Model ;
6+ using WireMock . RequestBuilders ;
7+ using WireMock . ResponseBuilders ;
8+ using WireMock . Server ;
79
8- namespace Octopus . OpenFeature . Provider . Tests ;
10+ namespace Octopus . OpenFeature . Provider . IntegrationTests ;
911
1012public class FixtureEvaluationTests
1113{
@@ -14,55 +16,70 @@ public static TheoryData<FixtureTestData> GetTestCases()
1416 var data = new TheoryData < FixtureTestData > ( ) ;
1517
1618 var fixtureDir = Path . Combine ( AppContext . BaseDirectory , "Fixtures" ) ;
17- foreach ( var file in Directory . GetFiles ( fixtureDir , "*.json" ) )
19+ foreach ( var dir in Directory . GetDirectories ( fixtureDir ) )
1820 {
19- var json = File . ReadAllText ( file ) ;
20- var fixture = JsonSerializer . Deserialize < FixtureFile > ( json , JsonSerializerOptions . Web )
21- ?? throw new InvalidOperationException ( $ "Failed to deserialize fixture file: { file } ") ;
21+ var togglesJson = File . ReadAllText ( Path . Combine ( dir , "toggles.json" ) ) ;
22+ var casesJson = File . ReadAllText ( Path . Combine ( dir , "cases.json" ) ) ;
23+ var cases = JsonSerializer . Deserialize < FixtureCase [ ] > ( casesJson , JsonSerializerOptions . Web )
24+ ?? throw new InvalidOperationException ( $ "Failed to deserialize cases in { dir } ") ;
2225
23- foreach ( var testCase in fixture . Cases )
26+ foreach ( var testCase in cases )
2427 {
25- data . Add ( new FixtureTestData ( fixture . Toggles , testCase ) ) ;
28+ data . Add ( new FixtureTestData ( togglesJson , testCase ) ) ;
2629 }
27-
2830 }
2931
3032 return data ;
3133 }
3234
3335 [ Theory ]
3436 [ MemberData ( nameof ( GetTestCases ) ) ]
35- public void Evaluate ( FixtureTestData testData )
37+ public async Task Evaluate ( FixtureTestData testData )
3638 {
37- var toggles = new FeatureToggles ( testData . Toggles , [ ] ) ;
38- var featureContext = new OctopusFeatureContext ( toggles , NullLoggerFactory . Instance ) ;
39+ using var server = WireMockServer . Start ( ) ;
40+
41+ server
42+ . Given ( Request . Create ( ) . WithPath ( "/api/featuretoggles/v3/" ) . UsingGet ( ) )
43+ . RespondWith ( Response . Create ( )
44+ . WithStatusCode ( 200 )
45+ . WithHeader ( "ContentHash" , Convert . ToBase64String ( [ 0x01 ] ) )
46+ . WithBody ( testData . TogglesJson ) ) ;
47+
48+ var configuration = new OctopusFeatureConfiguration ( "test-identifier" , new ProductMetadata ( "test-agent" ) )
49+ {
50+ ServerUri = new Uri ( server . Url ! )
51+ } ;
52+
53+ var provider = new OctopusFeatureProvider ( configuration ) ;
54+ await provider . InitializeAsync ( EvaluationContext . Empty ) ;
3955
4056 var evaluationContext = BuildContext ( testData . Case . Configuration . Context ) ;
4157
42- var result = featureContext . Evaluate (
58+ var result = await provider . ResolveBooleanValueAsync (
4359 testData . Case . Configuration . Slug ,
4460 testData . Case . Configuration . DefaultValue ,
4561 evaluationContext ) ;
4662
63+ await provider . ShutdownAsync ( ) ;
64+
4765 using var scope = new AssertionScope ( testData . Case . Description ) ;
4866 result . Value . Should ( ) . Be ( testData . Case . Expected . Value ) ;
4967
5068 if ( testData . Case . Expected . ErrorCode is { } errorCode )
5169 {
5270 result . ErrorType . Should ( ) . Be ( MapErrorCode ( errorCode ) ) ;
5371 }
54-
5572 else
5673 {
5774 result . ErrorType . Should ( ) . Be ( ErrorType . None ) ;
5875 }
59-
6076 }
6177
6278 static EvaluationContext ? BuildContext ( Dictionary < string , string > ? context )
6379 {
6480 if ( context is null )
6581 {
82+
6683 return null ;
6784 }
6885
@@ -85,11 +102,10 @@ public void Evaluate(FixtureTestData testData)
85102 } ;
86103}
87104
88- public class FixtureTestData ( FeatureToggleEvaluation [ ] toggles , FixtureCase @case )
105+ public class FixtureTestData ( string togglesJson , FixtureCase @case )
89106{
90- public FeatureToggleEvaluation [ ] Toggles { get ; } = toggles ;
107+ public string TogglesJson { get ; } = togglesJson ;
91108 public FixtureCase Case { get ; } = @case ;
92109
93- // Controls the name shown in Test Explorer for each theory row
94110 public override string ToString ( ) => Case . Description ;
95111}
0 commit comments