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.advisorynotifications.v1; 18 19 import static com.google.cloud.advisorynotifications.v1.AdvisoryNotificationsServiceClient.ListNotificationsPagedResponse; 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.advisorynotifications.v1.stub.HttpJsonAdvisoryNotificationsServiceStub; 31 import com.google.common.collect.Lists; 32 import com.google.protobuf.Timestamp; 33 import java.io.IOException; 34 import java.util.ArrayList; 35 import java.util.Arrays; 36 import java.util.List; 37 import javax.annotation.Generated; 38 import org.junit.After; 39 import org.junit.AfterClass; 40 import org.junit.Assert; 41 import org.junit.Before; 42 import org.junit.BeforeClass; 43 import org.junit.Test; 44 45 @Generated("by gapic-generator-java") 46 public class AdvisoryNotificationsServiceClientHttpJsonTest { 47 private static MockHttpService mockService; 48 private static AdvisoryNotificationsServiceClient client; 49 50 @BeforeClass startStaticServer()51 public static void startStaticServer() throws IOException { 52 mockService = 53 new MockHttpService( 54 HttpJsonAdvisoryNotificationsServiceStub.getMethodDescriptors(), 55 AdvisoryNotificationsServiceSettings.getDefaultEndpoint()); 56 AdvisoryNotificationsServiceSettings settings = 57 AdvisoryNotificationsServiceSettings.newHttpJsonBuilder() 58 .setTransportChannelProvider( 59 AdvisoryNotificationsServiceSettings.defaultHttpJsonTransportProviderBuilder() 60 .setHttpTransport(mockService) 61 .build()) 62 .setCredentialsProvider(NoCredentialsProvider.create()) 63 .build(); 64 client = AdvisoryNotificationsServiceClient.create(settings); 65 } 66 67 @AfterClass stopServer()68 public static void stopServer() { 69 client.close(); 70 } 71 72 @Before setUp()73 public void setUp() {} 74 75 @After tearDown()76 public void tearDown() throws Exception { 77 mockService.reset(); 78 } 79 80 @Test listNotificationsTest()81 public void listNotificationsTest() throws Exception { 82 Notification responsesElement = Notification.newBuilder().build(); 83 ListNotificationsResponse expectedResponse = 84 ListNotificationsResponse.newBuilder() 85 .setNextPageToken("") 86 .addAllNotifications(Arrays.asList(responsesElement)) 87 .build(); 88 mockService.addResponse(expectedResponse); 89 90 LocationName parent = LocationName.of("[ORGANIZATION]", "[LOCATION]"); 91 92 ListNotificationsPagedResponse pagedListResponse = client.listNotifications(parent); 93 94 List<Notification> resources = Lists.newArrayList(pagedListResponse.iterateAll()); 95 96 Assert.assertEquals(1, resources.size()); 97 Assert.assertEquals(expectedResponse.getNotificationsList().get(0), resources.get(0)); 98 99 List<String> actualRequests = mockService.getRequestPaths(); 100 Assert.assertEquals(1, actualRequests.size()); 101 102 String apiClientHeaderKey = 103 mockService 104 .getRequestHeaders() 105 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 106 .iterator() 107 .next(); 108 Assert.assertTrue( 109 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 110 .matcher(apiClientHeaderKey) 111 .matches()); 112 } 113 114 @Test listNotificationsExceptionTest()115 public void listNotificationsExceptionTest() throws Exception { 116 ApiException exception = 117 ApiExceptionFactory.createException( 118 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 119 mockService.addException(exception); 120 121 try { 122 LocationName parent = LocationName.of("[ORGANIZATION]", "[LOCATION]"); 123 client.listNotifications(parent); 124 Assert.fail("No exception raised"); 125 } catch (InvalidArgumentException e) { 126 // Expected exception. 127 } 128 } 129 130 @Test listNotificationsTest2()131 public void listNotificationsTest2() throws Exception { 132 Notification responsesElement = Notification.newBuilder().build(); 133 ListNotificationsResponse expectedResponse = 134 ListNotificationsResponse.newBuilder() 135 .setNextPageToken("") 136 .addAllNotifications(Arrays.asList(responsesElement)) 137 .build(); 138 mockService.addResponse(expectedResponse); 139 140 String parent = "organizations/organization-9365/locations/location-9365"; 141 142 ListNotificationsPagedResponse pagedListResponse = client.listNotifications(parent); 143 144 List<Notification> resources = Lists.newArrayList(pagedListResponse.iterateAll()); 145 146 Assert.assertEquals(1, resources.size()); 147 Assert.assertEquals(expectedResponse.getNotificationsList().get(0), resources.get(0)); 148 149 List<String> actualRequests = mockService.getRequestPaths(); 150 Assert.assertEquals(1, actualRequests.size()); 151 152 String apiClientHeaderKey = 153 mockService 154 .getRequestHeaders() 155 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 156 .iterator() 157 .next(); 158 Assert.assertTrue( 159 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 160 .matcher(apiClientHeaderKey) 161 .matches()); 162 } 163 164 @Test listNotificationsExceptionTest2()165 public void listNotificationsExceptionTest2() throws Exception { 166 ApiException exception = 167 ApiExceptionFactory.createException( 168 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 169 mockService.addException(exception); 170 171 try { 172 String parent = "organizations/organization-9365/locations/location-9365"; 173 client.listNotifications(parent); 174 Assert.fail("No exception raised"); 175 } catch (InvalidArgumentException e) { 176 // Expected exception. 177 } 178 } 179 180 @Test getNotificationTest()181 public void getNotificationTest() throws Exception { 182 Notification expectedResponse = 183 Notification.newBuilder() 184 .setName( 185 NotificationName.of("[ORGANIZATION]", "[LOCATION]", "[NOTIFICATION]").toString()) 186 .setSubject(Subject.newBuilder().build()) 187 .addAllMessages(new ArrayList<Message>()) 188 .setCreateTime(Timestamp.newBuilder().build()) 189 .setNotificationType(NotificationType.forNumber(0)) 190 .build(); 191 mockService.addResponse(expectedResponse); 192 193 NotificationName name = NotificationName.of("[ORGANIZATION]", "[LOCATION]", "[NOTIFICATION]"); 194 195 Notification actualResponse = client.getNotification(name); 196 Assert.assertEquals(expectedResponse, actualResponse); 197 198 List<String> actualRequests = mockService.getRequestPaths(); 199 Assert.assertEquals(1, actualRequests.size()); 200 201 String apiClientHeaderKey = 202 mockService 203 .getRequestHeaders() 204 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 205 .iterator() 206 .next(); 207 Assert.assertTrue( 208 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 209 .matcher(apiClientHeaderKey) 210 .matches()); 211 } 212 213 @Test getNotificationExceptionTest()214 public void getNotificationExceptionTest() throws Exception { 215 ApiException exception = 216 ApiExceptionFactory.createException( 217 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 218 mockService.addException(exception); 219 220 try { 221 NotificationName name = NotificationName.of("[ORGANIZATION]", "[LOCATION]", "[NOTIFICATION]"); 222 client.getNotification(name); 223 Assert.fail("No exception raised"); 224 } catch (InvalidArgumentException e) { 225 // Expected exception. 226 } 227 } 228 229 @Test getNotificationTest2()230 public void getNotificationTest2() throws Exception { 231 Notification expectedResponse = 232 Notification.newBuilder() 233 .setName( 234 NotificationName.of("[ORGANIZATION]", "[LOCATION]", "[NOTIFICATION]").toString()) 235 .setSubject(Subject.newBuilder().build()) 236 .addAllMessages(new ArrayList<Message>()) 237 .setCreateTime(Timestamp.newBuilder().build()) 238 .setNotificationType(NotificationType.forNumber(0)) 239 .build(); 240 mockService.addResponse(expectedResponse); 241 242 String name = 243 "organizations/organization-2434/locations/location-2434/notifications/notification-2434"; 244 245 Notification actualResponse = client.getNotification(name); 246 Assert.assertEquals(expectedResponse, actualResponse); 247 248 List<String> actualRequests = mockService.getRequestPaths(); 249 Assert.assertEquals(1, actualRequests.size()); 250 251 String apiClientHeaderKey = 252 mockService 253 .getRequestHeaders() 254 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 255 .iterator() 256 .next(); 257 Assert.assertTrue( 258 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 259 .matcher(apiClientHeaderKey) 260 .matches()); 261 } 262 263 @Test getNotificationExceptionTest2()264 public void getNotificationExceptionTest2() throws Exception { 265 ApiException exception = 266 ApiExceptionFactory.createException( 267 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 268 mockService.addException(exception); 269 270 try { 271 String name = 272 "organizations/organization-2434/locations/location-2434/notifications/notification-2434"; 273 client.getNotification(name); 274 Assert.fail("No exception raised"); 275 } catch (InvalidArgumentException e) { 276 // Expected exception. 277 } 278 } 279 } 280