1 /* 2 * Copyright 2022 Google LLC 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * https://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.google.devtools.cloudprofiler.v2; 18 19 import com.google.api.gax.core.NoCredentialsProvider; 20 import com.google.api.gax.grpc.GaxGrpcProperties; 21 import com.google.api.gax.grpc.testing.LocalChannelProvider; 22 import com.google.api.gax.grpc.testing.MockGrpcService; 23 import com.google.api.gax.grpc.testing.MockServiceHelper; 24 import com.google.api.gax.rpc.ApiClientHeaderProvider; 25 import com.google.api.gax.rpc.InvalidArgumentException; 26 import com.google.protobuf.AbstractMessage; 27 import com.google.protobuf.ByteString; 28 import com.google.protobuf.Duration; 29 import com.google.protobuf.FieldMask; 30 import io.grpc.StatusRuntimeException; 31 import java.io.IOException; 32 import java.util.ArrayList; 33 import java.util.Arrays; 34 import java.util.HashMap; 35 import java.util.List; 36 import java.util.UUID; 37 import javax.annotation.Generated; 38 import org.junit.After; 39 import org.junit.AfterClass; 40 import org.junit.Assert; 41 import org.junit.Before; 42 import org.junit.BeforeClass; 43 import org.junit.Test; 44 45 @Generated("by gapic-generator-java") 46 public class ProfilerServiceClientTest { 47 private static MockProfilerService mockProfilerService; 48 private static MockServiceHelper mockServiceHelper; 49 private LocalChannelProvider channelProvider; 50 private ProfilerServiceClient client; 51 52 @BeforeClass startStaticServer()53 public static void startStaticServer() { 54 mockProfilerService = new MockProfilerService(); 55 mockServiceHelper = 56 new MockServiceHelper( 57 UUID.randomUUID().toString(), Arrays.<MockGrpcService>asList(mockProfilerService)); 58 mockServiceHelper.start(); 59 } 60 61 @AfterClass stopServer()62 public static void stopServer() { 63 mockServiceHelper.stop(); 64 } 65 66 @Before setUp()67 public void setUp() throws IOException { 68 mockServiceHelper.reset(); 69 channelProvider = mockServiceHelper.createChannelProvider(); 70 ProfilerServiceSettings settings = 71 ProfilerServiceSettings.newBuilder() 72 .setTransportChannelProvider(channelProvider) 73 .setCredentialsProvider(NoCredentialsProvider.create()) 74 .build(); 75 client = ProfilerServiceClient.create(settings); 76 } 77 78 @After tearDown()79 public void tearDown() throws Exception { 80 client.close(); 81 } 82 83 @Test createProfileTest()84 public void createProfileTest() throws Exception { 85 Profile expectedResponse = 86 Profile.newBuilder() 87 .setName("name3373707") 88 .setProfileType(ProfileType.forNumber(0)) 89 .setDeployment(Deployment.newBuilder().build()) 90 .setDuration(Duration.newBuilder().build()) 91 .setProfileBytes(ByteString.EMPTY) 92 .putAllLabels(new HashMap<String, String>()) 93 .build(); 94 mockProfilerService.addResponse(expectedResponse); 95 96 CreateProfileRequest request = 97 CreateProfileRequest.newBuilder() 98 .setParent(ProjectName.of("[PROJECT]").toString()) 99 .setDeployment(Deployment.newBuilder().build()) 100 .addAllProfileType(new ArrayList<ProfileType>()) 101 .build(); 102 103 Profile actualResponse = client.createProfile(request); 104 Assert.assertEquals(expectedResponse, actualResponse); 105 106 List<AbstractMessage> actualRequests = mockProfilerService.getRequests(); 107 Assert.assertEquals(1, actualRequests.size()); 108 CreateProfileRequest actualRequest = ((CreateProfileRequest) actualRequests.get(0)); 109 110 Assert.assertEquals(request.getParent(), actualRequest.getParent()); 111 Assert.assertEquals(request.getDeployment(), actualRequest.getDeployment()); 112 Assert.assertEquals(request.getProfileTypeList(), actualRequest.getProfileTypeList()); 113 Assert.assertTrue( 114 channelProvider.isHeaderSent( 115 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 116 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 117 } 118 119 @Test createProfileExceptionTest()120 public void createProfileExceptionTest() throws Exception { 121 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 122 mockProfilerService.addException(exception); 123 124 try { 125 CreateProfileRequest request = 126 CreateProfileRequest.newBuilder() 127 .setParent(ProjectName.of("[PROJECT]").toString()) 128 .setDeployment(Deployment.newBuilder().build()) 129 .addAllProfileType(new ArrayList<ProfileType>()) 130 .build(); 131 client.createProfile(request); 132 Assert.fail("No exception raised"); 133 } catch (InvalidArgumentException e) { 134 // Expected exception. 135 } 136 } 137 138 @Test createOfflineProfileTest()139 public void createOfflineProfileTest() throws Exception { 140 Profile expectedResponse = 141 Profile.newBuilder() 142 .setName("name3373707") 143 .setProfileType(ProfileType.forNumber(0)) 144 .setDeployment(Deployment.newBuilder().build()) 145 .setDuration(Duration.newBuilder().build()) 146 .setProfileBytes(ByteString.EMPTY) 147 .putAllLabels(new HashMap<String, String>()) 148 .build(); 149 mockProfilerService.addResponse(expectedResponse); 150 151 ProjectName parent = ProjectName.of("[PROJECT]"); 152 Profile profile = Profile.newBuilder().build(); 153 154 Profile actualResponse = client.createOfflineProfile(parent, profile); 155 Assert.assertEquals(expectedResponse, actualResponse); 156 157 List<AbstractMessage> actualRequests = mockProfilerService.getRequests(); 158 Assert.assertEquals(1, actualRequests.size()); 159 CreateOfflineProfileRequest actualRequest = 160 ((CreateOfflineProfileRequest) actualRequests.get(0)); 161 162 Assert.assertEquals(parent.toString(), actualRequest.getParent()); 163 Assert.assertEquals(profile, actualRequest.getProfile()); 164 Assert.assertTrue( 165 channelProvider.isHeaderSent( 166 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 167 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 168 } 169 170 @Test createOfflineProfileExceptionTest()171 public void createOfflineProfileExceptionTest() throws Exception { 172 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 173 mockProfilerService.addException(exception); 174 175 try { 176 ProjectName parent = ProjectName.of("[PROJECT]"); 177 Profile profile = Profile.newBuilder().build(); 178 client.createOfflineProfile(parent, profile); 179 Assert.fail("No exception raised"); 180 } catch (InvalidArgumentException e) { 181 // Expected exception. 182 } 183 } 184 185 @Test createOfflineProfileTest2()186 public void createOfflineProfileTest2() throws Exception { 187 Profile expectedResponse = 188 Profile.newBuilder() 189 .setName("name3373707") 190 .setProfileType(ProfileType.forNumber(0)) 191 .setDeployment(Deployment.newBuilder().build()) 192 .setDuration(Duration.newBuilder().build()) 193 .setProfileBytes(ByteString.EMPTY) 194 .putAllLabels(new HashMap<String, String>()) 195 .build(); 196 mockProfilerService.addResponse(expectedResponse); 197 198 String parent = "parent-995424086"; 199 Profile profile = Profile.newBuilder().build(); 200 201 Profile actualResponse = client.createOfflineProfile(parent, profile); 202 Assert.assertEquals(expectedResponse, actualResponse); 203 204 List<AbstractMessage> actualRequests = mockProfilerService.getRequests(); 205 Assert.assertEquals(1, actualRequests.size()); 206 CreateOfflineProfileRequest actualRequest = 207 ((CreateOfflineProfileRequest) actualRequests.get(0)); 208 209 Assert.assertEquals(parent, actualRequest.getParent()); 210 Assert.assertEquals(profile, actualRequest.getProfile()); 211 Assert.assertTrue( 212 channelProvider.isHeaderSent( 213 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 214 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 215 } 216 217 @Test createOfflineProfileExceptionTest2()218 public void createOfflineProfileExceptionTest2() throws Exception { 219 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 220 mockProfilerService.addException(exception); 221 222 try { 223 String parent = "parent-995424086"; 224 Profile profile = Profile.newBuilder().build(); 225 client.createOfflineProfile(parent, profile); 226 Assert.fail("No exception raised"); 227 } catch (InvalidArgumentException e) { 228 // Expected exception. 229 } 230 } 231 232 @Test updateProfileTest()233 public void updateProfileTest() throws Exception { 234 Profile expectedResponse = 235 Profile.newBuilder() 236 .setName("name3373707") 237 .setProfileType(ProfileType.forNumber(0)) 238 .setDeployment(Deployment.newBuilder().build()) 239 .setDuration(Duration.newBuilder().build()) 240 .setProfileBytes(ByteString.EMPTY) 241 .putAllLabels(new HashMap<String, String>()) 242 .build(); 243 mockProfilerService.addResponse(expectedResponse); 244 245 Profile profile = Profile.newBuilder().build(); 246 FieldMask updateMask = FieldMask.newBuilder().build(); 247 248 Profile actualResponse = client.updateProfile(profile, updateMask); 249 Assert.assertEquals(expectedResponse, actualResponse); 250 251 List<AbstractMessage> actualRequests = mockProfilerService.getRequests(); 252 Assert.assertEquals(1, actualRequests.size()); 253 UpdateProfileRequest actualRequest = ((UpdateProfileRequest) actualRequests.get(0)); 254 255 Assert.assertEquals(profile, actualRequest.getProfile()); 256 Assert.assertEquals(updateMask, actualRequest.getUpdateMask()); 257 Assert.assertTrue( 258 channelProvider.isHeaderSent( 259 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 260 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 261 } 262 263 @Test updateProfileExceptionTest()264 public void updateProfileExceptionTest() throws Exception { 265 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 266 mockProfilerService.addException(exception); 267 268 try { 269 Profile profile = Profile.newBuilder().build(); 270 FieldMask updateMask = FieldMask.newBuilder().build(); 271 client.updateProfile(profile, updateMask); 272 Assert.fail("No exception raised"); 273 } catch (InvalidArgumentException e) { 274 // Expected exception. 275 } 276 } 277 } 278