Search in sources :

Example 26 with Money

use of com.google.type.Money in project gax-java by googleapis.

the class GrpcClientCallsTest method testAffinity.

@Test
public void testAffinity() throws IOException {
    MethodDescriptor<Color, Money> descriptor = FakeServiceGrpc.METHOD_RECOGNIZE;
    @SuppressWarnings("unchecked") ClientCall<Color, Money> clientCall0 = Mockito.mock(ClientCall.class);
    @SuppressWarnings("unchecked") ClientCall<Color, Money> clientCall1 = Mockito.mock(ClientCall.class);
    ManagedChannel channel0 = Mockito.mock(ManagedChannel.class);
    ManagedChannel channel1 = Mockito.mock(ManagedChannel.class);
    Mockito.when(channel0.newCall(Mockito.eq(descriptor), Mockito.<CallOptions>any())).thenReturn(clientCall0);
    Mockito.when(channel1.newCall(Mockito.eq(descriptor), Mockito.<CallOptions>any())).thenReturn(clientCall1);
    Channel pool = ChannelPool.create(ChannelPoolSettings.staticallySized(2), new FakeChannelFactory(Arrays.asList(channel0, channel1)));
    GrpcCallContext context = GrpcCallContext.createDefault().withChannel(pool);
    ClientCall<Color, Money> gotCallA = GrpcClientCalls.newCall(descriptor, context.withChannelAffinity(0));
    ClientCall<Color, Money> gotCallB = GrpcClientCalls.newCall(descriptor, context.withChannelAffinity(0));
    ClientCall<Color, Money> gotCallC = GrpcClientCalls.newCall(descriptor, context.withChannelAffinity(1));
    verify(channel0, Mockito.times(2)).newCall(Mockito.eq(descriptor), Mockito.any());
    verify(channel1, Mockito.times(1)).newCall(Mockito.eq(descriptor), Mockito.any());
}
Also used : Money(com.google.type.Money) Color(com.google.type.Color) ManagedChannel(io.grpc.ManagedChannel) Channel(io.grpc.Channel) ManagedChannel(io.grpc.ManagedChannel) FakeChannelFactory(com.google.api.gax.grpc.testing.FakeChannelFactory) Test(org.junit.Test)

Example 27 with Money

use of com.google.type.Money in project gax-java by googleapis.

the class GrpcClientCallsTest method testExtraHeaders.

@Test
public void testExtraHeaders() {
    Metadata emptyHeaders = new Metadata();
    final Map<String, List<String>> extraHeaders = new HashMap<>();
    extraHeaders.put("header-key-1", ImmutableList.<String>of("header-value-11", "header-value-12"));
    extraHeaders.put("header-key-2", ImmutableList.<String>of("header-value-21"));
    MethodDescriptor<Color, Money> descriptor = FakeServiceGrpc.METHOD_RECOGNIZE;
    @SuppressWarnings("unchecked") ClientCall<Color, Money> mockClientCall = Mockito.mock(ClientCall.class);
    @SuppressWarnings("unchecked") ClientCall.Listener<Money> mockListener = Mockito.mock(ClientCall.Listener.class);
    Channel mockChannel = Mockito.mock(ManagedChannel.class);
    Mockito.doAnswer(invocation -> {
        Metadata clientCallHeaders = (Metadata) invocation.getArguments()[1];
        Metadata.Key<String> key1 = Metadata.Key.of("header-key-1", Metadata.ASCII_STRING_MARSHALLER);
        Metadata.Key<String> key2 = Metadata.Key.of("header-key-2", Metadata.ASCII_STRING_MARSHALLER);
        assertThat(clientCallHeaders.getAll(key1)).containsExactly("header-value-11", "header-value-12");
        assertThat(clientCallHeaders.getAll(key2)).containsExactly("header-value-21");
        return null;
    }).when(mockClientCall).start(Mockito.<ClientCall.Listener<Money>>any(), Mockito.<Metadata>any());
    Mockito.when(mockChannel.newCall(Mockito.eq(descriptor), Mockito.<CallOptions>any())).thenReturn(mockClientCall);
    GrpcCallContext context = GrpcCallContext.createDefault().withChannel(mockChannel).withExtraHeaders(extraHeaders);
    GrpcClientCalls.newCall(descriptor, context).start(mockListener, emptyHeaders);
}
Also used : HashMap(java.util.HashMap) Color(com.google.type.Color) ManagedChannel(io.grpc.ManagedChannel) Channel(io.grpc.Channel) Metadata(io.grpc.Metadata) Money(com.google.type.Money) ClientCall(io.grpc.ClientCall) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) Test(org.junit.Test)

Example 28 with Money

use of com.google.type.Money in project gax-java by googleapis.

the class GrpcResponseMetadataTest method testResponseMetadataUnaryCall.

