-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathServiceBusFunctionTest.cs
More file actions
133 lines (111 loc) · 5.61 KB
/
ServiceBusFunctionTest.cs
File metadata and controls
133 lines (111 loc) · 5.61 KB
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
using Azure.Messaging.ServiceBus;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Net;
using System.Net.Http;
using System.Text.Json;
using UnitTestEx.Function;
using UnitTestEx.Json;
namespace UnitTestEx.MSTest.Test
{
[TestClass]
public class ServiceBusFunctionTest
{
[TestMethod]
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();
}
[TestMethod]
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>();
var r = 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();
}
[TestMethod]
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();
}
[TestMethod]
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();
}
[TestMethod]
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>();
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();
}
[TestMethod]
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();
}
[TestMethod]
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.IsNotNull(r);
Assert.AreEqual("test output", r.Content.ReadAsStringAsync().Result);
}
[TestMethod]
public void CreateServiceBusMessage()
{
using var test = FunctionTester.Create<Startup>();
var sbrm = test.CreateServiceBusMessage(new ServiceBusMessage() { Subject = "xxx" });
Assert.IsNotNull(sbrm);
Assert.IsNotNull(sbrm.Subject);
Assert.AreEqual("xxx", sbrm.Subject);
}
}
}