Skip to content

Add Tests #22

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 17 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/main/java/com/toopher/ApiRequestDetails.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.toopher;

import org.apache.http.NameValuePair;

import java.util.List;
import java.util.Map;

public abstract class ApiRequestDetails {
private List<NameValuePair> params;
private Map<String, String> extras;

public ApiRequestDetails(List<NameValuePair> params, Map<String, String> extras) {
this.params = params;
this.extras = extras;
}

public List<NameValuePair> getParams() { return params; }
public Map<String, String> getExtras() { return extras; }
}
14 changes: 9 additions & 5 deletions src/main/java/com/toopher/ApiResponseObject.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,29 @@
package com.toopher;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

public class ApiResponseObject {
/**
* A map of the raw API response data
*/
public Map raw;

public ApiResponseObject(JSONObject json) throws JSONException {
this.raw = jsonToMap(json);
}

private Map<String, Object> jsonToMap(JSONObject json) throws JSONException{
Map<String,Object> result = new HashMap<String,Object>();


if (json == null) {
return result;
}

for (Iterator<String> i = json.keys(); i.hasNext(); ) {
String key = i.next();
Object o = json.get(key);
Expand Down
66 changes: 66 additions & 0 deletions src/main/java/com/toopher/AuthenticationRequestDetails.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package com.toopher;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AuthenticationRequestDetails extends ApiRequestDetails {
public AuthenticationRequestDetails(List<NameValuePair> params, Map<String, String> extras) {
super(params, extras);
}

public static class Builder {
private List<NameValuePair> params = new ArrayList<NameValuePair>();
private Map<String, String> extras = new HashMap<String, String>();

public Builder() {
this(null);
}

public Builder(Map<String, String> extras) {
this.extras = extras == null ? new HashMap<String, String>() : extras;
}

public Builder addExtra(String key, String value) {
extras.put(key, value);
return this;
}

public Builder setActionName(String actionName) {
if (actionName != null && actionName.length() > 0) {
params.add(new BasicNameValuePair("action_name", actionName));
}
return this;
}

public Builder setPairingId(String pairingId) {
if (pairingId != null) {
params.add(new BasicNameValuePair("pairing_id", pairingId));
}
return this;
}

public Builder setTerminalName(String terminalName) {
if (terminalName != null) {
params.add(new BasicNameValuePair("terminal_name", terminalName));
}
return this;
}

public Builder setTerminalNameExtra(String terminalNameExtra) {
return addExtra("terminal_name_extra", terminalNameExtra);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not check for null and not empty for this and the following property?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is code from ToopherAPI that I refactored to use builders and factories. There wasn't a null check in the original code and I didn't want to add a null check for fear of changing the behavior, since we didn't have tests around it.

}

public Builder setUserName(String userName) {
return addExtra("user_name", userName);
}

public AuthenticationRequestDetails build() {
return new AuthenticationRequestDetails(params, extras);
}
}
}
27 changes: 21 additions & 6 deletions src/main/java/com/toopher/AuthenticationStatus.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import org.json.JSONException;
import org.json.JSONObject;
import java.util.Map;

/**
* Provide information about the status of an authentication request
Expand Down Expand Up @@ -44,9 +43,8 @@ public class AuthenticationStatus extends ApiResponseObject {
* The descriptive name for the terminal associated with the request
*/
public String terminalName;


public AuthenticationStatus(JSONObject json) throws JSONException{

public AuthenticationStatus(JSONObject json) throws JSONException {
super(json);

this.id = json.getString("id");
Expand All @@ -58,13 +56,30 @@ public AuthenticationStatus(JSONObject json) throws JSONException{
JSONObject terminal = json.getJSONObject("terminal");
this.terminalId = terminal.getString("id");
this.terminalName = terminal.getString("name");

}

public AuthenticationStatus(
String id,
boolean pending,
boolean granted,
boolean automated,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to make this Boolean in case the properties are null?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe, but probably not. Our API should be sending correctly-serialized data (true instead of "true") in the first place, and JSONObject will throw a JSONException if a nonexistent property is accessed (json.getBoolean("automated")).

String reason,
String terminalId,
String terminalName,
JSONObject json) {
super(json);
this.id = id;
this.pending = pending;
this.granted = granted;
this.automated = automated;
this.reason = reason;
this.terminalId = terminalId;
this.terminalName = terminalName;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What would the json parameter contain in this case? Without reviewing the code more thoroughly this method seems like it would simply create an object from json then set the same properties again.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

json is the JSONObject, like AuthenticationStatus#_raw_data (or whatever) in toopher-python. Off the top of my head, the only thing that the call to super does is save json as an instance variable.


@Override
public String toString() {
return String.format("[AuthenticationStatus: id=%s; pending=%b; granted=%b; automated=%b; reason=%s; terminalId=%s; terminalName=%s]",
id, pending, granted, automated, reason, terminalId, terminalName);
}

}
21 changes: 21 additions & 0 deletions src/main/java/com/toopher/AuthenticationStatusFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.toopher;

import org.json.JSONException;
import org.json.JSONObject;

public class AuthenticationStatusFactory {
public AuthenticationStatusFactory() {}

public AuthenticationStatus create(JSONObject json) throws JSONException {
final JSONObject terminal = json.getJSONObject("terminal");
return new AuthenticationStatus(
json.getString("id"),
json.getBoolean("pending"),
json.getBoolean("granted"),
json.getBoolean("automated"),
json.getString("reason"),
terminal.getString("id"),
terminal.getString("name"),
json);
}
}
3 changes: 2 additions & 1 deletion src/main/java/com/toopher/RequestError.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package com.toopher;

import java.io.IOException;
import org.apache.http.client.ClientProtocolException;
import org.json.JSONException;

import java.io.IOException;

/**
* Request errors from API calls
*
Expand Down
Loading