Skip to content

Instantly share code, notes, and snippets.

@dmcg
Created March 4, 2015 21:35
Show Gist options
  • Save dmcg/e0c8bd293177055f87bd to your computer and use it in GitHub Desktop.
Save dmcg/e0c8bd293177055f87bd to your computer and use it in GitHub Desktop.
String user = "{" +
"isAccountNonLocked : true," +
"isEnabled : true," +
"isAccountNonExpired : true," +
"isPasswordProvided : true," +
"isCredentialsNonExpired : true," +
"}";
mockery.checking(new JsonExpectations(user));
public class JsonExpectations implements ExpectationBuilder {
private final JSONObject json;
public JsonExpectations(JSONObject json) {
this.json = json;
}
public JsonExpectations(String json) {
this(parse(json));
}
@Override
public void buildExpectations(Action action, ExpectationCollector expectationCollector) {
try {
for (int i = 0; i < json.length(); i++) {
String name = json.names().getString(i);
Object value = json.get(name);
expectationCollector.add(expecationBuilderFor(name, value).toExpectation(action));
}
} catch (JSONException e) {
throw new RuntimeException(e);
}
}
private InvocationExpectationBuilder expecationBuilderFor(String name, Object value) {
InvocationExpectationBuilder builder = new InvocationExpectationBuilder();
builder.method(name);
builder.setAction(new ReturnValueAction(value));
return builder;
}
private static JSONObject parse(String user) {
try {
return new JSONObject(user);
} catch (JSONException e) {
throw new RuntimeException(e);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment