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.talent.v4beta1; 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.protobuf.AbstractMessage; 27 import com.google.protobuf.Timestamp; 28 import io.grpc.StatusRuntimeException; 29 import java.io.IOException; 30 import java.util.Arrays; 31 import java.util.List; 32 import java.util.UUID; 33 import javax.annotation.Generated; 34 import org.junit.After; 35 import org.junit.AfterClass; 36 import org.junit.Assert; 37 import org.junit.Before; 38 import org.junit.BeforeClass; 39 import org.junit.Test; 40 41 @Generated("by gapic-generator-java") 42 public class EventServiceClientTest { 43 private static MockEventService mockEventService; 44 private static MockServiceHelper mockServiceHelper; 45 private LocalChannelProvider channelProvider; 46 private EventServiceClient client; 47 48 @BeforeClass startStaticServer()49 public static void startStaticServer() { 50 mockEventService = new MockEventService(); 51 mockServiceHelper = 52 new MockServiceHelper( 53 UUID.randomUUID().toString(), Arrays.<MockGrpcService>asList(mockEventService)); 54 mockServiceHelper.start(); 55 } 56 57 @AfterClass stopServer()58 public static void stopServer() { 59 mockServiceHelper.stop(); 60 } 61 62 @Before setUp()63 public void setUp() throws IOException { 64 mockServiceHelper.reset(); 65 channelProvider = mockServiceHelper.createChannelProvider(); 66 EventServiceSettings settings = 67 EventServiceSettings.newBuilder() 68 .setTransportChannelProvider(channelProvider) 69 .setCredentialsProvider(NoCredentialsProvider.create()) 70 .build(); 71 client = EventServiceClient.create(settings); 72 } 73 74 @After tearDown()75 public void tearDown() throws Exception { 76 client.close(); 77 } 78 79 @Test createClientEventTest()80 public void createClientEventTest() throws Exception { 81 ClientEvent expectedResponse = 82 ClientEvent.newBuilder() 83 .setRequestId("requestId693933066") 84 .setEventId("eventId-1376502443") 85 .setCreateTime(Timestamp.newBuilder().build()) 86 .setEventNotes("eventNotes968522823") 87 .build(); 88 mockEventService.addResponse(expectedResponse); 89 90 ProjectName parent = ProjectName.of("[PROJECT]"); 91 ClientEvent clientEvent = ClientEvent.newBuilder().build(); 92 93 ClientEvent actualResponse = client.createClientEvent(parent, clientEvent); 94 Assert.assertEquals(expectedResponse, actualResponse); 95 96 List<AbstractMessage> actualRequests = mockEventService.getRequests(); 97 Assert.assertEquals(1, actualRequests.size()); 98 CreateClientEventRequest actualRequest = ((CreateClientEventRequest) actualRequests.get(0)); 99 100 Assert.assertEquals(parent.toString(), actualRequest.getParent()); 101 Assert.assertEquals(clientEvent, actualRequest.getClientEvent()); 102 Assert.assertTrue( 103 channelProvider.isHeaderSent( 104 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 105 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 106 } 107 108 @Test createClientEventExceptionTest()109 public void createClientEventExceptionTest() throws Exception { 110 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 111 mockEventService.addException(exception); 112 113 try { 114 ProjectName parent = ProjectName.of("[PROJECT]"); 115 ClientEvent clientEvent = ClientEvent.newBuilder().build(); 116 client.createClientEvent(parent, clientEvent); 117 Assert.fail("No exception raised"); 118 } catch (InvalidArgumentException e) { 119 // Expected exception. 120 } 121 } 122 123 @Test createClientEventTest2()124 public void createClientEventTest2() throws Exception { 125 ClientEvent expectedResponse = 126 ClientEvent.newBuilder() 127 .setRequestId("requestId693933066") 128 .setEventId("eventId-1376502443") 129 .setCreateTime(Timestamp.newBuilder().build()) 130 .setEventNotes("eventNotes968522823") 131 .build(); 132 mockEventService.addResponse(expectedResponse); 133 134 TenantName parent = TenantName.of("[PROJECT]", "[TENANT]"); 135 ClientEvent clientEvent = ClientEvent.newBuilder().build(); 136 137 ClientEvent actualResponse = client.createClientEvent(parent, clientEvent); 138 Assert.assertEquals(expectedResponse, actualResponse); 139 140 List<AbstractMessage> actualRequests = mockEventService.getRequests(); 141 Assert.assertEquals(1, actualRequests.size()); 142 CreateClientEventRequest actualRequest = ((CreateClientEventRequest) actualRequests.get(0)); 143 144 Assert.assertEquals(parent.toString(), actualRequest.getParent()); 145 Assert.assertEquals(clientEvent, actualRequest.getClientEvent()); 146 Assert.assertTrue( 147 channelProvider.isHeaderSent( 148 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 149 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 150 } 151 152 @Test createClientEventExceptionTest2()153 public void createClientEventExceptionTest2() throws Exception { 154 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 155 mockEventService.addException(exception); 156 157 try { 158 TenantName parent = TenantName.of("[PROJECT]", "[TENANT]"); 159 ClientEvent clientEvent = ClientEvent.newBuilder().build(); 160 client.createClientEvent(parent, clientEvent); 161 Assert.fail("No exception raised"); 162 } catch (InvalidArgumentException e) { 163 // Expected exception. 164 } 165 } 166 167 @Test createClientEventTest3()168 public void createClientEventTest3() throws Exception { 169 ClientEvent expectedResponse = 170 ClientEvent.newBuilder() 171 .setRequestId("requestId693933066") 172 .setEventId("eventId-1376502443") 173 .setCreateTime(Timestamp.newBuilder().build()) 174 .setEventNotes("eventNotes968522823") 175 .build(); 176 mockEventService.addResponse(expectedResponse); 177 178 String parent = "parent-995424086"; 179 ClientEvent clientEvent = ClientEvent.newBuilder().build(); 180 181 ClientEvent actualResponse = client.createClientEvent(parent, clientEvent); 182 Assert.assertEquals(expectedResponse, actualResponse); 183 184 List<AbstractMessage> actualRequests = mockEventService.getRequests(); 185 Assert.assertEquals(1, actualRequests.size()); 186 CreateClientEventRequest actualRequest = ((CreateClientEventRequest) actualRequests.get(0)); 187 188 Assert.assertEquals(parent, actualRequest.getParent()); 189 Assert.assertEquals(clientEvent, actualRequest.getClientEvent()); 190 Assert.assertTrue( 191 channelProvider.isHeaderSent( 192 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 193 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 194 } 195 196 @Test createClientEventExceptionTest3()197 public void createClientEventExceptionTest3() throws Exception { 198 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 199 mockEventService.addException(exception); 200 201 try { 202 String parent = "parent-995424086"; 203 ClientEvent clientEvent = ClientEvent.newBuilder().build(); 204 client.createClientEvent(parent, clientEvent); 205 Assert.fail("No exception raised"); 206 } catch (InvalidArgumentException e) { 207 // Expected exception. 208 } 209 } 210 } 211