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.support.v2; 18 19 import static com.google.cloud.support.v2.CommentServiceClient.ListCommentsPagedResponse; 20 21 import com.google.api.gax.core.NoCredentialsProvider; 22 import com.google.api.gax.httpjson.GaxHttpJsonProperties; 23 import com.google.api.gax.httpjson.testing.MockHttpService; 24 import com.google.api.gax.rpc.ApiClientHeaderProvider; 25 import com.google.api.gax.rpc.ApiException; 26 import com.google.api.gax.rpc.ApiExceptionFactory; 27 import com.google.api.gax.rpc.InvalidArgumentException; 28 import com.google.api.gax.rpc.StatusCode; 29 import com.google.api.gax.rpc.testing.FakeStatusCode; 30 import com.google.cloud.support.v2.stub.HttpJsonCommentServiceStub; 31 import com.google.common.collect.Lists; 32 import com.google.protobuf.Timestamp; 33 import java.io.IOException; 34 import java.util.Arrays; 35 import java.util.List; 36 import javax.annotation.Generated; 37 import org.junit.After; 38 import org.junit.AfterClass; 39 import org.junit.Assert; 40 import org.junit.Before; 41 import org.junit.BeforeClass; 42 import org.junit.Test; 43 44 @Generated("by gapic-generator-java") 45 public class CommentServiceClientHttpJsonTest { 46 private static MockHttpService mockService; 47 private static CommentServiceClient client; 48 49 @BeforeClass startStaticServer()50 public static void startStaticServer() throws IOException { 51 mockService = 52 new MockHttpService( 53 HttpJsonCommentServiceStub.getMethodDescriptors(), 54 CommentServiceSettings.getDefaultEndpoint()); 55 CommentServiceSettings settings = 56 CommentServiceSettings.newHttpJsonBuilder() 57 .setTransportChannelProvider( 58 CommentServiceSettings.defaultHttpJsonTransportProviderBuilder() 59 .setHttpTransport(mockService) 60 .build()) 61 .setCredentialsProvider(NoCredentialsProvider.create()) 62 .build(); 63 client = CommentServiceClient.create(settings); 64 } 65 66 @AfterClass stopServer()67 public static void stopServer() { 68 client.close(); 69 } 70 71 @Before setUp()72 public void setUp() {} 73 74 @After tearDown()75 public void tearDown() throws Exception { 76 mockService.reset(); 77 } 78 79 @Test listCommentsTest()80 public void listCommentsTest() throws Exception { 81 Comment responsesElement = Comment.newBuilder().build(); 82 ListCommentsResponse expectedResponse = 83 ListCommentsResponse.newBuilder() 84 .setNextPageToken("") 85 .addAllComments(Arrays.asList(responsesElement)) 86 .build(); 87 mockService.addResponse(expectedResponse); 88 89 CaseName parent = CaseName.ofProjectCaseName("[PROJECT]", "[CASE]"); 90 91 ListCommentsPagedResponse pagedListResponse = client.listComments(parent); 92 93 List<Comment> resources = Lists.newArrayList(pagedListResponse.iterateAll()); 94 95 Assert.assertEquals(1, resources.size()); 96 Assert.assertEquals(expectedResponse.getCommentsList().get(0), resources.get(0)); 97 98 List<String> actualRequests = mockService.getRequestPaths(); 99 Assert.assertEquals(1, actualRequests.size()); 100 101 String apiClientHeaderKey = 102 mockService 103 .getRequestHeaders() 104 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 105 .iterator() 106 .next(); 107 Assert.assertTrue( 108 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 109 .matcher(apiClientHeaderKey) 110 .matches()); 111 } 112 113 @Test listCommentsExceptionTest()114 public void listCommentsExceptionTest() throws Exception { 115 ApiException exception = 116 ApiExceptionFactory.createException( 117 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 118 mockService.addException(exception); 119 120 try { 121 CaseName parent = CaseName.ofProjectCaseName("[PROJECT]", "[CASE]"); 122 client.listComments(parent); 123 Assert.fail("No exception raised"); 124 } catch (InvalidArgumentException e) { 125 // Expected exception. 126 } 127 } 128 129 @Test listCommentsTest2()130 public void listCommentsTest2() throws Exception { 131 Comment responsesElement = Comment.newBuilder().build(); 132 ListCommentsResponse expectedResponse = 133 ListCommentsResponse.newBuilder() 134 .setNextPageToken("") 135 .addAllComments(Arrays.asList(responsesElement)) 136 .build(); 137 mockService.addResponse(expectedResponse); 138 139 String parent = "projects/project-2178/cases/case-2178"; 140 141 ListCommentsPagedResponse pagedListResponse = client.listComments(parent); 142 143 List<Comment> resources = Lists.newArrayList(pagedListResponse.iterateAll()); 144 145 Assert.assertEquals(1, resources.size()); 146 Assert.assertEquals(expectedResponse.getCommentsList().get(0), resources.get(0)); 147 148 List<String> actualRequests = mockService.getRequestPaths(); 149 Assert.assertEquals(1, actualRequests.size()); 150 151 String apiClientHeaderKey = 152 mockService 153 .getRequestHeaders() 154 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 155 .iterator() 156 .next(); 157 Assert.assertTrue( 158 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 159 .matcher(apiClientHeaderKey) 160 .matches()); 161 } 162 163 @Test listCommentsExceptionTest2()164 public void listCommentsExceptionTest2() throws Exception { 165 ApiException exception = 166 ApiExceptionFactory.createException( 167 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 168 mockService.addException(exception); 169 170 try { 171 String parent = "projects/project-2178/cases/case-2178"; 172 client.listComments(parent); 173 Assert.fail("No exception raised"); 174 } catch (InvalidArgumentException e) { 175 // Expected exception. 176 } 177 } 178 179 @Test createCommentTest()180 public void createCommentTest() throws Exception { 181 Comment expectedResponse = 182 Comment.newBuilder() 183 .setName("name3373707") 184 .setCreateTime(Timestamp.newBuilder().build()) 185 .setCreator(Actor.newBuilder().build()) 186 .setBody("body3029410") 187 .setPlainTextBody("plainTextBody-1301059495") 188 .build(); 189 mockService.addResponse(expectedResponse); 190 191 CaseName parent = CaseName.ofProjectCaseName("[PROJECT]", "[CASE]"); 192 Comment comment = Comment.newBuilder().build(); 193 194 Comment actualResponse = client.createComment(parent, comment); 195 Assert.assertEquals(expectedResponse, actualResponse); 196 197 List<String> actualRequests = mockService.getRequestPaths(); 198 Assert.assertEquals(1, actualRequests.size()); 199 200 String apiClientHeaderKey = 201 mockService 202 .getRequestHeaders() 203 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 204 .iterator() 205 .next(); 206 Assert.assertTrue( 207 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 208 .matcher(apiClientHeaderKey) 209 .matches()); 210 } 211 212 @Test createCommentExceptionTest()213 public void createCommentExceptionTest() throws Exception { 214 ApiException exception = 215 ApiExceptionFactory.createException( 216 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 217 mockService.addException(exception); 218 219 try { 220 CaseName parent = CaseName.ofProjectCaseName("[PROJECT]", "[CASE]"); 221 Comment comment = Comment.newBuilder().build(); 222 client.createComment(parent, comment); 223 Assert.fail("No exception raised"); 224 } catch (InvalidArgumentException e) { 225 // Expected exception. 226 } 227 } 228 229 @Test createCommentTest2()230 public void createCommentTest2() throws Exception { 231 Comment expectedResponse = 232 Comment.newBuilder() 233 .setName("name3373707") 234 .setCreateTime(Timestamp.newBuilder().build()) 235 .setCreator(Actor.newBuilder().build()) 236 .setBody("body3029410") 237 .setPlainTextBody("plainTextBody-1301059495") 238 .build(); 239 mockService.addResponse(expectedResponse); 240 241 String parent = "projects/project-2178/cases/case-2178"; 242 Comment comment = Comment.newBuilder().build(); 243 244 Comment actualResponse = client.createComment(parent, comment); 245 Assert.assertEquals(expectedResponse, actualResponse); 246 247 List<String> actualRequests = mockService.getRequestPaths(); 248 Assert.assertEquals(1, actualRequests.size()); 249 250 String apiClientHeaderKey = 251 mockService 252 .getRequestHeaders() 253 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 254 .iterator() 255 .next(); 256 Assert.assertTrue( 257 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 258 .matcher(apiClientHeaderKey) 259 .matches()); 260 } 261 262 @Test createCommentExceptionTest2()263 public void createCommentExceptionTest2() throws Exception { 264 ApiException exception = 265 ApiExceptionFactory.createException( 266 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 267 mockService.addException(exception); 268 269 try { 270 String parent = "projects/project-2178/cases/case-2178"; 271 Comment comment = Comment.newBuilder().build(); 272 client.createComment(parent, comment); 273 Assert.fail("No exception raised"); 274 } catch (InvalidArgumentException e) { 275 // Expected exception. 276 } 277 } 278 } 279