diff --git a/.settings/org.eclipse.core.resources.prefs b/.settings/org.eclipse.core.resources.prefs new file mode 100644 index 0000000..99f26c0 --- /dev/null +++ b/.settings/org.eclipse.core.resources.prefs @@ -0,0 +1,2 @@ +eclipse.preferences.version=1 +encoding/=UTF-8 diff --git a/.settings/org.eclipse.jdt.core.prefs b/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..0fee6a9 --- /dev/null +++ b/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,15 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.methodParameters=do not generate +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=1.8 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning +org.eclipse.jdt.core.compiler.release=disabled +org.eclipse.jdt.core.compiler.source=1.8 diff --git a/src/main/java/com/altiria/app/AltiriaClient.java b/src/main/java/com/altiria/app/AltiriaClient.java index 24fb19d..2baf13d 100644 --- a/src/main/java/com/altiria/app/AltiriaClient.java +++ b/src/main/java/com/altiria/app/AltiriaClient.java @@ -35,6 +35,7 @@ public class AltiriaClient { private Gson gson; private String login; private String passwd; + private boolean isApiKey; // connection timeout values are defined here private int connectionTimeout = 3000; @@ -57,10 +58,12 @@ public class AltiriaClient { * Simple constructor * @param login user login * @param password user password + * @param isApiKey set to true if apikey is used */ - public AltiriaClient(String login, String password) { + public AltiriaClient(String login, String password, boolean isApiKey) { this.login = login; this.passwd = password; + this.isApiKey = isApiKey; gson = new GsonBuilder().create(); } @@ -68,15 +71,17 @@ public AltiriaClient(String login, String password) { * This constructor includes response timeout. * @param login user login * @param password user password + * @param isApiKey set to true if apikey is used * @param timeout milliseconds for response timeout */ - public AltiriaClient(String login, String password, int timeout) { + public AltiriaClient(String login, String password, boolean isApiKey, int timeout) { this.login = login; this.passwd = password; + this.isApiKey = isApiKey; this.setTimeout(timeout); gson = new GsonBuilder().create(); } - + /** * Set the response timeout. * @param connectionTimeout milliseconds for connection timeout @@ -139,6 +144,15 @@ public String sendSms(AltiriaModelTextMessage textMessage) throws Exception { JsonObject credentialsJsonObject = new JsonObject(); JsonObject messageJsonObject = new JsonObject(); JsonArray destinationsJsonArray = null; + + if (this.login == null) { + log.error("ERROR: The login parameter is mandatory"); + throw new JsonException("LOGIN_NOT_NULL"); + } + if (this.passwd == null) { + log.error("ERROR: The password parameter is mandatory"); + throw new JsonException("PASSWORD_NOT_NULL"); + } if (textMessage.getDestination() == null || textMessage.getDestination().trim().isEmpty()) { log.error("ERROR: The destination parameter is mandatory"); @@ -172,8 +186,8 @@ public String sendSms(AltiriaModelTextMessage textMessage) throws Exception { if (textMessage.getEncoding() != null && textMessage.getEncoding().equals("unicode")) messageJsonObject.addProperty("encoding", "unicode"); - credentialsJsonObject.addProperty("login", this.login); - credentialsJsonObject.addProperty("passwd", this.passwd); + credentialsJsonObject.addProperty(this.isApiKey?"apikey":"login", this.login); + credentialsJsonObject.addProperty(this.isApiKey?"apisecret":"passwd", this.passwd); jsonObject.add("credentials", credentialsJsonObject); jsonObject.add("destination", destinationsJsonArray); @@ -254,11 +268,20 @@ public String getCredit() throws Exception { String credit = null; try { + if (this.login == null) { + log.error("ERROR: The login parameter is mandatory"); + throw new JsonException("LOGIN_NOT_NULL"); + } + if (this.passwd == null) { + log.error("ERROR: The password parameter is mandatory"); + throw new JsonException("PASSWORD_NOT_NULL"); + } + JsonObject jsonObject = new JsonObject(); JsonObject credentialsJsonObject = new JsonObject(); - credentialsJsonObject.addProperty("login", this.login); - credentialsJsonObject.addProperty("passwd", this.passwd); + credentialsJsonObject.addProperty(this.isApiKey?"apikey":"login", this.login); + credentialsJsonObject.addProperty(this.isApiKey?"apisecret":"passwd", this.passwd); jsonObject.add("credentials", credentialsJsonObject); jsonObject.addProperty("source", source); diff --git a/src/test/java/com/altiria/app/TestAltiriaSmsJavaClientGetCreditAuth.java b/src/test/java/com/altiria/app/TestAltiriaSmsJavaClientGetCreditAuth.java index 7736a91..8dc0b18 100644 --- a/src/test/java/com/altiria/app/TestAltiriaSmsJavaClientGetCreditAuth.java +++ b/src/test/java/com/altiria/app/TestAltiriaSmsJavaClientGetCreditAuth.java @@ -25,7 +25,7 @@ public void testErrorNoLogin() { log.debug("Enter testErrorNoLogin"); try { - AltiriaClient client = new AltiriaClient(null, password); + AltiriaClient client = new AltiriaClient(null, password, false); client.getCredit(); fail("JsonException should have been thrown"); } catch (JsonException je) { @@ -43,7 +43,7 @@ public void testErrorNoPassword() { log.debug("Enter testErrorNoPassword"); try { - AltiriaClient client = new AltiriaClient(login, null); + AltiriaClient client = new AltiriaClient(login, null, false); client.getCredit(); fail("JsonException should have been thrown"); } catch (JsonException je) { diff --git a/src/test/java/com/altiria/app/TestAltiriaSmsJavaClientGetCreditHttp.java b/src/test/java/com/altiria/app/TestAltiriaSmsJavaClientGetCreditHttp.java index 8c52171..2be4a27 100644 --- a/src/test/java/com/altiria/app/TestAltiriaSmsJavaClientGetCreditHttp.java +++ b/src/test/java/com/altiria/app/TestAltiriaSmsJavaClientGetCreditHttp.java @@ -14,7 +14,9 @@ public class TestAltiriaSmsJavaClientGetCreditHttp { //configurable parameters public String login = "user@mydomain.com"; public String password = "mypassword"; - + public String apiKey = "XXXXXXXX"; + public String apiSecret = "YYYYYYYY"; + private static final Logger log = LogManager.getLogger(TestAltiriaSmsJavaClientGetCreditHttp.class); /** @@ -25,7 +27,7 @@ public void testOk() { log.debug("Enter testOk"); try { - AltiriaClient client = new AltiriaClient(login, password); + AltiriaClient client = new AltiriaClient(login, password, false); String credit = client.getCredit(); //Check your credit here @@ -36,6 +38,25 @@ public void testOk() { } } + /** + * Basic case using apikey. + */ + @Test + public void testOkApikey() { + log.debug("Enter testOkApikey"); + + try { + AltiriaClient client = new AltiriaClient(apiKey, apiSecret, true); + String credit = client.getCredit(); + + //Check your credit here + //assertEquals("100.00", credit); + + } catch (Exception e) { + fail("Error: " + e.getMessage()); + } + } + /** * Invalid credentials. */ @@ -44,7 +65,7 @@ public void testErrorInvalidCredentials() { log.debug("Enter testErrorInvalidCredentials"); try { - AltiriaClient client = new AltiriaClient("unknown", password); + AltiriaClient client = new AltiriaClient("unknown", password, false); client.getCredit(); fail("AltiriaGwException should have been thrown"); diff --git a/src/test/java/com/altiria/app/TestAltiriaSmsJavaClientSendSmsAuth.java b/src/test/java/com/altiria/app/TestAltiriaSmsJavaClientSendSmsAuth.java index cf70959..2210360 100644 --- a/src/test/java/com/altiria/app/TestAltiriaSmsJavaClientSendSmsAuth.java +++ b/src/test/java/com/altiria/app/TestAltiriaSmsJavaClientSendSmsAuth.java @@ -31,7 +31,7 @@ public void testErrorNoLogin() { try { String message = "Lorem Ipsum is simply dummy text"; - AltiriaClient client = new AltiriaClient(null, password); + AltiriaClient client = new AltiriaClient(null, password, false); client.sendSms(destination, message); fail("JsonException should have been thrown"); @@ -52,7 +52,7 @@ public void testErrorNoPassword() { try { String message = "Lorem Ipsum is simply dummy text"; - AltiriaClient client = new AltiriaClient(login, null); + AltiriaClient client = new AltiriaClient(login, null, false); client.sendSms(destination, message); fail("JsonException should have been thrown"); @@ -73,7 +73,7 @@ public void testErrorNoDestination() { try { String message = "Lorem Ipsum is simply dummy text"; - AltiriaClient client = new AltiriaClient(login, null); + AltiriaClient client = new AltiriaClient(login, password, false); client.sendSms(null, message); fail("AltiriaGwException should have been thrown"); @@ -93,7 +93,7 @@ public void testErrorNoMessage() { log.debug("Enter testErrorNoMessage"); try { - AltiriaClient client = new AltiriaClient(login, null); + AltiriaClient client = new AltiriaClient(login, password, false); client.sendSms(destination, null); fail("AltiriaGwException should have been thrown"); diff --git a/src/test/java/com/altiria/app/TestAltiriaSmsJavaClientSendSmsHttp.java b/src/test/java/com/altiria/app/TestAltiriaSmsJavaClientSendSmsHttp.java index b1f0e5c..dfef117 100644 --- a/src/test/java/com/altiria/app/TestAltiriaSmsJavaClientSendSmsHttp.java +++ b/src/test/java/com/altiria/app/TestAltiriaSmsJavaClientSendSmsHttp.java @@ -24,6 +24,8 @@ public class TestAltiriaSmsJavaClientSendSmsHttp { //configurable parameters public String login = "user@mydomain.com"; public String password = "mypassword"; + public String apiKey = "XXXXXXXX"; + public String apiSecret = "YYYYYYYY"; //set to null if there is no sender public String sender = "mySender"; public String destination = "346XXXXXXXX"; @@ -47,7 +49,7 @@ public void testOkMandatoryParams() { try { String message = "Lorem Ipsum is simply dummy text"; - AltiriaClient client = new AltiriaClient(login, password); + AltiriaClient client = new AltiriaClient(login, password, false); String json = client.sendSms(destination, message); LinkedTreeMap < String, Object > mapBody = gson.fromJson(json, new TypeToken < LinkedTreeMap < String, Object >> () {}.getType()); @@ -63,6 +65,7 @@ public void testOkMandatoryParams() { /** * All params are sent. * Features: + * - apikey authentication * - sender * - delivery confirmation with identifier * - concatenated @@ -79,7 +82,7 @@ public void testOkAllParams() { String idAck = "myAlias"; String encoding = "unicode"; - AltiriaClient client = new AltiriaClient(login, password); + AltiriaClient client = new AltiriaClient(apiKey, apiSecret, true); AltiriaModelTextMessage textMessage = new AltiriaModelTextMessage(destination, message, sender); @@ -128,7 +131,7 @@ public void testErrorInvalidCredentials() { try { String message = "Lorem Ipsum is simply dummy text"; - AltiriaClient client = new AltiriaClient("unknown", password); + AltiriaClient client = new AltiriaClient("unknown", password, false); client.sendSms(destination, message); fail("AltiriaGwException should have been thrown"); @@ -151,7 +154,7 @@ public void testErrorInvalidDestination() { String message = "Lorem Ipsum is simply dummy text"; String invalidDestination = "invalid"; - AltiriaClient client = new AltiriaClient(login, password); + AltiriaClient client = new AltiriaClient(login, password, false); client.sendSms(invalidDestination, message); fail("AltiriaGwException should have been thrown"); @@ -173,7 +176,7 @@ public void testErrorEmptyMessage() { try { String message = ""; - AltiriaClient client = new AltiriaClient(login, password); + AltiriaClient client = new AltiriaClient(login, password, false); client.sendSms(destination, message); fail("AltiriaGwException should have been thrown");