6
6
using Microsoft . Extensions . Logging ;
7
7
using Microsoft . Extensions . Logging . Abstractions ;
8
8
using Moq ;
9
+ using Newtonsoft . Json . Linq ;
9
10
using Xunit ;
10
11
using FuncDescriptor = Microsoft . Azure . WebJobs . Host . Protocols . FunctionDescriptor ;
11
12
@@ -16,14 +17,21 @@ public class FunctionGroupListenerDecoratorTests
16
17
private readonly ILogger < FunctionGroupListenerDecorator > _logger
17
18
= NullLogger < FunctionGroupListenerDecorator > . Instance ;
18
19
20
+ private enum FuncGroup
21
+ {
22
+ None ,
23
+ Http ,
24
+ Other ,
25
+ }
26
+
19
27
[ Fact ]
20
28
public void Decorate_NoTargetGroupConfigured_ReturnsOriginalListener ( )
21
29
{
22
30
// Arrange
23
31
IFunctionDefinition definition = Mock . Of < IFunctionDefinition > ( ) ;
24
32
IListener original = Mock . Of < IListener > ( ) ;
25
33
IFunctionMetadataManager metadata = Mock . Of < IFunctionMetadataManager > ( ) ;
26
- IEnvironment environment = CreateEnvironment ( null ) ;
34
+ IEnvironment environment = CreateEnvironment ( FuncGroup . None ) ;
27
35
28
36
var context = new ListenerDecoratorContext ( definition , original . GetType ( ) , original ) ;
29
37
var decorator = new FunctionGroupListenerDecorator ( metadata , environment , _logger ) ;
@@ -42,7 +50,7 @@ public void Decorate_MetadataNotFound_ReturnsOriginalListener()
42
50
IFunctionDefinition definition = CreateDefinition ( "test" ) ;
43
51
IListener original = Mock . Of < IListener > ( ) ;
44
52
IFunctionMetadataManager metadata = Mock . Of < IFunctionMetadataManager > ( ) ;
45
- IEnvironment environment = CreateEnvironment ( "test-group" ) ;
53
+ IEnvironment environment = CreateEnvironment ( FuncGroup . Http ) ;
46
54
47
55
var context = new ListenerDecoratorContext ( definition , original . GetType ( ) , original ) ;
48
56
var decorator = new FunctionGroupListenerDecorator ( metadata , environment , _logger ) ;
@@ -60,8 +68,8 @@ public void Decorate_GroupMatch_ReturnsOriginalListener()
60
68
// Arrange
61
69
IFunctionDefinition definition = CreateDefinition ( "test" ) ;
62
70
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 ) ;
65
73
66
74
var context = new ListenerDecoratorContext ( definition , original . GetType ( ) , original ) ;
67
75
var decorator = new FunctionGroupListenerDecorator ( metadata , environment , _logger ) ;
@@ -79,8 +87,8 @@ public void Decorate_GroupDoesNotMatch_ReturnsNoOpListener()
79
87
// Arrange
80
88
IFunctionDefinition definition = CreateDefinition ( "test" ) ;
81
89
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 ) ;
84
92
85
93
var context = new ListenerDecoratorContext ( definition , original . GetType ( ) , original ) ;
86
94
var decorator = new FunctionGroupListenerDecorator ( metadata , environment , _logger ) ;
@@ -98,22 +106,45 @@ private static IFunctionDefinition CreateDefinition(string name)
98
106
return Mock . Of < IFunctionDefinition > ( m => m . Descriptor == descriptor ) ;
99
107
}
100
108
101
- private static IFunctionMetadataManager CreateMetadataManager ( string name , string group )
109
+ private static IFunctionMetadataManager CreateMetadataManager ( string name , bool group )
102
110
{
111
+ string trigger = group ? "httpTrigger" : "otherTrigger" ;
103
112
var metadata = new FunctionMetadata ( )
104
113
{
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
+ }
106
130
} ;
107
131
108
132
var mock = new Mock < IFunctionMetadataManager > ( ) ;
109
133
mock . Setup ( p => p . TryGetFunctionMetadata ( name , out metadata , false ) ) . Returns ( true ) ;
110
134
return mock . Object ;
111
135
}
112
136
113
- private static IEnvironment CreateEnvironment ( string group )
137
+ private static IEnvironment CreateEnvironment ( FuncGroup group )
114
138
{
139
+ string groupStr = group switch
140
+ {
141
+ FuncGroup . Http => "http" ,
142
+ FuncGroup . Other => "other" ,
143
+ _ => null ,
144
+ } ;
145
+
115
146
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 ) ;
117
148
return environment . Object ;
118
149
}
119
150
}
0 commit comments