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());
}
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);
}
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);
}
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();
}
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);
}
Aggregations