@Test
public void testResponseMetadataUnaryCall() {
    GrpcCallSettings<Color, Money> grpcCallSettings = GrpcCallSettings.create(FakeServiceGrpc.METHOD_RECOGNIZE);
    UnaryCallSettings<Color, Money> callSettings = UnaryCallSettings.<Color, Money>newUnaryCallSettingsBuilder().build();
    UnaryCallable<Color, Money> callable = GrpcCallableFactory.createUnaryCallable(grpcCallSettings, callSettings, clientContext);
    Assert.assertNull(requestHeaders);
    GrpcResponseMetadata responseMetadata = new GrpcResponseMetadata();
    callable.call(Color.getDefaultInstance(), responseMetadata.createContextWithHandlers());
    Assert.assertNotNull(requestHeaders);
    Metadata metadata = responseMetadata.getMetadata();
    Metadata trailingMetadata = responseMetadata.getTrailingMetadata();
    Assert.assertEquals(metadata.get(Key.of(HEADER_KEY, Metadata.ASCII_STRING_MARSHALLER)), HEADER_VALUE);
    Assert.assertEquals(trailingMetadata.get(Key.of(TRAILER_KEY, Metadata.ASCII_STRING_MARSHALLER)), TRAILER_VALUE);
}
Also used : Money(com.google.type.Money) Color(com.google.type.Color) Metadata(io.grpc.Metadata) Test(org.junit.Test)

Example 29 with Money

use of com.google.type.Money in project gax-java by googleapis.

the class GrpcResponseMetadataTest method setUp.

@Before
public void setUp() throws Exception {
    String serverName = "fakeservice";
    FakeServiceImplBase serviceImpl = Mockito.mock(FakeServiceImplBase.class);
    Mockito.doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) {
            Color color = invocation.getArgument(0);
            StreamObserver<Money> observer = invocation.getArgument(1);
            observer.onNext(Money.newBuilder().setCurrencyCode("USD").setUnits((long) (color.getRed() * 255)).build());
            observer.onCompleted();
            return null;
        }
    }).when(serviceImpl).recognize(Mockito.<Color>any(), Mockito.<StreamObserver<Money>>any());
    requestHeaders = null;
    inprocessServer = new InProcessServer<>(serviceImpl, serverName, new ServerInterceptor() {

        @Override
        public <ReqT, RespT> Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> call, Metadata headers, ServerCallHandler<ReqT, RespT> next) {
            requestHeaders = headers;
            return next.startCall(new SimpleForwardingServerCall<ReqT, RespT>(call) {

                @Override
                public void sendHeaders(Metadata responseHeaders) {
                    responseHeaders.put(Key.of(HEADER_KEY, Metadata.ASCII_STRING_MARSHALLER), HEADER_VALUE);
                    super.sendHeaders(responseHeaders);
                }

                @Override
                public void close(Status status, Metadata trailers) {
                    trailers.put(Key.of(TRAILER_KEY, Metadata.ASCII_STRING_MARSHALLER), TRAILER_VALUE);
                    super.close(status, trailers);
                }
            }, headers);
        }
    });
    inprocessServer.start();
    channel = InProcessChannelBuilder.forName(serverName).directExecutor().usePlaintext().intercept(new GrpcMetadataHandlerInterceptor()).build();
    clientContext = ClientContext.newBuilder().setTransportChannel(GrpcTransportChannel.create(channel)).setDefaultCallContext(GrpcCallContext.of(channel, CallOptions.DEFAULT)).build();
}
Also used : StreamObserver(io.grpc.stub.StreamObserver) Status(io.grpc.Status) ServerCallHandler(io.grpc.ServerCallHandler) Color(com.google.type.Color) Metadata(io.grpc.Metadata) Money(com.google.type.Money) SimpleForwardingServerCall(io.grpc.ForwardingServerCall.SimpleForwardingServerCall) ServerCall(io.grpc.ServerCall) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ServerInterceptor(io.grpc.ServerInterceptor) FakeServiceImplBase(com.google.api.gax.grpc.testing.FakeServiceGrpc.FakeServiceImplBase) Before(org.junit.Before)

Example 30 with Money

use of com.google.type.Money in project gax-java by googleapis.

the class ProtoOperationTransformersTest method testAnyResponseTransformer.

@Test
public void testAnyResponseTransformer() {
    ResponseTransformer<Money> transformer = ResponseTransformer.create(Money.class);
    Money inputMoney = Money.newBuilder().setCurrencyCode("USD").build();
    OperationSnapshot operationSnapshot = GrpcOperationSnapshot.create(Operation.newBuilder().setResponse(Any.pack(inputMoney)).build());
    Truth.assertThat(transformer.apply(operationSnapshot)).isEqualTo(inputMoney);
}
Also used : Money(com.google.type.Money) OperationSnapshot(com.google.api.gax.longrunning.OperationSnapshot) Test(org.junit.Test)

Aggregations

Money (com.google.type.Money)35 Test (org.junit.Test)28 Color (com.google.type.Color)19 OperationSnapshot (com.google.api.gax.longrunning.OperationSnapshot)13 ManagedChannel (io.grpc.ManagedChannel)8 UnavailableException (com.google.api.gax.rpc.UnavailableException)6 UnknownException (com.google.api.gax.rpc.UnknownException)6 Status (com.google.rpc.Status)6 Metadata (io.grpc.Metadata)6 CallOptions (io.grpc.CallOptions)5 Channel (io.grpc.Channel)5 ClientCall (io.grpc.ClientCall)4 DataLabelingJob (com.google.cloud.aiplatform.v1.DataLabelingJob)3 DatasetName (com.google.cloud.aiplatform.v1.DatasetName)3 JobServiceClient (com.google.cloud.aiplatform.v1.JobServiceClient)3 JobServiceSettings (com.google.cloud.aiplatform.v1.JobServiceSettings)3 LocationName (com.google.cloud.aiplatform.v1.LocationName)3 Value (com.google.protobuf.Value)3 Deadline (io.grpc.Deadline)3 StatusRuntimeException (io.grpc.StatusRuntimeException)3