com.google.protobuf.Any.pack()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(18.4k)|赞(0)|评价(0)|浏览(905)

本文整理了Java中com.google.protobuf.Any.pack()方法的一些代码示例,展示了Any.pack()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Any.pack()方法的具体详情如下:
包路径:com.google.protobuf.Any
类名称:Any
方法名:pack

Any.pack介绍

[英]Packs a message using the given type URL prefix. The type URL will be constructed by concatenating the message type's full name to the prefix with an optional "/" separator if the prefix doesn't end with "/" already.
[中]使用给定的类型URL前缀打包消息。如果前缀尚未以“/”结尾,则类型URL将通过使用可选的“/”分隔符将消息类型的全名连接到前缀来构造。

代码示例

代码示例来源:origin: googleapis/google-cloud-java

@Override
public Object getMetadata() {
 return Any.pack(metadata);
}

代码示例来源:origin: googleapis/google-cloud-java

@Override
public Object getResponse() {
 return Any.pack(response);
}

代码示例来源:origin: googleapis/google-cloud-java

@Test
public void pendingOperation() {
 com.google.longrunning.Operation proto =
   newBuilder()
     .setName("op1")
     .setDone(false)
     .setMetadata(Any.pack(CreateDatabaseMetadata.newBuilder().setDatabase(NAME).build()))
     .build();
 Operation<Database, String> op = Operation.create(rpc, proto, new ParserImpl());
 assertThat(op.getName()).isEqualTo("op1");
 assertThat(op.isDone()).isFalse();
 assertThat(op.isSuccessful()).isFalse();
 assertThat(op.getMetadata()).isEqualTo(NAME);
 assertThat(op.getResult()).isNull();
}

代码示例来源:origin: Netflix/conductor

@Test
  public void testSimpleMapping() throws JsonGenerationException, JsonMappingException, IOException {
    ObjectMapper m = new JsonMapperProvider().get();
    assertTrue(m.canSerialize(Any.class));

    Struct struct1 = Struct.newBuilder().putFields(
        "some-key", Value.newBuilder().setStringValue("some-value").build()
    ).build();

    Any source = Any.pack(struct1);

    StringWriter buf = new StringWriter();
    m.writer().writeValue(buf, source);

    Any dest = m.reader().forType(Any.class).readValue(buf.toString());
    assertEquals(source.getTypeUrl(), dest.getTypeUrl());

    Struct struct2 = dest.unpack(Struct.class);
    assertTrue(struct2.containsFields("some-key"));
    assertEquals(
        struct1.getFieldsOrThrow("some-key").getStringValue(),
        struct2.getFieldsOrThrow("some-key").getStringValue()
    );
  }
}

代码示例来源:origin: googleapis/google-cloud-java

@Test
public void successfulOperation() {
 com.google.spanner.admin.database.v1.Database db =
   com.google.spanner.admin.database.v1.Database.newBuilder()
     .setName(NAME)
     .setState(com.google.spanner.admin.database.v1.Database.State.READY)
     .build();
 com.google.longrunning.Operation proto =
   newBuilder().setName("op1").setDone(true).setResponse(Any.pack(db)).build();
 Operation<Database, String> op = Operation.create(rpc, proto, new ParserImpl());
 assertThat(op.getName()).isEqualTo("op1");
 assertThat(op.isDone()).isTrue();
 assertThat(op.isSuccessful()).isTrue();
 assertThat(op.getMetadata()).isNull();
 assertThat(op.getResult().getId().getName()).isEqualTo(NAME);
}

代码示例来源:origin: googleapis/google-cloud-java

@Test
public void reload() {
 com.google.longrunning.Operation proto = newBuilder().setName("op1").setDone(false).build();
 Operation<Database, String> op = Operation.create(rpc, proto, new ParserImpl());
 com.google.spanner.admin.database.v1.Database db =
   com.google.spanner.admin.database.v1.Database.newBuilder()
     .setName(NAME)
     .setState(com.google.spanner.admin.database.v1.Database.State.READY)
     .build();
 proto = newBuilder().setName("op1").setDone(true).setResponse(Any.pack(db)).build();
 when(rpc.getOperation("op1")).thenReturn(proto);
 op = op.reload();
 assertThat(op.getName()).isEqualTo("op1");
 assertThat(op.isDone()).isTrue();
 assertThat(op.isSuccessful()).isTrue();
 assertThat(op.getMetadata()).isNull();
 assertThat(op.getResult().getId().getName()).isEqualTo(NAME);
}

