Skip to content

Commit 7ef0735

Browse files
author
y.vanov
committed
updated: sms tests to use mock impl
updated: default rule for using
1 parent 3d61721 commit 7ef0735

File tree

2 files changed

+92
-44
lines changed

2 files changed

+92
-44
lines changed

Mailjet.Client/UrlHelper.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
namespace Mailjet.Client
2-
{
3-
using System.Collections.Generic;
4-
using System.Linq;
5-
using System.Net;
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
using System.Net;
64

5+
namespace Mailjet.Client
6+
{
77
public class UrlHelper
88
{
99
public static string CombineUrl(string url1, string url2)

Mailjet.Tests/MailjetClientTests.cs

Lines changed: 87 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1+
using Mailjet.Client;
2+
using Mailjet.Client.Resources;
3+
using Microsoft.VisualStudio.TestTools.UnitTesting;
4+
using Newtonsoft.Json.Linq;
5+
using RichardSzalay.MockHttp;
6+
using System;
7+
using sms = Mailjet.Client.Resources.SMS;
8+
19
namespace Mailjet.Tests
210
{
3-
using Mailjet.Client;
4-
using Mailjet.Client.Resources;
5-
using Microsoft.VisualStudio.TestTools.UnitTesting;
6-
using Newtonsoft.Json.Linq;
7-
using RichardSzalay.MockHttp;
8-
using sms = Mailjet.Client.Resources.SMS;
911

1012
[TestClass]
1113
public class MailjetClientTests
@@ -16,6 +18,18 @@ public class MailjetClientTests
1618
private const string TotalKey = "Total";
1719
private const string CountKey = "Count";
1820
private const string DataKey = "Data";
21+
private const string Status = "Status";
22+
private const string Code = "Code";
23+
private const string Name = "Name";
24+
private const string Description = "Description";
25+
26+
private string API_TOKEN;
27+
28+
[TestInitialize]
29+
public void TestInit()
30+
{
31+
API_TOKEN = "ApiToken";
32+
}
1933

2034
[TestMethod]
2135
public void TestGetAsync()
@@ -56,52 +70,70 @@ public void TestGetAsync()
5670
}
5771

5872
[TestMethod]
59-
public void TestSmsSendAsync()
73+
public void TestSmsCountAsync()
6074
{
61-
IMailjetClient client = new MailjetClient("ApiToken")
62-
{
63-
Version = ApiVersion.V4
64-
};
75+
int expectedTotal = 1;
76+
int expectedCount = 1;
77+
var expectedData = new JArray();
6578

66-
MailjetRequest request = new MailjetRequest
67-
{
68-
Resource = sms.Send.Resource
69-
}
70-
.Property(sms.Send.From, "MJPilot")
71-
.Property(sms.Send.To, "+33000000")
72-
.Property(sms.Send.Text, "Demo");
79+
var mockHttp = new MockHttpMessageHandler();
7380

74-
MailjetResponse response = client.PostAsync(request).Result;
81+
var jsonResponse = GenerateJsonResponse(expectedTotal, expectedCount, expectedData);
7582

76-
}
83+
// Setup a respond for the user api (including a wildcard in the URL)
84+
mockHttp.When("https://api.mailjet.com/v4/*")
85+
.Respond(JsonMediaType, jsonResponse); // Respond with JSON
7786

78-
[TestMethod]
79-
public void TestSmsCountAsync()
80-
{
81-
IMailjetClient client = new MailjetClient("ApiToken")
87+
IMailjetClient client = new MailjetClient(API_TOKEN, mockHttp)
8288
{
8389
Version = ApiVersion.V4
8490
};
8591

8692

87-
8893
MailjetRequest request = new MailjetRequest
8994
{
9095
Resource = sms.Count.Resource
9196
}
92-
.Filter(sms.Count.FromTS, "1033552800")
93-
.Filter(sms.Count.ToTS, "1033574400");
97+
.Filter(sms.Count.FromTS, DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().ToString())
98+
.Filter(sms.Count.ToTS, DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().ToString());
9499

95-
MailjetResponse result = client.GetAsync(request).Result;
96100

97-
Assert.IsTrue(result.IsSuccessStatusCode);
101+
MailjetResponse response = client.GetAsync(request).Result;
98102

103+
Assert.IsTrue(response.IsSuccessStatusCode);
104+
Assert.AreEqual(expectedTotal, response.GetTotal());
105+
Assert.AreEqual(expectedCount, response.GetCount());
106+
Assert.IsTrue(JToken.DeepEquals(expectedData, response.GetData()));
99107
}
100108

101109
[TestMethod]
102110
public void TestSmsExportAsync()
103111
{
104-
IMailjetClient client = new MailjetClient("ApiToken")
112+
int expectedCode = 1;
113+
string expectedName = "PENDING";
114+
string expectedDescription = "The request is accepted.";
115+
116+
var status = new JObject
117+
{
118+
{ Code, expectedCode},
119+
{ Name, expectedName},
120+
{ Description, expectedDescription}
121+
};
122+
123+
var smsExportResponse = new JObject
124+
{
125+
{ Status, status }
126+
};
127+
128+
var mockHttp = new MockHttpMessageHandler();
129+
// Setup a respond for the user api (including a wildcard in the URL)
130+
mockHttp.When("https://api.mailjet.com/v4/*")
131+
.Respond(JsonMediaType, GenerateJsonResponse(smsExportResponse)); // Respond with JSON
132+
133+
// timsestamp range offset
134+
int offset = 1000;
135+
136+
IMailjetClient client = new MailjetClient(API_TOKEN, mockHttp)
105137
{
106138
Version = ApiVersion.V4
107139
};
@@ -110,20 +142,30 @@ public void TestSmsExportAsync()
110142
{
111143
Resource = sms.Export.Resource
112144
}
113-
.Property(sms.Export.FromTS, 1527084481)
114-
.Property(sms.Export.ToTS, 1527085481);
145+
.Property(sms.Export.FromTS, DateTimeOffset.UtcNow.ToUnixTimeMilliseconds())
146+
.Property(sms.Export.ToTS, DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() + offset);
115147

116148

117149
MailjetResponse response = client.PostAsync(request).Result;
118150

119-
120151
Assert.IsTrue(response.IsSuccessStatusCode);
152+
Assert.AreEqual(expectedCode, response.GetData()[0][Status].Value<int>(Code));
153+
Assert.AreEqual(expectedName, response.GetData()[0][Status].Value<string>(Name));
154+
Assert.AreEqual(expectedDescription, response.GetData()[0][Status].Value<string>(Description));
121155
}
122156

123157
[TestMethod]
124-
public void TestSmsAsync()
158+
public void TestSmsStatisticsAsync()
125159
{
126-
IMailjetClient client = new MailjetClient("ApiToken")
160+
var expectedData = new JArray();
161+
var mockHttp = new MockHttpMessageHandler();
162+
var jsonResponse = GenerateJsonResponse(1, 1, expectedData);
163+
164+
// Setup a respond for the user api (including a wildcard in the URL)
165+
mockHttp.When("https://api.mailjet.com/v4/*")
166+
.Respond(JsonMediaType, jsonResponse); // Respond with JSON
167+
168+
IMailjetClient client = new MailjetClient(API_TOKEN, mockHttp)
127169
{
128170
Version = ApiVersion.V4
129171
};
@@ -132,12 +174,13 @@ public void TestSmsAsync()
132174
{
133175
Resource = sms.SMS.Resource
134176
}
135-
.Filter(sms.SMS.FromTS, 1527084481)
136-
.Filter(sms.SMS.ToTS, 1527085481);
177+
.Filter(sms.SMS.FromTS, DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().ToString())
178+
.Filter(sms.SMS.ToTS, DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().ToString());
137179

138-
MailjetResponse result = client.GetAsync(request).Result;
180+
MailjetResponse response = client.GetAsync(request).Result;
139181

140-
Assert.IsTrue(result.IsSuccessStatusCode);
182+
Assert.IsTrue(response.IsSuccessStatusCode);
183+
Assert.IsTrue(JToken.DeepEquals(expectedData, response.GetData()));
141184
}
142185

143186
private string GenerateJsonResponse(int total, int count, JArray data)
@@ -149,6 +192,11 @@ private string GenerateJsonResponse(int total, int count, JArray data)
149192
{ DataKey, data },
150193
};
151194

195+
return GenerateJsonResponse(jObject);
196+
}
197+
198+
private string GenerateJsonResponse(JObject jObject)
199+
{
152200
return jObject.ToString(Newtonsoft.Json.Formatting.None);
153201
}
154202
}

0 commit comments

Comments
 (0)