Skip to content

Commit

Permalink
Merge pull request #2 from rami3sam/master
Browse files Browse the repository at this point in the history
improved unit tests
  • Loading branch information
rami3sam authored Jun 20, 2022
2 parents c411095 + 205a91b commit f70fb0c
Show file tree
Hide file tree
Showing 16 changed files with 1,156 additions and 158 deletions.
11 changes: 11 additions & 0 deletions .idea/libraries/Gradle__net_bytebuddy_byte_buddy_1_12_10.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions .idea/libraries/Gradle__org_assertj_assertj_core_3_23_1.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

49 changes: 49 additions & 0 deletions .idea/modules/lib/solussdk.lib.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions lib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ dependencies {

testImplementation 'junit:junit'
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1'
testImplementation("org.assertj:assertj-core:3.23.1")
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1'

}
Expand Down
23 changes: 15 additions & 8 deletions lib/src/main/java/com/tuti/Library.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
package com.tuti;

import com.tuti.api.TutiApiClient;
import com.tuti.api.authentication.SignInInfo;
import com.tuti.api.authentication.SignInRequest;
import com.tuti.api.data.Card;

public class Library {

Expand All @@ -13,8 +14,8 @@ public static void main(String[] args) {
TutiApiClient client = new TutiApiClient(true);
client.setSingleThreaded(true);

client.SignIn(new SignInInfo("adonese","12345678"),
param -> {
client.SignIn(new SignInRequest("adonese","12345678"),
(param,response) -> {
jwt=param.getAuthorizationJWT();
System.out.println(jwt);

Expand All @@ -23,10 +24,16 @@ public static void main(String[] args) {

});
client.setAuthToken(jwt);
client.getCards(param -> {
System.out.println(param.getCards().get(0).getPAN());
},(param, e,res) -> {
System.out.println(param);
});

Card card = new Card();
card.setName("JAVA SDK");
card.setExpiryDate("2020");
card.setPAN("1111222233334444");
client.addCard(card,(objectReceived, rawResponse) -> {
System.out.println(objectReceived);
} ,
(errorReceived, exception, rawResponse) -> {
System.out.println(errorReceived);
});
}
}
131 changes: 57 additions & 74 deletions lib/src/main/java/com/tuti/api/TutiApiClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,20 @@

import com.google.gson.Gson;
import com.tuti.api.authentication.SignInResponse;
import com.tuti.api.authentication.SignUpInfo;
import com.tuti.api.authentication.SignUpRequest;
import com.tuti.api.authentication.SignUpResponse;
import com.tuti.api.authentication.SignInInfo;
import com.tuti.api.authentication.SignInRequest;
import com.tuti.api.data.Card;
import com.tuti.api.data.Cards;
import com.tuti.api.data.RequestMethods;
import com.tuti.api.ebs.EBSRequest;
import com.tuti.api.ebs.EBSResponse;
import com.tuti.model.Operations;
import okhttp3.*;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.Map;
import java.util.concurrent.TimeUnit;

public class TutiApiClient {
Expand All @@ -23,7 +25,7 @@ public class TutiApiClient {
private static volatile Gson gson = null;
private String serverURL;
private boolean isSingleThreaded = false;
private String authToken = "";
private String authToken = null;

public String getAuthToken() {
return authToken;
Expand Down Expand Up @@ -64,26 +66,26 @@ private String getServerURL(boolean development) {
return development ? developmentHost : productionHost;
}

public void SignIn(SignInInfo credentials,ResponseCallable<SignInResponse> onResponse,ErrorCallable<String> onError){
sendPostRequest(serverURL + Operations.SIGN_IN,credentials, SignInResponse.class,String.class,onResponse,onError);
public void SignIn(SignInRequest credentials, ResponseCallable<SignInResponse> onResponse, ErrorCallable<String> onError){
sendRequest( RequestMethods.POST,serverURL + Operations.SIGN_IN,credentials, SignInResponse.class,String.class,onResponse,onError,null);
}

public void Signup(SignUpInfo signUpInfo,ResponseCallable<SignUpResponse> onResponse,ErrorCallable<String> onError){
sendPostRequest( serverURL + Operations.SIGN_UP,signUpInfo,SignUpResponse.class,String.class,onResponse,onError);
public void Signup(SignUpRequest signUpRequest, ResponseCallable<SignUpResponse> onResponse, ErrorCallable<String> onError){
sendRequest( RequestMethods.POST,serverURL + Operations.SIGN_UP, signUpRequest,SignUpResponse.class,String.class,onResponse,onError,null);
}

public void sendEBSRequest(String URL,EBSRequest ebsRequest, ResponseCallable<EBSResponse> onResponse, ErrorCallable onError){
sendPostRequest( URL,ebsRequest,EBSResponse.class,EBSResponse.class,onResponse,onError);
public void sendEBSRequest(String URL,EBSRequest ebsRequest, ResponseCallable<EBSResponse> onResponse, ErrorCallable<EBSResponse> onError){
sendRequest( RequestMethods.POST, URL,ebsRequest,EBSResponse.class,EBSResponse.class,onResponse,onError,null);
}

public void getCards( ResponseCallable<Cards> onResponse, ErrorCallable onError){
sendGetRequest( serverURL + Operations.GET_CARDS, Cards.class,String.class,onResponse,onError);
public void getCards( ResponseCallable<Cards> onResponse, ErrorCallable<String> onError){
sendRequest( RequestMethods.GET,serverURL + Operations.GET_CARDS, null,Cards.class,String.class,onResponse,onError,null);
}

public void addCard(Card card, ResponseCallable<String> onResponse, ErrorCallable onError){
sendPostRequest( serverURL + Operations.ADD_CARD,card,String.class,String.class,onResponse,onError);
public void addCard(Card card, ResponseCallable<String> onResponse, ErrorCallable<String> onError){
sendRequest( RequestMethods.POST,serverURL + Operations.ADD_CARD,card,String.class,String.class,onResponse,onError,null);
}
public void sendPostRequest(String URL, Object requestToBeSent, Type ResponseType, Type ErrorType, ResponseCallable onResponse, ErrorCallable onError ){
public Thread sendRequest(RequestMethods method, String URL, Object requestToBeSent, Type ResponseType, Type ErrorType, ResponseCallable onResponse, ErrorCallable onError, Map<String,String> headers){

// create a runnable to run it in a new thread (so main thread never hangs)
Runnable runnable = () ->{
Expand All @@ -93,91 +95,72 @@ public void sendPostRequest(String URL, Object requestToBeSent, Type ResponseTyp

RequestBody requestBody = RequestBody.create(gson.toJson(requestToBeSent),JSON);

Request.Builder requestBuilder = new Request.Builder().url(URL);

Request request = new Request.Builder()
.url(URL)
.header("Authorization",authToken)
.post(requestBody)
.build();
if (authToken != null) requestBuilder.header("Authorization",authToken);

//add additional headers set by the user
if(headers != null){
for (String key : headers.keySet()){
requestBuilder.header(key, headers.get(key));
}
}

//check for http method set by the user
if (method == RequestMethods.POST){
requestBuilder.post(requestBody);
}else{
requestBuilder.get();
}

Request request = requestBuilder.build();

try (Response rawResponse = okHttpClient.newCall(request).execute()) {

try (Response response = okHttpClient.newCall(request).execute()) {
// check for http errors
if (response.code() >= 400 && response.code() < 600){
if (rawResponse.code() >= 400 && rawResponse.code() < 600){
// call onError if request has failed
if(onError != null) { onError.call(processResponse(response,ErrorType),null,response.code()); }
if(onError != null) { onError.call(parseResponse(rawResponse,ErrorType),null,rawResponse); }
}else{
// call onResponse if request has succeeded
if(onResponse != null) { onResponse.call(processResponse(response,ResponseType)); }
if(onResponse != null) { onResponse.call(parseResponse(rawResponse,ResponseType),rawResponse); }
}

}catch(Exception exception){
onError.call(null,exception,-1);
exception.printStackTrace();
if(onError != null) onError.call(null,exception,null);
}
};

// unit testing concurrent code on multiple threads is hard
if (isSingleThreaded){
new Thread(runnable).run();
}else {
new Thread(runnable).start();
}
}

public void sendGetRequest(String URL, Type ResponseType,Type ErrorType, ResponseCallable onResponse, ErrorCallable onError ){
Thread thread = new Thread(runnable);

// create a runnable to run it in a new thread (so main thread never hangs)
Runnable runnable = () ->{
OkHttpClient okHttpClient = getOkHttpInstance();
Gson gson = getGsonInstance();

Request request = new Request.Builder()
.url(URL)
.header("Authorization",authToken)
.get()
.build();

try (Response response = okHttpClient.newCall(request).execute()) {
// check for http errors
if (response.code() >= 400 && response.code() < 600){
// call onError if request has failed
if(onError != null) { onError.call(processResponse(response,ErrorType),null,response.code()); }

}else{
// call onResponse if request has succeeded
if(onResponse != null) { onResponse.call(processResponse(response,ResponseType)); }
}
if (isSingleThreaded) {
thread.run();
} else {
thread.start();
}

}catch(Exception exception){
exception.printStackTrace();
// call on error in case of exception
onError.call(null,exception,-1);
}
};
return thread;
}

// unit testing concurrent code on multiple threads is hard
if (isSingleThreaded){
new Thread(runnable).run();
}else {
new Thread(runnable).start();
}
private Object parseResponse(Response rawResponse, Type ResponseType) throws IOException {
Gson gson = getGsonInstance();
String responseAsString = rawResponse.body().string();
return isTypeStringOrNull(ResponseType) ? responseAsString : gson.fromJson(responseAsString, ResponseType);
}

Object processResponse(Response response,Type ResponseType) throws IOException {
Object finalResponse;
if (ResponseType == String.class || ResponseType == null) {
finalResponse = response.body().string();
}else{
finalResponse = gson.fromJson(response.body().string(), ResponseType);
}
return finalResponse;
private boolean isTypeStringOrNull(Type type){
return (type == String.class || type == null);
}

public interface ResponseCallable<T> {
void call(T param);
void call(T objectReceived,Response rawResponse);
}

public interface ErrorCallable<T> {
void call(T param,Exception exception,int responseCode);
void call(T errorReceived,Exception exception,Response rawResponse);
}
}

Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
package com.tuti.api.authentication;

public class SignInInfo {
public class SignInRequest {
private String username;
private String password;

public String getUsername() {
return username;
}

public SignInInfo(String username, String password) {
public SignInRequest(String username, String password) {
this.username = username;
this.password = password;
}

public SignInInfo setUsername(String username) {
public SignInRequest setUsername(String username) {
this.username = username;
return this;
}
Expand All @@ -22,7 +22,7 @@ public String getPassword() {
return password;
}

public SignInInfo setPassword(String password) {
public SignInRequest setPassword(String password) {
this.password = password;
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.google.gson.annotations.SerializedName;

public class SignUpInfo {
public class SignUpRequest {
private String username;
private String password;

Expand Down
15 changes: 15 additions & 0 deletions lib/src/main/java/com/tuti/api/authentication/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,19 @@ public void setIsMerchant(boolean isMerchant) {
this.isMerchant = isMerchant;
}

@Override
public String toString() {
return "User{" +
"id=" + id +
", createdAt='" + createdAt + '\'' +
", updateAt='" + updateAt + '\'' +
", DeletedAt='" + DeletedAt + '\'' +
", username='" + username + '\'' +
", fullname='" + fullname + '\'' +
", birthday='" + birthday + '\'' +
", mobileNumber='" + mobileNumber + '\'' +
", email='" + email + '\'' +
", isMerchant=" + isMerchant +
'}';
}
}
Loading

0 comments on commit f70fb0c

Please sign in to comment.