mirror of
https://github.com/google/nomulus.git
synced 2025-08-04 17:01:51 +02:00
Automatically refactor more exception testing to use new JUnit rules
------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=178911894
This commit is contained in:
parent
36ad38e5df
commit
7dc224627f
125 changed files with 1970 additions and 1982 deletions
|
@ -21,9 +21,7 @@ import com.google.common.collect.ImmutableMap;
|
|||
import google.registry.testing.FakeResponse;
|
||||
import java.util.Map;
|
||||
import org.json.simple.JSONValue;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.JUnit4;
|
||||
|
||||
|
@ -31,9 +29,6 @@ import org.junit.runners.JUnit4;
|
|||
@RunWith(JUnit4.class)
|
||||
public class JsonResponseTest {
|
||||
|
||||
@Rule
|
||||
public final ExpectedException thrown = ExpectedException.none();
|
||||
|
||||
FakeResponse fakeResponse = new FakeResponse();
|
||||
JsonResponse jsonResponse = new JsonResponse(fakeResponse);
|
||||
|
||||
|
|
|
@ -16,23 +16,19 @@ package google.registry.request;
|
|||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
import static google.registry.request.RequestModule.provideJsonPayload;
|
||||
import static google.registry.testing.JUnitBackports.assertThrows;
|
||||
import static google.registry.testing.JUnitBackports.expectThrows;
|
||||
|
||||
import com.google.common.net.MediaType;
|
||||
import google.registry.request.HttpException.BadRequestException;
|
||||
import google.registry.request.HttpException.UnsupportedMediaTypeException;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.JUnit4;
|
||||
|
||||
/** Unit tests for {@link RequestModule}. */
|
||||
@RunWith(JUnit4.class)
|
||||
public final class RequestModuleTest {
|
||||
|
||||
@Rule
|
||||
public final ExpectedException thrown = ExpectedException.none();
|
||||
|
||||
@Test
|
||||
public void testProvideJsonPayload() throws Exception {
|
||||
assertThat(provideJsonPayload(MediaType.JSON_UTF_8, "{\"k\":\"v\"}"))
|
||||
|
@ -47,27 +43,30 @@ public final class RequestModuleTest {
|
|||
|
||||
@Test
|
||||
public void testProvideJsonPayload_malformedInput_throws500() throws Exception {
|
||||
thrown.expect(BadRequestException.class);
|
||||
thrown.expectMessage("Malformed JSON");
|
||||
provideJsonPayload(MediaType.JSON_UTF_8, "{\"k\":");
|
||||
BadRequestException thrown =
|
||||
expectThrows(
|
||||
BadRequestException.class, () -> provideJsonPayload(MediaType.JSON_UTF_8, "{\"k\":"));
|
||||
assertThat(thrown).hasMessageThat().contains("Malformed JSON");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testProvideJsonPayload_emptyInput_throws500() throws Exception {
|
||||
thrown.expect(BadRequestException.class);
|
||||
thrown.expectMessage("Malformed JSON");
|
||||
provideJsonPayload(MediaType.JSON_UTF_8, "");
|
||||
BadRequestException thrown =
|
||||
expectThrows(BadRequestException.class, () -> provideJsonPayload(MediaType.JSON_UTF_8, ""));
|
||||
assertThat(thrown).hasMessageThat().contains("Malformed JSON");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testProvideJsonPayload_nonJsonContentType_throws415() throws Exception {
|
||||
thrown.expect(UnsupportedMediaTypeException.class);
|
||||
provideJsonPayload(MediaType.PLAIN_TEXT_UTF_8, "{}");
|
||||
assertThrows(
|
||||
UnsupportedMediaTypeException.class,
|
||||
() -> provideJsonPayload(MediaType.PLAIN_TEXT_UTF_8, "{}"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testProvideJsonPayload_contentTypeWithWeirdParam_throws415() throws Exception {
|
||||
thrown.expect(UnsupportedMediaTypeException.class);
|
||||
provideJsonPayload(MediaType.JSON_UTF_8.withParameter("omg", "handel"), "{}");
|
||||
assertThrows(
|
||||
UnsupportedMediaTypeException.class,
|
||||
() -> provideJsonPayload(MediaType.JSON_UTF_8.withParameter("omg", "handel"), "{}"));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -23,6 +23,7 @@ import static google.registry.request.RequestParameters.extractOptionalEnumParam
|
|||
import static google.registry.request.RequestParameters.extractOptionalParameter;
|
||||
import static google.registry.request.RequestParameters.extractRequiredDatetimeParameter;
|
||||
import static google.registry.request.RequestParameters.extractRequiredParameter;
|
||||
import static google.registry.testing.JUnitBackports.expectThrows;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
|
@ -30,19 +31,13 @@ import com.google.common.collect.ImmutableMap;
|
|||
import google.registry.request.HttpException.BadRequestException;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import org.joda.time.DateTime;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.JUnit4;
|
||||
|
||||
/** Unit tests for {@link RequestParameters}. */
|
||||
@RunWith(JUnit4.class)
|
||||
public class RequestParametersTest {
|
||||
|
||||
@Rule
|
||||
public final ExpectedException thrown = ExpectedException.none();
|
||||
|
||||
private final HttpServletRequest req = mock(HttpServletRequest.class);
|
||||
|
||||
@Test
|
||||
|
@ -53,17 +48,17 @@ public class RequestParametersTest {
|
|||
|
||||
@Test
|
||||
public void testExtractRequiredParameter_notPresent_throwsBadRequest() throws Exception {
|
||||
thrown.expect(BadRequestException.class);
|
||||
thrown.expectMessage("spin");
|
||||
extractRequiredParameter(req, "spin");
|
||||
BadRequestException thrown =
|
||||
expectThrows(BadRequestException.class, () -> extractRequiredParameter(req, "spin"));
|
||||
assertThat(thrown).hasMessageThat().contains("spin");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExtractRequiredParameter_empty_throwsBadRequest() throws Exception {
|
||||
when(req.getParameter("spin")).thenReturn("");
|
||||
thrown.expect(BadRequestException.class);
|
||||
thrown.expectMessage("spin");
|
||||
extractRequiredParameter(req, "spin");
|
||||
BadRequestException thrown =
|
||||
expectThrows(BadRequestException.class, () -> extractRequiredParameter(req, "spin"));
|
||||
assertThat(thrown).hasMessageThat().contains("spin");
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -146,9 +141,10 @@ public class RequestParametersTest {
|
|||
@Test
|
||||
public void testExtractEnumValue_nonExistentValue_throwsBadRequest() throws Exception {
|
||||
when(req.getParameter("spin")).thenReturn("sing");
|
||||
thrown.expect(BadRequestException.class);
|
||||
thrown.expectMessage("spin");
|
||||
extractEnumParameter(req, Club.class, "spin");
|
||||
BadRequestException thrown =
|
||||
expectThrows(
|
||||
BadRequestException.class, () -> extractEnumParameter(req, Club.class, "spin"));
|
||||
assertThat(thrown).hasMessageThat().contains("spin");
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -166,9 +162,10 @@ public class RequestParametersTest {
|
|||
@Test
|
||||
public void testOptionalExtractEnumValue_nonExistentValue_throwsBadRequest() throws Exception {
|
||||
when(req.getParameter("spin")).thenReturn("sing");
|
||||
thrown.expect(BadRequestException.class);
|
||||
thrown.expectMessage("spin");
|
||||
extractOptionalEnumParameter(req, Club.class, "spin");
|
||||
BadRequestException thrown =
|
||||
expectThrows(
|
||||
BadRequestException.class, () -> extractOptionalEnumParameter(req, Club.class, "spin"));
|
||||
assertThat(thrown).hasMessageThat().contains("spin");
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -181,9 +178,10 @@ public class RequestParametersTest {
|
|||
@Test
|
||||
public void testExtractRequiredDatetimeParameter_badValue_throwsBadRequest() throws Exception {
|
||||
when(req.getParameter("timeParam")).thenReturn("Tuesday at three o'clock");
|
||||
thrown.expect(BadRequestException.class);
|
||||
thrown.expectMessage("timeParam");
|
||||
extractRequiredDatetimeParameter(req, "timeParam");
|
||||
BadRequestException thrown =
|
||||
expectThrows(
|
||||
BadRequestException.class, () -> extractRequiredDatetimeParameter(req, "timeParam"));
|
||||
assertThat(thrown).hasMessageThat().contains("timeParam");
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -196,9 +194,10 @@ public class RequestParametersTest {
|
|||
@Test
|
||||
public void testExtractOptionalDatetimeParameter_badValue_throwsBadRequest() throws Exception {
|
||||
when(req.getParameter("timeParam")).thenReturn("Tuesday at three o'clock");
|
||||
thrown.expect(BadRequestException.class);
|
||||
thrown.expectMessage("timeParam");
|
||||
extractOptionalDatetimeParameter(req, "timeParam");
|
||||
BadRequestException thrown =
|
||||
expectThrows(
|
||||
BadRequestException.class, () -> extractOptionalDatetimeParameter(req, "timeParam"));
|
||||
assertThat(thrown).hasMessageThat().contains("timeParam");
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -209,8 +208,9 @@ public class RequestParametersTest {
|
|||
|
||||
@Test
|
||||
public void testExtractRequiredDatetimeParameter_noValue_throwsBadRequest() throws Exception {
|
||||
thrown.expect(BadRequestException.class);
|
||||
thrown.expectMessage("timeParam");
|
||||
extractRequiredDatetimeParameter(req, "timeParam");
|
||||
BadRequestException thrown =
|
||||
expectThrows(
|
||||
BadRequestException.class, () -> extractRequiredDatetimeParameter(req, "timeParam"));
|
||||
assertThat(thrown).hasMessageThat().contains("timeParam");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -24,9 +24,7 @@ import static org.mockito.Mockito.when;
|
|||
import java.io.PrintWriter;
|
||||
import java.io.StringWriter;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.JUnit4;
|
||||
|
||||
|
@ -34,9 +32,6 @@ import org.junit.runners.JUnit4;
|
|||
@RunWith(JUnit4.class)
|
||||
public class ResponseImplTest {
|
||||
|
||||
@Rule
|
||||
public final ExpectedException thrown = ExpectedException.none();
|
||||
|
||||
private final HttpServletResponse rsp = mock(HttpServletResponse.class);
|
||||
|
||||
@Test
|
||||
|
|
|
@ -17,13 +17,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 static google.registry.testing.JUnitBackports.expectThrows;
|
||||
|
||||
import com.google.common.base.Function;
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.Callable;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.JUnit4;
|
||||
|
||||
|
@ -31,18 +30,17 @@ import org.junit.runners.JUnit4;
|
|||
@RunWith(JUnit4.class)
|
||||
public final class RouterTest {
|
||||
|
||||
@Rule
|
||||
public final ExpectedException thrown = ExpectedException.none();
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
public interface Empty {}
|
||||
|
||||
@Test
|
||||
public void testRoute_noRoutes_throws() throws Exception {
|
||||
thrown.expect(IllegalArgumentException.class);
|
||||
thrown.expectMessage("No routes found for class: google.registry.request.RouterTest.Empty");
|
||||
Router.create(Empty.class);
|
||||
IllegalArgumentException thrown =
|
||||
expectThrows(IllegalArgumentException.class, () -> Router.create(Empty.class));
|
||||
assertThat(thrown)
|
||||
.hasMessageThat()
|
||||
.contains("No routes found for class: google.registry.request.RouterTest.Empty");
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
@ -144,10 +142,13 @@ public final class RouterTest {
|
|||
|
||||
@Test
|
||||
public void testRoute_methodsInComponentAreIgnored_throws() throws Exception {
|
||||
thrown.expect(IllegalArgumentException.class);
|
||||
thrown.expectMessage(
|
||||
"No routes found for class: google.registry.request.RouterTest.WeirdMethodsComponent");
|
||||
Router.create(WeirdMethodsComponent.class);
|
||||
IllegalArgumentException thrown =
|
||||
expectThrows(
|
||||
IllegalArgumentException.class, () -> Router.create(WeirdMethodsComponent.class));
|
||||
assertThat(thrown)
|
||||
.hasMessageThat()
|
||||
.contains(
|
||||
"No routes found for class: google.registry.request.RouterTest.WeirdMethodsComponent");
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
@ -171,8 +172,8 @@ public final class RouterTest {
|
|||
|
||||
@Test
|
||||
public void testCreate_twoTasksWithSameMethodAndPath_resultsInError() throws Exception {
|
||||
thrown.expect(IllegalArgumentException.class);
|
||||
thrown.expectMessage("Multiple entries with same key");
|
||||
Router.create(DuplicateComponent.class);
|
||||
IllegalArgumentException thrown =
|
||||
expectThrows(IllegalArgumentException.class, () -> Router.create(DuplicateComponent.class));
|
||||
assertThat(thrown).hasMessageThat().contains("Multiple entries with same key");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -17,6 +17,7 @@ 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 google.registry.testing.JUnitBackports.expectThrows;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verifyZeroInteractions;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
@ -38,7 +39,6 @@ import javax.servlet.http.HttpServletRequest;
|
|||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.JUnit4;
|
||||
|
||||
|
@ -48,10 +48,6 @@ public class RequestAuthenticatorTest {
|
|||
|
||||
@Rule
|
||||
public final AppEngineRule appEngine = AppEngineRule.builder().build();
|
||||
|
||||
@Rule
|
||||
public final ExpectedException thrown = ExpectedException.none();
|
||||
|
||||
private static final AuthSettings AUTH_NONE = AuthSettings.create(
|
||||
ImmutableList.of(AuthMethod.INTERNAL),
|
||||
AuthLevel.NONE,
|
||||
|
@ -391,44 +387,55 @@ public class RequestAuthenticatorTest {
|
|||
|
||||
@Test
|
||||
public void testCheckAuthConfig_NoMethods_failure() throws Exception {
|
||||
thrown.expect(IllegalArgumentException.class);
|
||||
thrown.expectMessage("Must specify at least one auth method");
|
||||
|
||||
RequestAuthenticator.checkAuthConfig(AUTH_NO_METHODS);
|
||||
IllegalArgumentException thrown =
|
||||
expectThrows(
|
||||
IllegalArgumentException.class,
|
||||
() -> RequestAuthenticator.checkAuthConfig(AUTH_NO_METHODS));
|
||||
assertThat(thrown).hasMessageThat().contains("Must specify at least one auth method");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCheckAuthConfig_WrongMethodOrdering_failure() throws Exception {
|
||||
thrown.expect(IllegalArgumentException.class);
|
||||
thrown
|
||||
.expectMessage("Auth methods must be unique and strictly in order - INTERNAL, API, LEGACY");
|
||||
|
||||
RequestAuthenticator.checkAuthConfig(AUTH_WRONG_METHOD_ORDERING);
|
||||
IllegalArgumentException thrown =
|
||||
expectThrows(
|
||||
IllegalArgumentException.class,
|
||||
() -> RequestAuthenticator.checkAuthConfig(AUTH_WRONG_METHOD_ORDERING));
|
||||
assertThat(thrown)
|
||||
.hasMessageThat()
|
||||
.contains("Auth methods must be unique and strictly in order - INTERNAL, API, LEGACY");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCheckAuthConfig_DuplicateMethods_failure() throws Exception {
|
||||
thrown.expect(IllegalArgumentException.class);
|
||||
thrown
|
||||
.expectMessage("Auth methods must be unique and strictly in order - INTERNAL, API, LEGACY");
|
||||
|
||||
RequestAuthenticator.checkAuthConfig(AUTH_DUPLICATE_METHODS);
|
||||
IllegalArgumentException thrown =
|
||||
expectThrows(
|
||||
IllegalArgumentException.class,
|
||||
() -> RequestAuthenticator.checkAuthConfig(AUTH_DUPLICATE_METHODS));
|
||||
assertThat(thrown)
|
||||
.hasMessageThat()
|
||||
.contains("Auth methods must be unique and strictly in order - INTERNAL, API, LEGACY");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCheckAuthConfig_InternalWithUser_failure() throws Exception {
|
||||
thrown.expect(IllegalArgumentException.class);
|
||||
thrown.expectMessage("Actions with INTERNAL auth method may not require USER auth level");
|
||||
|
||||
RequestAuthenticator.checkAuthConfig(AUTH_INTERNAL_WITH_USER);
|
||||
IllegalArgumentException thrown =
|
||||
expectThrows(
|
||||
IllegalArgumentException.class,
|
||||
() -> RequestAuthenticator.checkAuthConfig(AUTH_INTERNAL_WITH_USER));
|
||||
assertThat(thrown)
|
||||
.hasMessageThat()
|
||||
.contains("Actions with INTERNAL auth method may not require USER auth level");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCheckAuthConfig_WronglyIgnoringUser_failure() throws Exception {
|
||||
thrown.expect(IllegalArgumentException.class);
|
||||
thrown.expectMessage(
|
||||
"Actions with auth methods beyond INTERNAL must not specify the IGNORED user policy");
|
||||
|
||||
RequestAuthenticator.checkAuthConfig(AUTH_WRONGLY_IGNORING_USER);
|
||||
IllegalArgumentException thrown =
|
||||
expectThrows(
|
||||
IllegalArgumentException.class,
|
||||
() -> RequestAuthenticator.checkAuthConfig(AUTH_WRONGLY_IGNORING_USER));
|
||||
assertThat(thrown)
|
||||
.hasMessageThat()
|
||||
.contains(
|
||||
"Actions with auth methods beyond INTERNAL must not specify the IGNORED user policy");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -29,7 +29,6 @@ import org.joda.time.Duration;
|
|||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.JUnit4;
|
||||
|
||||
|
@ -43,9 +42,6 @@ public final class LockHandlerImplTest {
|
|||
public final AppEngineRule appEngine = AppEngineRule.builder()
|
||||
.build();
|
||||
|
||||
@Rule
|
||||
public final ExpectedException thrown = ExpectedException.none();
|
||||
|
||||
private static class CountingCallable implements Callable<Void> {
|
||||
int numCalled = 0;
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue