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