use of com.google.protobuf.Field in project gax-java by googleapis.
the class HttpJsonClientInterceptorTest method testCustomInterceptor.
@Test
public void testCustomInterceptor() throws ExecutionException, InterruptedException {
HttpJsonDirectCallable<Field, Field> callable = new HttpJsonDirectCallable<>(FAKE_METHOD_DESCRIPTOR);
HttpJsonCallContext callContext = HttpJsonCallContext.createDefault().withChannel(channel).withTimeout(Duration.ofSeconds(30));
Field request;
Field expectedResponse;
request = expectedResponse = // "echo" service
Field.newBuilder().setName("imTheBestField").setNumber(2).setCardinality(Cardinality.CARDINALITY_OPTIONAL).setDefaultValue("blah").build();
MOCK_SERVICE.addResponse(expectedResponse);
Field actualResponse = callable.futureCall(request, callContext).get();
// Test that the interceptors did not affect normal execution
assertThat(actualResponse).isEqualTo(expectedResponse);
assertThat(MOCK_SERVICE.getRequestPaths().size()).isEqualTo(1);
String headerValue = MOCK_SERVICE.getRequestHeaders().get("header-key").iterator().next();
// Test that internal interceptor worked (the one which inserts headers)
assertThat(headerValue).isEqualTo("headerValue");
// Test that the custom interceptor was called
assertThat(interceptor.capturedStatusCode).isEqualTo(200);
assertThat(interceptor.capturedResponseHeaders).isNotNull();
assertThat(interceptor.capturedMessage).isEqualTo(request);
}
use of com.google.protobuf.Field in project gax-java by googleapis.
the class HttpJsonDirectCallableTest method testSuccessfulUnaryResponse.
@Test
public void testSuccessfulUnaryResponse() throws ExecutionException, InterruptedException {
HttpJsonDirectCallable<Field, Field> callable = new HttpJsonDirectCallable<>(FAKE_METHOD_DESCRIPTOR);
HttpJsonCallContext callContext = HttpJsonCallContext.createDefault().withChannel(channel).withTimeout(Duration.ofSeconds(30));
Field request;
Field expectedResponse;
request = expectedResponse = // "echo" service
Field.newBuilder().setName("imTheBestField").setNumber(2).setCardinality(Cardinality.CARDINALITY_OPTIONAL).setDefaultValue("blah").build();
MOCK_SERVICE.addResponse(expectedResponse);
Field actualResponse = callable.futureCall(request, callContext).get();
assertThat(actualResponse).isEqualTo(expectedResponse);
assertThat(MOCK_SERVICE.getRequestPaths().size()).isEqualTo(1);
String headerValue = MOCK_SERVICE.getRequestHeaders().get("header-key").iterator().next();
assertThat(headerValue).isEqualTo("headerValue");
}
use of com.google.protobuf.Field in project gax-java by googleapis.
the class HttpJsonOperationSnapshotCallableTest method futureCallTest.
@Test
public void futureCallTest() throws ExecutionException, InterruptedException {
Option request = Option.newBuilder().setName("Arizona").build();
Field field = Field.newBuilder().setName("Georgia").build();
ApiCallContext context = mock(ApiCallContext.class);
OperationSnapshot operationSnapshot = HttpJsonOperationSnapshot.newBuilder().setName("California").setMetadata(2).setDone(true).setResponse("Florida").setError(0, "no error").build();
SettableApiFuture<Field> settableApiFuture = SettableApiFuture.create();
settableApiFuture.set(field);
when(operationSnapshotFactory.create(request, field)).thenReturn(operationSnapshot);
when(innerCallable.futureCall(request, context)).thenReturn(settableApiFuture);
ApiFuture<OperationSnapshot> futureCall = operationSnapCallable.futureCall(request, context);
Truth.assertThat(futureCall.get().getName()).isEqualTo("California");
}
use of com.google.protobuf.Field in project gax-java by googleapis.
the class ProtoMessageJsonStreamIteratorTest method testSingleElement.
@Test
public void testSingleElement() throws IOException {
Field[] expectedData = new Field[] { Field.newBuilder().setName("cat").addOptions(Option.newBuilder().setName("haha").build()).addOptions(Option.newBuilder().setName("hoho").build()).setNumber(1).setDefaultValue("mew").build() };
String jsonData = "[{\n" + " \"number\": 1,\n" + " \"name\": \"cat\",\n" + " \"options\": [{\n" + " \"name\": \"haha\"\n" + " }, {\n" + " \"name\": \"hoho\"\n" + " }],\n" + " \"defaultValue\": \"mew\"\n" + "}]";
ProtoMessageJsonStreamIterator streamIter = new ProtoMessageJsonStreamIterator(new StringReader(jsonData));
Truth.assertThat(streamIter.hasNext()).isTrue();
Field.Builder builder = Field.newBuilder();
JsonFormat.parser().merge(streamIter.next(), builder);
Truth.assertThat(builder.build()).isEqualTo(expectedData[0]);
Truth.assertThat(streamIter.hasNext()).isFalse();
streamIter.close();
// closing a closed iterator should be no-op.
streamIter.close();
}
use of com.google.protobuf.Field in project gax-java by googleapis.
the class ProtoMessageResponseParserTest method parseWithTypeRegistry.
@Test
public void parseWithTypeRegistry() {
Field actualField = parser.parse(new ByteArrayInputStream(fieldJson.getBytes(StandardCharsets.UTF_8)), TypeRegistry.newBuilder().build());
Truth.assertThat(actualField).isEqualTo(field);
}
Aggregations