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.dataproc.v1; 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.api.gax.rpc.StatusCode; 27 import com.google.longrunning.Operation; 28 import com.google.protobuf.AbstractMessage; 29 import com.google.protobuf.Any; 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 java.util.concurrent.ExecutionException; 38 import javax.annotation.Generated; 39 import org.junit.After; 40 import org.junit.AfterClass; 41 import org.junit.Assert; 42 import org.junit.Before; 43 import org.junit.BeforeClass; 44 import org.junit.Test; 45 46 @Generated("by gapic-generator-java") 47 public class NodeGroupControllerClientTest { 48 private static MockNodeGroupController mockNodeGroupController; 49 private static MockServiceHelper mockServiceHelper; 50 private LocalChannelProvider channelProvider; 51 private NodeGroupControllerClient client; 52 53 @BeforeClass startStaticServer()54 public static void startStaticServer() { 55 mockNodeGroupController = new MockNodeGroupController(); 56 mockServiceHelper = 57 new MockServiceHelper( 58 UUID.randomUUID().toString(), Arrays.<MockGrpcService>asList(mockNodeGroupController)); 59 mockServiceHelper.start(); 60 } 61 62 @AfterClass stopServer()63 public static void stopServer() { 64 mockServiceHelper.stop(); 65 } 66 67 @Before setUp()68 public void setUp() throws IOException { 69 mockServiceHelper.reset(); 70 channelProvider = mockServiceHelper.createChannelProvider(); 71 NodeGroupControllerSettings settings = 72 NodeGroupControllerSettings.newBuilder() 73 .setTransportChannelProvider(channelProvider) 74 .setCredentialsProvider(NoCredentialsProvider.create()) 75 .build(); 76 client = NodeGroupControllerClient.create(settings); 77 } 78 79 @After tearDown()80 public void tearDown() throws Exception { 81 client.close(); 82 } 83 84 @Test createNodeGroupTest()85 public void createNodeGroupTest() throws Exception { 86 NodeGroup expectedResponse = 87 NodeGroup.newBuilder() 88 .setName( 89 NodeGroupName.of("[PROJECT]", "[REGION]", "[CLUSTER]", "[NODE_GROUP]").toString()) 90 .addAllRoles(new ArrayList<NodeGroup.Role>()) 91 .setNodeGroupConfig(InstanceGroupConfig.newBuilder().build()) 92 .putAllLabels(new HashMap<String, String>()) 93 .build(); 94 Operation resultOperation = 95 Operation.newBuilder() 96 .setName("createNodeGroupTest") 97 .setDone(true) 98 .setResponse(Any.pack(expectedResponse)) 99 .build(); 100 mockNodeGroupController.addResponse(resultOperation); 101 102 ClusterRegionName parent = ClusterRegionName.of("[PROJECT]", "[REGION]", "[CLUSTER]"); 103 NodeGroup nodeGroup = NodeGroup.newBuilder().build(); 104 String nodeGroupId = "nodeGroupId1097129880"; 105 106 NodeGroup actualResponse = client.createNodeGroupAsync(parent, nodeGroup, nodeGroupId).get(); 107 Assert.assertEquals(expectedResponse, actualResponse); 108 109 List<AbstractMessage> actualRequests = mockNodeGroupController.getRequests(); 110 Assert.assertEquals(1, actualRequests.size()); 111 CreateNodeGroupRequest actualRequest = ((CreateNodeGroupRequest) actualRequests.get(0)); 112 113 Assert.assertEquals(parent.toString(), actualRequest.getParent()); 114 Assert.assertEquals(nodeGroup, actualRequest.getNodeGroup()); 115 Assert.assertEquals(nodeGroupId, actualRequest.getNodeGroupId()); 116 Assert.assertTrue( 117 channelProvider.isHeaderSent( 118 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 119 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 120 } 121 122 @Test createNodeGroupExceptionTest()123 public void createNodeGroupExceptionTest() throws Exception { 124 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 125 mockNodeGroupController.addException(exception); 126 127 try { 128 ClusterRegionName parent = ClusterRegionName.of("[PROJECT]", "[REGION]", "[CLUSTER]"); 129 NodeGroup nodeGroup = NodeGroup.newBuilder().build(); 130 String nodeGroupId = "nodeGroupId1097129880"; 131 client.createNodeGroupAsync(parent, nodeGroup, nodeGroupId).get(); 132 Assert.fail("No exception raised"); 133 } catch (ExecutionException e) { 134 Assert.assertEquals(InvalidArgumentException.class, e.getCause().getClass()); 135 InvalidArgumentException apiException = ((InvalidArgumentException) e.getCause()); 136 Assert.assertEquals(StatusCode.Code.INVALID_ARGUMENT, apiException.getStatusCode().getCode()); 137 } 138 } 139 140 @Test createNodeGroupTest2()141 public void createNodeGroupTest2() throws Exception { 142 NodeGroup expectedResponse = 143 NodeGroup.newBuilder() 144 .setName( 145 NodeGroupName.of("[PROJECT]", "[REGION]", "[CLUSTER]", "[NODE_GROUP]").toString()) 146 .addAllRoles(new ArrayList<NodeGroup.Role>()) 147 .setNodeGroupConfig(InstanceGroupConfig.newBuilder().build()) 148 .putAllLabels(new HashMap<String, String>()) 149 .build(); 150 Operation resultOperation = 151 Operation.newBuilder() 152 .setName("createNodeGroupTest") 153 .setDone(true) 154 .setResponse(Any.pack(expectedResponse)) 155 .build(); 156 mockNodeGroupController.addResponse(resultOperation); 157 158 String parent = "parent-995424086"; 159 NodeGroup nodeGroup = NodeGroup.newBuilder().build(); 160 String nodeGroupId = "nodeGroupId1097129880"; 161 162 NodeGroup actualResponse = client.createNodeGroupAsync(parent, nodeGroup, nodeGroupId).get(); 163 Assert.assertEquals(expectedResponse, actualResponse); 164 165 List<AbstractMessage> actualRequests = mockNodeGroupController.getRequests(); 166 Assert.assertEquals(1, actualRequests.size()); 167 CreateNodeGroupRequest actualRequest = ((CreateNodeGroupRequest) actualRequests.get(0)); 168 169 Assert.assertEquals(parent, actualRequest.getParent()); 170 Assert.assertEquals(nodeGroup, actualRequest.getNodeGroup()); 171 Assert.assertEquals(nodeGroupId, actualRequest.getNodeGroupId()); 172 Assert.assertTrue( 173 channelProvider.isHeaderSent( 174 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 175 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 176 } 177 178 @Test createNodeGroupExceptionTest2()179 public void createNodeGroupExceptionTest2() throws Exception { 180 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 181 mockNodeGroupController.addException(exception); 182 183 try { 184 String parent = "parent-995424086"; 185 NodeGroup nodeGroup = NodeGroup.newBuilder().build(); 186 String nodeGroupId = "nodeGroupId1097129880"; 187 client.createNodeGroupAsync(parent, nodeGroup, nodeGroupId).get(); 188 Assert.fail("No exception raised"); 189 } catch (ExecutionException e) { 190 Assert.assertEquals(InvalidArgumentException.class, e.getCause().getClass()); 191 InvalidArgumentException apiException = ((InvalidArgumentException) e.getCause()); 192 Assert.assertEquals(StatusCode.Code.INVALID_ARGUMENT, apiException.getStatusCode().getCode()); 193 } 194 } 195 196 @Test resizeNodeGroupTest()197 public void resizeNodeGroupTest() throws Exception { 198 NodeGroup expectedResponse = 199 NodeGroup.newBuilder() 200 .setName( 201 NodeGroupName.of("[PROJECT]", "[REGION]", "[CLUSTER]", "[NODE_GROUP]").toString()) 202 .addAllRoles(new ArrayList<NodeGroup.Role>()) 203 .setNodeGroupConfig(InstanceGroupConfig.newBuilder().build()) 204 .putAllLabels(new HashMap<String, String>()) 205 .build(); 206 Operation resultOperation = 207 Operation.newBuilder() 208 .setName("resizeNodeGroupTest") 209 .setDone(true) 210 .setResponse(Any.pack(expectedResponse)) 211 .build(); 212 mockNodeGroupController.addResponse(resultOperation); 213 214 String name = "name3373707"; 215 int size = 3530753; 216 217 NodeGroup actualResponse = client.resizeNodeGroupAsync(name, size).get(); 218 Assert.assertEquals(expectedResponse, actualResponse); 219 220 List<AbstractMessage> actualRequests = mockNodeGroupController.getRequests(); 221 Assert.assertEquals(1, actualRequests.size()); 222 ResizeNodeGroupRequest actualRequest = ((ResizeNodeGroupRequest) actualRequests.get(0)); 223 224 Assert.assertEquals(name, actualRequest.getName()); 225 Assert.assertEquals(size, actualRequest.getSize()); 226 Assert.assertTrue( 227 channelProvider.isHeaderSent( 228 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 229 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 230 } 231 232 @Test resizeNodeGroupExceptionTest()233 public void resizeNodeGroupExceptionTest() throws Exception { 234 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 235 mockNodeGroupController.addException(exception); 236 237 try { 238 String name = "name3373707"; 239 int size = 3530753; 240 client.resizeNodeGroupAsync(name, size).get(); 241 Assert.fail("No exception raised"); 242 } catch (ExecutionException e) { 243 Assert.assertEquals(InvalidArgumentException.class, e.getCause().getClass()); 244 InvalidArgumentException apiException = ((InvalidArgumentException) e.getCause()); 245 Assert.assertEquals(StatusCode.Code.INVALID_ARGUMENT, apiException.getStatusCode().getCode()); 246 } 247 } 248 249 @Test getNodeGroupTest()250 public void getNodeGroupTest() throws Exception { 251 NodeGroup expectedResponse = 252 NodeGroup.newBuilder() 253 .setName( 254 NodeGroupName.of("[PROJECT]", "[REGION]", "[CLUSTER]", "[NODE_GROUP]").toString()) 255 .addAllRoles(new ArrayList<NodeGroup.Role>()) 256 .setNodeGroupConfig(InstanceGroupConfig.newBuilder().build()) 257 .putAllLabels(new HashMap<String, String>()) 258 .build(); 259 mockNodeGroupController.addResponse(expectedResponse); 260 261 NodeGroupName name = NodeGroupName.of("[PROJECT]", "[REGION]", "[CLUSTER]", "[NODE_GROUP]"); 262 263 NodeGroup actualResponse = client.getNodeGroup(name); 264 Assert.assertEquals(expectedResponse, actualResponse); 265 266 List<AbstractMessage> actualRequests = mockNodeGroupController.getRequests(); 267 Assert.assertEquals(1, actualRequests.size()); 268 GetNodeGroupRequest actualRequest = ((GetNodeGroupRequest) actualRequests.get(0)); 269 270 Assert.assertEquals(name.toString(), actualRequest.getName()); 271 Assert.assertTrue( 272 channelProvider.isHeaderSent( 273 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 274 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 275 } 276 277 @Test getNodeGroupExceptionTest()278 public void getNodeGroupExceptionTest() throws Exception { 279 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 280 mockNodeGroupController.addException(exception); 281 282 try { 283 NodeGroupName name = NodeGroupName.of("[PROJECT]", "[REGION]", "[CLUSTER]", "[NODE_GROUP]"); 284 client.getNodeGroup(name); 285 Assert.fail("No exception raised"); 286 } catch (InvalidArgumentException e) { 287 // Expected exception. 288 } 289 } 290 291 @Test getNodeGroupTest2()292 public void getNodeGroupTest2() throws Exception { 293 NodeGroup expectedResponse = 294 NodeGroup.newBuilder() 295 .setName( 296 NodeGroupName.of("[PROJECT]", "[REGION]", "[CLUSTER]", "[NODE_GROUP]").toString()) 297 .addAllRoles(new ArrayList<NodeGroup.Role>()) 298 .setNodeGroupConfig(InstanceGroupConfig.newBuilder().build()) 299 .putAllLabels(new HashMap<String, String>()) 300 .build(); 301 mockNodeGroupController.addResponse(expectedResponse); 302 303 String name = "name3373707"; 304 305 NodeGroup actualResponse = client.getNodeGroup(name); 306 Assert.assertEquals(expectedResponse, actualResponse); 307 308 List<AbstractMessage> actualRequests = mockNodeGroupController.getRequests(); 309 Assert.assertEquals(1, actualRequests.size()); 310 GetNodeGroupRequest actualRequest = ((GetNodeGroupRequest) actualRequests.get(0)); 311 312 Assert.assertEquals(name, actualRequest.getName()); 313 Assert.assertTrue( 314 channelProvider.isHeaderSent( 315 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 316 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 317 } 318 319 @Test getNodeGroupExceptionTest2()320 public void getNodeGroupExceptionTest2() throws Exception { 321 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 322 mockNodeGroupController.addException(exception); 323 324 try { 325 String name = "name3373707"; 326 client.getNodeGroup(name); 327 Assert.fail("No exception raised"); 328 } catch (InvalidArgumentException e) { 329 // Expected exception. 330 } 331 } 332 } 333