Skip to content

Instantly share code, notes, and snippets.

@daichan4649
Last active December 22, 2015 23:29
Show Gist options
  • Save daichan4649/6547019 to your computer and use it in GitHub Desktop.
Save daichan4649/6547019 to your computer and use it in GitHub Desktop.
JSONIC test (decode to enum)
public class JsonicUtil {
public enum ResponseType {
TEST_1(Test1Response.class), ;
private Class<? extends JsonResponse> decodeTargetClazz;
private ResponseType(Class<? extends JsonResponse> clazz) {
this.decodeTargetClazz = clazz;
}
public Class<?> getDecodeTargetClass() {
return decodeTargetClazz;
}
}
public interface JsonResponse {
// marker
}
/**
* decode(json -> JsonResponse)
* @param responseType
* @param jsonText
* @return
*/
public static JsonResponse decode(ResponseType responseType, String jsonText) {
return JSON.decode(jsonText, responseType.getDecodeTargetClass());
}
}
public class JsonicUtilTest {
@Test
public void decodeTest() {
// ざっくり疎通
ResponseType responseType = null;
String jsonText = null;
JsonResponse actual;
JsonResponse expected;
responseType = ResponseType.TEST_1;
jsonText = "{\"number\": 100, \"text\": abeshi, \"type\": AAA, \"status\": 1}";
expected = new Test1Response(100, "abeshi", Type.AAA, "1");
actual = JsonicUtil.decode(responseType, jsonText);
System.out.println(actual);
Assert.assertEquals(expected, actual);
}
// type(JSON文字列=enum型文字列)
@Test
public void decodeType1() {
// type=空文字 --> null
ResponseType responseType;
String jsonText;
JsonResponse actual;
JsonResponse expected;
responseType = ResponseType.TEST_1;
jsonText = "{\"number\": 10, \"text\": abeshi, \"type\": \"\", \"status\": \"\"}";
expected = new Test1Response(10, "abeshi", null, "");
actual = JsonicUtil.decode(responseType, jsonText);
System.out.println(actual);
Assert.assertEquals(expected, actual);
}
@Test
public void decodeType2() {
// type=enumの型文字列 --> decode成功
// JSONICはvalueOf実行結果を比較するみたい。
ResponseType responseType;
String jsonText;
JsonResponse actual;
JsonResponse expected;
responseType = ResponseType.TEST_1;
jsonText = "{\"number\": 10, \"text\": abeshi, \"type\": AAA, \"status\": 1}";
expected = new Test1Response(10, "abeshi", Type.AAA, "1");
actual = JsonicUtil.decode(responseType, jsonText);
System.out.println(actual);
Assert.assertEquals(expected, actual);
}
@Test(expected = JSONException.class)
public void decodeType3() {
// type=文字列(enumの型文字列以外) --> JSONException(parse失敗)
ResponseType responseType;
String jsonText;
responseType = ResponseType.TEST_1;
jsonText = "{\"number\": 10, \"text\": abeshi, \"type\": ZZZ, \"status\": 1}";
JsonicUtil.decode(responseType, jsonText);
}
// status(JSON文字列をenumとして内部で使用するためのロジック)
@Test
public void decodeStatusOk() {
// status=1 --> StatusType.OK
ResponseType responseType;
String jsonText;
JsonResponse actual;
JsonResponse expected;
responseType = ResponseType.TEST_1;
jsonText = "{\"number\": 10, \"text\": abeshi, \"type\": AAA, \"status\": 1}";
expected = new Test1Response(10, "abeshi", Type.AAA, "1");
actual = JsonicUtil.decode(responseType, jsonText);
System.out.println(actual);
Assert.assertEquals(expected, actual);
}
@Test
public void decodeStatusNg() {
// status=2 --> StatusType.NG
ResponseType responseType;
String jsonText;
JsonResponse actual;
JsonResponse expected;
responseType = ResponseType.TEST_1;
jsonText = "{\"number\": 10, \"text\": abeshi, \"type\": AAA, \"status\": 2}";
expected = new Test1Response(10, "abeshi", Type.AAA, "2");
actual = JsonicUtil.decode(responseType, jsonText);
System.out.println(actual);
Assert.assertEquals(expected, actual);
}
}
public class Test1Response implements JsonResponse {
private int number;
private String text;
private Type type;
private String status;
public Test1Response() {
}
public Test1Response(int number, String text, Type type, String status) {
this.number = number;
this.text = text;
this.type = type;
this.status = status;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public Type getType() {
return type;
}
public void setType(Type type) {
this.type = type;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
/**
*
* @return
*/
public StatusType getStatusType() {
return StatusType.fromStringValue(status);
}
public enum Type {
NONE(""),
AAA("100"),
BBB("200"),
CCC("300");
private String stringValue;
private Type(String stringValue) {
this.stringValue = stringValue;
}
public String getStringValue() {
return stringValue;
}
}
public enum StatusType {
NONE(""),
OK("1"),
NG("2"),
CANCEL("3");
private String stringValue;
private StatusType(String stringValue) {
this.stringValue = stringValue;
}
public String getStringValue() {
return stringValue;
}
public static StatusType fromStringValue(String stringValue) {
for (StatusType type : StatusType.values()) {
if (type == NONE) {
continue;
}
if (type.stringValue.equals(stringValue)) {
return type;
}
}
return NONE;
}
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("number=").append(number);
sb.append(", text=").append(text);
sb.append(", type=").append(type);
sb.append(", status=").append(status);
sb.append(", statusType=").append(getStatusType());
return sb.toString();
}
@Override
public boolean equals(Object obj) {
if (obj == null || !(obj instanceof Test1Response)) {
return false;
}
Test1Response target = ((Test1Response) obj);
if (target.getNumber() != number) {
return false;
}
if (!target.getText().equals(text)) {
return false;
}
if (target.getType() != type) {
return false;
}
if (!target.getStatus().equals(status)) {
return false;
}
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment