Add helper methods to DatastoreAdmin Operation object

These are needed by Datastore export management actions.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=224242281
This commit is contained in:
weiminyu 2018-12-05 16:33:50 -08:00 committed by jianglai
parent a612e9bf66
commit 7d380256af
8 changed files with 164 additions and 7 deletions

View file

@ -13,6 +13,7 @@ java_library(
resources = glob(["**/testdata/*.json"]),
deps = [
"//java/google/registry/export/datastore",
"//java/google/registry/util",
"//javatests/google/registry/testing",
"@com_google_api_client",
"@com_google_guava",
@ -20,6 +21,7 @@ java_library(
"@com_google_http_client_jackson2",
"@com_google_truth",
"@com_google_truth_extensions_truth_java8_extension",
"@joda_time",
"@junit",
"@org_mockito_all",
],

View file

@ -134,6 +134,23 @@ public class DatastoreAdminTest {
assertThat(getAccessToken(httpRequest)).hasValue(ACCESS_TOKEN);
}
@Test
public void testListOperations_filterByState() throws IOException {
// TODO(weiminyu): consider adding a method to DatastoreAdmin to support query by state.
DatastoreAdmin.ListOperations listOperations =
datastoreAdmin.list("metadata.common.state=PROCESSING");
HttpRequest httpRequest = listOperations.buildHttpRequest();
assertThat(httpRequest.getUrl().toString())
.isEqualTo(
"https://datastore.googleapis.com/v1/projects/MyCloudProject/operations"
+ "?filter=metadata.common.state%3DPROCESSING");
assertThat(httpRequest.getRequestMethod()).isEqualTo("GET");
assertThat(httpRequest.getContent()).isNull();
simulateSendRequest(httpRequest);
assertThat(getAccessToken(httpRequest)).hasValue(ACCESS_TOKEN);
}
private static HttpRequest simulateSendRequest(HttpRequest httpRequest) {
try {
httpRequest.setUrl(new GenericUrl("https://localhost:65537")).execute();

View file

@ -15,14 +15,19 @@
package google.registry.export.datastore;
import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth8.assertThat;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import google.registry.export.datastore.Operation.CommonMetadata;
import google.registry.export.datastore.Operation.Metadata;
import google.registry.export.datastore.Operation.Progress;
import google.registry.testing.FakeClock;
import google.registry.testing.TestDataHelper;
import google.registry.util.Clock;
import java.io.IOException;
import org.joda.time.DateTime;
import org.joda.time.Duration;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
@ -37,6 +42,9 @@ public class OperationTest {
CommonMetadata commonMetadata = loadJson("common_metadata.json", CommonMetadata.class);
assertThat(commonMetadata.getState()).isEqualTo("SUCCESSFUL");
assertThat(commonMetadata.getOperationType()).isEqualTo("EXPORT_ENTITIES");
assertThat(commonMetadata.getStartTime())
.isEqualTo(DateTime.parse("2018-10-29T16:01:04.645299Z"));
assertThat(commonMetadata.getEndTime()).isEmpty();
}
@Test
@ -51,6 +59,12 @@ public class OperationTest {
Metadata metadata = loadJson("metadata.json", Metadata.class);
assertThat(metadata.getCommonMetadata().getOperationType()).isEqualTo("EXPORT_ENTITIES");
assertThat(metadata.getCommonMetadata().getState()).isEqualTo("SUCCESSFUL");
assertThat(metadata.getCommonMetadata().getStartTime())
.isEqualTo(DateTime.parse("2018-10-29T16:01:04.645299Z"));
assertThat(metadata.getCommonMetadata().getEndTime())
.hasValue(DateTime.parse("2018-10-29T16:02:19.009859Z"));
assertThat(metadata.getOutputUrlPrefix())
.isEqualTo("gs://domain-registry-alpha-datastore-export-test/2018-10-29T16:01:04_99364");
}
@Test
@ -61,6 +75,15 @@ public class OperationTest {
assertThat(operation.isProcessing()).isTrue();
assertThat(operation.isSuccessful()).isFalse();
assertThat(operation.isDone()).isFalse();
assertThat(operation.getStartTime()).isEqualTo(DateTime.parse("2018-10-29T16:01:04.645299Z"));
assertThat(operation.getExportFolderUrl())
.isEqualTo("gs://domain-registry-alpha-datastore-export-test/2018-10-29T16:01:04_99364");
assertThat(operation.getExportId()).isEqualTo("2018-10-29T16:01:04_99364");
assertThat(operation.getKinds()).containsExactly("Registry", "Registrar", "DomainBase");
assertThat(operation.toPrettyString())
.isEqualTo(
TestDataHelper.loadFile(OperationTest.class, "prettyprinted_operation.json").trim());
assertThat(operation.getProgress()).isEqualTo("Progress: N/A");
}
@Test
@ -68,6 +91,16 @@ public class OperationTest {
Operation.OperationList operationList =
loadJson("operation_list.json", Operation.OperationList.class);
assertThat(operationList.toList()).hasSize(2);
Clock clock = new FakeClock(DateTime.parse("2018-10-29T16:01:04.645299Z"));
((FakeClock) clock).advanceOneMilli();
assertThat(operationList.toList().get(0).getRunningTime(clock)).isEqualTo(Duration.millis(1));
assertThat(operationList.toList().get(0).getProgress())
.isEqualTo("Progress: [51797/54513 entities]");
assertThat(operationList.toList().get(1).getRunningTime(clock))
.isEqualTo(Duration.standardMinutes(1));
// Work completed may exceed work estimated
assertThat(operationList.toList().get(1).getProgress())
.isEqualTo("Progress: [96908367/73773755 bytes] [51797/54513 entities]");
}
private static <T> T loadJson(String fileName, Class<T> type) throws IOException {

View file

@ -1,6 +1,5 @@
{
"startTime": "2018-10-29T16:01:04.645299Z",
"endTime": "2018-10-29T16:02:19.009859Z",
"operationType": "EXPORT_ENTITIES",
"state": "SUCCESSFUL"
}

View file

@ -9,6 +9,10 @@
"operationType": "EXPORT_ENTITIES",
"state": "PROCESSING"
},
"progressEntities": {
"workCompleted": "51797",
"workEstimated": "54513"
},
"entityFilter": {
"kinds": [
"Registry",
@ -25,9 +29,18 @@
"@type": "type.googleapis.com/google.datastore.admin.v1.ExportEntitiesMetadata",
"common": {
"startTime": "2018-10-29T16:01:04.645299Z",
"endTime": "2018-10-29T16:02:04.645299Z",
"operationType": "EXPORT_ENTITIES",
"state": "PROCESSING"
},
"progressEntities": {
"workCompleted": "51797",
"workEstimated": "54513"
},
"progressBytes": {
"workCompleted": "96908367",
"workEstimated": "73773755"
},
"entityFilter": {
"kinds": [
"Registry",

View file

@ -0,0 +1,16 @@
{
"done" : false,
"metadata" : {
"common" : {
"operationType" : "EXPORT_ENTITIES",
"startTime" : "2018-10-29T16:01:04.645299Z",
"state" : "PROCESSING"
},
"entityFilter" : {
"kinds" : [ "Registry", "Registrar", "DomainBase" ]
},
"outputUrlPrefix" : "gs://domain-registry-alpha-datastore-export-test/2018-10-29T16:01:04_99364",
"@type" : "type.googleapis.com/google.datastore.admin.v1.ExportEntitiesMetadata"
},
"name" : "projects/domain-registry-alpha/operations/ASAzNjMwOTEyNjUJ"
}