-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSmartyTest.cs
More file actions
212 lines (174 loc) · 11 KB
/
SmartyTest.cs
File metadata and controls
212 lines (174 loc) · 11 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
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
using CoreZipCode.Services.ZipCode.SmartyApi;
using Moq;
using Moq.Protected;
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Xunit;
namespace CoreZipCode.Tests.Services.ZipCode.SmartyApi
{
public class SmartyTest
{
private const string ExpectedZipcodeResponse = "[{\"input_index\":0,\"city_states\":[{\"city\":\"Cupertino\",\"state_abbreviation\":\"CA\",\"state\":\"California\",\"mailable_city\":true},{\"city\":\"Monte Vista\",\"state_abbreviation\":\"CA\",\"state\":\"California\",\"mailable_city\":true},{\"city\":\"Permanente\",\"state_abbreviation\":\"CA\",\"state\":\"California\",\"mailable_city\":true}],\"zipcodes\":[{\"zipcode\":\"95014\",\"zipcode_type\":\"S\",\"default_city\":\"Cupertino\",\"county_fips\":\"06085\",\"county_name\":\"Santa Clara\",\"state_abbreviation\":\"CA\",\"state\":\"California\",\"latitude\":37.32098,\"longitude\":-122.03838,\"precision\":\"Zip5\"}]}]";
private const string ExpectedParamResponse = "[{\"input_index\":0,\"candidate_index\":0,\"delivery_line_1\":\"1600 Amphitheatre Pkwy\",\"last_line\":\"Mountain View CA 94043-1351\",\"delivery_point_barcode\":\"940431351000\",\"components\":{\"primary_number\":\"1600\",\"street_name\":\"Amphitheatre\",\"street_suffix\":\"Pkwy\",\"city_name\":\"Mountain View\",\"default_city_name\":\"Mountain View\",\"state_abbreviation\":\"CA\",\"zipcode\":\"94043\",\"plus4_code\":\"1351\",\"delivery_point\":\"00\",\"delivery_point_check_digit\":\"0\"},\"metadata\":{\"record_type\":\"S\",\"zip_type\":\"Standard\",\"county_fips\":\"06085\",\"county_name\":\"Santa Clara\",\"carrier_route\":\"C909\",\"congressional_district\":\"18\",\"rdi\":\"Commercial\",\"elot_sequence\":\"0094\",\"elot_sort\":\"A\",\"latitude\":37.42357,\"longitude\":-122.08661,\"precision\":\"Zip9\",\"time_zone\":\"Pacific\",\"utc_offset\":-8,\"dst\":true},\"analysis\":{\"dpv_match_code\":\"Y\",\"dpv_footnotes\":\"AABB\",\"dpv_cmra\":\"N\",\"dpv_vacant\":\"N\",\"active\":\"N\"}}]";
private const string ExpectedState = "CA";
private const string ExpectedCity = "Cupertino";
private const string ZipCodeTest = "95014";
private const string SendAsync = "SendAsync";
private const string MockUri = "https://unit.test.com/";
private const string SmartyParameterState = "CA";
private const string SmartyParameterCity = "Mountain View";
private const string SmartyParameterStreet = "1600 Amphitheatre Pkwy";
private const string InvalidStreetMessage = "Invalid Street, parameter over size of 64 characters.";
private const string InvalidCityMessage = "Invalid City, parameter over size of 64 characters.";
private const string InvalidStateMessage = "Invalid State, parameter over size of 32 characters.";
private const string InvalidZipCodeFormatMessage = "Invalid ZipCode Format";
private const string InvalidZipCodeSizeMessage = "Invalid ZipCode Size";
private const string AuthToken = "some auth token";
private const string AuthId = "some auth id";
private readonly Smarty _service;
private readonly Smarty _serviceParam;
public SmartyTest()
{
_service = ConfigureService(ExpectedZipcodeResponse);
_serviceParam = ConfigureService(ExpectedParamResponse);
}
private static Smarty ConfigureService(string response)
{
var handlerMock = new Mock<HttpMessageHandler>();
handlerMock
.Protected()
.Setup<Task<HttpResponseMessage>>(
SendAsync,
ItExpr.IsAny<HttpRequestMessage>(),
ItExpr.IsAny<CancellationToken>()
)
.ReturnsAsync(new HttpResponseMessage
{
StatusCode = HttpStatusCode.OK,
Content = new StringContent(response),
})
.Verifiable();
var httpClient = new HttpClient(handlerMock.Object)
{
BaseAddress = new Uri(MockUri)
};
return new Smarty(httpClient, "test-auth-id", "test-auth-token");
}
[Fact]
public static void ConstructorTest()
{
var actual = new Smarty(AuthId, AuthToken);
Assert.NotNull(actual);
}
[Fact]
public static void ConstructorNullTest()
{
Assert.Throws<ArgumentNullException>(() => new Smarty(new HttpClient(), AuthId, null));
Assert.Throws<ArgumentNullException>(() => new Smarty(new HttpClient(), null, AuthToken));
Assert.Throws<ArgumentNullException>(() => new Smarty(new HttpClient(), null, null));
Assert.Throws<ArgumentNullException>(() => new Smarty(new HttpClient(), AuthId, string.Empty));
Assert.Throws<ArgumentNullException>(() => new Smarty(new HttpClient(), string.Empty, AuthToken));
Assert.Throws<ArgumentNullException>(() => new Smarty(new HttpClient(), string.Empty, string.Empty));
Assert.Throws<ArgumentNullException>(() => new Smarty(AuthId, null));
Assert.Throws<ArgumentNullException>(() => new Smarty(null, AuthToken));
Assert.Throws<ArgumentNullException>(() => new Smarty(null, null));
Assert.Throws<ArgumentNullException>(() => new Smarty(AuthId, string.Empty));
Assert.Throws<ArgumentNullException>(() => new Smarty(string.Empty, AuthToken));
Assert.Throws<ArgumentNullException>(() => new Smarty(string.Empty, string.Empty));
}
[Fact]
public void MustGetSingleZipCodeJsonString()
{
var actual = _service.Execute(ZipCodeTest);
Assert.Equal(ExpectedZipcodeResponse, actual);
}
[Fact]
public void MustGetZipCodeByParamsJsonString()
{
var actual = _serviceParam.Execute(SmartyParameterState, SmartyParameterCity, SmartyParameterStreet);
Assert.Equal(ExpectedParamResponse, actual);
}
[Fact]
public void MustGetSingleZipCodeObject()
{
var actual = _service.GetAddress<List<SmartyModel>>(ZipCodeTest);
Assert.IsType<List<SmartyModel>>(actual);
Assert.Equal(ExpectedCity, actual[0].CityStates[0].City);
Assert.Equal(ExpectedState, actual[0].CityStates[0].StateAbbreviation);
}
[Fact]
public void MustGetZipCodeByParamsList()
{
var actual = _serviceParam.ListAddresses<SmartyParamsModel>(SmartyParameterState, SmartyParameterCity, SmartyParameterStreet);
Assert.IsType<List<SmartyParamsModel>>(actual);
Assert.True(actual.Count > 0);
Assert.Equal(SmartyParameterCity, actual[0].Components.CityName);
Assert.Equal(SmartyParameterState, actual[0].Components.StateAbbreviation);
}
[Fact]
public void MustThrowTheExceptions()
{
var exception = Assert.Throws<SmartyException>(() => _service.Execute(" 12345678901234567890 "));
Assert.Equal(InvalidZipCodeSizeMessage, exception.Message);
exception = Assert.Throws<SmartyException>(() => _service.Execute(" 12A"));
Assert.Equal(InvalidZipCodeSizeMessage, exception.Message);
exception = Assert.Throws<SmartyException>(() => _service.Execute(" 123A5678 "));
Assert.Equal(InvalidZipCodeFormatMessage, exception.Message);
exception = Assert.Throws<SmartyException>(() => _service.Execute("Lorem ipsum dolor sit amet amet sit", "Mountain View", "1600 Amphitheatre Pkwy"));
Assert.Equal(InvalidStateMessage, exception.Message);
exception = Assert.Throws<SmartyException>(() => _service.Execute("CA", "Lorem ipsum dolor sit amet, consectetur adipiscing elit posuere posuere.", "1600 Amphitheatre Pkwy"));
Assert.Equal(InvalidCityMessage, exception.Message);
exception = Assert.Throws<SmartyException>(() => _service.Execute("CA", "Mountain View", "Lorem ipsum dolor sit amet, consectetur adipiscing elit posuere posuere."));
Assert.Equal(InvalidStreetMessage, exception.Message);
}
[Fact]
public async Task MustGetSingleZipCodeJsonStringAsync()
{
var actual = await _service.ExecuteAsync(ZipCodeTest);
Assert.Equal(ExpectedZipcodeResponse, actual);
}
[Fact]
public async Task MustGetListZipCodeJsonStringAsync()
{
var actual = await _serviceParam.ExecuteAsync(SmartyParameterState, SmartyParameterCity, SmartyParameterStreet);
Assert.Equal(ExpectedParamResponse, actual);
}
[Fact]
public async Task MustGetSingleZipCodeObjectAsync()
{
var actual = await _service.GetAddressAsync<List<SmartyModel>>(ZipCodeTest);
Assert.IsType<List<SmartyModel>>(actual);
Assert.Equal(ExpectedCity, actual[0].CityStates[0].City);
Assert.Equal(ExpectedState, actual[0].CityStates[0].StateAbbreviation);
}
[Fact]
public async Task MustGetZipCodeByParamsListAsync()
{
var actual = await _serviceParam.ListAddressesAsync<SmartyParamsModel>(SmartyParameterState, SmartyParameterCity, SmartyParameterStreet);
Assert.IsType<List<SmartyParamsModel>>(actual);
Assert.True(actual.Count > 0);
Assert.Equal(SmartyParameterCity, actual[0].Components.CityName);
Assert.Equal(SmartyParameterState, actual[0].Components.StateAbbreviation);
}
[Fact]
public async Task MustThrowTheExceptionsAsync()
{
var exception = await Assert.ThrowsAsync<SmartyException>(() => _service.ExecuteAsync(" 12345678901234567890 "));
Assert.Equal(InvalidZipCodeSizeMessage, exception.Message);
exception = await Assert.ThrowsAsync<SmartyException>(() => _service.ExecuteAsync(" 12A"));
Assert.Equal(InvalidZipCodeSizeMessage, exception.Message);
exception = await Assert.ThrowsAsync<SmartyException>(() => _service.ExecuteAsync(" 123A5678 "));
Assert.Equal(InvalidZipCodeFormatMessage, exception.Message);
exception = await Assert.ThrowsAsync<SmartyException>(() => _service.ExecuteAsync("Lorem ipsum dolor sit amet amet sit", "Mountain View", "1600 Amphitheatre Pkwy"));
Assert.Equal(InvalidStateMessage, exception.Message);
exception = await Assert.ThrowsAsync<SmartyException>(() => _service.ExecuteAsync("CA", "Lorem ipsum dolor sit amet, consectetur adipiscing elit posuere posuere.", "1600 Amphitheatre Pkwy"));
Assert.Equal(InvalidCityMessage, exception.Message);
exception = await Assert.ThrowsAsync<SmartyException>(() => _service.ExecuteAsync("CA", "Mountain View", "Lorem ipsum dolor sit amet, consectetur adipiscing elit posuere posuere."));
Assert.Equal(InvalidStreetMessage, exception.Message);
}
}
}