diff --git a/java-profiler/google-cloud-profiler/pom.xml b/java-profiler/google-cloud-profiler/pom.xml index 25f4912e737a..b10e4f3e729a 100644 --- a/java-profiler/google-cloud-profiler/pom.xml +++ b/java-profiler/google-cloud-profiler/pom.xml @@ -58,6 +58,10 @@ com.google.api gax-grpc + + com.google.api + gax-httpjson + org.threeten threetenbp @@ -77,12 +81,24 @@ test + + com.google.api + gax + testlib + test + com.google.api gax-grpc testlib test + + com.google.api + gax-httpjson + testlib + test + diff --git a/java-profiler/google-cloud-profiler/src/main/java/com/google/devtools/cloudprofiler/v2/ProfilerServiceClient.java b/java-profiler/google-cloud-profiler/src/main/java/com/google/devtools/cloudprofiler/v2/ProfilerServiceClient.java index 71461385db27..fb07f73ee604 100644 --- a/java-profiler/google-cloud-profiler/src/main/java/com/google/devtools/cloudprofiler/v2/ProfilerServiceClient.java +++ b/java-profiler/google-cloud-profiler/src/main/java/com/google/devtools/cloudprofiler/v2/ProfilerServiceClient.java @@ -104,6 +104,21 @@ * ProfilerServiceClient.create(profilerServiceSettings); * } * + *

To use REST (HTTP1.1/JSON) transport (instead of gRPC) for sending and receiving requests over + * the wire: + * + *

{@code
+ * // This snippet has been automatically generated for illustrative purposes only.
+ * // It may require modifications to work in your environment.
+ * ProfilerServiceSettings profilerServiceSettings =
+ *     ProfilerServiceSettings.newBuilder()
+ *         .setTransportChannelProvider(
+ *             ProfilerServiceSettings.defaultHttpJsonTransportProviderBuilder().build())
+ *         .build();
+ * ProfilerServiceClient profilerServiceClient =
+ *     ProfilerServiceClient.create(profilerServiceSettings);
+ * }
+ * *

