Centralize RDAP test initialization and login

All RDAP actions inherit from a common RdapActionBase class.

All RDAP search actions inherit from a comman RdapSearchActionBase class, which inherits from RdapActionBase

Each of the base classes has @Before initialization needed for the tests, as well as utility functions (such as login and logout).

Currently, these were copied in all test classes. Instead, we created a similar test inheritance tree to centralize the initialization and place common utility functions.

This way, the @Before of every test only needs to initialize the variables new to the specific action, making the code somewhat clearer.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=217856844
This commit is contained in:
guyben 2018-10-19 05:37:29 -07:00 committed by jianglai
parent b254269d2f
commit 3a3b0b738a
45 changed files with 456 additions and 758 deletions

View file

@ -16,73 +16,22 @@ package google.registry.rdap;
import static com.google.common.truth.Truth.assertThat;
import static google.registry.testing.TestDataHelper.loadFile;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import com.google.appengine.api.users.User;
import com.google.common.collect.ImmutableMap;
import google.registry.model.ofy.Ofy;
import google.registry.rdap.RdapMetrics.EndpointType;
import google.registry.rdap.RdapMetrics.SearchType;
import google.registry.rdap.RdapMetrics.WildcardType;
import google.registry.rdap.RdapSearchResults.IncompletenessWarningType;
import google.registry.request.Action;
import google.registry.request.auth.AuthLevel;
import google.registry.request.auth.AuthResult;
import google.registry.request.auth.UserAuthInfo;
import google.registry.testing.FakeClock;
import google.registry.testing.FakeResponse;
import google.registry.testing.InjectRule;
import google.registry.ui.server.registrar.AuthenticatedRegistrarAccessor;
import java.util.Optional;
import org.joda.time.DateTime;
import org.json.simple.JSONValue;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
/** Unit tests for {@link RdapHelpAction}. */
@RunWith(JUnit4.class)
public class RdapHelpActionTest {
public class RdapHelpActionTest extends RdapActionBaseTestCase<RdapHelpAction> {
@Rule
public final InjectRule inject = new InjectRule();
private final FakeResponse response = new FakeResponse();
private final FakeClock clock = new FakeClock(DateTime.parse("2000-01-01TZ"));
private final AuthenticatedRegistrarAccessor registrarAccessor =
mock(AuthenticatedRegistrarAccessor.class);
private final User user = new User("rdap.user@example.com", "gmail.com", "12345");
private final UserAuthInfo userAuthInfo = UserAuthInfo.create(user, false);
private final RdapMetrics rdapMetrics = mock(RdapMetrics.class);
private RdapHelpAction action;
@Before
public void setUp() {
inject.setStaticField(Ofy.class, "clock", clock);
action = new RdapHelpAction();
action.clock = clock;
action.fullServletPath = "https://example.tld/rdap";
action.requestMethod = Action.Method.GET;
action.registrarAccessor = registrarAccessor;
action.authResult = AuthResult.create(AuthLevel.USER, userAuthInfo);
action.includeDeletedParam = Optional.empty();
action.registrarParam = Optional.empty();
action.formatOutputParam = Optional.empty();
action.response = response;
action.rdapJsonFormatter = RdapTestHelper.getTestRdapJsonFormatter();
action.rdapWhoisServer = null;
action.rdapMetrics = rdapMetrics;
}
private Object generateActualJson(String helpPath) {
action.requestPath = RdapHelpAction.PATH + helpPath;
action.run();
return JSONValue.parse(response.getPayload());
public RdapHelpActionTest() {
super(RdapHelpAction.class, RdapHelpAction.PATH);
}
private Object generateExpectedJson(String name, String expectedOutputFile) {