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.devtools.clouderrorreporting.v1beta1; 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.devtools.clouderrorreporting.v1beta1.stub.HttpJsonErrorGroupServiceStub; 29 import java.io.IOException; 30 import java.util.ArrayList; 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 ErrorGroupServiceClientHttpJsonTest { 42 private static MockHttpService mockService; 43 private static ErrorGroupServiceClient client; 44 45 @BeforeClass startStaticServer()46 public static void startStaticServer() throws IOException { 47 mockService = 48 new MockHttpService( 49 HttpJsonErrorGroupServiceStub.getMethodDescriptors(), 50 ErrorGroupServiceSettings.getDefaultEndpoint()); 51 ErrorGroupServiceSettings settings = 52 ErrorGroupServiceSettings.newHttpJsonBuilder() 53 .setTransportChannelProvider( 54 ErrorGroupServiceSettings.defaultHttpJsonTransportProviderBuilder() 55 .setHttpTransport(mockService) 56 .build()) 57 .setCredentialsProvider(NoCredentialsProvider.create()) 58 .build(); 59 client = ErrorGroupServiceClient.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 getGroupTest()76 public void getGroupTest() throws Exception { 77 ErrorGroup expectedResponse = 78 ErrorGroup.newBuilder() 79 .setName(ErrorGroupName.of("[PROJECT]", "[GROUP]").toString()) 80 .setGroupId("groupId293428218") 81 .addAllTrackingIssues(new ArrayList<TrackingIssue>()) 82 .setResolutionStatus(ResolutionStatus.forNumber(0)) 83 .build(); 84 mockService.addResponse(expectedResponse); 85 86 ErrorGroupName groupName = ErrorGroupName.of("[PROJECT]", "[GROUP]"); 87 88 ErrorGroup actualResponse = client.getGroup(groupName); 89 Assert.assertEquals(expectedResponse, actualResponse); 90 91 List<String> actualRequests = mockService.getRequestPaths(); 92 Assert.assertEquals(1, actualRequests.size()); 93 94 String apiClientHeaderKey = 95 mockService 96 .getRequestHeaders() 97 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 98 .iterator() 99 .next(); 100 Assert.assertTrue( 101 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 102 .matcher(apiClientHeaderKey) 103 .matches()); 104 } 105 106 @Test getGroupExceptionTest()107 public void getGroupExceptionTest() throws Exception { 108 ApiException exception = 109 ApiExceptionFactory.createException( 110 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 111 mockService.addException(exception); 112 113 try { 114 ErrorGroupName groupName = ErrorGroupName.of("[PROJECT]", "[GROUP]"); 115 client.getGroup(groupName); 116 Assert.fail("No exception raised"); 117 } catch (InvalidArgumentException e) { 118 // Expected exception. 119 } 120 } 121 122 @Test getGroupTest2()123 public void getGroupTest2() throws Exception { 124 ErrorGroup expectedResponse = 125 ErrorGroup.newBuilder() 126 .setName(ErrorGroupName.of("[PROJECT]", "[GROUP]").toString()) 127 .setGroupId("groupId293428218") 128 .addAllTrackingIssues(new ArrayList<TrackingIssue>()) 129 .setResolutionStatus(ResolutionStatus.forNumber(0)) 130 .build(); 131 mockService.addResponse(expectedResponse); 132 133 String groupName = "projects/project-3535/groups/group-3535"; 134 135 ErrorGroup actualResponse = client.getGroup(groupName); 136 Assert.assertEquals(expectedResponse, actualResponse); 137 138 List<String> actualRequests = mockService.getRequestPaths(); 139 Assert.assertEquals(1, actualRequests.size()); 140 141 String apiClientHeaderKey = 142 mockService 143 .getRequestHeaders() 144 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 145 .iterator() 146 .next(); 147 Assert.assertTrue( 148 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 149 .matcher(apiClientHeaderKey) 150 .matches()); 151 } 152 153 @Test getGroupExceptionTest2()154 public void getGroupExceptionTest2() throws Exception { 155 ApiException exception = 156 ApiExceptionFactory.createException( 157 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 158 mockService.addException(exception); 159 160 try { 161 String groupName = "projects/project-3535/groups/group-3535"; 162 client.getGroup(groupName); 163 Assert.fail("No exception raised"); 164 } catch (InvalidArgumentException e) { 165 // Expected exception. 166 } 167 } 168 169 @Test updateGroupTest()170 public void updateGroupTest() throws Exception { 171 ErrorGroup expectedResponse = 172 ErrorGroup.newBuilder() 173 .setName(ErrorGroupName.of("[PROJECT]", "[GROUP]").toString()) 174 .setGroupId("groupId293428218") 175 .addAllTrackingIssues(new ArrayList<TrackingIssue>()) 176 .setResolutionStatus(ResolutionStatus.forNumber(0)) 177 .build(); 178 mockService.addResponse(expectedResponse); 179 180 ErrorGroup group = 181 ErrorGroup.newBuilder() 182 .setName(ErrorGroupName.of("[PROJECT]", "[GROUP]").toString()) 183 .setGroupId("groupId293428218") 184 .addAllTrackingIssues(new ArrayList<TrackingIssue>()) 185 .setResolutionStatus(ResolutionStatus.forNumber(0)) 186 .build(); 187 188 ErrorGroup actualResponse = client.updateGroup(group); 189 Assert.assertEquals(expectedResponse, actualResponse); 190 191 List<String> actualRequests = mockService.getRequestPaths(); 192 Assert.assertEquals(1, actualRequests.size()); 193 194 String apiClientHeaderKey = 195 mockService 196 .getRequestHeaders() 197 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 198 .iterator() 199 .next(); 200 Assert.assertTrue( 201 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 202 .matcher(apiClientHeaderKey) 203 .matches()); 204 } 205 206 @Test updateGroupExceptionTest()207 public void updateGroupExceptionTest() throws Exception { 208 ApiException exception = 209 ApiExceptionFactory.createException( 210 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 211 mockService.addException(exception); 212 213 try { 214 ErrorGroup group = 215 ErrorGroup.newBuilder() 216 .setName(ErrorGroupName.of("[PROJECT]", "[GROUP]").toString()) 217 .setGroupId("groupId293428218") 218 .addAllTrackingIssues(new ArrayList<TrackingIssue>()) 219 .setResolutionStatus(ResolutionStatus.forNumber(0)) 220 .build(); 221 client.updateGroup(group); 222 Assert.fail("No exception raised"); 223 } catch (InvalidArgumentException e) { 224 // Expected exception. 225 } 226 } 227 } 228