Skip to content

Commit

Permalink
Apikey functionality is added
Browse files Browse the repository at this point in the history
  • Loading branch information
altiria committed Mar 24, 2022
1 parent f67e338 commit 1d68fc4
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 21 deletions.
2 changes: 2 additions & 0 deletions .settings/org.eclipse.core.resources.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
eclipse.preferences.version=1
encoding/<project>=UTF-8
15 changes: 15 additions & 0 deletions .settings/org.eclipse.jdt.core.prefs
Original file line number Diff line number Diff line change
@@ -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
37 changes: 30 additions & 7 deletions src/main/java/com/altiria/app/AltiriaClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -57,26 +58,30 @@ 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();
}

/**
* 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
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

/**
Expand All @@ -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
Expand All @@ -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.
*/
Expand All @@ -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");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand All @@ -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");

Expand All @@ -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");

Expand All @@ -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");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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());
Expand All @@ -63,6 +65,7 @@ public void testOkMandatoryParams() {
/**
* All params are sent.
* Features:
* - apikey authentication
* - sender
* - delivery confirmation with identifier
* - concatenated
Expand All @@ -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);

Expand Down Expand Up @@ -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");

Expand All @@ -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");

Expand All @@ -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");

Expand Down

0 comments on commit 1d68fc4

Please sign in to comment.