代码示例来源:origin: googleapis/google-cloud-java

@Test
public void waitForCompletes() throws Exception {
 com.google.longrunning.Operation proto = newBuilder().setName("op1").setDone(false).build();
 Operation<Database, String> op = Operation.create(rpc, proto, new ParserImpl());
 com.google.spanner.admin.database.v1.Database db =
   com.google.spanner.admin.database.v1.Database.newBuilder()
     .setName(NAME)
     .setState(com.google.spanner.admin.database.v1.Database.State.READY)
     .build();
 com.google.longrunning.Operation proto2 =
   newBuilder().setName("op1").setDone(true).setResponse(Any.pack(db)).build();
 when(rpc.getOperation("op1")).thenReturn(proto, proto2);
 op =
   op.waitFor(
     RetryOption.totalTimeout(Duration.ofSeconds(3)),
     RetryOption.initialRetryDelay(Duration.ZERO));
 assertThat(op.getName()).isEqualTo("op1");
 assertThat(op.isDone()).isTrue();
 assertThat(op.isSuccessful()).isTrue();
 assertThat(op.getMetadata()).isNull();
 assertThat(op.getResult().getId().getName()).isEqualTo(NAME);
}

代码示例来源:origin: googleapis/google-cloud-java

@Test
@SuppressWarnings("all")
public void instantiateWorkflowTemplateTest2() throws Exception {
 Empty expectedResponse = Empty.newBuilder().build();
 Operation resultOperation =
   Operation.newBuilder()
     .setName("instantiateWorkflowTemplateTest2")
     .setDone(true)
     .setResponse(Any.pack(expectedResponse))
     .build();
 mockWorkflowTemplateService.addResponse(resultOperation);
 Map<String, String> parameters = new HashMap<>();
 Empty actualResponse = client.instantiateWorkflowTemplateAsync(parameters).get();
 Assert.assertEquals(expectedResponse, actualResponse);
 List<GeneratedMessageV3> actualRequests = mockWorkflowTemplateService.getRequests();
 Assert.assertEquals(1, actualRequests.size());
 InstantiateWorkflowTemplateRequest actualRequest =
   (InstantiateWorkflowTemplateRequest) actualRequests.get(0);
 Assert.assertEquals(parameters, actualRequest.getParametersMap());
 Assert.assertTrue(
   channelProvider.isHeaderSent(
     ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
     GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}

代码示例来源:origin: googleapis/google-cloud-java

@Test
@SuppressWarnings("all")
public void asyncBatchAnnotateFilesTest() throws Exception {
 AsyncBatchAnnotateFilesResponse expectedResponse =
   AsyncBatchAnnotateFilesResponse.newBuilder().build();
 Operation resultOperation =
   Operation.newBuilder()
     .setName("asyncBatchAnnotateFilesTest")
     .setDone(true)
     .setResponse(Any.pack(expectedResponse))
     .build();
 mockImageAnnotator.addResponse(resultOperation);
 List<AsyncAnnotateFileRequest> requests = new ArrayList<>();
 AsyncBatchAnnotateFilesResponse actualResponse =
   client.asyncBatchAnnotateFilesAsync(requests).get();
 Assert.assertEquals(expectedResponse, actualResponse);
 List<GeneratedMessageV3> actualRequests = mockImageAnnotator.getRequests();
 Assert.assertEquals(1, actualRequests.size());
 AsyncBatchAnnotateFilesRequest actualRequest =
   (AsyncBatchAnnotateFilesRequest) actualRequests.get(0);
 Assert.assertEquals(requests, actualRequest.getRequestsList());
 Assert.assertTrue(
   channelProvider.isHeaderSent(
     ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
     GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}

代码示例来源:origin: googleapis/google-cloud-java

@Test
@SuppressWarnings("all")
public void annotateVideoTest() throws Exception {
 AnnotateVideoResponse expectedResponse = AnnotateVideoResponse.newBuilder().build();
 Operation resultOperation =
   Operation.newBuilder()
     .setName("annotateVideoTest")
     .setDone(true)
     .setResponse(Any.pack(expectedResponse))
     .build();
 mockVideoIntelligenceService.addResponse(resultOperation);
 String inputUri = "gs://demomaker/cat.mp4";
 Feature featuresElement = Feature.LABEL_DETECTION;
 List<Feature> features = Arrays.asList(featuresElement);
 AnnotateVideoResponse actualResponse = client.annotateVideoAsync(inputUri, features).get();
 Assert.assertEquals(expectedResponse, actualResponse);
 List<GeneratedMessageV3> actualRequests = mockVideoIntelligenceService.getRequests();
 Assert.assertEquals(1, actualRequests.size());
 AnnotateVideoRequest actualRequest = (AnnotateVideoRequest) actualRequests.get(0);
 Assert.assertEquals(inputUri, actualRequest.getInputUri());
 Assert.assertEquals(features, actualRequest.getFeaturesList());
 Assert.assertTrue(
   channelProvider.isHeaderSent(
     ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
     GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}

代码示例来源:origin: googleapis/google-cloud-java

@Test
@SuppressWarnings("all")
public void batchDeleteEntitiesTest() throws Exception {
 Empty expectedResponse = Empty.newBuilder().build();
 Operation resultOperation =
   Operation.newBuilder()
     .setName("batchDeleteEntitiesTest")
     .setDone(true)
     .setResponse(Any.pack(expectedResponse))
     .build();
 mockEntityTypes.addResponse(resultOperation);
 EntityTypeName parent = EntityTypeName.of("[PROJECT]", "[ENTITY_TYPE]");
 List<String> entityValues = new ArrayList<>();
 Empty actualResponse = client.batchDeleteEntitiesAsync(parent, entityValues).get();
 Assert.assertEquals(expectedResponse, actualResponse);
 List<GeneratedMessageV3> actualRequests = mockEntityTypes.getRequests();
 Assert.assertEquals(1, actualRequests.size());
 BatchDeleteEntitiesRequest actualRequest = (BatchDeleteEntitiesRequest) actualRequests.get(0);
 Assert.assertEquals(parent, EntityTypeName.parse(actualRequest.getParent()));
 Assert.assertEquals(entityValues, actualRequest.getEntityValuesList());
 Assert.assertTrue(
   channelProvider.isHeaderSent(
     ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
     GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}

代码示例来源:origin: googleapis/google-cloud-java

@Test
@SuppressWarnings("all")
public void updateDatabaseDdlTest() throws Exception {
 Empty expectedResponse = Empty.newBuilder().build();
 Operation resultOperation =
   Operation.newBuilder()
     .setName("updateDatabaseDdlTest")
     .setDone(true)
     .setResponse(Any.pack(expectedResponse))
     .build();
 mockDatabaseAdmin.addResponse(resultOperation);
 DatabaseName database = DatabaseName.of("[PROJECT]", "[INSTANCE]", "[DATABASE]");
 List<String> statements = new ArrayList<>();
 Empty actualResponse = client.updateDatabaseDdlAsync(database, statements).get();
 Assert.assertEquals(expectedResponse, actualResponse);
 List<GeneratedMessageV3> actualRequests = mockDatabaseAdmin.getRequests();
 Assert.assertEquals(1, actualRequests.size());
 UpdateDatabaseDdlRequest actualRequest = (UpdateDatabaseDdlRequest) actualRequests.get(0);
 Assert.assertEquals(database, DatabaseName.parse(actualRequest.getDatabase()));
 Assert.assertEquals(statements, actualRequest.getStatementsList());
 Assert.assertTrue(
   channelProvider.isHeaderSent(
     ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
     GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}

代码示例来源:origin: googleapis/google-cloud-java

@Test
@SuppressWarnings("all")
public void deleteDatasetTest() throws Exception {
 Empty expectedResponse = Empty.newBuilder().build();
 Operation resultOperation =
   Operation.newBuilder()
     .setName("deleteDatasetTest")
     .setDone(true)
     .setResponse(Any.pack(expectedResponse))
     .build();
 mockAutoMl.addResponse(resultOperation);
 DatasetName name = DatasetName.of("[PROJECT]", "[LOCATION]", "[DATASET]");
 Empty actualResponse = client.deleteDatasetAsync(name).get();
 Assert.assertEquals(expectedResponse, actualResponse);
 List<GeneratedMessageV3> actualRequests = mockAutoMl.getRequests();
 Assert.assertEquals(1, actualRequests.size());
 DeleteDatasetRequest actualRequest = (DeleteDatasetRequest) actualRequests.get(0);
 Assert.assertEquals(name, DatasetName.parse(actualRequest.getName()));
 Assert.assertTrue(
   channelProvider.isHeaderSent(
     ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
     GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}

代码示例来源:origin: googleapis/google-cloud-java

@Test
@SuppressWarnings("all")
public void deleteModelTest() throws Exception {
 Empty expectedResponse = Empty.newBuilder().build();
 Operation resultOperation =
   Operation.newBuilder()
     .setName("deleteModelTest")
     .setDone(true)
     .setResponse(Any.pack(expectedResponse))
     .build();
 mockAutoMl.addResponse(resultOperation);
 ModelName name = ModelName.of("[PROJECT]", "[LOCATION]", "[MODEL]");
 Empty actualResponse = client.deleteModelAsync(name).get();
 Assert.assertEquals(expectedResponse, actualResponse);
 List<GeneratedMessageV3> actualRequests = mockAutoMl.getRequests();
 Assert.assertEquals(1, actualRequests.size());
 DeleteModelRequest actualRequest = (DeleteModelRequest) actualRequests.get(0);
 Assert.assertEquals(name, ModelName.parse(actualRequest.getName()));
 Assert.assertTrue(
   channelProvider.isHeaderSent(
     ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
     GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}

代码示例来源:origin: googleapis/google-cloud-java

@Test
@SuppressWarnings("all")
public void deleteInstanceTest() throws Exception {
 Empty expectedResponse = Empty.newBuilder().build();
 Operation resultOperation =
   Operation.newBuilder()
     .setName("deleteInstanceTest")
     .setDone(true)
     .setResponse(Any.pack(expectedResponse))
     .build();
 mockCloudRedis.addResponse(resultOperation);
 InstanceName name = InstanceName.of("[PROJECT]", "[LOCATION]", "[INSTANCE]");
 Empty actualResponse = client.deleteInstanceAsync(name).get();
 Assert.assertEquals(expectedResponse, actualResponse);
 List<GeneratedMessageV3> actualRequests = mockCloudRedis.getRequests();
 Assert.assertEquals(1, actualRequests.size());
 DeleteInstanceRequest actualRequest = (DeleteInstanceRequest) actualRequests.get(0);
 Assert.assertEquals(name, InstanceName.parse(actualRequest.getName()));
 Assert.assertTrue(
   channelProvider.isHeaderSent(
     ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
     GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}

代码示例来源:origin: googleapis/google-cloud-java

@Test
@SuppressWarnings("all")
public void deleteDocumentTest() throws Exception {
 Empty expectedResponse = Empty.newBuilder().build();
 Operation resultOperation =
   Operation.newBuilder()
     .setName("deleteDocumentTest")
     .setDone(true)
     .setResponse(Any.pack(expectedResponse))
     .build();
 mockDocuments.addResponse(resultOperation);
 DocumentName name = DocumentName.of("[PROJECT]", "[KNOWLEDGE_BASE]", "[DOCUMENT]");
 Empty actualResponse = client.deleteDocumentAsync(name).get();
 Assert.assertEquals(expectedResponse, actualResponse);
 List<GeneratedMessageV3> actualRequests = mockDocuments.getRequests();
 Assert.assertEquals(1, actualRequests.size());
 DeleteDocumentRequest actualRequest = (DeleteDocumentRequest) actualRequests.get(0);
 Assert.assertEquals(name, DocumentName.parse(actualRequest.getName()));
 Assert.assertTrue(
   channelProvider.isHeaderSent(
     ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
     GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}

代码示例来源:origin: googleapis/google-cloud-java

@Test
@SuppressWarnings("all")
public void trainAgentTest() throws Exception {
 Empty expectedResponse = Empty.newBuilder().build();
 Operation resultOperation =
   Operation.newBuilder()
     .setName("trainAgentTest")
     .setDone(true)
     .setResponse(Any.pack(expectedResponse))
     .build();
 mockAgents.addResponse(resultOperation);
 ProjectName parent = ProjectName.of("[PROJECT]");
 Empty actualResponse = client.trainAgentAsync(parent).get();
 Assert.assertEquals(expectedResponse, actualResponse);
 List<GeneratedMessageV3> actualRequests = mockAgents.getRequests();
 Assert.assertEquals(1, actualRequests.size());
 TrainAgentRequest actualRequest = (TrainAgentRequest) actualRequests.get(0);
 Assert.assertEquals(parent, ProjectName.parse(actualRequest.getParent()));
 Assert.assertTrue(
   channelProvider.isHeaderSent(
     ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
     GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}

代码示例来源:origin: googleapis/google-cloud-java

@Test
@SuppressWarnings("all")
public void trainAgentTest() throws Exception {
 Empty expectedResponse = Empty.newBuilder().build();
 Operation resultOperation =
   Operation.newBuilder()
     .setName("trainAgentTest")
     .setDone(true)
     .setResponse(Any.pack(expectedResponse))
     .build();
 mockAgents.addResponse(resultOperation);
 ProjectName parent = ProjectName.of("[PROJECT]");
 Empty actualResponse = client.trainAgentAsync(parent).get();
 Assert.assertEquals(expectedResponse, actualResponse);
 List<GeneratedMessageV3> actualRequests = mockAgents.getRequests();
 Assert.assertEquals(1, actualRequests.size());
 TrainAgentRequest actualRequest = (TrainAgentRequest) actualRequests.get(0);
 Assert.assertEquals(parent, ProjectName.parse(actualRequest.getParent()));
 Assert.assertTrue(
   channelProvider.isHeaderSent(
     ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
     GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}

代码示例来源:origin: googleapis/google-cloud-java

@Test
@SuppressWarnings("all")
public void instantiateWorkflowTemplateTest() throws Exception {
 Empty expectedResponse = Empty.newBuilder().build();
 Operation resultOperation =
   Operation.newBuilder()
     .setName("instantiateWorkflowTemplateTest")
     .setDone(true)
     .setResponse(Any.pack(expectedResponse))
     .build();
 mockWorkflowTemplateService.addResponse(resultOperation);
 WorkflowTemplateName name =
   WorkflowTemplateName.of("[PROJECT]", "[REGION]", "[WORKFLOW_TEMPLATE]");
 Empty actualResponse = client.instantiateWorkflowTemplateAsync(name).get();
 Assert.assertEquals(expectedResponse, actualResponse);
 List<GeneratedMessageV3> actualRequests = mockWorkflowTemplateService.getRequests();
 Assert.assertEquals(1, actualRequests.size());
 InstantiateWorkflowTemplateRequest actualRequest =
   (InstantiateWorkflowTemplateRequest) actualRequests.get(0);
 Assert.assertEquals(name, WorkflowTemplateName.parse(actualRequest.getName()));
 Assert.assertTrue(
   channelProvider.isHeaderSent(
     ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
     GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}

代码示例来源:origin: googleapis/google-cloud-java

@Test
@SuppressWarnings("all")
public void instantiateWorkflowTemplateTest() throws Exception {
 Empty expectedResponse = Empty.newBuilder().build();
 Operation resultOperation =
   Operation.newBuilder()
     .setName("instantiateWorkflowTemplateTest")
     .setDone(true)
     .setResponse(Any.pack(expectedResponse))
     .build();
 mockWorkflowTemplateService.addResponse(resultOperation);
 WorkflowTemplateName name =
   WorkflowTemplateName.of("[PROJECT]", "[REGION]", "[WORKFLOW_TEMPLATE]");
 Empty actualResponse = client.instantiateWorkflowTemplateAsync(name).get();
 Assert.assertEquals(expectedResponse, actualResponse);
 List<GeneratedMessageV3> actualRequests = mockWorkflowTemplateService.getRequests();
 Assert.assertEquals(1, actualRequests.size());
 InstantiateWorkflowTemplateRequest actualRequest =
   (InstantiateWorkflowTemplateRequest) actualRequests.get(0);
 Assert.assertEquals(name, WorkflowTemplateName.parse(actualRequest.getName()));
 Assert.assertTrue(
   channelProvider.isHeaderSent(
     ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
     GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}

相关文章