mirror of
https://github.com/google/nomulus
synced 2026-08-18 05:06:16 +00:00
Inject a singleton Gson instead of creating it many times (#3058)
Creation of Gson objects is nontrivial and it's thread-safe so we might as well just use some singleton objects as much as possible rather than recreating them.
This commit is contained in:
@@ -28,9 +28,11 @@ import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import google.registry.keyring.api.Keyring;
|
||||
import google.registry.request.UrlConnectionService;
|
||||
import google.registry.testing.FakeClock;
|
||||
import google.registry.tools.GsonUtils;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
@@ -49,6 +51,7 @@ import org.mockito.junit.jupiter.MockitoExtension;
|
||||
class BsaCredentialTest {
|
||||
|
||||
private static final Duration AUTH_TOKEN_EXPIRY = Duration.ofMinutes(30);
|
||||
private static final Gson GSON = GsonUtils.provideGson();
|
||||
|
||||
@Mock OutputStream connectionOutputStream;
|
||||
@Mock HttpsURLConnection connection;
|
||||
@@ -60,7 +63,8 @@ class BsaCredentialTest {
|
||||
@BeforeEach
|
||||
void setup() throws Exception {
|
||||
credential =
|
||||
new BsaCredential(connectionService, "https://authUrl", AUTH_TOKEN_EXPIRY, keyring, clock);
|
||||
new BsaCredential(
|
||||
connectionService, "https://authUrl", AUTH_TOKEN_EXPIRY, keyring, GSON, clock);
|
||||
}
|
||||
|
||||
void setupHttp() throws Exception {
|
||||
|
||||
@@ -20,7 +20,7 @@ import static org.mockito.Mockito.when;
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import google.registry.groups.GmailClient;
|
||||
import google.registry.model.console.User;
|
||||
import google.registry.request.RequestModule;
|
||||
import google.registry.request.Modules;
|
||||
import google.registry.request.auth.AuthResult;
|
||||
import google.registry.security.XsrfTokenManager;
|
||||
import google.registry.ui.server.SendEmailUtils;
|
||||
@@ -50,6 +50,6 @@ public final class ConsoleApiParamsUtils {
|
||||
authResult,
|
||||
sendEmailUtils,
|
||||
xsrfTokenManager,
|
||||
RequestModule.provideGson());
|
||||
Modules.GsonModule.provideGson());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -55,6 +55,8 @@ class AuthModuleTest {
|
||||
private static final String ACCESS_TOKEN = "FakeAccessToken";
|
||||
private static final String REFRESH_TOKEN = "FakeReFreshToken";
|
||||
|
||||
private static final Gson GSON = GsonUtils.provideGson();
|
||||
|
||||
@SuppressWarnings("WeakerAccess")
|
||||
@TempDir
|
||||
Path folder;
|
||||
@@ -166,7 +168,7 @@ class AuthModuleTest {
|
||||
void test_provideLocalCredentialJson() {
|
||||
String credentialJson =
|
||||
AuthModule.provideLocalCredentialJson(
|
||||
AuthModuleTest::getSecrets, this::getCredential, null);
|
||||
AuthModuleTest::getSecrets, GSON, this::getCredential, null);
|
||||
Map<String, String> jsonMap =
|
||||
new Gson().fromJson(credentialJson, new TypeToken<Map<String, String>>() {}.getType());
|
||||
assertThat(jsonMap.get("type")).isEqualTo("authorized_user");
|
||||
@@ -182,7 +184,10 @@ class AuthModuleTest {
|
||||
Files.writeString(credentialFile.toPath(), "{some_field: some_value}");
|
||||
String credentialJson =
|
||||
AuthModule.provideLocalCredentialJson(
|
||||
AuthModuleTest::getSecrets, this::getCredential, credentialFile.getCanonicalPath());
|
||||
AuthModuleTest::getSecrets,
|
||||
GSON,
|
||||
this::getCredential,
|
||||
credentialFile.getCanonicalPath());
|
||||
assertThat(credentialJson).isEqualTo("{some_field: some_value}");
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user