/
RateServiceTests.cs
208 lines (181 loc) · 10.7 KB
/
RateServiceTests.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
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
#region SeeSharpShip.Tests is Copyright (C) 2011-2013 Michael J. Sumerano.
// This file is part of SeeSharpShip.Tests.
//
// SeeSharpShip.Tests is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// SeeSharpShip.Tests is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with SeeSharpShip.Tests. If not, see <http://www.gnu.org/licenses/>.
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;
using SeeSharpShip.Extensions;
using SeeSharpShip.Models.Usps;
using SeeSharpShip.Models.Usps.Domestic.Request;
using SeeSharpShip.Models.Usps.Domestic.Response;
using SeeSharpShip.Models.Usps.International.Request;
using SeeSharpShip.Models.Usps.International.Response;
using SeeSharpShip.Services.Usps;
using SeeSharpShip.Tests.Properties;
namespace SeeSharpShip.Tests.Usps {
[TestFixture]
public class RateServiceTests {
private IRateService _rateService;
private static string _userId;
private static string _password;
private static string _sourceZipCode;
[TestFixtureSetUp]
public void SetUp() {
//Default hits production API URL
//_rateService = new RateService();
//Uses test API URL by default. Configure in app.config.
_rateService = new RateService(Settings.Default.UspsApiUrl, new PostRequest());
_userId = Settings.Default.UspsUserId;
_password = Settings.Default.UspsPassword;
_sourceZipCode = Settings.Default.UspsSourceZip;
if (string.IsNullOrEmpty(_userId)) {
throw new Exception("You must set UspsUserId in app.config to run 'Explicit' integration tests");
}
if (string.IsNullOrEmpty(_password)) {
throw new Exception("You must set UspsPassword in app.config to run 'Explicit' integration tests");
}
if (string.IsNullOrEmpty(_sourceZipCode)) {
throw new Exception("You must set UspsSourceZip in app.config to run 'Explicit' integration tests");
}
}
[Test]
[Category("Domestic")]
[Explicit("Integration test that hits the real API")]
public void DomesticServices_ValidCredentials_ReturnsDistinctServices() {
List<ServiceInfo> response = _rateService.DomesticServices(_userId, _password, _sourceZipCode).ToList();
Assert.That(response.Count(), Is.EqualTo(response.Distinct().Count()));
}
[Test]
[Category("Domestic")]
[Explicit("Integration test that hits the real API")]
public void DomesticServices_ValidCredentials_ReturnsListOfServices() {
List<ServiceInfo> response = _rateService.DomesticServices(_userId, _password, _sourceZipCode).ToList();
Assert.That(response.Count, Is.GreaterThan(0));
}
/// <summary>
/// Tests fix for defect #7 introduced by USPS API changes
/// </summary>
[Test]
[Category("Domestic")]
[Explicit("Integration test that hits the real API")]
public void DomesticServices_ValidCredentials_ReturnsListOfServicesContainingEncodedTrademark() {
List<ServiceInfo> response = _rateService.DomesticServices(_userId, _password, _sourceZipCode).ToList();
bool containsEncodedTrademark = response.Any(r => r.FullName.Contains("<sup>™</sup>"));
Assert.That(containsEncodedTrademark, Is.Not.True);
}
[Test]
[Category("Domestic")]
[Explicit("Integration test that hits the real API")]
public void Get_DomesticOverweightFirstClass_ReturnsPackageError() {
string request =
string.Format(
"<RateV4Request PASSWORD=\"{1}\" USERID=\"{0}\"><Revision>2</Revision><Package ID=\"539720aa4cff99f7\"><Service>First Class</Service><FirstClassMailType>Letter</FirstClassMailType><ZipOrigination>18507</ZipOrigination><ZipDestination>18518</ZipDestination><Pounds>10</Pounds><Ounces>0</Ounces><Container>RECTANGULAR</Container><Size>LARGE</Size><Width>15</Width><Length>15</Length><Height>20</Height><Girth>70</Girth><Value>249.95</Value><Machinable>false</Machinable><ReturnLocations>false</ReturnLocations></Package></RateV4Request>",
_userId, _password);
RateV4Response response = _rateService.Get(request.ToObject<RateV4Request>());
Assert.That(response.Packages[0].Error, Is.Not.Null);
}
[Test]
[Category("Domestic")]
[Explicit("Integration test that hits the real API")]
public void Get_DomesticWithZipCodeLessThanFiveCharacters_ReturnsPackageError() {
RateV4Response response = _rateService.Get(RateServiceTestsData.GetDomesticRequestWithZipDestinationLessThan5CharactersLong());
Assert.That(response.Packages[0].Error, Is.Not.Null);
}
[Test]
[Category("Domestic")]
[Explicit("Integration test that hits the real API")]
public void Get_DomesticWithZipCodeLongerThanFiveDigits_RateV4ResponseWithNoErrors() {
string request =
string.Format(
"<RateV4Request PASSWORD=\"{1}\" USERID=\"{0}\"><Revision>2</Revision><Package ID=\"2022599e4dfb4687\"><Service>Priority</Service><ZipOrigination>18507</ZipOrigination><ZipDestination>27513-8629</ZipDestination><Pounds>2</Pounds><Ounces>0</Ounces><Container /><Size>REGULAR</Size><Width>1</Width><Length>1</Length><Height>1</Height><Girth>4</Girth><Value>42.95</Value><SpecialServices><SpecialService>1</SpecialService></SpecialServices><Machinable>false</Machinable><ReturnLocations>false</ReturnLocations></Package></RateV4Request>",
_userId, _password);
RateV4Response response = _rateService.Get(request.ToObject<RateV4Request>());
Assert.That(response.Error, Is.Null);
Assert.That(response.Packages[0].Error, Is.Null);
}
[Test]
[Category("Domestic")]
[Explicit("Integration test that hits the real API")]
public void Get_DomesticWithZipCodeNull_ThrowsNullReferenceException() {
Assert.Throws(typeof (NullReferenceException), () => _rateService.Get(RateServiceTestsData.GetDomesticRequestWithZipDestinationNull()));
}
[Test]
[Category("International")]
[Explicit("Integration test that hits the real API")]
public void Get_InternationalValueOfContentsOver1000_IntlRateV2Response() {
string request =
string.Format(
"<IntlRateV2Request PASSWORD=\"{1}\" USERID=\"{0}\"><Revision>2</Revision><Package ID=\"2fcd64731e0ff32b\"><Pounds>30</Pounds><Ounces>0</Ounces><Machinable>false</Machinable><MailType>Package</MailType><ValueOfContents>1550.00</ValueOfContents><Country>Canada</Country><Container /><Size>REGULAR</Size><Width>1</Width><Length>1</Length><Height>1</Height><Girth>4</Girth><OriginZip>18507</OriginZip><ExtraServices><ExtraService>1</ExtraService></ExtraServices></Package></IntlRateV2Request>",
_userId, _password);
IntlRateV2Response response = _rateService.Get(request.ToObject<IntlRateV2Request>());
Assert.That(response, Is.InstanceOf(typeof (IntlRateV2Response)));
}
[Test]
[Category("International")]
[Explicit("Integration test that hits the real API")]
public void Get_IntlRateV2Request_IntlRateV4Response() {
IntlRateV2Response response = _rateService.Get(RateServiceTestsData.GetInternationalRequest());
Assert.That(response, Is.InstanceOf<IntlRateV2Response>());
}
[Test]
[Category("International")]
[Explicit("Integration test that hits the real API")]
public void Get_InvalidIntlRateV2Request_RequestError() {
IntlRateV2Response response = _rateService.Get(new IntlRateV2Request {UserId = "kjzsdjbh fgkjashdf jhasdf"});
Assert.That(response.Error, Is.InstanceOf<RequestError>());
}
[Test]
[Category("Domestic")]
[Explicit("Integration test that hits the real API")]
public void Get_InvalidRateV4Request_RequestError() {
RateV4Response response = _rateService.Get(new RateV4Request {UserId = "kjzsdjbh fgkjashdf jhasdf"});
Assert.That(response.Error, Is.InstanceOf<RequestError>());
}
[Test]
[Category("Domestic")]
[Explicit("Integration test that hits the real API")]
public void Get_RateV4Request_RateV4Response() {
RateV4Response response = _rateService.Get(RateServiceTestsData.GetDomesticRequest());
Assert.That(response, Is.InstanceOf<RateV4Response>());
}
[Test]
[Category("International")]
[Explicit("Integration test that hits the real API")]
public void InternationalServices_ValidCredentials_ReturnsDistinctServices() {
IList<ServiceInfo> response = _rateService.InternationalServices(_userId, _password, _sourceZipCode).ToList();
Assert.That(response.Count, Is.EqualTo(response.Distinct().Count()));
}
[Test]
[Category("International")]
[Explicit("Integration test that hits the real API")]
public void InternationalServices_ValidCredentials_ReturnsListOfServices() {
List<ServiceInfo> response = _rateService.InternationalServices(_userId, _password, _sourceZipCode).ToList();
Assert.That(response.Count, Is.GreaterThan(0));
}
/// <summary>
/// Tests fix for defect #7 introduced by USPS API changes
/// </summary>
[Test]
[Category("International")]
[Explicit("Integration test that hits the real API")]
public void InternationalServices_ValidCredentials_ReturnsListOfServicesContainingEncodedTrademark() {
List<ServiceInfo> response = _rateService.InternationalServices(_userId, _password, _sourceZipCode).ToList();
bool containsEncodedTrademark = response.Any(r => r.FullName.Contains("<sup>™</sup>"));
Assert.That(containsEncodedTrademark, Is.Not.True);
}
}
}