mirror of
https://github.com/google/nomulus.git
synced 2025-07-21 18:26:12 +02:00
Daggerize/Actionize the load snapshot servlet
This is needed to eliminate the last non-injected use of BigqueryFactory. It gets rid of the deprecated HttpServletUtils now that we don't have lots of separate servlets, and changes the check snapshot servlet's usage of such. It also fixes up some related minor style issues in the update snapshot action (and its injectable parameters). ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=120547998
This commit is contained in:
parent
9e7934684e
commit
d65bf2a714
13 changed files with 248 additions and 419 deletions
|
@ -15,6 +15,9 @@
|
|||
package com.google.domain.registry.export;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
import static com.google.domain.registry.export.CheckSnapshotServlet.SNAPSHOT_KINDS_TO_LOAD_PARAM;
|
||||
import static com.google.domain.registry.export.CheckSnapshotServlet.SNAPSHOT_NAME_PARAM;
|
||||
import static com.google.domain.registry.testing.TaskQueueHelper.assertNoTasksEnqueued;
|
||||
import static com.google.domain.registry.testing.TaskQueueHelper.assertTasksEnqueued;
|
||||
import static javax.servlet.http.HttpServletResponse.SC_ACCEPTED;
|
||||
import static javax.servlet.http.HttpServletResponse.SC_BAD_REQUEST;
|
||||
|
@ -22,7 +25,6 @@ import static javax.servlet.http.HttpServletResponse.SC_NOT_MODIFIED;
|
|||
import static javax.servlet.http.HttpServletResponse.SC_OK;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.verifyZeroInteractions;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import com.google.common.base.Joiner;
|
||||
|
@ -76,9 +78,6 @@ public class CheckSnapshotServletTest {
|
|||
@Mock
|
||||
private DatastoreBackupService backupService;
|
||||
|
||||
@Mock
|
||||
private LoadSnapshotServlet loadSnapshotServlet;
|
||||
|
||||
private final FakeClock clock = new FakeClock(COMPLETE_TIME.plusMillis(1000));
|
||||
private final StringWriter httpOutput = new StringWriter();
|
||||
private final CheckSnapshotServlet servlet = new CheckSnapshotServlet();
|
||||
|
@ -86,7 +85,6 @@ public class CheckSnapshotServletTest {
|
|||
@Before
|
||||
public void before() throws Exception {
|
||||
inject.setStaticField(CheckSnapshotServlet.class, "backupService", backupService);
|
||||
inject.setStaticField(CheckSnapshotServlet.class, "loadSnapshotServlet", loadSnapshotServlet);
|
||||
inject.setStaticField(DatastoreBackupInfo.class, "clock", clock);
|
||||
|
||||
when(rsp.getWriter()).thenReturn(new PrintWriter(httpOutput));
|
||||
|
@ -111,21 +109,34 @@ public class CheckSnapshotServletTest {
|
|||
backupInfo.getGcsFilename());
|
||||
}
|
||||
|
||||
private static void assertLoadTaskEnqueued(String id, String file, String kinds)
|
||||
throws Exception {
|
||||
assertTasksEnqueued(
|
||||
"export-snapshot",
|
||||
new TaskMatcher()
|
||||
.url("/_dr/task/loadSnapshot")
|
||||
.method("POST")
|
||||
.param("id", id)
|
||||
.param("file", file)
|
||||
.param("kinds", kinds));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSuccess_enqueuePollTask() throws Exception {
|
||||
servlet.enqueuePollTask("some_snapshot_name", ImmutableSet.of("one", "two", "three"));
|
||||
assertTasksEnqueued(CheckSnapshotServlet.QUEUE,
|
||||
new TaskMatcher()
|
||||
.url(CheckSnapshotServlet.PATH)
|
||||
.param(CheckSnapshotServlet.SNAPSHOT_NAME_PARAM, "some_snapshot_name")
|
||||
.param(CheckSnapshotServlet.SNAPSHOT_KINDS_TO_LOAD_PARAM, "one,two,three")
|
||||
.param(SNAPSHOT_NAME_PARAM, "some_snapshot_name")
|
||||
.param(SNAPSHOT_KINDS_TO_LOAD_PARAM, "one,two,three")
|
||||
.method("POST"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPost_forPendingBackup_returnsNotModified() throws Exception {
|
||||
setPendingBackup();
|
||||
when(req.getParameter(CheckSnapshotServlet.SNAPSHOT_NAME_PARAM)).thenReturn("some_backup");
|
||||
when(req.getParameter(SNAPSHOT_NAME_PARAM)).thenReturn("some_backup");
|
||||
when(req.getParameter(SNAPSHOT_KINDS_TO_LOAD_PARAM)).thenReturn("one,two");
|
||||
when(backupService.findByName("some_backup")).thenReturn(backupInfo);
|
||||
|
||||
servlet.service(req, rsp);
|
||||
|
@ -135,7 +146,8 @@ public class CheckSnapshotServletTest {
|
|||
@Test
|
||||
public void testPost_forStalePendingBackupBackup_returnsAccepted() throws Exception {
|
||||
setPendingBackup();
|
||||
when(req.getParameter(CheckSnapshotServlet.SNAPSHOT_NAME_PARAM)).thenReturn("some_backup");
|
||||
when(req.getParameter(SNAPSHOT_NAME_PARAM)).thenReturn("some_backup");
|
||||
when(req.getParameter(SNAPSHOT_KINDS_TO_LOAD_PARAM)).thenReturn("one,two");
|
||||
when(backupService.findByName("some_backup")).thenReturn(backupInfo);
|
||||
clock.setTo(START_TIME
|
||||
.plus(Duration.standardHours(20))
|
||||
|
@ -150,76 +162,57 @@ public class CheckSnapshotServletTest {
|
|||
|
||||
@Test
|
||||
public void testPost_forCompleteBackup_enqueuesLoadTask() throws Exception {
|
||||
when(req.getParameter(CheckSnapshotServlet.SNAPSHOT_NAME_PARAM)).thenReturn("some_backup");
|
||||
when(req.getParameter(CheckSnapshotServlet.SNAPSHOT_KINDS_TO_LOAD_PARAM)).thenReturn("one,two");
|
||||
when(req.getParameter(SNAPSHOT_NAME_PARAM)).thenReturn("some_backup");
|
||||
when(req.getParameter(SNAPSHOT_KINDS_TO_LOAD_PARAM)).thenReturn("one,two");
|
||||
when(backupService.findByName("some_backup")).thenReturn(backupInfo);
|
||||
|
||||
servlet.service(req, rsp);
|
||||
verify(rsp).setStatus(SC_OK);
|
||||
verify(loadSnapshotServlet).enqueueLoadTask(
|
||||
"20140801_010203",
|
||||
"gs://somebucket/some_backup_20140801.backup_info",
|
||||
ImmutableSet.of("one", "two"));
|
||||
assertLoadTaskEnqueued(
|
||||
"20140801_010203", "gs://somebucket/some_backup_20140801.backup_info", "one,two");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPost_forCompleteAutoBackup_enqueuesLoadTask_usingBackupName() throws Exception {
|
||||
when(req.getParameter(CheckSnapshotServlet.SNAPSHOT_NAME_PARAM))
|
||||
.thenReturn("auto_snapshot_somestring");
|
||||
when(req.getParameter(CheckSnapshotServlet.SNAPSHOT_KINDS_TO_LOAD_PARAM)).thenReturn("one,two");
|
||||
when(req.getParameter(SNAPSHOT_NAME_PARAM)).thenReturn("auto_snapshot_somestring");
|
||||
when(req.getParameter(SNAPSHOT_KINDS_TO_LOAD_PARAM)).thenReturn("one,two");
|
||||
when(backupService.findByName("auto_snapshot_somestring")).thenReturn(backupInfo);
|
||||
|
||||
servlet.service(req, rsp);
|
||||
verify(rsp).setStatus(SC_OK);
|
||||
verify(loadSnapshotServlet).enqueueLoadTask(
|
||||
"somestring",
|
||||
"gs://somebucket/some_backup_20140801.backup_info",
|
||||
ImmutableSet.of("one", "two"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPost_forCompleteBackup_missingKindsToLoad_enqueuesLoadTask() throws Exception {
|
||||
when(req.getParameter(CheckSnapshotServlet.SNAPSHOT_NAME_PARAM)).thenReturn("some_backup");
|
||||
when(backupService.findByName("some_backup")).thenReturn(backupInfo);
|
||||
|
||||
servlet.service(req, rsp);
|
||||
verify(rsp).setStatus(SC_OK);
|
||||
verify(loadSnapshotServlet).enqueueLoadTask(
|
||||
"20140801_010203",
|
||||
"gs://somebucket/some_backup_20140801.backup_info",
|
||||
ImmutableSet.of("one", "two", "three"));
|
||||
assertLoadTaskEnqueued(
|
||||
"somestring", "gs://somebucket/some_backup_20140801.backup_info", "one,two");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPost_forCompleteBackup_withExtraKindsToLoad_enqueuesLoadTask() throws Exception {
|
||||
when(req.getParameter(CheckSnapshotServlet.SNAPSHOT_NAME_PARAM)).thenReturn("some_backup");
|
||||
when(req.getParameter(CheckSnapshotServlet.SNAPSHOT_KINDS_TO_LOAD_PARAM)).thenReturn("one,foo");
|
||||
when(req.getParameter(SNAPSHOT_NAME_PARAM)).thenReturn("some_backup");
|
||||
when(req.getParameter(SNAPSHOT_KINDS_TO_LOAD_PARAM)).thenReturn("one,foo");
|
||||
when(backupService.findByName("some_backup")).thenReturn(backupInfo);
|
||||
|
||||
servlet.service(req, rsp);
|
||||
verify(rsp).setStatus(SC_OK);
|
||||
verify(loadSnapshotServlet).enqueueLoadTask(
|
||||
"20140801_010203",
|
||||
"gs://somebucket/some_backup_20140801.backup_info",
|
||||
ImmutableSet.of("one"));
|
||||
assertLoadTaskEnqueued(
|
||||
"20140801_010203", "gs://somebucket/some_backup_20140801.backup_info", "one");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPost_forCompleteBackup_withEmptyKindsToLoad_skipsLoadTask() throws Exception {
|
||||
when(req.getParameter(CheckSnapshotServlet.SNAPSHOT_NAME_PARAM)).thenReturn("some_backup");
|
||||
when(req.getParameter(CheckSnapshotServlet.SNAPSHOT_KINDS_TO_LOAD_PARAM)).thenReturn("");
|
||||
when(req.getParameter(SNAPSHOT_NAME_PARAM)).thenReturn("some_backup");
|
||||
when(req.getParameter(SNAPSHOT_KINDS_TO_LOAD_PARAM)).thenReturn("");
|
||||
when(backupService.findByName("some_backup")).thenReturn(backupInfo);
|
||||
|
||||
servlet.service(req, rsp);
|
||||
verify(rsp).setStatus(SC_OK);
|
||||
verifyZeroInteractions(loadSnapshotServlet);
|
||||
assertNoTasksEnqueued("export-snapshot");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPost_forBadBackup_returnsBadRequest() throws Exception {
|
||||
when(req.getParameter(CheckSnapshotServlet.SNAPSHOT_NAME_PARAM)).thenReturn("some_backup");
|
||||
when(backupService.findByName("some_backup")).thenThrow(
|
||||
new IllegalArgumentException("No backup found"));
|
||||
when(req.getParameter(SNAPSHOT_NAME_PARAM)).thenReturn("some_backup");
|
||||
when(req.getParameter(SNAPSHOT_KINDS_TO_LOAD_PARAM)).thenReturn("one,two");
|
||||
when(backupService.findByName("some_backup"))
|
||||
.thenThrow(new IllegalArgumentException("No backup found"));
|
||||
|
||||
servlet.service(req, rsp);
|
||||
verify(rsp).sendError(SC_BAD_REQUEST, "Bad backup name some_backup: No backup found");
|
||||
|
@ -228,14 +221,16 @@ public class CheckSnapshotServletTest {
|
|||
@Test
|
||||
public void testPost_noBackupSpecified_returnsError() throws Exception {
|
||||
when(req.getMethod()).thenReturn("POST");
|
||||
when(req.getParameter(SNAPSHOT_NAME_PARAM)).thenReturn(null);
|
||||
when(req.getParameter(SNAPSHOT_KINDS_TO_LOAD_PARAM)).thenReturn("one,two");
|
||||
servlet.service(req, rsp);
|
||||
verify(rsp).sendError(SC_BAD_REQUEST, "Missing required parameter: name");
|
||||
verify(rsp).sendError(SC_BAD_REQUEST, "Missing parameter: name");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGet_returnsInformation() throws Exception {
|
||||
when(req.getMethod()).thenReturn("GET");
|
||||
when(req.getParameter(CheckSnapshotServlet.SNAPSHOT_NAME_PARAM)).thenReturn("some_backup");
|
||||
when(req.getParameter(SNAPSHOT_NAME_PARAM)).thenReturn("some_backup");
|
||||
when(backupService.findByName("some_backup")).thenReturn(backupInfo);
|
||||
|
||||
servlet.service(req, rsp);
|
||||
|
@ -254,7 +249,7 @@ public class CheckSnapshotServletTest {
|
|||
@Test
|
||||
public void testGet_forBadBackup_returnsError() throws Exception {
|
||||
when(req.getMethod()).thenReturn("GET");
|
||||
when(req.getParameter(CheckSnapshotServlet.SNAPSHOT_NAME_PARAM)).thenReturn("some_backup");
|
||||
when(req.getParameter(SNAPSHOT_NAME_PARAM)).thenReturn("some_backup");
|
||||
when(backupService.findByName("some_backup")).thenThrow(
|
||||
new IllegalArgumentException("No backup found"));
|
||||
|
||||
|
@ -266,6 +261,6 @@ public class CheckSnapshotServletTest {
|
|||
public void testGet_noBackupSpecified_returnsError() throws Exception {
|
||||
when(req.getMethod()).thenReturn("GET");
|
||||
servlet.service(req, rsp);
|
||||
verify(rsp).sendError(SC_BAD_REQUEST, "Missing required parameter: name");
|
||||
verify(rsp).sendError(SC_BAD_REQUEST, "Missing parameter: name");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -16,21 +16,20 @@ package com.google.domain.registry.export;
|
|||
|
||||
import static com.google.common.collect.Iterables.transform;
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
import static com.google.domain.registry.export.LoadSnapshotAction.LOAD_SNAPSHOT_FILE_PARAM;
|
||||
import static com.google.domain.registry.export.LoadSnapshotAction.LOAD_SNAPSHOT_ID_PARAM;
|
||||
import static com.google.domain.registry.export.LoadSnapshotAction.LOAD_SNAPSHOT_KINDS_PARAM;
|
||||
import static com.google.domain.registry.export.LoadSnapshotAction.PATH;
|
||||
import static com.google.domain.registry.export.LoadSnapshotAction.QUEUE;
|
||||
import static com.google.domain.registry.export.LoadSnapshotAction.enqueueLoadSnapshotTask;
|
||||
import static com.google.domain.registry.testing.TaskQueueHelper.assertTasksEnqueued;
|
||||
import static javax.servlet.http.HttpServletResponse.SC_BAD_REQUEST;
|
||||
import static javax.servlet.http.HttpServletResponse.SC_OK;
|
||||
import static org.joda.time.DateTimeZone.UTC;
|
||||
import static org.mockito.Matchers.any;
|
||||
import static org.mockito.Matchers.anyString;
|
||||
import static org.mockito.Matchers.eq;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import com.google.api.client.http.HttpRequestInitializer;
|
||||
import com.google.api.client.http.HttpTransport;
|
||||
import com.google.api.client.json.JsonFactory;
|
||||
import com.google.api.services.bigquery.Bigquery;
|
||||
import com.google.api.services.bigquery.model.Dataset;
|
||||
import com.google.api.services.bigquery.model.Job;
|
||||
|
@ -41,12 +40,12 @@ import com.google.common.base.Function;
|
|||
import com.google.common.collect.ImmutableSet;
|
||||
import com.google.common.collect.Iterables;
|
||||
import com.google.domain.registry.bigquery.BigqueryFactory;
|
||||
import com.google.domain.registry.config.TestRegistryConfig;
|
||||
import com.google.domain.registry.export.BigqueryPollJobAction.BigqueryPollJobEnqueuer;
|
||||
import com.google.domain.registry.request.HttpException.BadRequestException;
|
||||
import com.google.domain.registry.request.HttpException.InternalServerErrorException;
|
||||
import com.google.domain.registry.testing.AppEngineRule;
|
||||
import com.google.domain.registry.testing.ExceptionRule;
|
||||
import com.google.domain.registry.testing.FakeClock;
|
||||
import com.google.domain.registry.testing.InjectRule;
|
||||
import com.google.domain.registry.testing.RegistryConfigRule;
|
||||
import com.google.domain.registry.testing.TaskQueueHelper.TaskMatcher;
|
||||
|
||||
import org.joda.time.DateTime;
|
||||
|
@ -58,20 +57,12 @@ import org.mockito.ArgumentCaptor;
|
|||
import org.mockito.Mock;
|
||||
import org.mockito.runners.MockitoJUnitRunner;
|
||||
|
||||
import java.io.PrintWriter;
|
||||
import java.io.StringWriter;
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
import javax.servlet.ServletConfig;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
/** Unit tests for {@link LoadSnapshotServlet}. */
|
||||
/** Unit tests for {@link LoadSnapshotAction}. */
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class LoadSnapshotServletTest {
|
||||
|
||||
@Rule
|
||||
public final InjectRule inject = new InjectRule();
|
||||
public class LoadSnapshotActionTest {
|
||||
|
||||
@Rule
|
||||
public final AppEngineRule appEngine = AppEngineRule.builder()
|
||||
|
@ -79,21 +70,7 @@ public class LoadSnapshotServletTest {
|
|||
.build();
|
||||
|
||||
@Rule
|
||||
public final RegistryConfigRule configRule = new RegistryConfigRule(new TestRegistryConfig() {
|
||||
@Override public String getProjectId() {
|
||||
return "Project-Id";
|
||||
}
|
||||
|
||||
@Override public String getSnapshotsDataset() {
|
||||
return "testdataset";
|
||||
}
|
||||
});
|
||||
|
||||
@Mock
|
||||
private HttpServletRequest req;
|
||||
|
||||
@Mock
|
||||
private HttpServletResponse rsp;
|
||||
public final ExceptionRule thrown = new ExceptionRule();
|
||||
|
||||
@Mock
|
||||
private BigqueryFactory bigqueryFactory;
|
||||
|
@ -116,70 +93,48 @@ public class LoadSnapshotServletTest {
|
|||
@Mock
|
||||
private BigqueryPollJobEnqueuer bigqueryPollEnqueuer;
|
||||
|
||||
private static final DateTime NOW = new DateTime(1391096117045L, UTC);
|
||||
|
||||
private FakeClock clock = new FakeClock();
|
||||
private final StringWriter httpOutput = new StringWriter();
|
||||
private final LoadSnapshotServlet servlet = new LoadSnapshotServlet();
|
||||
private FakeClock clock = new FakeClock(new DateTime(1391096117045L, UTC));
|
||||
private LoadSnapshotAction action;
|
||||
|
||||
@Before
|
||||
public void before() throws Exception {
|
||||
clock.setTo(NOW);
|
||||
inject.setStaticField(LoadSnapshotServlet.class, "clock", clock);
|
||||
inject.setStaticField(LoadSnapshotServlet.class, "bigqueryFactory", bigqueryFactory);
|
||||
inject.setStaticField(LoadSnapshotServlet.class, "bigqueryPollEnqueuer", bigqueryPollEnqueuer);
|
||||
|
||||
when(rsp.getWriter()).thenReturn(new PrintWriter(httpOutput));
|
||||
|
||||
when(bigqueryFactory.create(
|
||||
anyString(),
|
||||
any(HttpTransport.class),
|
||||
any(JsonFactory.class),
|
||||
any(HttpRequestInitializer.class)))
|
||||
.thenReturn(bigquery);
|
||||
|
||||
when(bigqueryFactory.create("Project-Id", "snapshots")).thenReturn(bigquery);
|
||||
when(bigquery.jobs()).thenReturn(bigqueryJobs);
|
||||
when(bigqueryJobs.insert(eq("Project-Id"), any(Job.class))).thenReturn(bigqueryJobsInsert);
|
||||
|
||||
when(bigquery.datasets()).thenReturn(bigqueryDatasets);
|
||||
when(bigqueryDatasets.insert(eq("Project-Id"), any(Dataset.class)))
|
||||
.thenReturn(bigqueryDatasetsInsert);
|
||||
|
||||
servlet.init(mock(ServletConfig.class));
|
||||
when(req.getMethod()).thenReturn("POST");
|
||||
action = new LoadSnapshotAction();
|
||||
action.bigqueryFactory = bigqueryFactory;
|
||||
action.bigqueryPollEnqueuer = bigqueryPollEnqueuer;
|
||||
action.clock = clock;
|
||||
action.projectId = "Project-Id";
|
||||
action.snapshotFile = "gs://bucket/snapshot.backup_info";
|
||||
action.snapshotId = "id12345";
|
||||
action.snapshotKinds = "one,two,three";
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSuccess_enqueueLoadTask() throws Exception {
|
||||
servlet.enqueueLoadTask(
|
||||
enqueueLoadSnapshotTask(
|
||||
"id12345", "gs://bucket/snapshot.backup_info", ImmutableSet.of("one", "two", "three"));
|
||||
assertTasksEnqueued(LoadSnapshotServlet.QUEUE,
|
||||
assertTasksEnqueued(
|
||||
QUEUE,
|
||||
new TaskMatcher()
|
||||
.url(LoadSnapshotServlet.PATH)
|
||||
.url(PATH)
|
||||
.method("POST")
|
||||
.param(LoadSnapshotServlet.SNAPSHOT_ID_PARAM, "id12345")
|
||||
.param(LoadSnapshotServlet.SNAPSHOT_FILE_PARAM, "gs://bucket/snapshot.backup_info")
|
||||
.param(LoadSnapshotServlet.SNAPSHOT_KINDS_PARAM, "one,two,three"));
|
||||
.param(LOAD_SNAPSHOT_ID_PARAM, "id12345")
|
||||
.param(LOAD_SNAPSHOT_FILE_PARAM, "gs://bucket/snapshot.backup_info")
|
||||
.param(LOAD_SNAPSHOT_KINDS_PARAM, "one,two,three"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSuccess_doPost() throws Exception {
|
||||
when(req.getParameter(LoadSnapshotServlet.SNAPSHOT_ID_PARAM)).thenReturn("id12345");
|
||||
when(req.getParameter(LoadSnapshotServlet.SNAPSHOT_FILE_PARAM))
|
||||
.thenReturn("gs://bucket/snapshot.backup_info");
|
||||
when(req.getParameter(LoadSnapshotServlet.SNAPSHOT_KINDS_PARAM))
|
||||
.thenReturn("one,two,three");
|
||||
action.run();
|
||||
|
||||
servlet.service(req, rsp);
|
||||
|
||||
// Check that we attempted to create the snapshots dataset.
|
||||
ArgumentCaptor<Dataset> datasetArgument = ArgumentCaptor.forClass(Dataset.class);
|
||||
verify(bigqueryDatasets).insert(eq("Project-Id"), datasetArgument.capture());
|
||||
assertThat(datasetArgument.getValue().getDatasetReference().getProjectId())
|
||||
.isEqualTo("Project-Id");
|
||||
assertThat(datasetArgument.getValue().getDatasetReference().getDatasetId())
|
||||
.isEqualTo("testdataset");
|
||||
verify(bigqueryDatasetsInsert).execute();
|
||||
// Verify that bigqueryFactory was called in a way that would create the dataset if it didn't
|
||||
// already exist.
|
||||
verify(bigqueryFactory).create("Project-Id", "snapshots");
|
||||
|
||||
// Capture the load jobs we inserted to do additional checking on them.
|
||||
ArgumentCaptor<Job> jobArgument = ArgumentCaptor.forClass(Job.class);
|
||||
|
@ -193,7 +148,7 @@ public class LoadSnapshotServletTest {
|
|||
JobConfigurationLoad config = job.getConfiguration().getLoad();
|
||||
assertThat(config.getSourceFormat()).isEqualTo("DATASTORE_BACKUP");
|
||||
assertThat(config.getDestinationTable().getProjectId()).isEqualTo("Project-Id");
|
||||
assertThat(config.getDestinationTable().getDatasetId()).isEqualTo("testdataset");
|
||||
assertThat(config.getDestinationTable().getDatasetId()).isEqualTo("snapshots");
|
||||
}
|
||||
|
||||
// Check the job IDs for each load job.
|
||||
|
@ -231,64 +186,37 @@ public class LoadSnapshotServletTest {
|
|||
new JobReference()
|
||||
.setProjectId("Project-Id")
|
||||
.setJobId("load-snapshot-id12345-one-1391096117045"),
|
||||
UpdateSnapshotViewAction.createViewUpdateTask("testdataset", "id12345_one", "one"),
|
||||
UpdateSnapshotViewAction.createViewUpdateTask("snapshots", "id12345_one", "one"),
|
||||
QueueFactory.getQueue(UpdateSnapshotViewAction.QUEUE));
|
||||
verify(bigqueryPollEnqueuer).enqueuePollTask(
|
||||
new JobReference()
|
||||
.setProjectId("Project-Id")
|
||||
.setJobId("load-snapshot-id12345-two-1391096117045"),
|
||||
UpdateSnapshotViewAction.createViewUpdateTask("testdataset", "id12345_two", "two"),
|
||||
UpdateSnapshotViewAction.createViewUpdateTask("snapshots", "id12345_two", "two"),
|
||||
QueueFactory.getQueue(UpdateSnapshotViewAction.QUEUE));
|
||||
verify(bigqueryPollEnqueuer).enqueuePollTask(
|
||||
new JobReference()
|
||||
.setProjectId("Project-Id")
|
||||
.setJobId("load-snapshot-id12345-three-1391096117045"),
|
||||
UpdateSnapshotViewAction.createViewUpdateTask("testdataset", "id12345_three", "three"),
|
||||
UpdateSnapshotViewAction.createViewUpdateTask("snapshots", "id12345_three", "three"),
|
||||
QueueFactory.getQueue(UpdateSnapshotViewAction.QUEUE));
|
||||
|
||||
verify(rsp).setStatus(SC_OK);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFailure_doPost_missingIdHeader() throws Exception {
|
||||
when(req.getParameter(LoadSnapshotServlet.SNAPSHOT_FILE_PARAM))
|
||||
.thenReturn("gs://bucket/snapshot.backup_info");
|
||||
when(req.getParameter(LoadSnapshotServlet.SNAPSHOT_KINDS_PARAM))
|
||||
.thenReturn("one,two,three");
|
||||
|
||||
servlet.service(req, rsp);
|
||||
verify(rsp).sendError(SC_BAD_REQUEST, "Missing required parameter: id");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFailure_doPost_missingFileHeader() throws Exception {
|
||||
when(req.getParameter(LoadSnapshotServlet.SNAPSHOT_ID_PARAM)).thenReturn("id12345");
|
||||
when(req.getParameter(LoadSnapshotServlet.SNAPSHOT_KINDS_PARAM))
|
||||
.thenReturn("one,two,three");
|
||||
|
||||
servlet.service(req, rsp);
|
||||
verify(rsp).sendError(SC_BAD_REQUEST, "Missing required parameter: file");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFailure_doPost_missingKindHeader() throws Exception {
|
||||
when(req.getParameter(LoadSnapshotServlet.SNAPSHOT_ID_PARAM)).thenReturn("id12345");
|
||||
when(req.getParameter(LoadSnapshotServlet.SNAPSHOT_FILE_PARAM))
|
||||
.thenReturn("gs://bucket/snapshot.backup_info");
|
||||
|
||||
servlet.service(req, rsp);
|
||||
verify(rsp).sendError(SC_BAD_REQUEST, "Missing required parameter: kinds");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFailure_doPost_badGcsFilename() throws Exception {
|
||||
when(req.getParameter(LoadSnapshotServlet.SNAPSHOT_ID_PARAM)).thenReturn("id12345");
|
||||
when(req.getParameter(LoadSnapshotServlet.SNAPSHOT_FILE_PARAM))
|
||||
.thenReturn("gs://bucket/snapshot");
|
||||
when(req.getParameter(LoadSnapshotServlet.SNAPSHOT_KINDS_PARAM))
|
||||
.thenReturn("one,two,three");
|
||||
action.snapshotFile = "gs://bucket/snapshot";
|
||||
thrown.expect(
|
||||
BadRequestException.class,
|
||||
"Error calling load snapshot: backup info file extension missing");
|
||||
action.run();
|
||||
}
|
||||
|
||||
servlet.service(req, rsp);
|
||||
verify(rsp).sendError(SC_BAD_REQUEST, "backup info file extension missing");
|
||||
@Test
|
||||
public void testFailure_doPost_bigqueryThrowsException() throws Exception {
|
||||
when(bigqueryJobsInsert.execute()).thenThrow(new IOException("The Internet has gone missing"));
|
||||
thrown.expect(
|
||||
InternalServerErrorException.class,
|
||||
"Error loading snapshot: The Internet has gone missing");
|
||||
action.run();
|
||||
}
|
||||
}
|
|
@ -15,9 +15,9 @@
|
|||
package com.google.domain.registry.export;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
import static com.google.domain.registry.export.UpdateSnapshotViewAction.SNAPSHOT_DATASET_ID_PARAM;
|
||||
import static com.google.domain.registry.export.UpdateSnapshotViewAction.SNAPSHOT_KIND_PARAM;
|
||||
import static com.google.domain.registry.export.UpdateSnapshotViewAction.SNAPSHOT_TABLE_ID_PARAM;
|
||||
import static com.google.domain.registry.export.UpdateSnapshotViewAction.UPDATE_SNAPSHOT_DATASET_ID_PARAM;
|
||||
import static com.google.domain.registry.export.UpdateSnapshotViewAction.UPDATE_SNAPSHOT_KIND_PARAM;
|
||||
import static com.google.domain.registry.export.UpdateSnapshotViewAction.UPDATE_SNAPSHOT_TABLE_ID_PARAM;
|
||||
import static com.google.domain.registry.testing.TaskQueueHelper.assertTasksEnqueued;
|
||||
import static org.mockito.Matchers.any;
|
||||
import static org.mockito.Matchers.anyString;
|
||||
|
@ -30,11 +30,9 @@ import com.google.api.services.bigquery.model.Dataset;
|
|||
import com.google.api.services.bigquery.model.Table;
|
||||
import com.google.appengine.api.taskqueue.QueueFactory;
|
||||
import com.google.domain.registry.bigquery.BigqueryFactory;
|
||||
import com.google.domain.registry.config.TestRegistryConfig;
|
||||
import com.google.domain.registry.request.HttpException.InternalServerErrorException;
|
||||
import com.google.domain.registry.testing.AppEngineRule;
|
||||
import com.google.domain.registry.testing.ExceptionRule;
|
||||
import com.google.domain.registry.testing.RegistryConfigRule;
|
||||
import com.google.domain.registry.testing.TaskQueueHelper.TaskMatcher;
|
||||
|
||||
import org.junit.Before;
|
||||
|
@ -59,17 +57,6 @@ public class UpdateSnapshotViewActionTest {
|
|||
@Rule
|
||||
public final ExceptionRule thrown = new ExceptionRule();
|
||||
|
||||
@Rule
|
||||
public final RegistryConfigRule configRule = new RegistryConfigRule(new TestRegistryConfig() {
|
||||
@Override public String getProjectId() {
|
||||
return "Project-Id";
|
||||
}
|
||||
|
||||
@Override public String getLatestSnapshotDataset() {
|
||||
return "testdataset";
|
||||
}
|
||||
});
|
||||
|
||||
@Mock
|
||||
private BigqueryFactory bigqueryFactory;
|
||||
|
||||
|
@ -104,8 +91,9 @@ public class UpdateSnapshotViewActionTest {
|
|||
action = new UpdateSnapshotViewAction();
|
||||
action.bigqueryFactory = bigqueryFactory;
|
||||
action.datasetId = "some_dataset";
|
||||
action.tableId = "12345_fookind";
|
||||
action.kindName = "fookind";
|
||||
action.projectId = "Project-Id";
|
||||
action.tableId = "12345_fookind";
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -116,9 +104,9 @@ public class UpdateSnapshotViewActionTest {
|
|||
new TaskMatcher()
|
||||
.url(UpdateSnapshotViewAction.PATH)
|
||||
.method("POST")
|
||||
.param(SNAPSHOT_DATASET_ID_PARAM, "some_dataset")
|
||||
.param(SNAPSHOT_TABLE_ID_PARAM, "12345_fookind")
|
||||
.param(SNAPSHOT_KIND_PARAM, "fookind"));
|
||||
.param(UPDATE_SNAPSHOT_DATASET_ID_PARAM, "some_dataset")
|
||||
.param(UPDATE_SNAPSHOT_TABLE_ID_PARAM, "12345_fookind")
|
||||
.param(UPDATE_SNAPSHOT_KIND_PARAM, "fookind"));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -128,7 +116,7 @@ public class UpdateSnapshotViewActionTest {
|
|||
// Check that we updated the view.
|
||||
ArgumentCaptor<Table> tableArg = ArgumentCaptor.forClass(Table.class);
|
||||
verify(bigqueryTables).update(
|
||||
eq("Project-Id"), eq("testdataset"), eq("fookind"), tableArg.capture());
|
||||
eq("Project-Id"), eq("latest_snapshot"), eq("fookind"), tableArg.capture());
|
||||
assertThat(tableArg.getValue().getView().getQuery())
|
||||
.isEqualTo("SELECT * FROM [some_dataset.12345_fookind]");
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue