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.v4; 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.talent.v4.stub.HttpJsonEventServiceStub; 29 import com.google.protobuf.Timestamp; 30 import java.io.IOException; 31 import java.util.List; 32 import javax.annotation.Generated; 33 import org.junit.After; 34 import org.junit.AfterClass; 35 import org.junit.Assert; 36 import org.junit.Before; 37 import org.junit.BeforeClass; 38 import org.junit.Test; 39 40 @Generated("by gapic-generator-java") 41 public class EventServiceClientHttpJsonTest { 42 private static MockHttpService mockService; 43 private static EventServiceClient client; 44 45 @BeforeClass startStaticServer()46 public static void startStaticServer() throws IOException { 47 mockService = 48 new MockHttpService( 49 HttpJsonEventServiceStub.getMethodDescriptors(), 50 EventServiceSettings.getDefaultEndpoint()); 51 EventServiceSettings settings = 52 EventServiceSettings.newHttpJsonBuilder() 53 .setTransportChannelProvider( 54 EventServiceSettings.defaultHttpJsonTransportProviderBuilder() 55 .setHttpTransport(mockService) 56 .build()) 57 .setCredentialsProvider(NoCredentialsProvider.create()) 58 .build(); 59 client = EventServiceClient.create(settings); 60 } 61 62 @AfterClass stopServer()63 public static void stopServer() { 64 client.close(); 65 } 66 67 @Before setUp()68 public void setUp() {} 69 70 @After tearDown()71 public void tearDown() throws Exception { 72 mockService.reset(); 73 } 74 75 @Test createClientEventTest()76 public void createClientEventTest() throws Exception { 77 ClientEvent expectedResponse = 78 ClientEvent.newBuilder() 79 .setRequestId("requestId693933066") 80 .setEventId("eventId-1376502443") 81 .setCreateTime(Timestamp.newBuilder().build()) 82 .setEventNotes("eventNotes968522823") 83 .build(); 84 mockService.addResponse(expectedResponse); 85 86 TenantName parent = TenantName.of("[PROJECT]", "[TENANT]"); 87 ClientEvent clientEvent = ClientEvent.newBuilder().build(); 88 89 ClientEvent actualResponse = client.createClientEvent(parent, clientEvent); 90 Assert.assertEquals(expectedResponse, actualResponse); 91 92 List<String> actualRequests = mockService.getRequestPaths(); 93 Assert.assertEquals(1, actualRequests.size()); 94 95 String apiClientHeaderKey = 96 mockService 97 .getRequestHeaders() 98 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 99 .iterator() 100 .next(); 101 Assert.assertTrue( 102 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 103 .matcher(apiClientHeaderKey) 104 .matches()); 105 } 106 107 @Test createClientEventExceptionTest()108 public void createClientEventExceptionTest() throws Exception { 109 ApiException exception = 110 ApiExceptionFactory.createException( 111 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 112 mockService.addException(exception); 113 114 try { 115 TenantName parent = TenantName.of("[PROJECT]", "[TENANT]"); 116 ClientEvent clientEvent = ClientEvent.newBuilder().build(); 117 client.createClientEvent(parent, clientEvent); 118 Assert.fail("No exception raised"); 119 } catch (InvalidArgumentException e) { 120 // Expected exception. 121 } 122 } 123 124 @Test createClientEventTest2()125 public void createClientEventTest2() throws Exception { 126 ClientEvent expectedResponse = 127 ClientEvent.newBuilder() 128 .setRequestId("requestId693933066") 129 .setEventId("eventId-1376502443") 130 .setCreateTime(Timestamp.newBuilder().build()) 131 .setEventNotes("eventNotes968522823") 132 .build(); 133 mockService.addResponse(expectedResponse); 134 135 String parent = "projects/project-8940/tenants/tenant-8940"; 136 ClientEvent clientEvent = ClientEvent.newBuilder().build(); 137 138 ClientEvent actualResponse = client.createClientEvent(parent, clientEvent); 139 Assert.assertEquals(expectedResponse, actualResponse); 140 141 List<String> actualRequests = mockService.getRequestPaths(); 142 Assert.assertEquals(1, actualRequests.size()); 143 144 String apiClientHeaderKey = 145 mockService 146 .getRequestHeaders() 147 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 148 .iterator() 149 .next(); 150 Assert.assertTrue( 151 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 152 .matcher(apiClientHeaderKey) 153 .matches()); 154 } 155 156 @Test createClientEventExceptionTest2()157 public void createClientEventExceptionTest2() throws Exception { 158 ApiException exception = 159 ApiExceptionFactory.createException( 160 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 161 mockService.addException(exception); 162 163 try { 164 String parent = "projects/project-8940/tenants/tenant-8940"; 165 ClientEvent clientEvent = ClientEvent.newBuilder().build(); 166 client.createClientEvent(parent, clientEvent); 167 Assert.fail("No exception raised"); 168 } catch (InvalidArgumentException e) { 169 // Expected exception. 170 } 171 } 172 } 173