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.cloud.debugger.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.devtools.clouddebugger.v2.Breakpoint; 27 import com.google.devtools.clouddebugger.v2.Debuggee; 28 import com.google.devtools.clouddebugger.v2.DeleteBreakpointRequest; 29 import com.google.devtools.clouddebugger.v2.GetBreakpointRequest; 30 import com.google.devtools.clouddebugger.v2.GetBreakpointResponse; 31 import com.google.devtools.clouddebugger.v2.ListBreakpointsRequest; 32 import com.google.devtools.clouddebugger.v2.ListBreakpointsResponse; 33 import com.google.devtools.clouddebugger.v2.ListDebuggeesRequest; 34 import com.google.devtools.clouddebugger.v2.ListDebuggeesResponse; 35 import com.google.devtools.clouddebugger.v2.SetBreakpointRequest; 36 import com.google.devtools.clouddebugger.v2.SetBreakpointResponse; 37 import com.google.protobuf.AbstractMessage; 38 import com.google.protobuf.Empty; 39 import io.grpc.StatusRuntimeException; 40 import java.io.IOException; 41 import java.util.ArrayList; 42 import java.util.Arrays; 43 import java.util.List; 44 import java.util.UUID; 45 import javax.annotation.Generated; 46 import org.junit.After; 47 import org.junit.AfterClass; 48 import org.junit.Assert; 49 import org.junit.Before; 50 import org.junit.BeforeClass; 51 import org.junit.Test; 52 53 @Generated("by gapic-generator-java") 54 public class Debugger2ClientTest { 55 private static MockDebugger2 mockDebugger2; 56 private static MockServiceHelper mockServiceHelper; 57 private LocalChannelProvider channelProvider; 58 private Debugger2Client client; 59 60 @BeforeClass startStaticServer()61 public static void startStaticServer() { 62 mockDebugger2 = new MockDebugger2(); 63 mockServiceHelper = 64 new MockServiceHelper( 65 UUID.randomUUID().toString(), Arrays.<MockGrpcService>asList(mockDebugger2)); 66 mockServiceHelper.start(); 67 } 68 69 @AfterClass stopServer()70 public static void stopServer() { 71 mockServiceHelper.stop(); 72 } 73 74 @Before setUp()75 public void setUp() throws IOException { 76 mockServiceHelper.reset(); 77 channelProvider = mockServiceHelper.createChannelProvider(); 78 Debugger2Settings settings = 79 Debugger2Settings.newBuilder() 80 .setTransportChannelProvider(channelProvider) 81 .setCredentialsProvider(NoCredentialsProvider.create()) 82 .build(); 83 client = Debugger2Client.create(settings); 84 } 85 86 @After tearDown()87 public void tearDown() throws Exception { 88 client.close(); 89 } 90 91 @Test setBreakpointTest()92 public void setBreakpointTest() throws Exception { 93 SetBreakpointResponse expectedResponse = 94 SetBreakpointResponse.newBuilder().setBreakpoint(Breakpoint.newBuilder().build()).build(); 95 mockDebugger2.addResponse(expectedResponse); 96 97 String debuggeeId = "debuggeeId-1833285553"; 98 Breakpoint breakpoint = Breakpoint.newBuilder().build(); 99 String clientVersion = "clientVersion771880589"; 100 101 SetBreakpointResponse actualResponse = 102 client.setBreakpoint(debuggeeId, breakpoint, clientVersion); 103 Assert.assertEquals(expectedResponse, actualResponse); 104 105 List<AbstractMessage> actualRequests = mockDebugger2.getRequests(); 106 Assert.assertEquals(1, actualRequests.size()); 107 SetBreakpointRequest actualRequest = ((SetBreakpointRequest) actualRequests.get(0)); 108 109 Assert.assertEquals(debuggeeId, actualRequest.getDebuggeeId()); 110 Assert.assertEquals(breakpoint, actualRequest.getBreakpoint()); 111 Assert.assertEquals(clientVersion, actualRequest.getClientVersion()); 112 Assert.assertTrue( 113 channelProvider.isHeaderSent( 114 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 115 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 116 } 117 118 @Test setBreakpointExceptionTest()119 public void setBreakpointExceptionTest() throws Exception { 120 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 121 mockDebugger2.addException(exception); 122 123 try { 124 String debuggeeId = "debuggeeId-1833285553"; 125 Breakpoint breakpoint = Breakpoint.newBuilder().build(); 126 String clientVersion = "clientVersion771880589"; 127 client.setBreakpoint(debuggeeId, breakpoint, clientVersion); 128 Assert.fail("No exception raised"); 129 } catch (InvalidArgumentException e) { 130 // Expected exception. 131 } 132 } 133 134 @Test getBreakpointTest()135 public void getBreakpointTest() throws Exception { 136 GetBreakpointResponse expectedResponse = 137 GetBreakpointResponse.newBuilder().setBreakpoint(Breakpoint.newBuilder().build()).build(); 138 mockDebugger2.addResponse(expectedResponse); 139 140 String debuggeeId = "debuggeeId-1833285553"; 141 String breakpointId = "breakpointId570266860"; 142 String clientVersion = "clientVersion771880589"; 143 144 GetBreakpointResponse actualResponse = 145 client.getBreakpoint(debuggeeId, breakpointId, clientVersion); 146 Assert.assertEquals(expectedResponse, actualResponse); 147 148 List<AbstractMessage> actualRequests = mockDebugger2.getRequests(); 149 Assert.assertEquals(1, actualRequests.size()); 150 GetBreakpointRequest actualRequest = ((GetBreakpointRequest) actualRequests.get(0)); 151 152 Assert.assertEquals(debuggeeId, actualRequest.getDebuggeeId()); 153 Assert.assertEquals(breakpointId, actualRequest.getBreakpointId()); 154 Assert.assertEquals(clientVersion, actualRequest.getClientVersion()); 155 Assert.assertTrue( 156 channelProvider.isHeaderSent( 157 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 158 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 159 } 160 161 @Test getBreakpointExceptionTest()162 public void getBreakpointExceptionTest() throws Exception { 163 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 164 mockDebugger2.addException(exception); 165 166 try { 167 String debuggeeId = "debuggeeId-1833285553"; 168 String breakpointId = "breakpointId570266860"; 169 String clientVersion = "clientVersion771880589"; 170 client.getBreakpoint(debuggeeId, breakpointId, clientVersion); 171 Assert.fail("No exception raised"); 172 } catch (InvalidArgumentException e) { 173 // Expected exception. 174 } 175 } 176 177 @Test deleteBreakpointTest()178 public void deleteBreakpointTest() throws Exception { 179 Empty expectedResponse = Empty.newBuilder().build(); 180 mockDebugger2.addResponse(expectedResponse); 181 182 String debuggeeId = "debuggeeId-1833285553"; 183 String breakpointId = "breakpointId570266860"; 184 String clientVersion = "clientVersion771880589"; 185 186 client.deleteBreakpoint(debuggeeId, breakpointId, clientVersion); 187 188 List<AbstractMessage> actualRequests = mockDebugger2.getRequests(); 189 Assert.assertEquals(1, actualRequests.size()); 190 DeleteBreakpointRequest actualRequest = ((DeleteBreakpointRequest) actualRequests.get(0)); 191 192 Assert.assertEquals(debuggeeId, actualRequest.getDebuggeeId()); 193 Assert.assertEquals(breakpointId, actualRequest.getBreakpointId()); 194 Assert.assertEquals(clientVersion, actualRequest.getClientVersion()); 195 Assert.assertTrue( 196 channelProvider.isHeaderSent( 197 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 198 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 199 } 200 201 @Test deleteBreakpointExceptionTest()202 public void deleteBreakpointExceptionTest() throws Exception { 203 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 204 mockDebugger2.addException(exception); 205 206 try { 207 String debuggeeId = "debuggeeId-1833285553"; 208 String breakpointId = "breakpointId570266860"; 209 String clientVersion = "clientVersion771880589"; 210 client.deleteBreakpoint(debuggeeId, breakpointId, clientVersion); 211 Assert.fail("No exception raised"); 212 } catch (InvalidArgumentException e) { 213 // Expected exception. 214 } 215 } 216 217 @Test listBreakpointsTest()218 public void listBreakpointsTest() throws Exception { 219 ListBreakpointsResponse expectedResponse = 220 ListBreakpointsResponse.newBuilder() 221 .addAllBreakpoints(new ArrayList<Breakpoint>()) 222 .setNextWaitToken("nextWaitToken1051070417") 223 .build(); 224 mockDebugger2.addResponse(expectedResponse); 225 226 String debuggeeId = "debuggeeId-1833285553"; 227 String clientVersion = "clientVersion771880589"; 228 229 ListBreakpointsResponse actualResponse = client.listBreakpoints(debuggeeId, clientVersion); 230 Assert.assertEquals(expectedResponse, actualResponse); 231 232 List<AbstractMessage> actualRequests = mockDebugger2.getRequests(); 233 Assert.assertEquals(1, actualRequests.size()); 234 ListBreakpointsRequest actualRequest = ((ListBreakpointsRequest) actualRequests.get(0)); 235 236 Assert.assertEquals(debuggeeId, actualRequest.getDebuggeeId()); 237 Assert.assertEquals(clientVersion, actualRequest.getClientVersion()); 238 Assert.assertTrue( 239 channelProvider.isHeaderSent( 240 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 241 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 242 } 243 244 @Test listBreakpointsExceptionTest()245 public void listBreakpointsExceptionTest() throws Exception { 246 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 247 mockDebugger2.addException(exception); 248 249 try { 250 String debuggeeId = "debuggeeId-1833285553"; 251 String clientVersion = "clientVersion771880589"; 252 client.listBreakpoints(debuggeeId, clientVersion); 253 Assert.fail("No exception raised"); 254 } catch (InvalidArgumentException e) { 255 // Expected exception. 256 } 257 } 258 259 @Test listDebuggeesTest()260 public void listDebuggeesTest() throws Exception { 261 ListDebuggeesResponse expectedResponse = 262 ListDebuggeesResponse.newBuilder().addAllDebuggees(new ArrayList<Debuggee>()).build(); 263 mockDebugger2.addResponse(expectedResponse); 264 265 String project = "project-309310695"; 266 String clientVersion = "clientVersion771880589"; 267 268 ListDebuggeesResponse actualResponse = client.listDebuggees(project, clientVersion); 269 Assert.assertEquals(expectedResponse, actualResponse); 270 271 List<AbstractMessage> actualRequests = mockDebugger2.getRequests(); 272 Assert.assertEquals(1, actualRequests.size()); 273 ListDebuggeesRequest actualRequest = ((ListDebuggeesRequest) actualRequests.get(0)); 274 275 Assert.assertEquals(project, actualRequest.getProject()); 276 Assert.assertEquals(clientVersion, actualRequest.getClientVersion()); 277 Assert.assertTrue( 278 channelProvider.isHeaderSent( 279 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 280 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 281 } 282 283 @Test listDebuggeesExceptionTest()284 public void listDebuggeesExceptionTest() throws Exception { 285 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 286 mockDebugger2.addException(exception); 287 288 try { 289 String project = "project-309310695"; 290 String clientVersion = "clientVersion771880589"; 291 client.listDebuggees(project, clientVersion); 292 Assert.fail("No exception raised"); 293 } catch (InvalidArgumentException e) { 294 // Expected exception. 295 } 296 } 297 } 298