### Question:
RestAuthRedirectCallbackHandler extends AbstractRestAuthCallbackHandler<RedirectCallback> { public JsonValue convertToJson(RedirectCallback callback, int index) throws RestAuthException { JsonValue callbacksJson = json(array( createOutputField("redirectUrl", callback.getRedirectUrl()), createOutputField("redirectMethod", callback.getMethod()), createOutputField("trackingCookie", callback.getTrackingCookie()))); JsonValue jsonValue = json(object( field("type", CALLBACK_NAME), field("output", callbacksJson.getObject()) )); if (callback.getRedirectData() != null) { callbacksJson.add(createOutputField("redirectData", callback.getRedirectData())); } return jsonValue; }  RedirectCallback handle(HttpServletRequest request, HttpServletResponse response,
            JsonValue postBody, RedirectCallback originalCallback); String getCallbackClassName(); JsonValue convertToJson(RedirectCallback callback, int index); RedirectCallback convertFromJson(RedirectCallback callback, JsonValue jsonCallback);  }### Answer:
@Test public void shouldFailConvertToJson() throws RestAuthException { RedirectCallback redirectCallback = mock(RedirectCallback.class); given(redirectCallback.getRedirectUrl()).willReturn("REDIRECT_URL"); given(redirectCallback.getMethod()).willReturn("REDIRECT_METHOD"); given(redirectCallback.getRedirectData()).willReturn(Collections.<String, String>emptyMap()); JsonValue json = restAuthRedirectCallbackHandler.convertToJson(redirectCallback, 1); assertThat(json.asMap()).hasSize(2); assertThat(json.get("type").asString()).isEqualTo("RedirectCallback"); assertThat(json.get("output").asList()).hasSize(4); assertThat(json.get("output").get(0).get("name").asString()).isEqualTo("redirectUrl"); assertThat(json.get("output").get(0).get("value").asString()).isEqualTo("REDIRECT_URL"); assertThat(json.get("output").get(1).get("name").asString()).isEqualTo("redirectMethod"); assertThat(json.get("output").get(1).get("value").asString()).isEqualTo("REDIRECT_METHOD"); assertThat(json.get("output").get(2).get("name").asString()).isEqualTo("trackingCookie"); assertThat(json.get("output").get(2).get("value").asBoolean()).isEqualTo(false); assertThat(json.get("output").get(3).get("name").asString()).isEqualTo("redirectData"); assertThat(json.get("output").get(3).get("value").asMap()).hasSize(0); }
@Test public void shouldSerialiseToJsonCorrectly() throws Exception { RedirectCallback redirectCallback = mock(RedirectCallback.class); final Map<String, String> redirectData = Collections.singletonMap("foo", "bar"); given(redirectCallback.getRedirectUrl()).willReturn("REDIRECT_URL"); given(redirectCallback.getMethod()).willReturn("REDIRECT_METHOD"); given(redirectCallback.getRedirectData()).willReturn(redirectData); String json = JsonValueBuilder.getObjectMapper().writeValueAsString( restAuthRedirectCallbackHandler.convertToJson(redirectCallback, 1).getObject()); JsonValue parsed = JsonValueBuilder.toJsonValue(json); assertThat(parsed).stringAt("/type").isEqualTo("RedirectCallback"); assertThat(parsed).hasArray("/output").hasSize(4); assertThat(parsed).hasObject("/output/0").containsExactly(entry("name", "redirectUrl"), entry("value", "REDIRECT_URL")); assertThat(parsed).hasObject("/output/1").containsExactly(entry("name", "redirectMethod"), entry("value", "REDIRECT_METHOD")); assertThat(parsed).hasObject("/output/2").containsExactly(entry("name", "trackingCookie"), entry("value", false)); assertThat(parsed).hasObject("/output/3").containsExactly(entry("name", "redirectData"), entry("value", redirectData)); }