Search in sources :

Example 6 with Field

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);
}
Also used : Field(com.google.protobuf.Field) Test(org.junit.Test)

Example 7 with Field

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");
}
Also used : Field(com.google.protobuf.Field) Test(org.junit.Test)

Example 8 with Field

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");
}
Also used : Field(com.google.protobuf.Field) Option(com.google.protobuf.Option) ApiCallContext(com.google.api.gax.rpc.ApiCallContext) OperationSnapshot(com.google.api.gax.longrunning.OperationSnapshot) Test(org.junit.Test)

Example 9 with Field

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();
}
Also used : Field(com.google.protobuf.Field) StringReader(java.io.StringReader) Test(org.junit.Test)

Example 10 with Field

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);
}
Also used : Field(com.google.protobuf.Field) ByteArrayInputStream(java.io.ByteArrayInputStream) Test(org.junit.Test)

Aggregations

Field (com.google.protobuf.Field)11 Test (org.junit.Test)11 StringReader (java.io.StringReader)4 OperationSnapshot (com.google.api.gax.longrunning.OperationSnapshot)2 ApiCallContext (com.google.api.gax.rpc.ApiCallContext)2 Option (com.google.protobuf.Option)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ExecutionException (java.util.concurrent.ExecutionException)2 HttpResponseException (com.google.api.client.http.HttpResponseException)1 ApiException (com.google.api.gax.rpc.ApiException)1