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.ListActiveBreakpointsRequest; 29 import com.google.devtools.clouddebugger.v2.ListActiveBreakpointsResponse; 30 import com.google.devtools.clouddebugger.v2.RegisterDebuggeeRequest; 31 import com.google.devtools.clouddebugger.v2.RegisterDebuggeeResponse; 32 import com.google.devtools.clouddebugger.v2.UpdateActiveBreakpointRequest; 33 import com.google.devtools.clouddebugger.v2.UpdateActiveBreakpointResponse; 34 import com.google.protobuf.AbstractMessage; 35 import io.grpc.StatusRuntimeException; 36 import java.io.IOException; 37 import java.util.ArrayList; 38 import java.util.Arrays; 39 import java.util.List; 40 import java.util.UUID; 41 import javax.annotation.Generated; 42 import org.junit.After; 43 import org.junit.AfterClass; 44 import org.junit.Assert; 45 import org.junit.Before; 46 import org.junit.BeforeClass; 47 import org.junit.Test; 48 49 @Generated("by gapic-generator-java") 50 public class Controller2ClientTest { 51 private static MockController2 mockController2; 52 private static MockServiceHelper mockServiceHelper; 53 private LocalChannelProvider channelProvider; 54 private Controller2Client client; 55 56 @BeforeClass startStaticServer()57 public static void startStaticServer() { 58 mockController2 = new MockController2(); 59 mockServiceHelper = 60 new MockServiceHelper( 61 UUID.randomUUID().toString(), Arrays.<MockGrpcService>asList(mockController2)); 62 mockServiceHelper.start(); 63 } 64 65 @AfterClass stopServer()66 public static void stopServer() { 67 mockServiceHelper.stop(); 68 } 69 70 @Before setUp()71 public void setUp() throws IOException { 72 mockServiceHelper.reset(); 73 channelProvider = mockServiceHelper.createChannelProvider(); 74 Controller2Settings settings = 75 Controller2Settings.newBuilder() 76 .setTransportChannelProvider(channelProvider) 77 .setCredentialsProvider(NoCredentialsProvider.create()) 78 .build(); 79 client = Controller2Client.create(settings); 80 } 81 82 @After tearDown()83 public void tearDown() throws Exception { 84 client.close(); 85 } 86 87 @Test registerDebuggeeTest()88 public void registerDebuggeeTest() throws Exception { 89 RegisterDebuggeeResponse expectedResponse = 90 RegisterDebuggeeResponse.newBuilder().setDebuggee(Debuggee.newBuilder().build()).build(); 91 mockController2.addResponse(expectedResponse); 92 93 Debuggee debuggee = Debuggee.newBuilder().build(); 94 95 RegisterDebuggeeResponse actualResponse = client.registerDebuggee(debuggee); 96 Assert.assertEquals(expectedResponse, actualResponse); 97 98 List<AbstractMessage> actualRequests = mockController2.getRequests(); 99 Assert.assertEquals(1, actualRequests.size()); 100 RegisterDebuggeeRequest actualRequest = ((RegisterDebuggeeRequest) actualRequests.get(0)); 101 102 Assert.assertEquals(debuggee, actualRequest.getDebuggee()); 103 Assert.assertTrue( 104 channelProvider.isHeaderSent( 105 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 106 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 107 } 108 109 @Test registerDebuggeeExceptionTest()110 public void registerDebuggeeExceptionTest() throws Exception { 111 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 112 mockController2.addException(exception); 113 114 try { 115 Debuggee debuggee = Debuggee.newBuilder().build(); 116 client.registerDebuggee(debuggee); 117 Assert.fail("No exception raised"); 118 } catch (InvalidArgumentException e) { 119 // Expected exception. 120 } 121 } 122 123 @Test listActiveBreakpointsTest()124 public void listActiveBreakpointsTest() throws Exception { 125 ListActiveBreakpointsResponse expectedResponse = 126 ListActiveBreakpointsResponse.newBuilder() 127 .addAllBreakpoints(new ArrayList<Breakpoint>()) 128 .setNextWaitToken("nextWaitToken1051070417") 129 .setWaitExpired(true) 130 .build(); 131 mockController2.addResponse(expectedResponse); 132 133 String debuggeeId = "debuggeeId-1833285553"; 134 135 ListActiveBreakpointsResponse actualResponse = client.listActiveBreakpoints(debuggeeId); 136 Assert.assertEquals(expectedResponse, actualResponse); 137 138 List<AbstractMessage> actualRequests = mockController2.getRequests(); 139 Assert.assertEquals(1, actualRequests.size()); 140 ListActiveBreakpointsRequest actualRequest = 141 ((ListActiveBreakpointsRequest) actualRequests.get(0)); 142 143 Assert.assertEquals(debuggeeId, actualRequest.getDebuggeeId()); 144 Assert.assertTrue( 145 channelProvider.isHeaderSent( 146 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 147 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 148 } 149 150 @Test listActiveBreakpointsExceptionTest()151 public void listActiveBreakpointsExceptionTest() throws Exception { 152 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 153 mockController2.addException(exception); 154 155 try { 156 String debuggeeId = "debuggeeId-1833285553"; 157 client.listActiveBreakpoints(debuggeeId); 158 Assert.fail("No exception raised"); 159 } catch (InvalidArgumentException e) { 160 // Expected exception. 161 } 162 } 163 164 @Test updateActiveBreakpointTest()165 public void updateActiveBreakpointTest() throws Exception { 166 UpdateActiveBreakpointResponse expectedResponse = 167 UpdateActiveBreakpointResponse.newBuilder().build(); 168 mockController2.addResponse(expectedResponse); 169 170 String debuggeeId = "debuggeeId-1833285553"; 171 Breakpoint breakpoint = Breakpoint.newBuilder().build(); 172 173 UpdateActiveBreakpointResponse actualResponse = 174 client.updateActiveBreakpoint(debuggeeId, breakpoint); 175 Assert.assertEquals(expectedResponse, actualResponse); 176 177 List<AbstractMessage> actualRequests = mockController2.getRequests(); 178 Assert.assertEquals(1, actualRequests.size()); 179 UpdateActiveBreakpointRequest actualRequest = 180 ((UpdateActiveBreakpointRequest) actualRequests.get(0)); 181 182 Assert.assertEquals(debuggeeId, actualRequest.getDebuggeeId()); 183 Assert.assertEquals(breakpoint, actualRequest.getBreakpoint()); 184 Assert.assertTrue( 185 channelProvider.isHeaderSent( 186 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 187 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 188 } 189 190 @Test updateActiveBreakpointExceptionTest()191 public void updateActiveBreakpointExceptionTest() throws Exception { 192 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 193 mockController2.addException(exception); 194 195 try { 196 String debuggeeId = "debuggeeId-1833285553"; 197 Breakpoint breakpoint = Breakpoint.newBuilder().build(); 198 client.updateActiveBreakpoint(debuggeeId, breakpoint); 199 Assert.fail("No exception raised"); 200 } catch (InvalidArgumentException e) { 201 // Expected exception. 202 } 203 } 204 } 205