mirror of
https://github.com/google/nomulus.git
synced 2025-05-07 07:27:52 +02:00
203 lines
7.4 KiB
Java
203 lines
7.4 KiB
Java
// Copyright 2016 Google Inc. All Rights Reserved.
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
package com.google.domain.registry.export;
|
|
|
|
import static com.google.common.truth.Truth.assertThat;
|
|
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.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.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.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.testing.AppEngineRule;
|
|
import com.google.domain.registry.testing.InjectRule;
|
|
import com.google.domain.registry.testing.RegistryConfigRule;
|
|
import com.google.domain.registry.testing.TaskQueueHelper.TaskMatcher;
|
|
|
|
import org.junit.Before;
|
|
import org.junit.Rule;
|
|
import org.junit.Test;
|
|
import org.junit.runner.RunWith;
|
|
import org.mockito.ArgumentCaptor;
|
|
import org.mockito.Mock;
|
|
import org.mockito.runners.MockitoJUnitRunner;
|
|
|
|
import java.io.PrintWriter;
|
|
import java.io.StringWriter;
|
|
|
|
import javax.servlet.ServletConfig;
|
|
import javax.servlet.http.HttpServletRequest;
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
|
/** Unit tests for {@link UpdateSnapshotViewServlet}. */
|
|
@RunWith(MockitoJUnitRunner.class)
|
|
public class UpdateSnapshotViewServletTest {
|
|
|
|
@Rule
|
|
public final InjectRule inject = new InjectRule();
|
|
|
|
@Rule
|
|
public final AppEngineRule appEngine = AppEngineRule.builder()
|
|
.withTaskQueue()
|
|
.build();
|
|
|
|
@Rule
|
|
public final RegistryConfigRule configRule = new RegistryConfigRule(new TestRegistryConfig() {
|
|
@Override public String getProjectId() {
|
|
return "Project-Id";
|
|
}
|
|
|
|
@Override public String getLatestSnapshotDataset() {
|
|
return "testdataset";
|
|
}
|
|
});
|
|
|
|
@Mock
|
|
private HttpServletRequest req;
|
|
|
|
@Mock
|
|
private HttpServletResponse rsp;
|
|
|
|
@Mock
|
|
private BigqueryFactory bigqueryFactory;
|
|
|
|
@Mock
|
|
private Bigquery bigquery;
|
|
|
|
@Mock
|
|
private Bigquery.Datasets bigqueryDatasets;
|
|
|
|
@Mock
|
|
private Bigquery.Datasets.Insert bigqueryDatasetsInsert;
|
|
|
|
@Mock
|
|
private Bigquery.Tables bigqueryTables;
|
|
|
|
@Mock
|
|
private Bigquery.Tables.Update bigqueryTablesUpdate;
|
|
|
|
private final StringWriter httpOutput = new StringWriter();
|
|
private final UpdateSnapshotViewServlet servlet = new UpdateSnapshotViewServlet();
|
|
|
|
@Before
|
|
public void before() throws Exception {
|
|
inject.setStaticField(UpdateSnapshotViewServlet.class, "bigqueryFactory", bigqueryFactory);
|
|
|
|
when(req.getMethod()).thenReturn("POST");
|
|
when(rsp.getWriter()).thenReturn(new PrintWriter(httpOutput));
|
|
|
|
when(bigqueryFactory.create(
|
|
anyString(),
|
|
any(HttpTransport.class),
|
|
any(JsonFactory.class),
|
|
any(HttpRequestInitializer.class)))
|
|
.thenReturn(bigquery);
|
|
|
|
when(bigquery.datasets()).thenReturn(bigqueryDatasets);
|
|
when(bigqueryDatasets.insert(eq("Project-Id"), any(Dataset.class)))
|
|
.thenReturn(bigqueryDatasetsInsert);
|
|
|
|
when(bigquery.tables()).thenReturn(bigqueryTables);
|
|
when(bigqueryTables.update(
|
|
eq("Project-Id"), any(String.class), any(String.class), any(Table.class)))
|
|
.thenReturn(bigqueryTablesUpdate);
|
|
|
|
servlet.init(mock(ServletConfig.class));
|
|
}
|
|
|
|
@Test
|
|
public void testSuccess_createViewUpdateTask() throws Exception {
|
|
QueueFactory.getDefaultQueue().add(
|
|
UpdateSnapshotViewServlet.createViewUpdateTask("some_dataset", "12345_fookind", "fookind"));
|
|
assertTasksEnqueued("default",
|
|
new TaskMatcher()
|
|
.url(UpdateSnapshotViewServlet.PATH)
|
|
.method("POST")
|
|
.param(UpdateSnapshotViewServlet.SNAPSHOT_DATASET_ID_PARAM, "some_dataset")
|
|
.param(UpdateSnapshotViewServlet.SNAPSHOT_TABLE_ID_PARAM, "12345_fookind")
|
|
.param(UpdateSnapshotViewServlet.SNAPSHOT_KIND_PARAM, "fookind"));
|
|
}
|
|
|
|
@Test
|
|
public void testSuccess_doPost() throws Exception {
|
|
when(req.getParameter(UpdateSnapshotViewServlet.SNAPSHOT_DATASET_ID_PARAM))
|
|
.thenReturn("some_dataset");
|
|
when(req.getParameter(UpdateSnapshotViewServlet.SNAPSHOT_TABLE_ID_PARAM))
|
|
.thenReturn("12345_fookind");
|
|
when(req.getParameter(UpdateSnapshotViewServlet.SNAPSHOT_KIND_PARAM))
|
|
.thenReturn("fookind");
|
|
|
|
servlet.service(req, rsp);
|
|
|
|
// Check that we attempted to create the latest_snapshot 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();
|
|
|
|
// 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());
|
|
assertThat(tableArg.getValue().getView().getQuery())
|
|
.isEqualTo("SELECT * FROM [some_dataset.12345_fookind]");
|
|
|
|
verify(rsp).setStatus(SC_OK);
|
|
}
|
|
|
|
@Test
|
|
public void testFailure_doPost_missingDatasetIdHeader() throws Exception {
|
|
when(req.getParameter(UpdateSnapshotViewServlet.SNAPSHOT_TABLE_ID_PARAM))
|
|
.thenReturn("12345_fookind");
|
|
when(req.getParameter(UpdateSnapshotViewServlet.SNAPSHOT_KIND_PARAM)).thenReturn("fookind");
|
|
servlet.service(req, rsp);
|
|
verify(rsp).sendError(SC_BAD_REQUEST, "Missing required parameter: dataset");
|
|
}
|
|
|
|
@Test
|
|
public void testFailure_doPost_missingTableIdHeader() throws Exception {
|
|
when(req.getParameter(UpdateSnapshotViewServlet.SNAPSHOT_DATASET_ID_PARAM))
|
|
.thenReturn("some_dataset");
|
|
when(req.getParameter(UpdateSnapshotViewServlet.SNAPSHOT_KIND_PARAM)).thenReturn("fookind");
|
|
servlet.service(req, rsp);
|
|
verify(rsp).sendError(SC_BAD_REQUEST, "Missing required parameter: table");
|
|
}
|
|
|
|
@Test
|
|
public void testFailure_doPost_missingKindHeader() throws Exception {
|
|
when(req.getParameter(UpdateSnapshotViewServlet.SNAPSHOT_DATASET_ID_PARAM))
|
|
.thenReturn("some_dataset");
|
|
when(req.getParameter(UpdateSnapshotViewServlet.SNAPSHOT_TABLE_ID_PARAM))
|
|
.thenReturn("12345_fookind");
|
|
servlet.service(req, rsp);
|
|
verify(rsp).sendError(SC_BAD_REQUEST, "Missing required parameter: kind");
|
|
}
|
|
}
|