### Question:
RestAuthPasswordCallbackHandler extends AbstractRestAuthCallbackHandler<PasswordCallback> { public PasswordCallback convertFromJson(PasswordCallback callback, JsonValue jsonCallback) throws RestAuthException { validateCallbackType(CALLBACK_NAME, jsonCallback); JsonValue input = jsonCallback.get("input"); if (input.size() != 1) { throw new JsonException("JSON Callback does not include a input field"); } JsonValue inputField = input.get(0); String value = inputField.get("value").asString(); callback.setPassword(value.toCharArray()); return callback; }  PasswordCallback handle(HttpServletRequest request, HttpServletResponse response,
            JsonValue postBody, PasswordCallback originalCallback); String getCallbackClassName(); JsonValue convertToJson(PasswordCallback callback, int index); PasswordCallback convertFromJson(PasswordCallback callback, JsonValue jsonCallback);  }### Answer:
@Test public void shouldConvertFromJson() throws RestAuthException { PasswordCallback passwordCallback = new PasswordCallback("Enter password:", false); JsonValue jsonPasswordCallback = JsonValueBuilder.jsonValue() .array("input") .addLast(JsonValueBuilder.jsonValue().put("value", "PASSWORD").build()) .array("output") .addLast(JsonValueBuilder.jsonValue().put("value", "Enter password:").build()) .put("type", "PasswordCallback") .build(); PasswordCallback convertedPasswordCallback = restAuthPasswordCallbackHandler.convertFromJson(passwordCallback, jsonPasswordCallback); Assert.assertEquals(passwordCallback, convertedPasswordCallback); Assert.assertEquals("Enter password:", convertedPasswordCallback.getPrompt()); Assert.assertEquals("PASSWORD", new String(convertedPasswordCallback.getPassword())); }
@Test (expectedExceptions = RestAuthException.class) public void shouldFailToConvertFromJsonWithInvalidType() throws RestAuthException { PasswordCallback passwordCallback = new PasswordCallback("Enter password:", false); JsonValue jsonPasswordCallback = JsonValueBuilder.jsonValue() .array("input") .addLast(JsonValueBuilder.jsonValue().put("value", "PASSWORD").build()) .array("output") .addLast(JsonValueBuilder.jsonValue().put("value", "Enter password:").build()) .put("type", "NameCallback") .build(); restAuthPasswordCallbackHandler.convertFromJson(passwordCallback, jsonPasswordCallback); fail(); }
@Test public void shouldNotFailToConvertFromJsonWithTypeLowerCase() throws RestAuthException { PasswordCallback passwordCallback = new PasswordCallback("Enter password:", false); JsonValue jsonPasswordCallback = JsonValueBuilder.jsonValue() .array("input") .addLast(JsonValueBuilder.jsonValue().put("value", "PASSWORD").build()) .array("output") .addLast(JsonValueBuilder.jsonValue().put("value", "Enter password:").build()) .put("type", "passwordcallback") .build(); PasswordCallback convertedPasswordCallback = restAuthPasswordCallbackHandler.convertFromJson(passwordCallback, jsonPasswordCallback); Assert.assertEquals(passwordCallback, convertedPasswordCallback); Assert.assertEquals("Enter password:", convertedPasswordCallback.getPrompt()); Assert.assertEquals("PASSWORD", new String(convertedPasswordCallback.getPassword())); }