Skip to content

Instantly share code, notes, and snippets.

@forforf
Created December 5, 2012 21:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save forforf/4219870 to your computer and use it in GitHub Desktop.
Save forforf/4219870 to your computer and use it in GitHub Desktop.
Recommendations for Request.java based on test results
//This passes all but one test
package src;
// Had to include these
import java.util.Map;
import java.util.HashMap;
class Request {
public static final String NOCODE = "NOCODE";
public enum CodeType { NONE, NOCODE, CODE }
String userId, deviceId, accessCode, appVer;
//Initializes params to an empty HashMap
Map<String, String> params = new HashMap<String, String>();
public Map<String, String> getParams() {
if (params == null)
params = new HashMap<String, String>();
return params;
}
public boolean isValid() {
this.cleanParams();
return userId != null && deviceId != null;
}
public CodeType getCodeType() {
this.cleanParams();
if (accessCode == null || accessCode.length() < 1) return CodeType.NONE;
if (NOCODE.equalsIgnoreCase(accessCode)) return CodeType.NOCODE;
return CodeType.CODE;
}
//This approach gives a way for the class to clean itself
//other possible approaches:
// Pre-parse incoming data
// Clean up data prior to use
//I'm fairly ignorant of java, so there may be even more elegant approaches
public void cleanParams(){
if (userId !=null) {
userId = userId.trim();
}
if (deviceId != null) {
deviceId = deviceId.trim();
}
if (accessCode != null) {
accessCode = accessCode.trim();
}
if (appVer != null) {
appVer = appVer.trim();
}
}
}
package src;
// Had to include these
import java.util.Map;
import java.util.HashMap;
class Request {
public static final String NOCODE = "NOCODE";
public enum CodeType { NONE, NOCODE, CODE }
String userId, deviceId, accessCode, appVer;
Map<String, String> params;
public Map<String, String> getParams() {
if (params == null)
params = new HashMap<String, String>();
return params;
}
public boolean isValid() {
return userId != null && deviceId != null;
}
public CodeType getCodeType() {
if (accessCode == null || accessCode.length() < 1) return CodeType.NONE;
if (NOCODE.equalsIgnoreCase(accessCode)) return CodeType.NOCODE;
return CodeType.CODE;
}
}
package src;
import com.google.gson.Gson;
import junit.framework.TestCase;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class RequestTest extends TestCase {
private String jsonstr = "{\"userId\":\"abc@def.com\", \"deviceId\":\"abc123\", \"accessCode\":\"ABCDEF\", \"appVer\":\"v1.2.3\"}";
//has whitespace as json key to see if gson balks (answer: it does not)
private String jsonblank = "{\"userId\":\" \", \"deviceId\":\"\", \"accessCode\":\" \", \"appVer\":\"\", \" \":\" \"}";
private String jsonValid = "{\"userId\":\"abc@def.com\", \"deviceId\":\"abc123\"}";
private String jsonInvalid1 = "{\"userId\":\"abc@def.com\", \"deviceId\":null}";
private String jsonInvalid2 = "{\"userId\":\"abc@def.com\"}";
private String jsonInvalid3 = "{\"userId\":null, \"deviceId\":\"abc123\"}";
private String jsonInvalid4 = "{\"deviceId\":\"abc123\"}";
private String jsonInvalid5 = "{\"userId\":null, \"deviceId\":null}";
private String jsonInvalid6 = "{}";
private String jsonInvalid7 = "{\"userId\":\"\", \"deviceId\":\"abc123\"}";
private String jsonInvalid8 = "{\"userId\":\"abc@def.com\", \"deviceId\":\" \"}";
private String jsonCodeNoneNone = "{\"userId\":\"abc@def.com\", \"deviceId\":\"abc123\"}";
private String jsonCodeNoneNull = "{\"userId\":\"abc@def.com\", \"deviceId\":\"abc123\", \"accessCode\":null}";
private String jsonCodeNoneBlank = "{\"userId\":\"abc@def.com\", \"deviceId\":\"abc123\", \"accessCode\":\" \"}";
private String jsonGetNewCodeUpcase = "{\"userId\":\"abc@def.com\", \"deviceId\":\"abc123\", \"accessCode\":\"NOCODE\", \"appVer\":\"v1.2.3\"}";
private String jsonGetNewCodeMixedcase = "{\"userId\":\"abc@def.com\", \"deviceId\":\"abc123\", \"accessCode\":\"noCoDe\", \"appVer\":\"v1.2.3\"}";
private String jsonCodeUpcase = "{\"userId\":\"abc@def.com\", \"deviceId\":\"abc123\", \"accessCode\":\"ABCDEF\", \"appVer\":\"v1.2.3\"}";
private String jsonCodeMixedcase = "{\"userId\":\"abc@def.com\", \"deviceId\":\"abc123\", \"accessCode\":\"aBcdeF\", \"appVer\":\"v1.2.3\"}";
private List<String> jsonInvalidNull = Arrays.asList(jsonInvalid1, jsonInvalid2, jsonInvalid3, jsonInvalid4, jsonInvalid5, jsonInvalid6);
private List<String> jsonInvalidBlank = Arrays.asList(jsonInvalid7, jsonInvalid8);
//Skipping testing malformed parameters (e.g.,wrong format or size)
//as Request class doesn't detect anyway
//Skipping testing for white space on access code since the Request class
//is not validating access code format (only that something that might be a code exists)
private Gson gson = new Gson();
private Request reqBasic;
private Request reqBlank;
private Request reqValid;
private Request reqCodeNoneNone;
private Request reqCodeNoneNull;
private Request reqCodeNoneBlank;
private Request reqGetNewCodeUpcase;
private Request reqGetNewCodeMixedcase;
private Request reqCodeUpcase;
private Request reqCodeMixedcase;
private List<Request> reqInvalidNull = new ArrayList<Request>();
private List<Request> reqInvalidBlank = new ArrayList<Request>();
public void setUp() throws Exception {
reqBasic = gson.fromJson(jsonstr, Request.class);
reqBlank = gson.fromJson(jsonblank, Request.class);
reqValid = gson.fromJson(jsonValid, Request.class);
for(String json : jsonInvalidNull){
reqInvalidNull.add(gson.fromJson(json, Request.class));
}
for(String json : jsonInvalidBlank){
reqInvalidBlank.add(gson.fromJson(json, Request.class));
}
reqCodeNoneNone = gson.fromJson(jsonCodeNoneNone, Request.class);
reqCodeNoneNull = gson.fromJson(jsonCodeNoneNull, Request.class);
reqCodeNoneBlank = gson.fromJson(jsonCodeNoneBlank, Request.class);
reqGetNewCodeUpcase = gson.fromJson(jsonGetNewCodeUpcase, Request.class);
reqGetNewCodeMixedcase = gson.fromJson(jsonGetNewCodeMixedcase, Request.class);
reqCodeUpcase = gson.fromJson(jsonCodeUpcase, Request.class);
reqCodeMixedcase = gson.fromJson(jsonCodeMixedcase, Request.class);
}
public void tearDown() throws Exception {
}
public void testBlankJson() throws Exception {
assertTrue(reqBlank.userId.isEmpty());
assertTrue(reqBlank.deviceId.isEmpty());
assertTrue(reqBlank.accessCode.isEmpty());
assertTrue(reqBlank.appVer.isEmpty());
}
//Is get Params supposed to be empty initially?
public void testGetParamsEmpty() throws Exception {
//initial state
assertTrue(reqBasic.getParams().isEmpty());
//manually force params to be null
//but it should return empty object (not null)
reqBasic.params = null;
assertNotNull(reqBasic.getParams());
assertTrue(reqBasic.getParams().isEmpty());
}
public void testPutParamsBeforeGetParamsCalled() throws Exception {
//Fails because params is null
try {
reqBasic.params.put("a", "A");
assertTrue(reqBasic.getParams().containsKey("a"));
} catch (NullPointerException ex) {
fail("Method behaves differently based on invisible internal state");
}
};
//same test as before, except getParams() has been called
public void testPutParamsAfterGetParamsCalled() throws Exception {
//Call the getParamsMethod
reqBasic.getParams(); //returns empty, but silently changes internal state
//Passes because params is has been set by method call
try {
reqBasic.params.put("a", "A");
assertTrue(reqBasic.getParams().containsKey("a"));
} catch (NullPointerException ex) {
fail("Method behaves differently based on invisible internal state");
}
};
public void testGetParamsBasic() throws Exception {
//side affect is it initializes params
//which also means params can be null
reqBasic.getParams();
reqBasic.params.put("a", "A");
reqBasic.params.put("b", "B");
reqBasic.params.put("null", null);
assertTrue(reqBasic.getParams().containsKey("a"));
assertTrue(reqBasic.getParams().containsKey("b"));
assertTrue(reqBasic.getParams().containsValue("A"));
assertTrue(reqBasic.getParams().containsValue("B"));
assertNull(reqBasic.getParams().get("null"));
}
public void testIsValidTrue() throws Exception {
assertTrue(reqValid.isValid());
}
public void testIsValidFalseNull() throws Exception {
for (Request req : reqInvalidNull){
assertFalse(req.isValid());
}
}
public void testIsValidFalseBlank() throws Exception {
for (Request req : reqInvalidBlank){
assertFalse(req.isValid());
}
}
public void testGetCodeTypeNoneNone() throws Exception {
assertEquals(reqCodeNoneNone.getCodeType(), Request.CodeType.NONE);
}
public void testGetCodeTypeNoneNull() throws Exception {
assertEquals(reqCodeNoneNull.getCodeType(), Request.CodeType.NONE);
}
public void testGetCodeTypeNoneBlank() throws Exception {
assertEquals(reqCodeNoneBlank.getCodeType(), Request.CodeType.NONE);
}
public void testGetCodeTypeNewCode() throws Exception {
assertEquals(reqGetNewCodeUpcase.getCodeType(), Request.CodeType.NOCODE);
assertEquals(reqGetNewCodeMixedcase.getCodeType(), Request.CodeType.NOCODE);
}
public void testGetCodeTypeExisting() throws Exception {
assertEquals(reqCodeUpcase.getCodeType(), Request.CodeType.CODE);
assertEquals(reqCodeMixedcase.getCodeType(), Request.CodeType.CODE);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment