Search in sources :

Example 31 with ByteString

use of org.apache.beam.vendor.grpc.v1p26p0.com.google.protobuf.ByteString in project google-cloud-java by GoogleCloudPlatform.

the class TopicAdminClientTest method getIamPolicyTest.

@Test
@SuppressWarnings("all")
public void getIamPolicyTest() {
    int version = 351608024;
    ByteString etag = ByteString.copyFromUtf8("21");
    Policy expectedResponse = Policy.newBuilder().setVersion(version).setEtag(etag).build();
    mockIAMPolicy.addResponse(expectedResponse);
    String formattedResource = TopicName.create("[PROJECT]", "[TOPIC]").toString();
    Policy actualResponse = client.getIamPolicy(formattedResource);
    Assert.assertEquals(expectedResponse, actualResponse);
    List<GeneratedMessageV3> actualRequests = mockIAMPolicy.getRequests();
    Assert.assertEquals(1, actualRequests.size());
    GetIamPolicyRequest actualRequest = (GetIamPolicyRequest) actualRequests.get(0);
    Assert.assertEquals(formattedResource, actualRequest.getResource());
}
Also used : Policy(com.google.iam.v1.Policy) ByteString(com.google.protobuf.ByteString) ByteString(com.google.protobuf.ByteString) GetIamPolicyRequest(com.google.iam.v1.GetIamPolicyRequest) GeneratedMessageV3(com.google.protobuf.GeneratedMessageV3) Test(org.junit.Test)

Example 32 with ByteString

use of org.apache.beam.vendor.grpc.v1p26p0.com.google.protobuf.ByteString in project google-cloud-java by GoogleCloudPlatform.

the class DatastoreTest method testQueryPaginationWithLimit.

@Test
public void testQueryPaginationWithLimit() throws DatastoreException {
    List<RunQueryResponse> responses = buildResponsesForQueryPaginationWithLimit();
    List<ByteString> endCursors = Lists.newArrayListWithCapacity(responses.size());
    for (RunQueryResponse response : responses) {
        EasyMock.expect(rpcMock.runQuery(EasyMock.anyObject(RunQueryRequest.class))).andReturn(response);
        if (response.getBatch().getMoreResults() != QueryResultBatch.MoreResultsType.NOT_FINISHED) {
            endCursors.add(response.getBatch().getEndCursor());
        }
    }
    EasyMock.replay(rpcFactoryMock, rpcMock);
    Datastore datastore = rpcMockOptions.getService();
    int limit = 2;
    int totalCount = 0;
    Iterator<ByteString> cursorIter = endCursors.iterator();
    StructuredQuery<Entity> query = Query.newEntityQueryBuilder().setLimit(limit).build();
    while (true) {
        QueryResults<Entity> results = datastore.run(query);
        int resultCount = 0;
        while (results.hasNext()) {
            results.next();
            resultCount++;
            totalCount++;
        }
        assertTrue(cursorIter.hasNext());
        Cursor expectedEndCursor = Cursor.copyFrom(cursorIter.next().toByteArray());
        assertEquals(expectedEndCursor, results.getCursorAfter());
        if (resultCount < limit) {
            break;
        }
        query = query.toBuilder().setStartCursor(results.getCursorAfter()).build();
    }
    assertEquals(5, totalCount);
    EasyMock.verify(rpcFactoryMock, rpcMock);
}
Also used : RunQueryResponse(com.google.datastore.v1.RunQueryResponse) ByteString(com.google.protobuf.ByteString) RunQueryRequest(com.google.datastore.v1.RunQueryRequest) Test(org.junit.Test)

Example 33 with ByteString

use of org.apache.beam.vendor.grpc.v1p26p0.com.google.protobuf.ByteString in project google-cloud-java by GoogleCloudPlatform.

the class CreateTopicAndPublishMessages method publishMessages.

public static void publishMessages() throws Exception {
    // [START pubsub_publish]
    TopicName topicName = TopicName.create("my-project-id", "my-topic-id");
    Publisher publisher = null;
    List<ApiFuture<String>> messageIdFutures = new ArrayList<>();
    try {
        // Create a publisher instance with default settings bound to the topic
        publisher = Publisher.defaultBuilder(topicName).build();
        List<String> messages = Arrays.asList("first message", "second message");
        // schedule publishing one message at a time : messages get automatically batched
        for (String message : messages) {
            ByteString data = ByteString.copyFromUtf8(message);
            PubsubMessage pubsubMessage = PubsubMessage.newBuilder().setData(data).build();
            // Once published, returns a server-assigned message id (unique within the topic)
            ApiFuture<String> messageIdFuture = publisher.publish(pubsubMessage);
            messageIdFutures.add(messageIdFuture);
        }
    } finally {
        // wait on any pending publish requests.
        List<String> messageIds = ApiFutures.allAsList(messageIdFutures).get();
        for (String messageId : messageIds) {
            System.out.println("published with message ID: " + messageId);
        }
        if (publisher != null) {
            // When finished with the publisher, shutdown to free up resources.
            publisher.shutdown();
        }
    }
// [END pubsub_publish]
}
Also used : ApiFuture(com.google.api.core.ApiFuture) ByteString(com.google.protobuf.ByteString) ArrayList(java.util.ArrayList) Publisher(com.google.cloud.pubsub.spi.v1.Publisher) ByteString(com.google.protobuf.ByteString) PubsubMessage(com.google.pubsub.v1.PubsubMessage) TopicName(com.google.pubsub.v1.TopicName)

Example 34 with ByteString

use of org.apache.beam.vendor.grpc.v1p26p0.com.google.protobuf.ByteString in project google-cloud-java by GoogleCloudPlatform.

the class PublisherSnippets method publish.

/** Example of publishing a message. */
// [TARGET publish(PubsubMessage)]
// [VARIABLE "my_message"]
public ApiFuture<String> publish(String message) {
    ByteString data = ByteString.copyFromUtf8(message);
    PubsubMessage pubsubMessage = PubsubMessage.newBuilder().setData(data).build();
    ApiFuture<String> messageIdFuture = publisher.publish(pubsubMessage);
    ApiFutures.addCallback(messageIdFuture, new ApiFutureCallback<String>() {

        public void onSuccess(String messageId) {
            System.out.println("published with message id: " + messageId);
        }

        public void onFailure(Throwable t) {
            System.out.println("failed to publish: " + t);
        }
    });
    return messageIdFuture;
}
Also used : ByteString(com.google.protobuf.ByteString) ByteString(com.google.protobuf.ByteString) PubsubMessage(com.google.pubsub.v1.PubsubMessage)

Example 35 with ByteString

use of org.apache.beam.vendor.grpc.v1p26p0.com.google.protobuf.ByteString in project compiler by boalang.

the class JsonFormat method printUnknownFields.

protected static void printUnknownFields(UnknownFieldSet unknownFields, JsonGenerator generator) throws IOException {
    boolean firstField = true;
    for (Map.Entry<Integer, UnknownFieldSet.Field> entry : unknownFields.asMap().entrySet()) {
        UnknownFieldSet.Field field = entry.getValue();
        if (firstField) {
            firstField = false;
        } else {
            generator.print(", ");
        }
        generator.print("\"");
        generator.print(entry.getKey().toString());
        generator.print("\"");
        generator.print(": [");
        boolean firstValue = true;
        for (long value : field.getVarintList()) {
            if (firstValue) {
                firstValue = false;
            } else {
                generator.print(", ");
            }
            generator.print(unsignedToString(value));
        }
        for (int value : field.getFixed32List()) {
            if (firstValue) {
                firstValue = false;
            } else {
                generator.print(", ");
            }
            generator.print(String.format((Locale) null, "0x%08x", value));
        }
        for (long value : field.getFixed64List()) {
            if (firstValue) {
                firstValue = false;
            } else {
                generator.print(", ");
            }
            generator.print(String.format((Locale) null, "0x%016x", value));
        }
        for (ByteString value : field.getLengthDelimitedList()) {
            if (firstValue) {
                firstValue = false;
            } else {
                generator.print(", ");
            }
            generator.print("\"");
            generator.print(escapeBytes(value));
            generator.print("\"");
        }
        for (UnknownFieldSet value : field.getGroupList()) {
            if (firstValue) {
                firstValue = false;
            } else {
                generator.print(", ");
            }
            generator.print("{\n");
            printUnknownFields(value, generator);
            generator.print("}\n");
        }
        generator.print("]");
    }
}
Also used : BigInteger(java.math.BigInteger) Locale(java.util.Locale) ByteString(com.google.protobuf.ByteString) Map(java.util.Map) UnknownFieldSet(com.google.protobuf.UnknownFieldSet)

Aggregations

ByteString (com.google.protobuf.ByteString)406 Test (org.junit.Test)143 ArrayList (java.util.ArrayList)65 ByteString (org.apache.beam.vendor.grpc.v1p43p2.com.google.protobuf.ByteString)63 HashMap (java.util.HashMap)41 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)40 IOException (java.io.IOException)37 List (java.util.List)33 Map (java.util.Map)33 ServerRequest (com.pokegoapi.main.ServerRequest)17 ExecutionException (java.util.concurrent.ExecutionException)16 AnnotateImageRequest (com.google.cloud.vision.v1.AnnotateImageRequest)14 AnnotateImageResponse (com.google.cloud.vision.v1.AnnotateImageResponse)14 BatchAnnotateImagesResponse (com.google.cloud.vision.v1.BatchAnnotateImagesResponse)14 Feature (com.google.cloud.vision.v1.Feature)14 Image (com.google.cloud.vision.v1.Image)14 ImageAnnotatorClient (com.google.cloud.vision.v1.ImageAnnotatorClient)14 FileInputStream (java.io.FileInputStream)13 ByteBuffer (java.nio.ByteBuffer)13 WebImage (com.google.cloud.vision.v1.WebDetection.WebImage)12