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.servicedirectory.v1; 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.servicedirectory.v1.stub.HttpJsonLookupServiceStub; 29 import java.io.IOException; 30 import java.util.List; 31 import javax.annotation.Generated; 32 import org.junit.After; 33 import org.junit.AfterClass; 34 import org.junit.Assert; 35 import org.junit.Before; 36 import org.junit.BeforeClass; 37 import org.junit.Test; 38 39 @Generated("by gapic-generator-java") 40 public class LookupServiceClientHttpJsonTest { 41 private static MockHttpService mockService; 42 private static LookupServiceClient client; 43 44 @BeforeClass startStaticServer()45 public static void startStaticServer() throws IOException { 46 mockService = 47 new MockHttpService( 48 HttpJsonLookupServiceStub.getMethodDescriptors(), 49 LookupServiceSettings.getDefaultEndpoint()); 50 LookupServiceSettings settings = 51 LookupServiceSettings.newHttpJsonBuilder() 52 .setTransportChannelProvider( 53 LookupServiceSettings.defaultHttpJsonTransportProviderBuilder() 54 .setHttpTransport(mockService) 55 .build()) 56 .setCredentialsProvider(NoCredentialsProvider.create()) 57 .build(); 58 client = LookupServiceClient.create(settings); 59 } 60 61 @AfterClass stopServer()62 public static void stopServer() { 63 client.close(); 64 } 65 66 @Before setUp()67 public void setUp() {} 68 69 @After tearDown()70 public void tearDown() throws Exception { 71 mockService.reset(); 72 } 73 74 @Test resolveServiceTest()75 public void resolveServiceTest() throws Exception { 76 ResolveServiceResponse expectedResponse = 77 ResolveServiceResponse.newBuilder().setService(Service.newBuilder().build()).build(); 78 mockService.addResponse(expectedResponse); 79 80 ResolveServiceRequest request = 81 ResolveServiceRequest.newBuilder() 82 .setName( 83 ServiceName.of("[PROJECT]", "[LOCATION]", "[NAMESPACE]", "[SERVICE]").toString()) 84 .setMaxEndpoints(2074789987) 85 .setEndpointFilter("endpointFilter-1834249875") 86 .build(); 87 88 ResolveServiceResponse actualResponse = client.resolveService(request); 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 resolveServiceExceptionTest()107 public void resolveServiceExceptionTest() 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 ResolveServiceRequest request = 115 ResolveServiceRequest.newBuilder() 116 .setName( 117 ServiceName.of("[PROJECT]", "[LOCATION]", "[NAMESPACE]", "[SERVICE]").toString()) 118 .setMaxEndpoints(2074789987) 119 .setEndpointFilter("endpointFilter-1834249875") 120 .build(); 121 client.resolveService(request); 122 Assert.fail("No exception raised"); 123 } catch (InvalidArgumentException e) { 124 // Expected exception. 125 } 126 } 127 } 128