Skip to content

Commit ce45c0e

Browse files
Merge pull request #11 from thewalrusisben/pr-review
Test Fixes
2 parents 805639b + 6b29856 commit ce45c0e

3 files changed

Lines changed: 32 additions & 14 deletions

File tree

api/src/main/java/com/messagebird/MessageBirdClient.java

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
import java.util.LinkedHashMap;
6565
import java.util.LinkedList;
6666
import java.util.List;
67+
import java.util.Locale;
6768
import java.util.Map;
6869

6970
/**
@@ -1596,15 +1597,31 @@ private void verifyOffsetAndLimit(Integer offset, Integer limit) {
15961597
}
15971598
}
15981599

1600+
/**
1601+
* Checks whether a particular country code is a recognized ISO Country.
1602+
*
1603+
* @param countryCode The country code in which the Number should be purchased.
1604+
* @throws IllegalArgumentException
1605+
*/
1606+
private Boolean countryCodeIsValid(String countryCode) throws IllegalArgumentException {
1607+
final Boolean isValid = Arrays.asList(Locale.getISOCountries()).contains(countryCode);
1608+
if (!isValid) {
1609+
throw new IllegalArgumentException("Invalid Country Code Provided.");
1610+
}
1611+
return true;
1612+
}
1613+
15991614
/**
16001615
* Lists Numbers that are available to purchase in a particular country code, without any filters.
16011616
*
16021617
* @param countryCode The country code in which the Number should be purchased.
16031618
* @throws GeneralException general exception
16041619
* @throws UnauthorizedException if client is unauthorized
16051620
* @throws NotFoundException if the resource is missing
1621+
* @throws IllegalArgumentException if the country code provided is invalid
16061622
*/
1607-
public PhoneNumbersResponse listNumbersForPurchase(String countryCode) throws GeneralException, UnauthorizedException, NotFoundException {
1623+
public PhoneNumbersResponse listNumbersForPurchase(String countryCode) throws GeneralException, UnauthorizedException, NotFoundException, IllegalArgumentException {
1624+
countryCodeIsValid(countryCode);
16081625
final String url = String.format("%s/available-phone-numbers", NUMBERS_CALLS_BASE_URL);
16091626
return messageBirdService.requestByID(url, countryCode, PhoneNumbersResponse.class);
16101627
}
@@ -1617,8 +1634,10 @@ public PhoneNumbersResponse listNumbersForPurchase(String countryCode) throws Ge
16171634
* @throws GeneralException general exception
16181635
* @throws UnauthorizedException if client is unauthorized
16191636
* @throws NotFoundException if the resource is missing
1637+
* @throws IllegalArgumentException if the country code provided is invalid
16201638
*/
1621-
public PhoneNumbersResponse listNumbersForPurchase(String countryCode, PhoneNumbersLookup params) throws GeneralException, UnauthorizedException, NotFoundException {
1639+
public PhoneNumbersResponse listNumbersForPurchase(String countryCode, PhoneNumbersLookup params) throws GeneralException, UnauthorizedException, NotFoundException, IllegalArgumentException {
1640+
countryCodeIsValid(countryCode);
16221641
final String url = String.format("%s/available-phone-numbers", NUMBERS_CALLS_BASE_URL);
16231642
return messageBirdService.requestByID(url, countryCode, params.toHashMap(), PhoneNumbersResponse.class);
16241643
}
@@ -1630,13 +1649,17 @@ public PhoneNumbersResponse listNumbersForPurchase(String countryCode, PhoneNumb
16301649
* @param countryCode The country code in which the Number should be purchased.
16311650
* @throws GeneralException general exception
16321651
* @throws UnauthorizedException if client is unauthorized
1652+
* @throws IllegalArgumentException if the country code provided is invalid
16331653
*/
1634-
public PurchasedNumberCreatedResponse purchaseNumber(String number, String countryCode, int billingIntervalMonths) throws UnauthorizedException, GeneralException {
1654+
public PurchasedNumberCreatedResponse purchaseNumber(String number, String countryCode, int billingIntervalMonths) throws UnauthorizedException, GeneralException, IllegalArgumentException {
1655+
countryCodeIsValid(countryCode);
16351656
final String url = String.format("%s/phone-numbers", NUMBERS_CALLS_BASE_URL);
1636-
16371657
final Map<String, Object> payload = new LinkedHashMap<String, Object>();
16381658
payload.put("number", number);
16391659
payload.put("countryCode", countryCode);
1660+
if (!Arrays.asList(1, 3, 6, 9).contains(billingIntervalMonths)) {
1661+
throw new IllegalArgumentException("Billing Interval Must Be Either 1, 3, 6, or 9.");
1662+
}
16401663
payload.put("billingIntervalMonths", billingIntervalMonths);
16411664

16421665
return messageBirdService.sendPayLoad(url, payload, PurchasedNumberCreatedResponse.class);

examples/src/main/java/ExampleListNumbersForPurchase.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111

1212
public class ExampleListNumbersForPurchase {
1313
public static void main(String[] args) {
14-
if (args.length < 1) {
15-
System.out.println("Please specify your access key.");
14+
if (args.length < 2) {
15+
System.out.println("Please specify your access key and a country code to test.");
1616
return;
1717
}
1818
// First create your service object
@@ -22,7 +22,7 @@ public static void main(String[] args) {
2222
final MessageBirdClient messageBirdClient = new MessageBirdClient(wsr);
2323

2424
try {
25-
if (args.length > 1) {
25+
if (args.length > 2) {
2626
PhoneNumbersLookup options = new PhoneNumbersLookup();
2727
options.setFeatures(PhoneNumberFeature.VOICE, PhoneNumberFeature.SMS);
2828
options.setType(PhoneNumberType.MOBILE);
@@ -32,7 +32,7 @@ public static void main(String[] args) {
3232
System.out.print(options.toString());
3333
System.out.println(String.format("Request Made With Params: %s", messageBirdClient.listNumbersForPurchase("US", options)));
3434
} else {
35-
System.out.println(String.format("Request Made Without Params: %s", messageBirdClient.listNumbersForPurchase("NL")));
35+
System.out.println(String.format("Request Made Without Params: %s", messageBirdClient.listNumbersForPurchase(args[1])));
3636
}
3737
} catch (UnauthorizedException | GeneralException | NotFoundException exception) {
3838
if (exception.getErrors() != null) {

examples/src/main/java/ExampleListPurchasedNumbers.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import com.messagebird.exceptions.NotFoundException;
66
import com.messagebird.exceptions.UnauthorizedException;
77
import com.messagebird.objects.PhoneNumberFeature;
8-
import com.messagebird.objects.PhoneNumberType;
98
import com.messagebird.objects.PurchasedNumbersFilter;
109

1110
public class ExampleListPurchasedNumbers {
@@ -15,16 +14,12 @@ public static void main(String[] args) {
1514
return;
1615
}
1716
// First create your service object
18-
final MessageBirdService wsr = new MessageBirdServiceImpl(args[0], "https://numbers.messagebird.com");
17+
final MessageBirdService wsr = new MessageBirdServiceImpl(args[0]);
1918

2019
// Add the service to the client
2120
final MessageBirdClient messageBirdClient = new MessageBirdClient(wsr);
2221

2322
PurchasedNumbersFilter filter = new PurchasedNumbersFilter();
24-
25-
filter.addFeature(PhoneNumberFeature.SMS);
26-
filter.setType(PhoneNumberType.MOBILE);
27-
2823
filter.setLimit(25);
2924

3025
try {

0 commit comments

Comments
 (0)