mirror of
https://github.com/google/nomulus.git
synced 2025-05-03 13:37:51 +02:00
This change renames directories in preparation for the great package rename. The repository is now in a broken state because the code itself hasn't been updated. However this should ensure that git correctly preserves history for each file.
91 lines
3.4 KiB
Java
91 lines
3.4 KiB
Java
// Copyright 2016 The Domain Registry Authors. All Rights Reserved.
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
package com.google.domain.registry.security;
|
|
|
|
import static com.google.common.base.Suppliers.memoize;
|
|
import static com.google.common.truth.Truth.assertThat;
|
|
import static com.google.common.truth.Truth.assert_;
|
|
import static com.google.domain.registry.security.JsonHttp.JSON_SAFETY_PREFIX;
|
|
|
|
import com.google.common.base.Supplier;
|
|
|
|
import org.json.simple.JSONValue;
|
|
import org.json.simple.parser.ParseException;
|
|
|
|
import java.io.BufferedReader;
|
|
import java.io.StringReader;
|
|
import java.io.StringWriter;
|
|
import java.util.Map;
|
|
|
|
/**
|
|
* Helper class for testing JSON RPC servlets.
|
|
*/
|
|
public final class JsonHttpTestUtils {
|
|
|
|
/** Returns JSON payload for mocked result of {@code rsp.getReader()}. */
|
|
public static BufferedReader createJsonPayload(Map<String, ?> object) {
|
|
return createJsonPayload(JSONValue.toJSONString(object));
|
|
}
|
|
|
|
/** @see #createJsonPayload(Map) */
|
|
public static BufferedReader createJsonPayload(String jsonText) {
|
|
return new BufferedReader(new StringReader(jsonText));
|
|
}
|
|
|
|
/**
|
|
* Returns JSON data parsed out of a JsonTransportServlet response stored in the given writer.
|
|
* If the data will be fetched multiple times, consider {@link #createJsonResponseSupplier}.
|
|
*
|
|
* <p>Example Mockito usage:<pre> {@code
|
|
*
|
|
* StringWriter writer = new StringWriter();
|
|
* when(rsp.getWriter()).thenReturn(new PrintWriter(writer));
|
|
* servlet.service(req, rsp);
|
|
* assertThat(getJsonResponse(writer)).containsEntry("status", "SUCCESS");}</pre>
|
|
*/
|
|
public static Map<String, Object> getJsonResponse(StringWriter writer) {
|
|
String jsonText = writer.toString();
|
|
assertThat(jsonText).startsWith(JSON_SAFETY_PREFIX);
|
|
jsonText = jsonText.substring(JSON_SAFETY_PREFIX.length());
|
|
try {
|
|
@SuppressWarnings("unchecked")
|
|
Map<String, Object> json = (Map<String, Object>) JSONValue.parseWithException(jsonText);
|
|
return json;
|
|
} catch (ClassCastException | ParseException e) {
|
|
assert_().fail("Bad JSON: %s\n%s", e.getMessage(), jsonText);
|
|
throw new AssertionError();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Returns a memoized supplier that'll provide the JSON response object of the tested servlet.
|
|
*
|
|
* <p>This works with Mockito as follows:<pre> {@code
|
|
*
|
|
* StringWriter writer = new StringWriter();
|
|
* Supplier<Map<String, Object>> json = createJsonResponseSupplier(writer);
|
|
* when(rsp.getWriter()).thenReturn(new PrintWriter(writer));
|
|
* servlet.service(req, rsp);
|
|
* assertThat(json.get()).containsEntry("status", "SUCCESS");}</pre>
|
|
*/
|
|
public static Supplier<Map<String, Object>> createJsonResponseSupplier(
|
|
final StringWriter writer) {
|
|
return memoize(new Supplier<Map<String, Object>>() {
|
|
@Override
|
|
public Map<String, Object> get() {
|
|
return getJsonResponse(writer);
|
|
}});
|
|
}
|
|
}
|