generated from Avanade/avanade-template
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathServiceBusFunctionTest.cs
165 lines (138 loc) · 6.88 KB
/
ServiceBusFunctionTest.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Text.Json;
using UnitTestEx.Function;
using UnitTestEx.Json;
namespace UnitTestEx.NUnit.Test
{
[TestFixture]
public class ServiceBusFunctionTest
{
[Test]
public void Object_Success()
{
var mcf = MockHttpClientFactory.Create();
mcf.CreateClient("XXX", new Uri("https://somesys"))
.Request(HttpMethod.Post, "person").WithJsonBody(new { firstName = "Bob", lastName = "Smith" }).Respond.With(HttpStatusCode.OK);
using var test = FunctionTester.Create<Startup>();
test.ReplaceHttpClientFactory(mcf)
.ServiceBusTrigger<ServiceBusFunction>()
.Run(f => f.Run(new Person { FirstName = "Bob", LastName = "Smith" }, test.Logger))
.AssertSuccess();
mcf.VerifyAll();
}
[Test]
public void Object_HttpClientError()
{
var mcf = MockHttpClientFactory.Create().UseJsonComparerOptions(new JsonElementComparerOptions { NullComparison = JsonElementComparison.Semantic });
mcf.CreateClient("XXX", new Uri("https://somesys"))
.Request(HttpMethod.Post, "person").WithJsonBody(new { firstName = "Bob", lastName = (string)null }).Respond.With(HttpStatusCode.InternalServerError);
using var test = FunctionTester.Create<Startup>();
test.ReplaceHttpClientFactory(mcf)
.Type<ServiceBusFunction>()
.Run(f => f.Run(new Person { FirstName = "Bob", LastName = (string)null }, test.Logger))
.AssertException<HttpRequestException>("Response status code does not indicate success: 500 (Internal Server Error).");
mcf.VerifyAll();
}
[Test]
public void Object_ThrowsException()
{
var mcf = MockHttpClientFactory.Create();
using var test = FunctionTester.Create<Startup>();
test.ReplaceHttpClientFactory(mcf)
.ServiceBusTrigger<ServiceBusFunction>()
.Run(f => f.Run(new Person { FirstName = null, LastName = "Smith" }, test.Logger))
.AssertException<InvalidOperationException>("First name is required.");
mcf.VerifyAll();
}
[Test]
public void ServiceBusMessage_Success()
{
var mcf = MockHttpClientFactory.Create();
mcf.CreateClient("XXX", new Uri("https://somesys"))
.Request(HttpMethod.Post, "person").WithJsonBody(new { firstName = "Bob", lastName = "Smith" }).Respond.With(HttpStatusCode.OK);
using var test = FunctionTester.Create<Startup>();
test.ReplaceHttpClientFactory(mcf)
.ServiceBusTrigger<ServiceBusFunction>()
.Run(f => f.Run2(test.CreateServiceBusMessageFromValue(new Person { FirstName = "Bob", LastName = "Smith" }), test.Logger))
.AssertSuccess();
mcf.VerifyAll();
}
[Test]
public void ServiceBusMessage_HttpClientError()
{
var mcf = MockHttpClientFactory.Create().UseJsonSerializer(new Json.JsonSerializer(new JsonSerializerOptions(JsonSerializerDefaults.Web)));
mcf.CreateClient("XXX", new Uri("https://somesys"))
.Request(HttpMethod.Post, "person").WithJsonBody(new { firstName = "Bob", lastName = (string)null }).Respond.With(HttpStatusCode.InternalServerError);
using var test = FunctionTester.Create<Startup>();
var r = test.ReplaceHttpClientFactory(mcf)
.ServiceBusTrigger<ServiceBusFunction>()
.Run(f => f.Run2(test.CreateServiceBusMessageFromValue(new Person { FirstName = "Bob", LastName = (string)null }), test.Logger))
.AssertException<HttpRequestException>("Response status code does not indicate success: 500 (Internal Server Error).");
mcf.VerifyAll();
}
[Test]
public void ServiceBusMessage_ThrowsException()
{
var mcf = MockHttpClientFactory.Create();
using var test = FunctionTester.Create<Startup>();
test.ReplaceHttpClientFactory(mcf)
.ServiceBusTrigger<ServiceBusFunction>()
.Run(f => f.Run2(test.CreateServiceBusMessageFromValue(new Person { FirstName = null, LastName = "Smith" }), test.Logger))
.AssertException<InvalidOperationException>("First name is required.");
mcf.VerifyAll();
}
[Test]
public void ServiceBusMessage_AssertActions()
{
using var test = FunctionTester.Create<Startup>();
var msg = test.CreateServiceBusMessageFromValue(new Person { FirstName = null, LastName = "Smith" });
var act = test.CreateWebJobsServiceBusMessageActions();
test.ServiceBusTrigger<ServiceBusFunction>()
.Run(f => f.Run3(msg, act, test.Logger))
.AssertSuccess();
act.AssertDeadLetter("Validation error", "First name is required");
}
[Test]
public void ServiceProvider()
{
var mcf = MockHttpClientFactory.Create();
mcf.CreateClient("XXX", new Uri("https://somesys")).Request(HttpMethod.Get, "test").Respond.With("test output");
using var test = FunctionTester.Create<Startup>();
var hc = test.ReplaceHttpClientFactory(mcf)
.Services.GetService<IHttpClientFactory>().CreateClient("XXX");
var r = hc.GetAsync("test").Result;
Assert.That(r, Is.Not.Null);
Assert.That(r.Content.ReadAsStringAsync().Result, Is.EqualTo("test output"));
}
[Test]
public void Configuration()
{
using var test = FunctionTester.Create<Startup>();
var cv = test.Configuration.GetValue<string>("SpecialKey");
Assert.That(cv, Is.EqualTo("VerySpecialValue"));
}
[Test]
public void Configuration_Overrride()
{
// Demonstrates how to override the configuration settings for a test.
using var test = FunctionTester.Create<Startup>(additionalConfiguration: [new("SpecialKey", "NotSoSpecial")]);
var cv = test.Configuration.GetValue<string>("SpecialKey");
Assert.That(cv, Is.EqualTo("NotSoSpecial"));
}
[Test]
public void Configuration_Overrride_Use()
{
// Demonstrates how to override the configuration settings for a test.
using var test = FunctionTester.Create<Startup>();
test.UseAdditionalConfiguration([new("SpecialKey", "NotSoSpecial")]);
var cv = test.Configuration.GetValue<string>("SpecialKey");
Assert.That(cv, Is.EqualTo("NotSoSpecial"));
}
}
}