mirror of
https://github.com/google/nomulus.git
synced 2025-08-04 17:01:51 +02:00
Switch from Guava Optionals to Java 8 Optionals
This was a surprisingly involved change. Some of the difficulties included java.util.Optional purposely not being Serializable (so I had to move a few Optionals in mapreduce classes to @Nullable) and having to add the Truth Java8 extension library for assertion support. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=171863777
This commit is contained in:
parent
184b2b56ac
commit
c0f8da0c6e
581 changed files with 1325 additions and 932 deletions
|
@ -15,10 +15,11 @@ java_library(
|
|||
"//java/google/registry/request/auth",
|
||||
"//java/google/registry/security",
|
||||
"//javatests/google/registry/testing",
|
||||
"//third_party/java/truth",
|
||||
"@com_google_appengine_api_1_0_sdk//:testonly",
|
||||
"@com_google_guava",
|
||||
"@com_google_guava_testlib",
|
||||
"@com_google_truth",
|
||||
"@com_google_truth_extensions_truth_java8_extension",
|
||||
"@com_googlecode_json_simple",
|
||||
"@javax_inject",
|
||||
"@javax_servlet_api",
|
||||
|
|
|
@ -15,6 +15,7 @@
|
|||
package google.registry.request;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
import static com.google.common.truth.Truth8.assertThat;
|
||||
import static google.registry.request.JsonResponse.JSON_SAFETY_PREFIX;
|
||||
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
|
|
|
@ -15,6 +15,7 @@
|
|||
package google.registry.request;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
import static com.google.common.truth.Truth8.assertThat;
|
||||
import static google.registry.request.Action.Method.GET;
|
||||
import static google.registry.request.Action.Method.POST;
|
||||
import static google.registry.request.auth.Auth.AUTH_INTERNAL_OR_ADMIN;
|
||||
|
@ -26,7 +27,6 @@ import static org.mockito.Mockito.verifyZeroInteractions;
|
|||
import static org.mockito.Mockito.when;
|
||||
|
||||
import com.google.appengine.api.users.User;
|
||||
import com.google.common.base.Optional;
|
||||
import com.google.common.testing.NullPointerTester;
|
||||
import google.registry.request.HttpException.ServiceUnavailableException;
|
||||
import google.registry.request.auth.AuthLevel;
|
||||
|
@ -38,6 +38,7 @@ import google.registry.testing.Providers;
|
|||
import google.registry.testing.UserInfo;
|
||||
import java.io.PrintWriter;
|
||||
import java.io.StringWriter;
|
||||
import java.util.Optional;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import org.junit.After;
|
||||
|
@ -412,7 +413,7 @@ public final class RequestHandlerTest {
|
|||
|
||||
assertThat(providedAuthResult).isNotNull();
|
||||
assertThat(providedAuthResult.authLevel()).isEqualTo(AuthLevel.NONE);
|
||||
assertThat(providedAuthResult.userAuthInfo()).isAbsent();
|
||||
assertThat(providedAuthResult.userAuthInfo()).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -420,7 +421,7 @@ public final class RequestHandlerTest {
|
|||
when(req.getMethod()).thenReturn("GET");
|
||||
when(req.getRequestURI()).thenReturn("/auth/adminUser");
|
||||
when(requestAuthenticator.authorize(AUTH_INTERNAL_OR_ADMIN.authSettings(), req))
|
||||
.thenReturn(Optional.<AuthResult>absent());
|
||||
.thenReturn(Optional.<AuthResult>empty());
|
||||
|
||||
handler.handleRequest(req, rsp);
|
||||
|
||||
|
@ -442,7 +443,7 @@ public final class RequestHandlerTest {
|
|||
assertThat(providedAuthResult.authLevel()).isEqualTo(AuthLevel.USER);
|
||||
assertThat(providedAuthResult.userAuthInfo()).isPresent();
|
||||
assertThat(providedAuthResult.userAuthInfo().get().user()).isEqualTo(testUser);
|
||||
assertThat(providedAuthResult.userAuthInfo().get().oauthTokenInfo()).isAbsent();
|
||||
assertThat(providedAuthResult.userAuthInfo().get().oauthTokenInfo()).isEmpty();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -15,6 +15,7 @@
|
|||
package google.registry.request;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
import static com.google.common.truth.Truth8.assertThat;
|
||||
import static google.registry.request.RequestModule.provideJsonPayload;
|
||||
|
||||
import com.google.common.net.MediaType;
|
||||
|
|
|
@ -15,6 +15,7 @@
|
|||
package google.registry.request;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
import static com.google.common.truth.Truth8.assertThat;
|
||||
import static google.registry.request.RequestParameters.extractBooleanParameter;
|
||||
import static google.registry.request.RequestParameters.extractEnumParameter;
|
||||
import static google.registry.request.RequestParameters.extractOptionalDatetimeParameter;
|
||||
|
@ -70,13 +71,13 @@ public class RequestParametersTest {
|
|||
|
||||
@Test
|
||||
public void testExtractOptionalParameter_notPresent_returnsAbsent() throws Exception {
|
||||
assertThat(extractOptionalParameter(req, "spin")).isAbsent();
|
||||
assertThat(extractOptionalParameter(req, "spin")).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExtractOptionalParameter_empty_returnsAbsent() throws Exception {
|
||||
when(req.getParameter("spin")).thenReturn("");
|
||||
assertThat(extractOptionalParameter(req, "spin")).isAbsent();
|
||||
assertThat(extractOptionalParameter(req, "spin")).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -177,7 +178,7 @@ public class RequestParametersTest {
|
|||
@Test
|
||||
public void testExtractOptionalDatetimeParameter_empty_returnsAbsent() throws Exception {
|
||||
when(req.getParameter("timeParam")).thenReturn("");
|
||||
assertThat(extractOptionalDatetimeParameter(req, "timeParam")).isAbsent();
|
||||
assertThat(extractOptionalDatetimeParameter(req, "timeParam")).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
|
@ -16,6 +16,7 @@ package google.registry.request;
|
|||
|
||||
import static com.google.common.net.MediaType.PLAIN_TEXT_UTF_8;
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
import static com.google.common.truth.Truth8.assertThat;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.verifyNoMoreInteractions;
|
||||
|
|
|
@ -15,11 +15,12 @@
|
|||
package google.registry.request;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
import static com.google.common.truth.Truth8.assertThat;
|
||||
import static google.registry.request.auth.Auth.AUTH_INTERNAL_ONLY;
|
||||
|
||||
import com.google.common.base.Function;
|
||||
import com.google.common.base.Optional;
|
||||
import google.registry.testing.ExceptionRule;
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.Callable;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
|
@ -67,12 +68,12 @@ public final class RouterTest {
|
|||
|
||||
@Test
|
||||
public void testRoute_pathMismatch_returnsAbsent() throws Exception {
|
||||
assertThat(Router.create(SlothComponent.class).route("/doge")).isAbsent();
|
||||
assertThat(Router.create(SlothComponent.class).route("/doge")).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRoute_pathIsAPrefix_notAllowedByDefault() throws Exception {
|
||||
assertThat(Router.create(SlothComponent.class).route("/sloth/extra")).isAbsent();
|
||||
assertThat(Router.create(SlothComponent.class).route("/sloth/extra")).isEmpty();
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
@ -95,10 +96,10 @@ public final class RouterTest {
|
|||
|
||||
@Test
|
||||
public void testRoute_prefixDoesNotMatch_returnsAbsent() throws Exception {
|
||||
assertThat(Router.create(PrefixComponent.class).route("")).isAbsent();
|
||||
assertThat(Router.create(PrefixComponent.class).route("/")).isAbsent();
|
||||
assertThat(Router.create(PrefixComponent.class).route("/ulysses")).isAbsent();
|
||||
assertThat(Router.create(PrefixComponent.class).route("/man/of/sadness")).isAbsent();
|
||||
assertThat(Router.create(PrefixComponent.class).route("")).isEmpty();
|
||||
assertThat(Router.create(PrefixComponent.class).route("/")).isEmpty();
|
||||
assertThat(Router.create(PrefixComponent.class).route("/ulysses")).isEmpty();
|
||||
assertThat(Router.create(PrefixComponent.class).route("/man/of/sadness")).isEmpty();
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
|
|
@ -17,12 +17,13 @@ java_library(
|
|||
"//java/google/registry/security",
|
||||
"//javatests/google/registry/testing",
|
||||
"//third_party/java/objectify:objectify-v4_1",
|
||||
"//third_party/java/truth",
|
||||
"@com_google_appengine_api_1_0_sdk//:testonly",
|
||||
"@com_google_appengine_tools_appengine_gcs_client",
|
||||
"@com_google_appengine_tools_sdk",
|
||||
"@com_google_code_findbugs_jsr305",
|
||||
"@com_google_guava",
|
||||
"@com_google_truth",
|
||||
"@com_google_truth_extensions_truth_java8_extension",
|
||||
"@javax_servlet_api",
|
||||
"@junit",
|
||||
"@org_mockito_all",
|
||||
|
|
|
@ -16,13 +16,13 @@ package google.registry.request.auth;
|
|||
|
||||
import static com.google.common.net.HttpHeaders.AUTHORIZATION;
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
import static com.google.common.truth.Truth8.assertThat;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verifyZeroInteractions;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import com.google.appengine.api.users.User;
|
||||
import com.google.appengine.api.users.UserService;
|
||||
import com.google.common.base.Optional;
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
import google.registry.request.auth.RequestAuthenticator.AuthMethod;
|
||||
|
@ -34,6 +34,7 @@ import google.registry.testing.ExceptionRule;
|
|||
import google.registry.testing.FakeClock;
|
||||
import google.registry.testing.FakeOAuthService;
|
||||
import google.registry.testing.FakeUserService;
|
||||
import java.util.Optional;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
|
@ -155,7 +156,7 @@ public class RequestAuthenticatorTest {
|
|||
verifyZeroInteractions(mockUserService);
|
||||
assertThat(authResult).isPresent();
|
||||
assertThat(authResult.get().authLevel()).isEqualTo(AuthLevel.APP);
|
||||
assertThat(authResult.get().userAuthInfo()).isAbsent();
|
||||
assertThat(authResult.get().userAuthInfo()).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -163,7 +164,7 @@ public class RequestAuthenticatorTest {
|
|||
Optional<AuthResult> authResult = runTest(mockUserService, AUTH_INTERNAL_ONLY);
|
||||
|
||||
verifyZeroInteractions(mockUserService);
|
||||
assertThat(authResult).isAbsent();
|
||||
assertThat(authResult).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -175,14 +176,14 @@ public class RequestAuthenticatorTest {
|
|||
verifyZeroInteractions(mockUserService);
|
||||
assertThat(authResult).isPresent();
|
||||
assertThat(authResult.get().authLevel()).isEqualTo(AuthLevel.APP);
|
||||
assertThat(authResult.get().userAuthInfo()).isAbsent();
|
||||
assertThat(authResult.get().userAuthInfo()).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAnyUserAnyMethod_notLoggedIn() throws Exception {
|
||||
Optional<AuthResult> authResult = runTest(fakeUserService, AUTH_ANY_USER_ANY_METHOD);
|
||||
|
||||
assertThat(authResult).isAbsent();
|
||||
assertThat(authResult).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -191,7 +192,7 @@ public class RequestAuthenticatorTest {
|
|||
|
||||
Optional<AuthResult> authResult = runTest(fakeUserService, AUTH_ANY_USER_ANY_METHOD);
|
||||
|
||||
assertThat(authResult).isAbsent();
|
||||
assertThat(authResult).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -207,7 +208,7 @@ public class RequestAuthenticatorTest {
|
|||
assertThat(authResult.get().userAuthInfo()).isPresent();
|
||||
assertThat(authResult.get().userAuthInfo().get().user()).isEqualTo(testUser);
|
||||
assertThat(authResult.get().userAuthInfo().get().isUserAdmin()).isFalse();
|
||||
assertThat(authResult.get().userAuthInfo().get().oauthTokenInfo()).isAbsent();
|
||||
assertThat(authResult.get().userAuthInfo().get().oauthTokenInfo()).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -221,14 +222,14 @@ public class RequestAuthenticatorTest {
|
|||
assertThat(authResult.get().authLevel()).isEqualTo(AuthLevel.USER);
|
||||
assertThat(authResult.get().userAuthInfo()).isPresent();
|
||||
assertThat(authResult.get().userAuthInfo().get().user()).isEqualTo(testUser);
|
||||
assertThat(authResult.get().userAuthInfo().get().oauthTokenInfo()).isAbsent();
|
||||
assertThat(authResult.get().userAuthInfo().get().oauthTokenInfo()).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAdminUserAnyMethod_notLoggedIn() throws Exception {
|
||||
Optional<AuthResult> authResult = runTest(fakeUserService, AUTH_ADMIN_USER_ANY_METHOD);
|
||||
|
||||
assertThat(authResult).isAbsent();
|
||||
assertThat(authResult).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -237,7 +238,7 @@ public class RequestAuthenticatorTest {
|
|||
|
||||
Optional<AuthResult> authResult = runTest(fakeUserService, AUTH_ADMIN_USER_ANY_METHOD);
|
||||
|
||||
assertThat(authResult).isAbsent();
|
||||
assertThat(authResult).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -246,7 +247,7 @@ public class RequestAuthenticatorTest {
|
|||
|
||||
Optional<AuthResult> authResult = runTest(fakeUserService, AUTH_ADMIN_USER_ANY_METHOD);
|
||||
|
||||
assertThat(authResult).isAbsent();
|
||||
assertThat(authResult).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -262,7 +263,7 @@ public class RequestAuthenticatorTest {
|
|||
assertThat(authResult.get().userAuthInfo()).isPresent();
|
||||
assertThat(authResult.get().userAuthInfo().get().user()).isEqualTo(testUser);
|
||||
assertThat(authResult.get().userAuthInfo().get().isUserAdmin()).isTrue();
|
||||
assertThat(authResult.get().userAuthInfo().get().oauthTokenInfo()).isAbsent();
|
||||
assertThat(authResult.get().userAuthInfo().get().oauthTokenInfo()).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -317,7 +318,7 @@ public class RequestAuthenticatorTest {
|
|||
|
||||
Optional<AuthResult> authResult = runTest(fakeUserService, AUTH_ANY_USER_NO_LEGACY);
|
||||
|
||||
assertThat(authResult).isAbsent();
|
||||
assertThat(authResult).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -329,7 +330,7 @@ public class RequestAuthenticatorTest {
|
|||
|
||||
Optional<AuthResult> authResult = runTest(fakeUserService, AUTH_ANY_USER_NO_LEGACY);
|
||||
|
||||
assertThat(authResult).isAbsent();
|
||||
assertThat(authResult).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -341,7 +342,7 @@ public class RequestAuthenticatorTest {
|
|||
|
||||
Optional<AuthResult> authResult = runTest(fakeUserService, AUTH_ANY_USER_NO_LEGACY);
|
||||
|
||||
assertThat(authResult).isAbsent();
|
||||
assertThat(authResult).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -353,7 +354,7 @@ public class RequestAuthenticatorTest {
|
|||
|
||||
Optional<AuthResult> authResult = runTest(fakeUserService, AUTH_ANY_USER_NO_LEGACY);
|
||||
|
||||
assertThat(authResult).isAbsent();
|
||||
assertThat(authResult).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -385,7 +386,7 @@ public class RequestAuthenticatorTest {
|
|||
|
||||
Optional<AuthResult> authResult = runTest(fakeUserService, AUTH_ANY_USER_NO_LEGACY);
|
||||
|
||||
assertThat(authResult).isAbsent();
|
||||
assertThat(authResult).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
|
@ -16,12 +16,13 @@ java_library(
|
|||
"//java/google/registry/request/lock",
|
||||
"//javatests/google/registry/testing",
|
||||
"//third_party/java/objectify:objectify-v4_1",
|
||||
"//third_party/java/truth",
|
||||
"@com_google_appengine_api_1_0_sdk//:testonly",
|
||||
"@com_google_appengine_tools_appengine_gcs_client",
|
||||
"@com_google_appengine_tools_sdk",
|
||||
"@com_google_code_findbugs_jsr305",
|
||||
"@com_google_guava",
|
||||
"@com_google_truth",
|
||||
"@com_google_truth_extensions_truth_java8_extension",
|
||||
"@javax_servlet_api",
|
||||
"@joda_time",
|
||||
"@junit",
|
||||
|
|
|
@ -15,15 +15,16 @@
|
|||
package google.registry.request.lock;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
import static com.google.common.truth.Truth8.assertThat;
|
||||
import static org.junit.Assert.fail;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
import com.google.common.base.Optional;
|
||||
import google.registry.model.server.Lock;
|
||||
import google.registry.testing.AppEngineRule;
|
||||
import google.registry.testing.ExceptionRule;
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.Callable;
|
||||
import javax.annotation.Nullable;
|
||||
import org.joda.time.Duration;
|
||||
|
@ -77,7 +78,7 @@ public final class LockHandlerImplTest {
|
|||
assertThat(resourceName).isEqualTo("resourceName");
|
||||
assertThat(tld).isEqualTo("tld");
|
||||
assertThat(leaseLength).isEqualTo(ONE_DAY);
|
||||
return Optional.fromNullable(acquiredLock);
|
||||
return Optional.ofNullable(acquiredLock);
|
||||
}
|
||||
};
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue