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.HttpJsonController2Stub; 29 import com.google.devtools.clouddebugger.v2.Breakpoint; 30 import com.google.devtools.clouddebugger.v2.Debuggee; 31 import com.google.devtools.clouddebugger.v2.ListActiveBreakpointsResponse; 32 import com.google.devtools.clouddebugger.v2.RegisterDebuggeeResponse; 33 import com.google.devtools.clouddebugger.v2.SourceLocation; 34 import com.google.devtools.clouddebugger.v2.StackFrame; 35 import com.google.devtools.clouddebugger.v2.StatusMessage; 36 import com.google.devtools.clouddebugger.v2.UpdateActiveBreakpointResponse; 37 import com.google.devtools.clouddebugger.v2.Variable; 38 import com.google.protobuf.Timestamp; 39 import java.io.IOException; 40 import java.util.ArrayList; 41 import java.util.HashMap; 42 import java.util.List; 43 import javax.annotation.Generated; 44 import org.junit.After; 45 import org.junit.AfterClass; 46 import org.junit.Assert; 47 import org.junit.Before; 48 import org.junit.BeforeClass; 49 import org.junit.Test; 50 51 @Generated("by gapic-generator-java") 52 public class Controller2ClientHttpJsonTest { 53 private static MockHttpService mockService; 54 private static Controller2Client client; 55 56 @BeforeClass startStaticServer()57 public static void startStaticServer() throws IOException { 58 mockService = 59 new MockHttpService( 60 HttpJsonController2Stub.getMethodDescriptors(), 61 Controller2Settings.getDefaultEndpoint()); 62 Controller2Settings settings = 63 Controller2Settings.newHttpJsonBuilder() 64 .setTransportChannelProvider( 65 Controller2Settings.defaultHttpJsonTransportProviderBuilder() 66 .setHttpTransport(mockService) 67 .build()) 68 .setCredentialsProvider(NoCredentialsProvider.create()) 69 .build(); 70 client = Controller2Client.create(settings); 71 } 72 73 @AfterClass stopServer()74 public static void stopServer() { 75 client.close(); 76 } 77 78 @Before setUp()79 public void setUp() {} 80 81 @After tearDown()82 public void tearDown() throws Exception { 83 mockService.reset(); 84 } 85 86 @Test registerDebuggeeTest()87 public void registerDebuggeeTest() throws Exception { 88 RegisterDebuggeeResponse expectedResponse = 89 RegisterDebuggeeResponse.newBuilder().setDebuggee(Debuggee.newBuilder().build()).build(); 90 mockService.addResponse(expectedResponse); 91 92 Debuggee debuggee = Debuggee.newBuilder().build(); 93 94 RegisterDebuggeeResponse actualResponse = client.registerDebuggee(debuggee); 95 Assert.assertEquals(expectedResponse, actualResponse); 96 97 List<String> actualRequests = mockService.getRequestPaths(); 98 Assert.assertEquals(1, actualRequests.size()); 99 100 String apiClientHeaderKey = 101 mockService 102 .getRequestHeaders() 103 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 104 .iterator() 105 .next(); 106 Assert.assertTrue( 107 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 108 .matcher(apiClientHeaderKey) 109 .matches()); 110 } 111 112 @Test registerDebuggeeExceptionTest()113 public void registerDebuggeeExceptionTest() throws Exception { 114 ApiException exception = 115 ApiExceptionFactory.createException( 116 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 117 mockService.addException(exception); 118 119 try { 120 Debuggee debuggee = Debuggee.newBuilder().build(); 121 client.registerDebuggee(debuggee); 122 Assert.fail("No exception raised"); 123 } catch (InvalidArgumentException e) { 124 // Expected exception. 125 } 126 } 127 128 @Test listActiveBreakpointsTest()129 public void listActiveBreakpointsTest() throws Exception { 130 ListActiveBreakpointsResponse expectedResponse = 131 ListActiveBreakpointsResponse.newBuilder() 132 .addAllBreakpoints(new ArrayList<Breakpoint>()) 133 .setNextWaitToken("nextWaitToken1051070417") 134 .setWaitExpired(true) 135 .build(); 136 mockService.addResponse(expectedResponse); 137 138 String debuggeeId = "debuggeeId-7253"; 139 140 ListActiveBreakpointsResponse actualResponse = client.listActiveBreakpoints(debuggeeId); 141 Assert.assertEquals(expectedResponse, actualResponse); 142 143 List<String> actualRequests = mockService.getRequestPaths(); 144 Assert.assertEquals(1, actualRequests.size()); 145 146 String apiClientHeaderKey = 147 mockService 148 .getRequestHeaders() 149 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 150 .iterator() 151 .next(); 152 Assert.assertTrue( 153 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 154 .matcher(apiClientHeaderKey) 155 .matches()); 156 } 157 158 @Test listActiveBreakpointsExceptionTest()159 public void listActiveBreakpointsExceptionTest() throws Exception { 160 ApiException exception = 161 ApiExceptionFactory.createException( 162 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 163 mockService.addException(exception); 164 165 try { 166 String debuggeeId = "debuggeeId-7253"; 167 client.listActiveBreakpoints(debuggeeId); 168 Assert.fail("No exception raised"); 169 } catch (InvalidArgumentException e) { 170 // Expected exception. 171 } 172 } 173 174 @Test updateActiveBreakpointTest()175 public void updateActiveBreakpointTest() throws Exception { 176 UpdateActiveBreakpointResponse expectedResponse = 177 UpdateActiveBreakpointResponse.newBuilder().build(); 178 mockService.addResponse(expectedResponse); 179 180 String debuggeeId = "debuggeeId-7253"; 181 Breakpoint breakpoint = 182 Breakpoint.newBuilder() 183 .setId("id-4047") 184 .setLocation(SourceLocation.newBuilder().build()) 185 .setCondition("condition-861311717") 186 .addAllExpressions(new ArrayList<String>()) 187 .setLogMessageFormat("logMessageFormat-1136427526") 188 .setIsFinalState(true) 189 .setCreateTime(Timestamp.newBuilder().build()) 190 .setFinalTime(Timestamp.newBuilder().build()) 191 .setUserEmail("userEmail315299473") 192 .setStatus(StatusMessage.newBuilder().build()) 193 .addAllStackFrames(new ArrayList<StackFrame>()) 194 .addAllEvaluatedExpressions(new ArrayList<Variable>()) 195 .addAllVariableTable(new ArrayList<Variable>()) 196 .putAllLabels(new HashMap<String, String>()) 197 .build(); 198 199 UpdateActiveBreakpointResponse actualResponse = 200 client.updateActiveBreakpoint(debuggeeId, breakpoint); 201 Assert.assertEquals(expectedResponse, actualResponse); 202 203 List<String> actualRequests = mockService.getRequestPaths(); 204 Assert.assertEquals(1, actualRequests.size()); 205 206 String apiClientHeaderKey = 207 mockService 208 .getRequestHeaders() 209 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 210 .iterator() 211 .next(); 212 Assert.assertTrue( 213 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 214 .matcher(apiClientHeaderKey) 215 .matches()); 216 } 217 218 @Test updateActiveBreakpointExceptionTest()219 public void updateActiveBreakpointExceptionTest() throws Exception { 220 ApiException exception = 221 ApiExceptionFactory.createException( 222 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 223 mockService.addException(exception); 224 225 try { 226 String debuggeeId = "debuggeeId-7253"; 227 Breakpoint breakpoint = 228 Breakpoint.newBuilder() 229 .setId("id-4047") 230 .setLocation(SourceLocation.newBuilder().build()) 231 .setCondition("condition-861311717") 232 .addAllExpressions(new ArrayList<String>()) 233 .setLogMessageFormat("logMessageFormat-1136427526") 234 .setIsFinalState(true) 235 .setCreateTime(Timestamp.newBuilder().build()) 236 .setFinalTime(Timestamp.newBuilder().build()) 237 .setUserEmail("userEmail315299473") 238 .setStatus(StatusMessage.newBuilder().build()) 239 .addAllStackFrames(new ArrayList<StackFrame>()) 240 .addAllEvaluatedExpressions(new ArrayList<Variable>()) 241 .addAllVariableTable(new ArrayList<Variable>()) 242 .putAllLabels(new HashMap<String, String>()) 243 .build(); 244 client.updateActiveBreakpoint(debuggeeId, breakpoint); 245 Assert.fail("No exception raised"); 246 } catch (InvalidArgumentException e) { 247 // Expected exception. 248 } 249 } 250 } 251