Refactor AppEngineConnection

AppEngineConnection can now connect to all services and not just the tools.

The default is still the tools.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=218734983
This commit is contained in:
guyben 2018-10-25 12:57:16 -07:00 committed by jianglai
parent 97aa98eb35
commit b48061b792
31 changed files with 269 additions and 329 deletions

View file

@ -23,7 +23,6 @@ import com.google.common.collect.ImmutableMap;
import com.google.common.io.Files;
import com.google.common.net.MediaType;
import google.registry.testing.UriParameters;
import google.registry.tools.CommandWithConnection.Connection;
import java.io.File;
import java.nio.charset.StandardCharsets;
import org.mockito.ArgumentCaptor;
@ -46,13 +45,14 @@ public abstract class CreateOrUpdatePremiumListCommandTestCase<
}
void verifySentParams(
Connection connection, String path, ImmutableMap<String, String> parameterMap)
throws Exception {
verify(connection).send(
eq(path),
urlParamCaptor.capture(),
eq(MediaType.FORM_DATA),
requestBodyCaptor.capture());
AppEngineConnection connection, String path, ImmutableMap<String, String> parameterMap)
throws Exception {
verify(connection)
.sendPostRequest(
eq(path),
urlParamCaptor.capture(),
eq(MediaType.FORM_DATA),
requestBodyCaptor.capture());
assertThat(new ImmutableMap.Builder<String, String>()
.putAll(urlParamCaptor.getValue())
.putAll(UriParameters.parse(new String(requestBodyCaptor.getValue(), UTF_8)).entries())

View file

@ -28,7 +28,6 @@ import com.beust.jcommander.ParameterException;
import com.google.common.base.VerifyException;
import com.google.common.collect.ImmutableMap;
import com.google.common.net.MediaType;
import google.registry.tools.CommandWithConnection.Connection;
import google.registry.tools.server.CreatePremiumListAction;
import org.junit.Before;
import org.junit.Test;
@ -38,8 +37,7 @@ import org.mockito.Mock;
public class CreatePremiumListCommandTest<C extends CreatePremiumListCommand>
extends CreateOrUpdatePremiumListCommandTestCase<C> {
@Mock
Connection connection;
@Mock AppEngineConnection connection;
String premiumTermsPath;
String premiumTermsCsv;
@ -53,12 +51,12 @@ public class CreatePremiumListCommandTest<C extends CreatePremiumListCommand>
"example_premium_terms.csv",
loadFile(CreatePremiumListCommandTest.class, "example_premium_terms.csv"));
servletPath = "/_dr/admin/createPremiumList";
when(connection.send(
eq(CreatePremiumListAction.PATH),
anyMapOf(String.class, String.class),
any(MediaType.class),
any(byte[].class)))
.thenReturn(JSON_SAFETY_PREFIX + "{\"status\":\"success\",\"lines\":[]}");
when(connection.sendPostRequest(
eq(CreatePremiumListAction.PATH),
anyMapOf(String.class, String.class),
any(MediaType.class),
any(byte[].class)))
.thenReturn(JSON_SAFETY_PREFIX + "{\"status\":\"success\",\"lines\":[]}");
}
@Test
@ -86,13 +84,12 @@ public class CreatePremiumListCommandTest<C extends CreatePremiumListCommand>
public void testRun_errorResponse() throws Exception {
reset(connection);
command.setConnection(connection);
when(connection.send(
eq(CreatePremiumListAction.PATH),
anyMapOf(String.class, String.class),
any(MediaType.class),
any(byte[].class)))
.thenReturn(
JSON_SAFETY_PREFIX + "{\"status\":\"error\",\"error\":\"foo already exists\"}");
when(connection.sendPostRequest(
eq(CreatePremiumListAction.PATH),
anyMapOf(String.class, String.class),
any(MediaType.class),
any(byte[].class)))
.thenReturn(JSON_SAFETY_PREFIX + "{\"status\":\"error\",\"error\":\"foo already exists\"}");
VerifyException thrown =
assertThrows(
VerifyException.class, () -> runCommandForced("-i=" + premiumTermsPath, "-n=foo"));

View file

@ -34,7 +34,6 @@ import com.google.common.collect.Range;
import com.google.common.net.MediaType;
import google.registry.model.registrar.Registrar;
import google.registry.testing.CertificateSamples;
import google.registry.tools.CommandWithConnection.Connection;
import java.io.IOException;
import java.util.Optional;
import org.joda.money.CurrencyUnit;
@ -47,8 +46,7 @@ import org.mockito.Mockito;
/** Unit tests for {@link CreateRegistrarCommand}. */
public class CreateRegistrarCommandTest extends CommandTestCase<CreateRegistrarCommand> {
@Mock
private Connection connection;
@Mock private AppEngineConnection connection;
@Before
public void init() {
@ -93,11 +91,12 @@ public class CreateRegistrarCommandTest extends CommandTestCase<CreateRegistrarC
assertThat(registrar.getPremiumPriceAckRequired()).isFalse();
assertThat(registrar.getPoNumber()).isEmpty();
verify(connection).send(
eq("/_dr/admin/createGroups"),
eq(ImmutableMap.of("clientId", "clientz")),
eq(MediaType.PLAIN_TEXT_UTF_8),
eq(new byte[0]));
verify(connection)
.sendPostRequest(
eq("/_dr/admin/createGroups"),
eq(ImmutableMap.of("clientId", "clientz")),
eq(MediaType.PLAIN_TEXT_UTF_8),
eq(new byte[0]));
}
@Test
@ -210,12 +209,11 @@ public class CreateRegistrarCommandTest extends CommandTestCase<CreateRegistrarC
@SuppressWarnings("unchecked")
@Test
public void testFailure_groupCreationFails() throws Exception {
when(
connection.send(
Mockito.anyString(),
Mockito.anyMapOf(String.class, String.class),
Mockito.any(MediaType.class),
Mockito.any(byte[].class)))
when(connection.sendPostRequest(
Mockito.anyString(),
Mockito.anyMapOf(String.class, String.class),
Mockito.any(MediaType.class),
Mockito.any(byte[].class)))
.thenThrow(new IOException("BAD ROBOT NO COOKIE"));
runCommandForced(
"--name=blobio",

View file

@ -21,7 +21,6 @@ import static org.mockito.Mockito.verify;
import com.google.common.collect.ImmutableMap;
import com.google.common.net.MediaType;
import google.registry.tools.CommandWithConnection.Connection;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
@ -30,8 +29,7 @@ import org.mockito.Mock;
public class CreateRegistrarGroupsCommandTest extends
CommandTestCase<CreateRegistrarGroupsCommand> {
@Mock
private Connection connection;
@Mock private AppEngineConnection connection;
@Before
public void init() {
@ -41,16 +39,18 @@ public class CreateRegistrarGroupsCommandTest extends
@Test
public void test_createGroupsForTwoRegistrars() throws Exception {
runCommandForced("NewRegistrar", "TheRegistrar");
verify(connection).send(
eq("/_dr/admin/createGroups"),
eq(ImmutableMap.of("clientId", "NewRegistrar")),
eq(MediaType.PLAIN_TEXT_UTF_8),
eq(new byte[0]));
verify(connection).send(
eq("/_dr/admin/createGroups"),
eq(ImmutableMap.of("clientId", "TheRegistrar")),
eq(MediaType.PLAIN_TEXT_UTF_8),
eq(new byte[0]));
verify(connection)
.sendPostRequest(
eq("/_dr/admin/createGroups"),
eq(ImmutableMap.of("clientId", "NewRegistrar")),
eq(MediaType.PLAIN_TEXT_UTF_8),
eq(new byte[0]));
verify(connection)
.sendPostRequest(
eq("/_dr/admin/createGroups"),
eq(ImmutableMap.of("clientId", "TheRegistrar")),
eq(MediaType.PLAIN_TEXT_UTF_8),
eq(new byte[0]));
assertInStdout("Success!");
}

View file

@ -16,13 +16,19 @@ package google.registry.tools;
import static com.google.common.truth.Truth.assertThat;
import static google.registry.testing.JUnitBackports.assertThrows;
import static google.registry.tools.AppEngineConnection.Service.BACKEND;
import static google.registry.tools.AppEngineConnection.Service.DEFAULT;
import static google.registry.tools.AppEngineConnection.Service.PUBAPI;
import static google.registry.tools.AppEngineConnection.Service.TOOLS;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;
import com.google.common.collect.ImmutableMap;
import com.google.common.net.MediaType;
import google.registry.tools.CommandWithConnection.Connection;
import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
@ -31,34 +37,42 @@ import org.mockito.Mock;
/** Unit tests for {@link RefreshDnsForAllDomainsCommand}. */
public class CurlCommandTest extends CommandTestCase<CurlCommand> {
@Mock private Connection connection;
@Mock private AppEngineConnection connection;
@Mock private AppEngineConnection connectionForService;
@Before
public void init() {
command.setConnection(connection);
when(connection.withService(any())).thenReturn(connectionForService);
}
@Captor ArgumentCaptor<ImmutableMap<String, String>> urlParamCaptor;
@Test
public void testGetInvocation() throws Exception {
runCommand("--path=/foo/bar?a=1&b=2");
verify(connection)
runCommand("--path=/foo/bar?a=1&b=2", "--service=TOOLS");
verify(connection).withService(TOOLS);
verifyNoMoreInteractions(connection);
verify(connectionForService)
.sendGetRequest(eq("/foo/bar?a=1&b=2"), eq(ImmutableMap.<String, String>of()));
}
@Test
public void testExplicitGetInvocation() throws Exception {
runCommand("--path=/foo/bar?a=1&b=2", "--request=GET");
verify(connection)
runCommand("--path=/foo/bar?a=1&b=2", "--request=GET", "--service=BACKEND");
verify(connection).withService(BACKEND);
verifyNoMoreInteractions(connection);
verify(connectionForService)
.sendGetRequest(eq("/foo/bar?a=1&b=2"), eq(ImmutableMap.<String, String>of()));
}
@Test
public void testPostInvocation() throws Exception {
runCommand("--path=/foo/bar?a=1&b=2", "--data=some data");
verify(connection)
.send(
runCommand("--path=/foo/bar?a=1&b=2", "--data=some data", "--service=DEFAULT");
verify(connection).withService(DEFAULT);
verifyNoMoreInteractions(connection);
verify(connectionForService)
.sendPostRequest(
eq("/foo/bar?a=1&b=2"),
eq(ImmutableMap.<String, String>of()),
eq(MediaType.PLAIN_TEXT_UTF_8),
@ -67,9 +81,12 @@ public class CurlCommandTest extends CommandTestCase<CurlCommand> {
@Test
public void testMultiDataPost() throws Exception {
runCommand("--path=/foo/bar?a=1&b=2", "--data=first=100", "-d", "second=200");
verify(connection)
.send(
runCommand(
"--path=/foo/bar?a=1&b=2", "--data=first=100", "-d", "second=200", "--service=PUBAPI");
verify(connection).withService(PUBAPI);
verifyNoMoreInteractions(connection);
verify(connectionForService)
.sendPostRequest(
eq("/foo/bar?a=1&b=2"),
eq(ImmutableMap.<String, String>of()),
eq(MediaType.PLAIN_TEXT_UTF_8),
@ -78,9 +95,11 @@ public class CurlCommandTest extends CommandTestCase<CurlCommand> {
@Test
public void testExplicitPostInvocation() throws Exception {
runCommand("--path=/foo/bar?a=1&b=2", "--request=POST");
verify(connection)
.send(
runCommand("--path=/foo/bar?a=1&b=2", "--request=POST", "--service=TOOLS");
verify(connection).withService(TOOLS);
verifyNoMoreInteractions(connection);
verify(connectionForService)
.sendPostRequest(
eq("/foo/bar?a=1&b=2"),
eq(ImmutableMap.<String, String>of()),
eq(MediaType.PLAIN_TEXT_UTF_8),
@ -94,7 +113,10 @@ public class CurlCommandTest extends CommandTestCase<CurlCommand> {
IllegalArgumentException.class,
() ->
runCommand(
"--path=/foo/bar?a=1&b=2", "--request=GET", "--data=inappropriate data"));
"--path=/foo/bar?a=1&b=2",
"--request=GET",
"--data=inappropriate data",
"--service=TOOLS"));
assertThat(thrown).hasMessageThat().contains("You may not specify a body for a get method.");
}
}

View file

@ -20,7 +20,7 @@ import com.google.api.client.auth.oauth2.Credential;
import com.google.api.client.http.HttpRequest;
import com.google.api.client.http.HttpRequestFactory;
import com.google.api.client.http.HttpRequestInitializer;
import com.google.common.net.HostAndPort;
import google.registry.config.RegistryConfig;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@ -50,10 +50,8 @@ public class DefaultRequestFactoryModuleTest {
@Test
public void test_provideHttpRequestFactory_localhost() {
// Make sure that localhost creates a request factory with an initializer.
HttpRequestFactory factory =
module.provideHttpRequestFactory(
new AppEngineConnectionFlags(HostAndPort.fromParts("localhost", 1000)),
() -> FAKE_CREDENTIAL);
RegistryConfig.CONFIG_SETTINGS.get().appEngine.isLocal = true;
HttpRequestFactory factory = module.provideHttpRequestFactory(() -> FAKE_CREDENTIAL);
HttpRequestInitializer initializer = factory.getInitializer();
assertThat(initializer).isNotNull();
assertThat(initializer).isNotSameAs(FAKE_CREDENTIAL);
@ -62,11 +60,8 @@ public class DefaultRequestFactoryModuleTest {
@Test
public void test_provideHttpRequestFactory_remote() {
// Make sure that example.com creates a request factory with the UNITTEST client id but no
// initializer.
HttpRequestFactory factory =
module.provideHttpRequestFactory(
new AppEngineConnectionFlags(HostAndPort.fromParts("example.com", 1000)),
() -> FAKE_CREDENTIAL);
RegistryConfig.CONFIG_SETTINGS.get().appEngine.isLocal = false;
HttpRequestFactory factory = module.provideHttpRequestFactory(() -> FAKE_CREDENTIAL);
assertThat(factory.getInitializer()).isSameAs(FAKE_CREDENTIAL);
}
}

View file

@ -28,7 +28,6 @@ import com.google.common.base.Splitter;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.net.MediaType;
import google.registry.tools.CommandWithConnection.Connection;
import google.registry.tools.server.ToolsTestData;
import java.net.URLDecoder;
import java.util.Map;
@ -49,7 +48,7 @@ import org.mockito.ArgumentCaptor;
*/
public class EppToolVerifier {
private final Connection connection = mock(Connection.class);
private final AppEngineConnection connection = mock(AppEngineConnection.class);
private String clientId;
private boolean superuser;
@ -167,11 +166,9 @@ public class EppToolVerifier {
return;
}
ArgumentCaptor<byte[]> params = ArgumentCaptor.forClass(byte[].class);
verify(connection, atLeast(0)).send(
eq("/_dr/epptool"),
eq(ImmutableMap.of()),
eq(MediaType.FORM_DATA),
params.capture());
verify(connection, atLeast(0))
.sendPostRequest(
eq("/_dr/epptool"), eq(ImmutableMap.of()), eq(MediaType.FORM_DATA), params.capture());
capturedParams = ImmutableList.copyOf(params.getAllValues());
paramIndex = 0;
}
@ -198,7 +195,7 @@ public class EppToolVerifier {
}
/** Returns the (mock) Connection that is being monitored by this verifier. */
private Connection getConnection() {
private AppEngineConnection getConnection() {
return connection;
}
}

View file

@ -60,7 +60,7 @@ public class ListDomainsCommandTest extends ListObjectsCommandTestCase<ListDomai
public void test_bothParamsSpecified() throws Exception {
runCommand("--tlds=foo,bar", "--limit=100");
verify(connection)
.send(
.sendPostRequest(
eq(getTaskPath()),
eq(ImmutableMap.of("tlds", "foo,bar", "limit", 100)),
eq(MediaType.PLAIN_TEXT_UTF_8),
@ -73,7 +73,7 @@ public class ListDomainsCommandTest extends ListObjectsCommandTestCase<ListDomai
persistResource(newRegistry("fake", "FAKE").asBuilder().setTldType(TldType.TEST).build());
runCommand();
verify(connection)
.send(
.sendPostRequest(
eq(getTaskPath()),
eq(ImmutableMap.of("tlds", "tldone,tldtwo", "limit", Integer.MAX_VALUE)),
eq(MediaType.PLAIN_TEXT_UTF_8),

View file

@ -28,7 +28,6 @@ import static org.mockito.Mockito.when;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.net.MediaType;
import google.registry.tools.CommandWithConnection.Connection;
import java.util.Optional;
import org.junit.Before;
import org.junit.Test;
@ -38,7 +37,7 @@ import org.mockito.Mock;
public abstract class ListObjectsCommandTestCase<C extends ListObjectsCommand>
extends CommandTestCase<C> {
@Mock Connection connection;
@Mock AppEngineConnection connection;
/** Where to find the servlet task; set by the subclass. */
abstract String getTaskPath();
@ -62,7 +61,7 @@ public abstract class ListObjectsCommandTestCase<C extends ListObjectsCommand>
.collect(toImmutableList());
}
command.setConnection(connection);
when(connection.send(
when(connection.sendPostRequest(
eq(getTaskPath()),
anyMapOf(String.class, Object.class),
eq(MediaType.PLAIN_TEXT_UTF_8),
@ -82,7 +81,7 @@ public abstract class ListObjectsCommandTestCase<C extends ListObjectsCommand>
fullFieldNames.ifPresent(aBoolean -> params.put(FULL_FIELD_NAMES_PARAM, aBoolean));
params.putAll(getOtherParameters());
verify(connection)
.send(
.sendPostRequest(
eq(getTaskPath()), eq(params.build()), eq(MediaType.PLAIN_TEXT_UTF_8), eq(new byte[0]));
}

View file

@ -24,7 +24,6 @@ import static org.mockito.Mockito.verifyZeroInteractions;
import com.google.common.collect.ImmutableMap;
import com.google.common.net.MediaType;
import google.registry.model.registrar.Registrar;
import google.registry.tools.CommandWithConnection.Connection;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@ -32,7 +31,7 @@ import org.junit.runners.JUnit4;
@RunWith(JUnit4.class)
public class LoadTestCommandTest extends CommandTestCase<LoadTestCommand> {
Connection connection = mock(Connection.class);
AppEngineConnection connection = mock(AppEngineConnection.class);
@Before
public void setUp() {
@ -55,11 +54,9 @@ public class LoadTestCommandTest extends CommandTestCase<LoadTestCommand> {
.put("contactInfos", 1)
.put("runSeconds", 4600)
.build();
verify(connection).send(
eq("/_dr/loadtest"),
eq(parms),
eq(MediaType.PLAIN_TEXT_UTF_8),
eq(new byte[0]));
verify(connection)
.sendPostRequest(
eq("/_dr/loadtest"), eq(parms), eq(MediaType.PLAIN_TEXT_UTF_8), eq(new byte[0]));
}
@Test
@ -86,11 +83,9 @@ public class LoadTestCommandTest extends CommandTestCase<LoadTestCommand> {
.put("contactInfos", 15)
.put("runSeconds", 16)
.build();
verify(connection).send(
eq("/_dr/loadtest"),
eq(parms),
eq(MediaType.PLAIN_TEXT_UTF_8),
eq(new byte[0]));
verify(connection)
.sendPostRequest(
eq("/_dr/loadtest"), eq(parms), eq(MediaType.PLAIN_TEXT_UTF_8), eq(new byte[0]));
}
@Test

View file

@ -23,7 +23,6 @@ import static org.mockito.Mockito.when;
import com.google.common.collect.ImmutableMap;
import com.google.common.net.MediaType;
import google.registry.tools.CommandWithConnection.Connection;
import google.registry.tools.server.UpdatePremiumListAction;
import org.junit.Before;
import org.junit.Test;
@ -33,8 +32,7 @@ import org.mockito.Mock;
public class UpdatePremiumListCommandTest<C extends UpdatePremiumListCommand>
extends CreateOrUpdatePremiumListCommandTestCase<C> {
@Mock
Connection connection;
@Mock AppEngineConnection connection;
String premiumTermsPath;
String premiumTermsCsv;
@ -48,12 +46,12 @@ public class UpdatePremiumListCommandTest<C extends UpdatePremiumListCommand>
writeToNamedTmpFile(
"example_premium_terms.csv",
loadFile(UpdatePremiumListCommandTest.class, "example_premium_terms.csv"));
when(connection.send(
eq(UpdatePremiumListAction.PATH),
anyMapOf(String.class, String.class),
any(MediaType.class),
any(byte[].class)))
.thenReturn(JSON_SAFETY_PREFIX + "{\"status\":\"success\",\"lines\":[]}");
when(connection.sendPostRequest(
eq(UpdatePremiumListAction.PATH),
anyMapOf(String.class, String.class),
any(MediaType.class),
any(byte[].class)))
.thenReturn(JSON_SAFETY_PREFIX + "{\"status\":\"success\",\"lines\":[]}");
}
@Test

View file

@ -27,7 +27,6 @@ import static org.mockito.Mockito.when;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import google.registry.model.registrar.Registrar;
import google.registry.tools.CommandWithConnection.Connection;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
@ -35,7 +34,7 @@ import org.mockito.Mock;
/** Unit tests for {@link VerifyOteCommand}. */
public class VerifyOteCommandTest extends CommandTestCase<VerifyOteCommand> {
@Mock private Connection connection;
@Mock private AppEngineConnection connection;
@Before
public void init() throws Exception {