Please refer to the GitHub repository's samples for more quickstart code snippets. */ @Generated("by gapic-generator-java") diff --git a/java-profiler/google-cloud-profiler/src/main/java/com/google/devtools/cloudprofiler/v2/ProfilerServiceSettings.java b/java-profiler/google-cloud-profiler/src/main/java/com/google/devtools/cloudprofiler/v2/ProfilerServiceSettings.java index a72117e9fb6a..33f1b1ea583c 100644 --- a/java-profiler/google-cloud-profiler/src/main/java/com/google/devtools/cloudprofiler/v2/ProfilerServiceSettings.java +++ b/java-profiler/google-cloud-profiler/src/main/java/com/google/devtools/cloudprofiler/v2/ProfilerServiceSettings.java @@ -21,6 +21,7 @@ import com.google.api.gax.core.GoogleCredentialsProvider; import com.google.api.gax.core.InstantiatingExecutorProvider; import com.google.api.gax.grpc.InstantiatingGrpcChannelProvider; +import com.google.api.gax.httpjson.InstantiatingHttpJsonChannelProvider; import com.google.api.gax.rpc.ApiClientHeaderProvider; import com.google.api.gax.rpc.ClientContext; import com.google.api.gax.rpc.ClientSettings; @@ -108,11 +109,18 @@ public static GoogleCredentialsProvider.Builder defaultCredentialsProviderBuilde return ProfilerServiceStubSettings.defaultCredentialsProviderBuilder(); } - /** Returns a builder for the default ChannelProvider for this service. */ + /** Returns a builder for the default gRPC ChannelProvider for this service. */ public static InstantiatingGrpcChannelProvider.Builder defaultGrpcTransportProviderBuilder() { return ProfilerServiceStubSettings.defaultGrpcTransportProviderBuilder(); } + /** Returns a builder for the default REST ChannelProvider for this service. */ + @BetaApi + public static InstantiatingHttpJsonChannelProvider.Builder + defaultHttpJsonTransportProviderBuilder() { + return ProfilerServiceStubSettings.defaultHttpJsonTransportProviderBuilder(); + } + public static TransportChannelProvider defaultTransportChannelProvider() { return ProfilerServiceStubSettings.defaultTransportChannelProvider(); } @@ -122,11 +130,17 @@ public static ApiClientHeaderProvider.Builder defaultApiClientHeaderProviderBuil return ProfilerServiceStubSettings.defaultApiClientHeaderProviderBuilder(); } - /** Returns a new builder for this class. */ + /** Returns a new gRPC builder for this class. */ public static Builder newBuilder() { return Builder.createDefault(); } + /** Returns a new REST builder for this class. */ + @BetaApi + public static Builder newHttpJsonBuilder() { + return Builder.createHttpJsonDefault(); + } + /** Returns a new builder for this class. */ public static Builder newBuilder(ClientContext clientContext) { return new Builder(clientContext); @@ -164,6 +178,11 @@ private static Builder createDefault() { return new Builder(ProfilerServiceStubSettings.newBuilder()); } + @BetaApi + private static Builder createHttpJsonDefault() { + return new Builder(ProfilerServiceStubSettings.newHttpJsonBuilder()); + } + public ProfilerServiceStubSettings.Builder getStubSettingsBuilder() { return ((ProfilerServiceStubSettings.Builder) getStubSettings()); } diff --git a/java-profiler/google-cloud-profiler/src/main/java/com/google/devtools/cloudprofiler/v2/stub/HttpJsonProfilerServiceCallableFactory.java b/java-profiler/google-cloud-profiler/src/main/java/com/google/devtools/cloudprofiler/v2/stub/HttpJsonProfilerServiceCallableFactory.java new file mode 100644 index 000000000000..389fa629a101 --- /dev/null +++ b/java-profiler/google-cloud-profiler/src/main/java/com/google/devtools/cloudprofiler/v2/stub/HttpJsonProfilerServiceCallableFactory.java @@ -0,0 +1,105 @@ +/* + * Copyright 2022 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.devtools.cloudprofiler.v2.stub; + +import com.google.api.core.BetaApi; +import com.google.api.gax.httpjson.HttpJsonCallSettings; +import com.google.api.gax.httpjson.HttpJsonCallableFactory; +import com.google.api.gax.httpjson.HttpJsonOperationSnapshotCallable; +import com.google.api.gax.httpjson.HttpJsonStubCallableFactory; +import com.google.api.gax.httpjson.longrunning.stub.OperationsStub; +import com.google.api.gax.rpc.BatchingCallSettings; +import com.google.api.gax.rpc.ClientContext; +import com.google.api.gax.rpc.OperationCallSettings; +import com.google.api.gax.rpc.OperationCallable; +import com.google.api.gax.rpc.PagedCallSettings; +import com.google.api.gax.rpc.ServerStreamingCallSettings; +import com.google.api.gax.rpc.ServerStreamingCallable; +import com.google.api.gax.rpc.UnaryCallSettings; +import com.google.api.gax.rpc.UnaryCallable; +import com.google.longrunning.Operation; +import javax.annotation.Generated; + +// AUTO-GENERATED DOCUMENTATION AND CLASS. +/** + * REST callable factory implementation for the ProfilerService service API. + * + *

