66using Microsoft . Extensions . Logging ;
77using Microsoft . Extensions . Logging . Abstractions ;
88using Moq ;
9+ using Newtonsoft . Json . Linq ;
910using Xunit ;
1011using FuncDescriptor = Microsoft . Azure . WebJobs . Host . Protocols . FunctionDescriptor ;
1112
@@ -16,14 +17,21 @@ public class FunctionGroupListenerDecoratorTests
1617 private readonly ILogger < FunctionGroupListenerDecorator > _logger
1718 = NullLogger < FunctionGroupListenerDecorator > . Instance ;
1819
20+ private enum FuncGroup
21+ {
22+ None ,
23+ Http ,
24+ Other ,
25+ }
26+
1927 [ Fact ]
2028 public void Decorate_NoTargetGroupConfigured_ReturnsOriginalListener ( )
2129 {
2230 // Arrange
2331 IFunctionDefinition definition = Mock . Of < IFunctionDefinition > ( ) ;
2432 IListener original = Mock . Of < IListener > ( ) ;
2533 IFunctionMetadataManager metadata = Mock . Of < IFunctionMetadataManager > ( ) ;
26- IEnvironment environment = CreateEnvironment ( null ) ;
34+ IEnvironment environment = CreateEnvironment ( FuncGroup . None ) ;
2735
2836 var context = new ListenerDecoratorContext ( definition , original . GetType ( ) , original ) ;
2937 var decorator = new FunctionGroupListenerDecorator ( metadata , environment , _logger ) ;
@@ -42,7 +50,7 @@ public void Decorate_MetadataNotFound_ReturnsOriginalListener()
4250 IFunctionDefinition definition = CreateDefinition ( "test" ) ;
4351 IListener original = Mock . Of < IListener > ( ) ;
4452 IFunctionMetadataManager metadata = Mock . Of < IFunctionMetadataManager > ( ) ;
45- IEnvironment environment = CreateEnvironment ( "test-group" ) ;
53+ IEnvironment environment = CreateEnvironment ( FuncGroup . Http ) ;
4654
4755 var context = new ListenerDecoratorContext ( definition , original . GetType ( ) , original ) ;
4856 var decorator = new FunctionGroupListenerDecorator ( metadata , environment , _logger ) ;
@@ -60,8 +68,8 @@ public void Decorate_GroupMatch_ReturnsOriginalListener()
6068 // Arrange
6169 IFunctionDefinition definition = CreateDefinition ( "test" ) ;
6270 IListener original = Mock . Of < IListener > ( ) ;
63- IFunctionMetadataManager metadata = CreateMetadataManager ( "test" , "test-group" ) ;
64- IEnvironment environment = CreateEnvironment ( "test-group" ) ;
71+ IFunctionMetadataManager metadata = CreateMetadataManager ( "test" , true ) ;
72+ IEnvironment environment = CreateEnvironment ( FuncGroup . Http ) ;
6573
6674 var context = new ListenerDecoratorContext ( definition , original . GetType ( ) , original ) ;
6775 var decorator = new FunctionGroupListenerDecorator ( metadata , environment , _logger ) ;
@@ -79,8 +87,8 @@ public void Decorate_GroupDoesNotMatch_ReturnsNoOpListener()
7987 // Arrange
8088 IFunctionDefinition definition = CreateDefinition ( "test" ) ;
8189 IListener original = Mock . Of < IListener > ( ) ;
82- IFunctionMetadataManager metadata = CreateMetadataManager ( "test" , "test-group" ) ;
83- IEnvironment environment = CreateEnvironment ( "other-group" ) ;
90+ IFunctionMetadataManager metadata = CreateMetadataManager ( "test" , true ) ;
91+ IEnvironment environment = CreateEnvironment ( FuncGroup . Other ) ;
8492
8593 var context = new ListenerDecoratorContext ( definition , original . GetType ( ) , original ) ;
8694 var decorator = new FunctionGroupListenerDecorator ( metadata , environment , _logger ) ;
@@ -98,22 +106,45 @@ private static IFunctionDefinition CreateDefinition(string name)
98106 return Mock . Of < IFunctionDefinition > ( m => m . Descriptor == descriptor ) ;
99107 }
100108
101- private static IFunctionMetadataManager CreateMetadataManager ( string name , string group )
109+ private static IFunctionMetadataManager CreateMetadataManager ( string name , bool group )
102110 {
111+ string trigger = group ? "httpTrigger" : "otherTrigger" ;
103112 var metadata = new FunctionMetadata ( )
104113 {
105- Properties = { [ "FunctionGroup" ] = group } ,
114+ Name = "TestFunction1" ,
115+ Bindings =
116+ {
117+ new BindingMetadata
118+ {
119+ Name = "input" ,
120+ Type = trigger ,
121+ Direction = BindingDirection . In ,
122+ Raw = new JObject ( )
123+ {
124+ [ "name" ] = "input" ,
125+ [ "type" ] = trigger ,
126+ [ "direction" ] = "in" ,
127+ } ,
128+ }
129+ }
106130 } ;
107131
108132 var mock = new Mock < IFunctionMetadataManager > ( ) ;
109133 mock . Setup ( p => p . TryGetFunctionMetadata ( name , out metadata , false ) ) . Returns ( true ) ;
110134 return mock . Object ;
111135 }
112136
113- private static IEnvironment CreateEnvironment ( string group )
137+ private static IEnvironment CreateEnvironment ( FuncGroup group )
114138 {
139+ string groupStr = group switch
140+ {
141+ FuncGroup . Http => "http" ,
142+ FuncGroup . Other => "other" ,
143+ _ => null ,
144+ } ;
145+
115146 var environment = new Mock < IEnvironment > ( MockBehavior . Strict ) ;
116- environment . Setup ( p => p . GetEnvironmentVariable ( EnvironmentSettingNames . FunctionsTargetGroup ) ) . Returns ( group ) ;
147+ environment . Setup ( p => p . GetEnvironmentVariable ( EnvironmentSettingNames . FunctionsTargetGroup ) ) . Returns ( groupStr ) ;
117148 return environment . Object ;
118149 }
119150 }
0 commit comments