This class is for advanced usage. + */ +@Generated("by gapic-generator-java") +@BetaApi +public class HttpJsonProfilerServiceCallableFactory + implements HttpJsonStubCallableFactory { + + @Override + public UnaryCallable createUnaryCallable( + HttpJsonCallSettings httpJsonCallSettings, + UnaryCallSettings callSettings, + ClientContext clientContext) { + return HttpJsonCallableFactory.createUnaryCallable( + httpJsonCallSettings, callSettings, clientContext); + } + + @Override + public + UnaryCallable createPagedCallable( + HttpJsonCallSettings httpJsonCallSettings, + PagedCallSettings callSettings, + ClientContext clientContext) { + return HttpJsonCallableFactory.createPagedCallable( + httpJsonCallSettings, callSettings, clientContext); + } + + @Override + public UnaryCallable createBatchingCallable( + HttpJsonCallSettings httpJsonCallSettings, + BatchingCallSettings callSettings, + ClientContext clientContext) { + return HttpJsonCallableFactory.createBatchingCallable( + httpJsonCallSettings, callSettings, clientContext); + } + + @BetaApi( + "The surface for long-running operations is not stable yet and may change in the future.") + @Override + public + OperationCallable createOperationCallable( + HttpJsonCallSettings httpJsonCallSettings, + OperationCallSettings callSettings, + ClientContext clientContext, + OperationsStub operationsStub) { + UnaryCallable innerCallable = + HttpJsonCallableFactory.createBaseUnaryCallable( + httpJsonCallSettings, callSettings.getInitialCallSettings(), clientContext); + HttpJsonOperationSnapshotCallable initialCallable = + new HttpJsonOperationSnapshotCallable( + innerCallable, + httpJsonCallSettings.getMethodDescriptor().getOperationSnapshotFactory()); + return HttpJsonCallableFactory.createOperationCallable( + callSettings, clientContext, operationsStub.longRunningClient(), initialCallable); + } + + @Override + public + ServerStreamingCallable createServerStreamingCallable( + HttpJsonCallSettings httpJsonCallSettings, + ServerStreamingCallSettings callSettings, + ClientContext clientContext) { + return HttpJsonCallableFactory.createServerStreamingCallable( + httpJsonCallSettings, callSettings, clientContext); + } +} diff --git a/java-profiler/google-cloud-profiler/src/main/java/com/google/devtools/cloudprofiler/v2/stub/HttpJsonProfilerServiceStub.java b/java-profiler/google-cloud-profiler/src/main/java/com/google/devtools/cloudprofiler/v2/stub/HttpJsonProfilerServiceStub.java new file mode 100644 index 000000000000..2ba41df61809 --- /dev/null +++ b/java-profiler/google-cloud-profiler/src/main/java/com/google/devtools/cloudprofiler/v2/stub/HttpJsonProfilerServiceStub.java @@ -0,0 +1,302 @@ +/* + * Copyright 2022 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.devtools.cloudprofiler.v2.stub; + +import com.google.api.core.BetaApi; +import com.google.api.core.InternalApi; +import com.google.api.gax.core.BackgroundResource; +import com.google.api.gax.core.BackgroundResourceAggregation; +import com.google.api.gax.httpjson.ApiMethodDescriptor; +import com.google.api.gax.httpjson.HttpJsonCallSettings; +import com.google.api.gax.httpjson.HttpJsonStubCallableFactory; +import com.google.api.gax.httpjson.ProtoMessageRequestFormatter; +import com.google.api.gax.httpjson.ProtoMessageResponseParser; +import com.google.api.gax.httpjson.ProtoRestSerializer; +import com.google.api.gax.rpc.ClientContext; +import com.google.api.gax.rpc.UnaryCallable; +import com.google.devtools.cloudprofiler.v2.CreateOfflineProfileRequest; +import com.google.devtools.cloudprofiler.v2.CreateProfileRequest; +import com.google.devtools.cloudprofiler.v2.Profile; +import com.google.devtools.cloudprofiler.v2.UpdateProfileRequest; +import com.google.protobuf.TypeRegistry; +import java.io.IOException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.concurrent.TimeUnit; +import javax.annotation.Generated; + +// AUTO-GENERATED DOCUMENTATION AND CLASS. +/** + * REST stub implementation for the ProfilerService service API. + * + *

This class is for advanced usage and reflects the underlying API directly. + */ +@Generated("by gapic-generator-java") +@BetaApi +public class HttpJsonProfilerServiceStub extends ProfilerServiceStub { + private static final TypeRegistry typeRegistry = TypeRegistry.newBuilder().build(); + + private static final ApiMethodDescriptor + createProfileMethodDescriptor = + ApiMethodDescriptor.newBuilder() + .setFullMethodName("google.devtools.cloudprofiler.v2.ProfilerService/CreateProfile") + .setHttpMethod("POST") + .setType(ApiMethodDescriptor.MethodType.UNARY) + .setRequestFormatter( + ProtoMessageRequestFormatter.newBuilder() + .setPath( + "/v2/{parent=projects/*}/profiles", + request -> { + Map fields = new HashMap<>(); + ProtoRestSerializer serializer = + ProtoRestSerializer.create(); + serializer.putPathParam(fields, "parent", request.getParent()); + return fields; + }) + .setQueryParamsExtractor( + request -> { + Map> fields = new HashMap<>(); + ProtoRestSerializer serializer = + ProtoRestSerializer.create(); + return fields; + }) + .setRequestBodyExtractor( + request -> + ProtoRestSerializer.create() + .toBody("*", request.toBuilder().clearParent().build())) + .build()) + .setResponseParser( + ProtoMessageResponseParser.newBuilder() + .setDefaultInstance(Profile.getDefaultInstance()) + .setDefaultTypeRegistry(typeRegistry) + .build()) + .build(); + + private static final ApiMethodDescriptor + createOfflineProfileMethodDescriptor = + ApiMethodDescriptor.newBuilder() + .setFullMethodName( + "google.devtools.cloudprofiler.v2.ProfilerService/CreateOfflineProfile") + .setHttpMethod("POST") + .setType(ApiMethodDescriptor.MethodType.UNARY) + .setRequestFormatter( + ProtoMessageRequestFormatter.newBuilder() + .setPath( + "/v2/{parent=projects/*}/profiles:createOffline", + request -> { + Map fields = new HashMap<>(); + ProtoRestSerializer serializer = + ProtoRestSerializer.create(); + serializer.putPathParam(fields, "parent", request.getParent()); + return fields; + }) + .setQueryParamsExtractor( + request -> { + Map> fields = new HashMap<>(); + ProtoRestSerializer serializer = + ProtoRestSerializer.create(); + return fields; + }) + .setRequestBodyExtractor( + request -> + ProtoRestSerializer.create().toBody("profile", request.getProfile())) + .build()) + .setResponseParser( + ProtoMessageResponseParser.newBuilder() + .setDefaultInstance(Profile.getDefaultInstance()) + .setDefaultTypeRegistry(typeRegistry) + .build()) + .build(); + + private static final ApiMethodDescriptor + updateProfileMethodDescriptor = + ApiMethodDescriptor.newBuilder() + .setFullMethodName("google.devtools.cloudprofiler.v2.ProfilerService/UpdateProfile") + .setHttpMethod("PATCH") + .setType(ApiMethodDescriptor.MethodType.UNARY) + .setRequestFormatter( + ProtoMessageRequestFormatter.newBuilder() + .setPath( + "/v2/{profile.name=projects/*/profiles/*}", + request -> { + Map fields = new HashMap<>(); + ProtoRestSerializer serializer = + ProtoRestSerializer.create(); + serializer.putPathParam( + fields, "profile.name", request.getProfile().getName()); + return fields; + }) + .setQueryParamsExtractor( + request -> { + Map> fields = new HashMap<>(); + ProtoRestSerializer serializer = + ProtoRestSerializer.create(); + serializer.putQueryParam(fields, "updateMask", request.getUpdateMask()); + return fields; + }) + .setRequestBodyExtractor( + request -> + ProtoRestSerializer.create().toBody("profile", request.getProfile())) + .build()) + .setResponseParser( + ProtoMessageResponseParser.newBuilder() + .setDefaultInstance(Profile.getDefaultInstance()) + .setDefaultTypeRegistry(typeRegistry) + .build()) + .build(); + + private final UnaryCallable createProfileCallable; + private final UnaryCallable createOfflineProfileCallable; + private final UnaryCallable updateProfileCallable; + + private final BackgroundResource backgroundResources; + private final HttpJsonStubCallableFactory callableFactory; + + public static final HttpJsonProfilerServiceStub create(ProfilerServiceStubSettings settings) + throws IOException { + return new HttpJsonProfilerServiceStub(settings, ClientContext.create(settings)); + } + + public static final HttpJsonProfilerServiceStub create(ClientContext clientContext) + throws IOException { + return new HttpJsonProfilerServiceStub( + ProfilerServiceStubSettings.newHttpJsonBuilder().build(), clientContext); + } + + public static final HttpJsonProfilerServiceStub create( + ClientContext clientContext, HttpJsonStubCallableFactory callableFactory) throws IOException { + return new HttpJsonProfilerServiceStub( + ProfilerServiceStubSettings.newHttpJsonBuilder().build(), clientContext, callableFactory); + } + + /** + * Constructs an instance of HttpJsonProfilerServiceStub, using the given settings. This is + * protected so that it is easy to make a subclass, but otherwise, the static factory methods + * should be preferred. + */ + protected HttpJsonProfilerServiceStub( + ProfilerServiceStubSettings settings, ClientContext clientContext) throws IOException { + this(settings, clientContext, new HttpJsonProfilerServiceCallableFactory()); + } + + /** + * Constructs an instance of HttpJsonProfilerServiceStub, using the given settings. This is + * protected so that it is easy to make a subclass, but otherwise, the static factory methods + * should be preferred. + */ + protected HttpJsonProfilerServiceStub( + ProfilerServiceStubSettings settings, + ClientContext clientContext, + HttpJsonStubCallableFactory callableFactory) + throws IOException { + this.callableFactory = callableFactory; + + HttpJsonCallSettings createProfileTransportSettings = + HttpJsonCallSettings.newBuilder() + .setMethodDescriptor(createProfileMethodDescriptor) + .setTypeRegistry(typeRegistry) + .build(); + HttpJsonCallSettings + createOfflineProfileTransportSettings = + HttpJsonCallSettings.newBuilder() + .setMethodDescriptor(createOfflineProfileMethodDescriptor) + .setTypeRegistry(typeRegistry) + .build(); + HttpJsonCallSettings updateProfileTransportSettings = + HttpJsonCallSettings.newBuilder() + .setMethodDescriptor(updateProfileMethodDescriptor) + .setTypeRegistry(typeRegistry) + .build(); + + this.createProfileCallable = + callableFactory.createUnaryCallable( + createProfileTransportSettings, settings.createProfileSettings(), clientContext); + this.createOfflineProfileCallable = + callableFactory.createUnaryCallable( + createOfflineProfileTransportSettings, + settings.createOfflineProfileSettings(), + clientContext); + this.updateProfileCallable = + callableFactory.createUnaryCallable( + updateProfileTransportSettings, settings.updateProfileSettings(), clientContext); + + this.backgroundResources = + new BackgroundResourceAggregation(clientContext.getBackgroundResources()); + } + + @InternalApi + public static List getMethodDescriptors() { + List methodDescriptors = new ArrayList<>(); + methodDescriptors.add(createProfileMethodDescriptor); + methodDescriptors.add(createOfflineProfileMethodDescriptor); + methodDescriptors.add(updateProfileMethodDescriptor); + return methodDescriptors; + } + + @Override + public UnaryCallable createProfileCallable() { + return createProfileCallable; + } + + @Override + public UnaryCallable createOfflineProfileCallable() { + return createOfflineProfileCallable; + } + + @Override + public UnaryCallable updateProfileCallable() { + return updateProfileCallable; + } + + @Override + public final void close() { + try { + backgroundResources.close(); + } catch (RuntimeException e) { + throw e; + } catch (Exception e) { + throw new IllegalStateException("Failed to close resource", e); + } + } + + @Override + public void shutdown() { + backgroundResources.shutdown(); + } + + @Override + public boolean isShutdown() { + return backgroundResources.isShutdown(); + } + + @Override + public boolean isTerminated() { + return backgroundResources.isTerminated(); + } + + @Override + public void shutdownNow() { + backgroundResources.shutdownNow(); + } + + @Override + public boolean awaitTermination(long duration, TimeUnit unit) throws InterruptedException { + return backgroundResources.awaitTermination(duration, unit); + } +} diff --git a/java-profiler/google-cloud-profiler/src/main/java/com/google/devtools/cloudprofiler/v2/stub/ProfilerServiceStubSettings.java b/java-profiler/google-cloud-profiler/src/main/java/com/google/devtools/cloudprofiler/v2/stub/ProfilerServiceStubSettings.java index f05c293bdd94..f2ed96c41d2c 100644 --- a/java-profiler/google-cloud-profiler/src/main/java/com/google/devtools/cloudprofiler/v2/stub/ProfilerServiceStubSettings.java +++ b/java-profiler/google-cloud-profiler/src/main/java/com/google/devtools/cloudprofiler/v2/stub/ProfilerServiceStubSettings.java @@ -24,6 +24,9 @@ import com.google.api.gax.grpc.GaxGrpcProperties; import com.google.api.gax.grpc.GrpcTransportChannel; import com.google.api.gax.grpc.InstantiatingGrpcChannelProvider; +import com.google.api.gax.httpjson.GaxHttpJsonProperties; +import com.google.api.gax.httpjson.HttpJsonTransportChannel; +import com.google.api.gax.httpjson.InstantiatingHttpJsonChannelProvider; import com.google.api.gax.retrying.RetrySettings; import com.google.api.gax.rpc.ApiClientHeaderProvider; import com.google.api.gax.rpc.ClientContext; @@ -114,6 +117,11 @@ public ProfilerServiceStub createStub() throws IOException { .equals(GrpcTransportChannel.getGrpcTransportName())) { return GrpcProfilerServiceStub.create(this); } + if (getTransportChannelProvider() + .getTransportName() + .equals(HttpJsonTransportChannel.getHttpJsonTransportName())) { + return HttpJsonProfilerServiceStub.create(this); + } throw new UnsupportedOperationException( String.format( "Transport not supported: %s", getTransportChannelProvider().getTransportName())); @@ -146,18 +154,25 @@ public static GoogleCredentialsProvider.Builder defaultCredentialsProviderBuilde .setUseJwtAccessWithScope(true); } - /** Returns a builder for the default ChannelProvider for this service. */ + /** Returns a builder for the default gRPC ChannelProvider for this service. */ public static InstantiatingGrpcChannelProvider.Builder defaultGrpcTransportProviderBuilder() { return InstantiatingGrpcChannelProvider.newBuilder() .setMaxInboundMessageSize(Integer.MAX_VALUE); } + /** Returns a builder for the default REST ChannelProvider for this service. */ + @BetaApi + public static InstantiatingHttpJsonChannelProvider.Builder + defaultHttpJsonTransportProviderBuilder() { + return InstantiatingHttpJsonChannelProvider.newBuilder(); + } + public static TransportChannelProvider defaultTransportChannelProvider() { return defaultGrpcTransportProviderBuilder().build(); } @BetaApi("The surface for customizing headers is not stable yet and may change in the future.") - public static ApiClientHeaderProvider.Builder defaultApiClientHeaderProviderBuilder() { + public static ApiClientHeaderProvider.Builder defaultGrpcApiClientHeaderProviderBuilder() { return ApiClientHeaderProvider.newBuilder() .setGeneratedLibToken( "gapic", GaxProperties.getLibraryVersion(ProfilerServiceStubSettings.class)) @@ -165,11 +180,30 @@ public static ApiClientHeaderProvider.Builder defaultApiClientHeaderProviderBuil GaxGrpcProperties.getGrpcTokenName(), GaxGrpcProperties.getGrpcVersion()); } - /** Returns a new builder for this class. */ + @BetaApi("The surface for customizing headers is not stable yet and may change in the future.") + public static ApiClientHeaderProvider.Builder defaultHttpJsonApiClientHeaderProviderBuilder() { + return ApiClientHeaderProvider.newBuilder() + .setGeneratedLibToken( + "gapic", GaxProperties.getLibraryVersion(ProfilerServiceStubSettings.class)) + .setTransportToken( + GaxHttpJsonProperties.getHttpJsonTokenName(), + GaxHttpJsonProperties.getHttpJsonVersion()); + } + + public static ApiClientHeaderProvider.Builder defaultApiClientHeaderProviderBuilder() { + return ProfilerServiceStubSettings.defaultGrpcApiClientHeaderProviderBuilder(); + } + + /** Returns a new gRPC builder for this class. */ public static Builder newBuilder() { return Builder.createDefault(); } + /** Returns a new REST builder for this class. */ + public static Builder newHttpJsonBuilder() { + return Builder.createHttpJsonDefault(); + } + /** Returns a new builder for this class. */ public static Builder newBuilder(ClientContext clientContext) { return new Builder(clientContext); @@ -274,6 +308,19 @@ private static Builder createDefault() { return initDefaults(builder); } + private static Builder createHttpJsonDefault() { + Builder builder = new Builder(((ClientContext) null)); + + builder.setTransportChannelProvider(defaultHttpJsonTransportProviderBuilder().build()); + builder.setCredentialsProvider(defaultCredentialsProviderBuilder().build()); + builder.setInternalHeaderProvider(defaultHttpJsonApiClientHeaderProviderBuilder().build()); + builder.setEndpoint(getDefaultEndpoint()); + builder.setMtlsEndpoint(getDefaultMtlsEndpoint()); + builder.setSwitchToMtlsEndpointAllowed(true); + + return initDefaults(builder); + } + private static Builder initDefaults(Builder builder) { builder .createProfileSettings() diff --git a/java-profiler/google-cloud-profiler/src/test/java/com/google/devtools/cloudprofiler/v2/ProfilerServiceClientHttpJsonTest.java b/java-profiler/google-cloud-profiler/src/test/java/com/google/devtools/cloudprofiler/v2/ProfilerServiceClientHttpJsonTest.java new file mode 100644 index 000000000000..a8a14a9efb0f --- /dev/null +++ b/java-profiler/google-cloud-profiler/src/test/java/com/google/devtools/cloudprofiler/v2/ProfilerServiceClientHttpJsonTest.java @@ -0,0 +1,267 @@ +/* + * Copyright 2022 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.devtools.cloudprofiler.v2; + +import com.google.api.gax.core.NoCredentialsProvider; +import com.google.api.gax.httpjson.GaxHttpJsonProperties; +import com.google.api.gax.httpjson.testing.MockHttpService; +import com.google.api.gax.rpc.ApiClientHeaderProvider; +import com.google.api.gax.rpc.ApiException; +import com.google.api.gax.rpc.ApiExceptionFactory; +import com.google.api.gax.rpc.InvalidArgumentException; +import com.google.api.gax.rpc.StatusCode; +import com.google.api.gax.rpc.testing.FakeStatusCode; +import com.google.devtools.cloudprofiler.v2.stub.HttpJsonProfilerServiceStub; +import com.google.protobuf.ByteString; +import com.google.protobuf.Duration; +import com.google.protobuf.FieldMask; +import java.io.IOException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import javax.annotation.Generated; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +@Generated("by gapic-generator-java") +public class ProfilerServiceClientHttpJsonTest { + private static MockHttpService mockService; + private static ProfilerServiceClient client; + + @BeforeClass + public static void startStaticServer() throws IOException { + mockService = + new MockHttpService( + HttpJsonProfilerServiceStub.getMethodDescriptors(), + ProfilerServiceSettings.getDefaultEndpoint()); + ProfilerServiceSettings settings = + ProfilerServiceSettings.newHttpJsonBuilder() + .setTransportChannelProvider( + ProfilerServiceSettings.defaultHttpJsonTransportProviderBuilder() + .setHttpTransport(mockService) + .build()) + .setCredentialsProvider(NoCredentialsProvider.create()) + .build(); + client = ProfilerServiceClient.create(settings); + } + + @AfterClass + public static void stopServer() { + client.close(); + } + + @Before + public void setUp() {} + + @After + public void tearDown() throws Exception { + mockService.reset(); + } + + @Test + public void createProfileTest() throws Exception { + Profile expectedResponse = + Profile.newBuilder() + .setName("name3373707") + .setProfileType(ProfileType.forNumber(0)) + .setDeployment(Deployment.newBuilder().build()) + .setDuration(Duration.newBuilder().build()) + .setProfileBytes(ByteString.EMPTY) + .putAllLabels(new HashMap()) + .build(); + mockService.addResponse(expectedResponse); + + CreateProfileRequest request = + CreateProfileRequest.newBuilder() + .setParent("projects/project-2353") + .setDeployment(Deployment.newBuilder().build()) + .addAllProfileType(new ArrayList()) + .build(); + + Profile actualResponse = client.createProfile(request); + Assert.assertEquals(expectedResponse, actualResponse); + + List actualRequests = mockService.getRequestPaths(); + Assert.assertEquals(1, actualRequests.size()); + + String apiClientHeaderKey = + mockService + .getRequestHeaders() + .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) + .iterator() + .next(); + Assert.assertTrue( + GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() + .matcher(apiClientHeaderKey) + .matches()); + } + + @Test + public void createProfileExceptionTest() throws Exception { + ApiException exception = + ApiExceptionFactory.createException( + new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); + mockService.addException(exception); + + try { + CreateProfileRequest request = + CreateProfileRequest.newBuilder() + .setParent("projects/project-2353") + .setDeployment(Deployment.newBuilder().build()) + .addAllProfileType(new ArrayList()) + .build(); + client.createProfile(request); + Assert.fail("No exception raised"); + } catch (InvalidArgumentException e) { + // Expected exception. + } + } + + @Test + public void createOfflineProfileTest() throws Exception { + Profile expectedResponse = + Profile.newBuilder() + .setName("name3373707") + .setProfileType(ProfileType.forNumber(0)) + .setDeployment(Deployment.newBuilder().build()) + .setDuration(Duration.newBuilder().build()) + .setProfileBytes(ByteString.EMPTY) + .putAllLabels(new HashMap()) + .build(); + mockService.addResponse(expectedResponse); + + CreateOfflineProfileRequest request = + CreateOfflineProfileRequest.newBuilder() + .setParent("projects/project-2353") + .setProfile(Profile.newBuilder().build()) + .build(); + + Profile actualResponse = client.createOfflineProfile(request); + Assert.assertEquals(expectedResponse, actualResponse); + + List actualRequests = mockService.getRequestPaths(); + Assert.assertEquals(1, actualRequests.size()); + + String apiClientHeaderKey = + mockService + .getRequestHeaders() + .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) + .iterator() + .next(); + Assert.assertTrue( + GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() + .matcher(apiClientHeaderKey) + .matches()); + } + + @Test + public void createOfflineProfileExceptionTest() throws Exception { + ApiException exception = + ApiExceptionFactory.createException( + new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); + mockService.addException(exception); + + try { + CreateOfflineProfileRequest request = + CreateOfflineProfileRequest.newBuilder() + .setParent("projects/project-2353") + .setProfile(Profile.newBuilder().build()) + .build(); + client.createOfflineProfile(request); + Assert.fail("No exception raised"); + } catch (InvalidArgumentException e) { + // Expected exception. + } + } + + @Test + public void updateProfileTest() throws Exception { + Profile expectedResponse = + Profile.newBuilder() + .setName("name3373707") + .setProfileType(ProfileType.forNumber(0)) + .setDeployment(Deployment.newBuilder().build()) + .setDuration(Duration.newBuilder().build()) + .setProfileBytes(ByteString.EMPTY) + .putAllLabels(new HashMap()) + .build(); + mockService.addResponse(expectedResponse); + + UpdateProfileRequest request = + UpdateProfileRequest.newBuilder() + .setProfile( + Profile.newBuilder() + .setName("projects/project-4284/profiles/profile-4284") + .setProfileType(ProfileType.forNumber(0)) + .setDeployment(Deployment.newBuilder().build()) + .setDuration(Duration.newBuilder().build()) + .setProfileBytes(ByteString.EMPTY) + .putAllLabels(new HashMap()) + .build()) + .setUpdateMask(FieldMask.newBuilder().build()) + .build(); + + Profile actualResponse = client.updateProfile(request); + Assert.assertEquals(expectedResponse, actualResponse); + + List actualRequests = mockService.getRequestPaths(); + Assert.assertEquals(1, actualRequests.size()); + + String apiClientHeaderKey = + mockService + .getRequestHeaders() + .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) + .iterator() + .next(); + Assert.assertTrue( + GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() + .matcher(apiClientHeaderKey) + .matches()); + } + + @Test + public void updateProfileExceptionTest() throws Exception { + ApiException exception = + ApiExceptionFactory.createException( + new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); + mockService.addException(exception); + + try { + UpdateProfileRequest request = + UpdateProfileRequest.newBuilder() + .setProfile( + Profile.newBuilder() + .setName("projects/project-4284/profiles/profile-4284") + .setProfileType(ProfileType.forNumber(0)) + .setDeployment(Deployment.newBuilder().build()) + .setDuration(Duration.newBuilder().build()) + .setProfileBytes(ByteString.EMPTY) + .putAllLabels(new HashMap()) + .build()) + .setUpdateMask(FieldMask.newBuilder().build()) + .build(); + client.updateProfile(request); + Assert.fail("No exception raised"); + } catch (InvalidArgumentException e) { + // Expected exception. + } + } +}